From e606b0045dbe8b6ad9f1f8f7fe24d6910d920c5f Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 3 Sep 2013 13:23:58 -0400 Subject: [PATCH] Working on bee bale --- apiapp.go | 14 +++++++++ autorouter.go | 2 +- bale.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ bee.go | 3 +- bee.json | 9 ++++++ new.go | 14 +++++++++ pack.go | 14 +++++++++ run.go | 29 ++++++++++++------ test.go | 17 +++++++++-- util.go | 14 +++++++++ watch.go | 14 +++++++++ 11 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 bale.go diff --git a/apiapp.go b/apiapp.go index 18b1a91..78c9921 100644 --- a/apiapp.go +++ b/apiapp.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( diff --git a/autorouter.go b/autorouter.go index 2de7bfb..99dee37 100644 --- a/autorouter.go +++ b/autorouter.go @@ -1,4 +1,4 @@ -// Copyright 2013 Bee Authors +// Copyright 2013 bee authors // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain diff --git a/bale.go b/bale.go new file mode 100644 index 0000000..1d0a2e3 --- /dev/null +++ b/bale.go @@ -0,0 +1,81 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package main + +import ( + "os" + "path" + "path/filepath" + "runtime" + "strings" + + "github.com/Unknwon/com" +) + +var cmdBale = &Command{ + UsageLine: "bale", + Short: "packs non-Go files to Go source files", + Long: ` +bale packs non-Go files to Go source files and + +auto-generate unpack function to main package then run it + +during the runtime. + +This is mainly used for zealots who are requiring 100% Go code.`, +} + +func init() { + cmdBale.Run = runBale +} + +func runBale(cmd *Command, args []string) { + err := loadConfig() + if err != nil { + com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) + } + + os.Mkdir("bale", os.ModePerm) + + for _, p := range conf.Bale.Dirs { + filepath.Walk(p, walkFn) + } +} + +func walkFn(resPath string, info os.FileInfo, err error) error { + if info.IsDir() || filterSuffix(resPath) { + return nil + } + + resPath = strings.Replace(resPath, "_", "__", -1) + resPath = strings.Replace(resPath, ".", "___", -1) + sep := "/" + if runtime.GOOS == "windows" { + sep = "\\" + } + resPath = strings.Replace(resPath, sep, "_", -1) + os.MkdirAll(path.Dir(resPath), os.ModePerm) + os.Create("bale/" + resPath + ".go") + return nil +} + +func filterSuffix(name string) bool { + for _, s := range conf.Bale.IngExt { + if strings.HasSuffix(name, s) { + return true + } + } + return false +} diff --git a/bee.go b/bee.go index e31c536..d4f5552 100644 --- a/bee.go +++ b/bee.go @@ -1,4 +1,4 @@ -// Copyright 2013 Bee Authors +// Copyright 2013 bee authors // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain @@ -77,6 +77,7 @@ var commands = []*Command{ cmdApiapp, cmdRouter, cmdTest, + cmdBale, //cmdReStart, } diff --git a/bee.json b/bee.json index 725d11c..8e3c7eb 100644 --- a/bee.json +++ b/bee.json @@ -4,5 +4,14 @@ "controllers": "", "models": "", "others": [] + }, + "bale": { + "import": "github.com/astaxie/bee/bale", + "dirs": [ + "views", "static", "conf" + ], + "ignore_ext": [ + ".go" + ] } } \ No newline at end of file diff --git a/new.go b/new.go index cb5f96e..31375b8 100644 --- a/new.go +++ b/new.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( diff --git a/pack.go b/pack.go index 4ab9f2c..305ef88 100644 --- a/pack.go +++ b/pack.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( diff --git a/run.go b/run.go index ad0297d..7b657b5 100644 --- a/run.go +++ b/run.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( @@ -61,10 +75,11 @@ var conf struct { Others []string // Other directories. } `json:"dir_structure"` - MainFiles struct { - Main string `json:"main.go"` - Others []string // Others files of package main. - } `json:"main_files"` + Bale struct { + Import string + Dirs []string + IngExt []string `json:"ignore_ext"` + } } func runApp(cmd *Command, args []string) { @@ -79,7 +94,7 @@ func runApp(cmd *Command, args []string) { err := loadConfig() if err != nil { - com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]", err) + com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) } var paths []string paths = append(paths, @@ -89,7 +104,6 @@ func runApp(cmd *Command, args []string) { // Because monitor files has some issues, we watch current directory // and ignore non-go files. paths = append(paths, conf.DirStruct.Others...) - paths = append(paths, conf.MainFiles.Others...) NewWatcher(paths) appname = args[0] @@ -127,8 +141,5 @@ func loadConfig() error { if len(conf.DirStruct.Models) == 0 { conf.DirStruct.Models = "models" } - if len(conf.MainFiles.Main) == 0 { - conf.MainFiles.Main = "main.go" - } return nil } diff --git a/test.go b/test.go index 0bb9468..978edd8 100644 --- a/test.go +++ b/test.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( @@ -33,7 +47,7 @@ func testApp(cmd *Command, args []string) { err := loadConfig() if err != nil { - com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]", err) + com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) } var paths []string paths = append(paths, @@ -43,7 +57,6 @@ func testApp(cmd *Command, args []string) { // Because monitor files has some issues, we watch current directory // and ignore non-go files. paths = append(paths, conf.DirStruct.Others...) - paths = append(paths, conf.MainFiles.Others...) NewWatcher(paths) appname = args[0] diff --git a/util.go b/util.go index 2649ef0..080e336 100644 --- a/util.go +++ b/util.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( diff --git a/watch.go b/watch.go index f0d46ca..f0f23f2 100644 --- a/watch.go +++ b/watch.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import (