diff --git a/engine/load.go b/engine/load.go index c16413e08e..687f4a50bb 100644 --- a/engine/load.go +++ b/engine/load.go @@ -17,6 +17,7 @@ import ( "github.com/yaoapp/xiang/page" "github.com/yaoapp/xiang/plugin" "github.com/yaoapp/xiang/query" + "github.com/yaoapp/xiang/script" "github.com/yaoapp/xiang/server" "github.com/yaoapp/xiang/share" "github.com/yaoapp/xiang/table" @@ -40,6 +41,7 @@ func Load(cfg config.Config) { query.Load(cfg) // 加载数据分析引擎 share.Load(cfg) // 加载共享库 lib + script.Load(cfg) // 加载JS处理器 script model.Load(cfg) // 加载数据模型 model flow.Load(cfg) // 加载业务逻辑 Flow plugin.Load(cfg) // 加载业务插件 plugin diff --git a/script/script.go b/script/script.go new file mode 100644 index 0000000000..91f9671931 --- /dev/null +++ b/script/script.go @@ -0,0 +1,42 @@ +package script + +import ( + "fmt" + "path/filepath" + + "github.com/yaoapp/gou" + "github.com/yaoapp/kun/log" + "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xiang/share" +) + +// Load 加载共享库 +func Load(cfg config.Config) error { + if share.BUILDIN { + return LoadBuildIn("scripts") + } + return LoadFrom(filepath.Join(cfg.Root, "scripts")) +} + +// LoadBuildIn 从制品中读取 +func LoadBuildIn(dir string) error { + return nil +} + +// LoadFrom 从特定目录加载共享库 +func LoadFrom(dir string) error { + + if share.DirNotExists(dir) { + return fmt.Errorf("%s does not exists", dir) + } + + // 加载共享脚本 + err := share.Walk(dir, ".js", func(root, filename string) { + name := share.SpecName(root, filename) + err := gou.Yao.Load(filename, name) + if err != nil { + log.Error("加载脚本失败 %s", err.Error()) + } + }) + return err +} diff --git a/script/script_test.go b/script/script_test.go new file mode 100644 index 0000000000..a08dd02da1 --- /dev/null +++ b/script/script_test.go @@ -0,0 +1,20 @@ +package script + +import ( + "os" + "path" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/yaoapp/gou" +) + +func init() { + rootLib := path.Join(os.Getenv("YAO_DEV"), "/tests/scripts") + LoadFrom(rootLib) +} +func TestScript(t *testing.T) { + res, err := gou.Yao.New("time", "hello").Call("world") + assert.Nil(t, err) + assert.Equal(t, "name:world", res) +} diff --git a/share/importable.go b/share/importable.go index 16cd137880..dffada8349 100644 --- a/share/importable.go +++ b/share/importable.go @@ -8,34 +8,44 @@ import ( "github.com/yaoapp/gou" "github.com/yaoapp/gou/query/share" "github.com/yaoapp/kun/exception" + "github.com/yaoapp/kun/log" "github.com/yaoapp/kun/maps" "github.com/yaoapp/xiang/config" - "github.com/yaoapp/xiang/xlog" ) // Libs 共享库 var Libs = map[string]map[string]interface{}{} // Load 加载共享库 -func Load(cfg config.Config) { - LoadFrom(filepath.Join(cfg.Root, "libs")) +func Load(cfg config.Config) error { + if BUILDIN { + return LoadBuildIn("libs") + } + return LoadFrom(filepath.Join(cfg.Root, "libs")) +} + +// LoadBuildIn 从制品中读取 +func LoadBuildIn(dir string) error { + return nil } // LoadFrom 从特定目录加载共享库 -func LoadFrom(dir string) { +func LoadFrom(dir string) error { if DirNotExists(dir) { - return + return fmt.Errorf("%s does not exists", dir) } // 加载共享数据 - Walk(dir, ".json", func(root, filename string) { + err := Walk(dir, ".json", func(root, filename string) { name := SpecName(root, filename) content := ReadFile(filename) libs := map[string]map[string]interface{}{} err := jsoniter.Unmarshal(content, &libs) if err != nil { exception.New("共享数据结构异常 %s", 400, err).Throw() + log.Error("加载脚本失败 %s", err.Error()) + return } for key, lib := range libs { key := fmt.Sprintf("%s.%s", name, key) @@ -47,14 +57,19 @@ func LoadFrom(dir string) { } }) + if err != nil { + return err + } + // 加载共享脚本 - Walk(dir, ".js", func(root, filename string) { + err = Walk(dir, ".js", func(root, filename string) { name := SpecName(root, filename) err := gou.Yao.Load(filename, name) if err != nil { - xlog.Printf("加载脚本失败 %s", err.Error()) + log.Error("加载脚本失败 %s", err.Error()) } }) + return err } // UnmarshalJSON Column 字段JSON解析 diff --git a/share/importable_test.go b/share/importable_test.go index 8f76065018..a5189aaa95 100644 --- a/share/importable_test.go +++ b/share/importable_test.go @@ -7,7 +7,6 @@ import ( jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" - "github.com/yaoapp/gou" ) func init() { @@ -64,9 +63,3 @@ func TestAPI(t *testing.T) { jsoniter.Unmarshal([]byte(content), &api) assert.Equal(t, []interface{}{nil, nil, float64(10)}, api.Default) } - -func TestScript(t *testing.T) { - res, err := gou.Yao.New("time", "hello").Call("world") - assert.Nil(t, err) - assert.Equal(t, "name:world", res) -} diff --git a/tests/libs/rules.js b/tests/scripts/rules.js similarity index 100% rename from tests/libs/rules.js rename to tests/scripts/rules.js diff --git a/tests/libs/time.js b/tests/scripts/time.js similarity index 100% rename from tests/libs/time.js rename to tests/scripts/time.js diff --git a/tests/libs/tree.js b/tests/scripts/tree.js similarity index 100% rename from tests/libs/tree.js rename to tests/scripts/tree.js