1
0
forked from x/icebergs

add autogen

This commit is contained in:
shaoying 2020-09-17 07:03:15 +08:00
parent 67341983ce
commit b9aa5baf1c
9 changed files with 329 additions and 139 deletions

View File

@ -1,8 +1,8 @@
package ctx
import (
"github.com/shylinux/icebergs"
"github.com/shylinux/toolkits"
ice "github.com/shylinux/icebergs"
kit "github.com/shylinux/toolkits"
)
func _parse_arg_all(m *ice.Message, arg ...string) (bool, []string) {
@ -41,8 +41,10 @@ var Index = &ice.Context{Name: "ctx", Help: "配置模块",
return
}
if len(arg) == 1 {
m.Cmdy(COMMAND, arg[0])
if len(arg) > 1 && arg[1] == COMMAND {
m.Search(arg[0]+".", func(sup *ice.Context, sub *ice.Context, key string) {
m.Copy(m.Spawn(sub).Cmd(COMMAND))
})
} else {
m.Search(arg[0]+".", func(p *ice.Context, s *ice.Context, key string) {
msg := m.Spawn(s)

View File

@ -1,11 +1,12 @@
package lex
import (
"github.com/shylinux/icebergs"
ice "github.com/shylinux/icebergs"
)
var Index = &ice.Context{Name: "lex", Help: "词法模块",
Caches: map[string]*ice.Cache{},
const LEX = "lex"
var Index = &ice.Context{Name: LEX, Help: "词法模块",
Configs: map[string]*ice.Config{},
Commands: map[string]*ice.Command{
"hi": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {

View File

@ -1,6 +1,8 @@
package nfs
import (
"bytes"
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/mdb"
@ -180,11 +182,29 @@ func _file_show(m *ice.Message, name string) {
}
if f, e := os.OpenFile(path.Join(m.Option(DIR_ROOT), name), os.O_RDONLY, 0640); os.IsNotExist(e) {
switch cb := m.Optionv(CAT_CB).(type) {
case func(string, int):
bio := bufio.NewScanner(bytes.NewBufferString(m.Cmdx("web.spide", "dev", "raw", "GET", path.Join("/share/local/", name))))
for i := 0; bio.Scan(); i++ {
cb(bio.Text(), i)
}
default:
m.Cmdy("web.spide", "dev", "raw", "GET", path.Join("/share/local/", name))
}
} else if e == nil {
defer f.Close()
if s, e := f.Stat(); m.Assert(e) {
switch cb := m.Optionv(CAT_CB).(type) {
case func(string, int):
bio := bufio.NewScanner(f)
for i := 0; bio.Scan(); i++ {
cb(bio.Text(), i)
}
default:
buf := make([]byte, s.Size())
if n, e := f.Read(buf); m.Assert(e) {
m.Log_IMPORT(kit.MDB_FILE, name, kit.MDB_SIZE, n)
@ -192,6 +212,7 @@ func _file_show(m *ice.Message, name string) {
}
}
}
}
}
func _file_save(m *ice.Message, name string, text ...string) {
if f, p, e := kit.Create(name); m.Assert(e) {
@ -211,7 +232,7 @@ func _file_copy(m *ice.Message, name string, from ...string) {
if s, e := os.Open(v); !m.Warn(e != nil, "%s", e) {
defer s.Close()
if n, e := io.Copy(f, s); !m.Warn(e != nil, "%s", e) {
m.Log_IMPORT(kit.MDB_FILE, p, kit.MDB_SIZE, n)
m.Log_EXPORT(kit.MDB_FILE, p, kit.MDB_SIZE, n)
}
}
}
@ -278,6 +299,7 @@ func _file_search(m *ice.Message, kind, name, text string, arg ...string) {
}
const (
CAT_CB = "cat_cb"
CAT = "cat"
SAVE = "save"
COPY = "copy"
@ -353,6 +375,17 @@ var Index = &ice.Context{Name: "nfs", Help: "存储模块",
mdb.RENDER: {Name: "render type name text", Help: "渲染", Hand: func(m *ice.Message, arg ...string) {
_file_show(m, path.Join(arg[2], arg[1]))
}},
"append": {Name: "append", Help: "追加", Hand: func(m *ice.Message, arg ...string) {
if f, e := os.OpenFile(arg[0], os.O_WRONLY|os.O_APPEND, 0664); m.Assert(e) {
defer f.Close()
for _, k := range arg[1:] {
if n, e := f.WriteString(k); m.Assert(e) {
m.Log_EXPORT(kit.MDB_FILE, arg[0], kit.MDB_SIZE, n)
}
}
}
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
}},
DIR: {Name: "dir path=auto field... auto 上传", Help: "目录", Action: map[string]*ice.Action{

View File

@ -87,8 +87,11 @@ func init() {
[ "$ISH_CONF_PRE" != "" ] || source ./.ish/plug.sh || source ~/.ish/plug.sh
require miss.sh
# ish_miss_prepare_compile
# ish_miss_prepare_install
ish_miss_prepare_compile
ish_miss_prepare_install
# ish_miss_prepare_icebergs
# ish_miss_prepare_toolkits
`,
)},

View File

@ -4,6 +4,7 @@ import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/aaa"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/nfs"
kit "github.com/shylinux/toolkits"
"strings"
@ -47,31 +48,56 @@ func init() {
ROUTE: {Name: ROUTE, Help: "路由器", Value: kit.Data(kit.MDB_SHORT, kit.MDB_ROUTE)},
},
Commands: map[string]*ice.Command{
ROUTE: {Name: "route route cmd auto 启动 添加", Help: "路由", Action: map[string]*ice.Action{
"invite": {Name: "invite", Help: "添加", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy("web.code.publish", "contexts", "tmux")
m.Cmdy("web.code.publish", "contexts", "base")
m.Cmdy("web.code.publish", "contexts", "miss")
}},
ROUTE: {Name: "route route ctx cmd auto 启动 添加", Help: "路由", Action: map[string]*ice.Action{
"inputs": {Name: "inputs", Help: "补全", Hand: func(m *ice.Message, arg ...string) {
switch arg[0] {
case "cmd":
m.Cmdy(SPACE, m.Option("route"), "command")
case "name":
m.Cmdy(SPACE, m.Option("route"), "dream")
case "template":
m.Option(nfs.DIR_DEEP, true)
m.Cmdy(nfs.DIR, "usr/icebergs")
m.Sort("path")
}
}},
"invite": {Name: "invite", Help: "添加", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy("web.code.publish", "contexts", "tmux")
m.Cmdy("web.code.publish", "contexts", "base")
m.Cmdy("web.code.publish", "contexts", "miss")
}},
"start": {Name: "start type=worker,server name=hi@key repos", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy(SPACE, m.Option("route"), "dream", "start", arg)
m.Sleep("3s")
}},
"gen": {Name: "gen module=hi@key template=usr/icebergs/misc/zsh/zsh.go@key", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy(SPACE, m.Option("route"), "web.code.autogen",
"create", "name", m.Option("module"), "from", m.Option("template"))
}},
"stop": {Name: "stop", Help: "结束", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy(SPACE, m.Option("route"), "exit")
m.Sleep("3s")
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 2 && arg[0] != "" {
m.Cmdy(SPACE, arg[0], kit.Split(kit.Keys(arg[1], strings.Join(arg[2:], " "))))
return
}
if len(arg) > 1 && arg[0] != "" {
m.Cmdy(SPACE, arg[0], kit.Split(strings.Join(arg[1:], " ")))
m.Cmd(SPACE, arg[0], "context", arg[1], "command").Table(func(index int, value map[string]string, head []string) {
m.Push("cmd", value["key"])
m.Push("name", value["name"])
m.Push("help", value["help"])
})
return
}
if len(arg) > 0 && arg[0] != "" {
m.Cmd(SPACE, arg[0], "context").Table(func(index int, value map[string]string, head []string) {
m.Push("ctx", kit.Keys(value["ups"], value["name"]))
m.Push("status", value["status"])
m.Push("stream", value["stream"])
m.Push("help", value["help"])
})
return
}
@ -84,9 +110,9 @@ func init() {
kit.MergeURL(m.Option(ice.MSG_USERWEB), "pod", kit.Keys(m.Option("pod", value[kit.MDB_ROUTE]))))
switch value[kit.MDB_TYPE] {
case SERVER:
m.PushRender("action", "button", "启动")
m.PushRender("action", "button", "创建", "启动")
case WORKER:
m.PushRender("action", "button", "结束")
m.PushRender("action", "button", "创建", "结束")
}
})
m.Sort(kit.MDB_ROUTE)

96
core/code/autogen.go Normal file
View File

@ -0,0 +1,96 @@
package code
import (
"path"
"strings"
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/base/nfs"
kit "github.com/shylinux/toolkits"
)
const AUTOGEN = "autogen"
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
AUTOGEN: {Name: AUTOGEN, Help: "生成器", Value: kit.Data(
kit.MDB_FIELD, "time,id,name,from",
)},
},
Commands: map[string]*ice.Command{
AUTOGEN: {Name: "autogen auto 创建", Help: "生成器", Action: map[string]*ice.Action{
mdb.CREATE: {Name: "create name=hi@key from=usr/icebergs/misc/zsh/zsh.go@key", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
if p := path.Join("src", m.Option("name"), m.Option("name")+".shy"); !kit.FileExists(p) {
m.Cmd(nfs.SAVE, p, `chapter "`+m.Option("name")+`"`, "\n", `field "`+m.Option("name")+`" web.code.`+m.Option("name")+"."+m.Option("name"))
m.Cmd("nfs.file", "append", "src/main.shy", "\n", `source `+m.Option("name")+"/"+m.Option("name")+".shy", "\n")
}
p := path.Join("src", m.Option("name"), m.Option("name")+".go")
if kit.FileExists(p) {
return
}
// module file
list := []string{}
up, low := "", ""
name := m.Option("name")
key := strings.ToUpper(m.Option("name"))
m.Option(nfs.CAT_CB, func(line string, index int) {
if strings.HasPrefix(line, "package") {
line = "package " + m.Option("name")
}
if up == "" && strings.HasPrefix(line, "const") {
if ls := kit.Split(line); len(ls) > 3 {
up, low = ls[1], ls[3]
}
}
if up != "" {
line = strings.ReplaceAll(line, up, key)
line = strings.ReplaceAll(line, low, name)
}
list = append(list, line)
})
m.Cmd(nfs.CAT, m.Option("from"))
m.Cmd(nfs.SAVE, p, strings.Join(list, "\n"))
// go.mod
mod := ""
m.Option(nfs.CAT_CB, func(line string, index int) {
if strings.HasPrefix(line, "module") {
mod = strings.Split(line, " ")[1]
m.Info("module", mod)
}
})
m.Cmd(nfs.CAT, "go.mod")
// src/main.go
list = []string{}
m.Option(nfs.CAT_CB, func(line string, index int) {
list = append(list, line)
if strings.HasPrefix(line, "import (") {
list = append(list, ` _ "`+mod+"/src/"+m.Option("name")+`"`, "")
}
})
m.Cmd(nfs.CAT, "src/main.go")
m.Cmd(nfs.SAVE, "src/main.go", strings.Join(list, "\n"))
m.Cmdy(mdb.INSERT, m.Prefix(AUTOGEN), "", mdb.LIST, arg)
}},
mdb.INPUTS: {Name: "inputs", Help: "补全", Hand: func(m *ice.Message, arg ...string) {
switch arg[0] {
case "from":
m.Option(nfs.DIR_DEEP, true)
m.Cmdy(nfs.DIR, "usr/icebergs")
m.Sort("path")
}
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Option(mdb.FIELDS, kit.Select(m.Conf(m.Prefix(AUTOGEN), kit.META_FIELD), mdb.DETAIL, len(arg) > 0))
m.Cmdy(mdb.SELECT, m.Prefix(AUTOGEN), "", mdb.LIST, kit.MDB_ID, arg)
}},
},
}, nil)
}

126
core/wiki/trash.go Normal file
View File

@ -0,0 +1,126 @@
package wiki
import (
"path"
"strings"
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/nfs"
"github.com/shylinux/icebergs/base/ssh"
"github.com/shylinux/icebergs/base/web"
kit "github.com/shylinux/toolkits"
)
func _wiki_list(m *ice.Message, cmd, name string, arg ...string) bool {
m.Debug(name)
if strings.HasSuffix(name, "/") {
m.Option(nfs.DIR_ROOT, m.Conf(cmd, "meta.path"))
m.Option(nfs.DIR_TYPE, nfs.TYPE_DIR)
m.Cmdy(nfs.DIR, name, "time size path")
m.Option(nfs.DIR_TYPE, nfs.TYPE_FILE)
m.Option(nfs.DIR_REG, m.Conf(cmd, "meta.regs"))
m.Cmdy(nfs.DIR, name, "time size path")
return true
}
return false
}
func _wiki_show(m *ice.Message, cmd, name string, arg ...string) {
m.Cmdy(nfs.CAT, path.Join(m.Conf(cmd, "meta.path"), name))
}
func _wiki_save(m *ice.Message, cmd, name, text string, arg ...string) {
m.Cmd(nfs.SAVE, path.Join(m.Conf(cmd, "meta.path"), name), text)
}
func _wiki_upload(m *ice.Message, cmd string) {
m.Cmdy(web.CACHE, web.UPLOAD)
m.Cmdy(web.CACHE, web.WATCH, m.Option(web.DATA), path.Join(m.Conf(cmd, "meta.path"), m.Option("path"), m.Option("name")))
}
func reply(m *ice.Message, cmd string, arg ...string) bool {
// 文件列表
m.Option(nfs.DIR_ROOT, m.Conf(cmd, "meta.path"))
if len(arg) == 0 || strings.HasSuffix(arg[0], "/") {
m.Option("_display", "table")
// if m.Option(nfs.DIR_DEEP) == "true" {
// return true
// }
// 目录列表
m.Option(nfs.DIR_TYPE, nfs.DIR)
m.Cmdy(nfs.DIR, kit.Select("./", arg, 0))
// 文件列表
m.Option(nfs.DIR_TYPE, nfs.FILE)
m.Option(nfs.DIR_REG, m.Conf(cmd, "meta.regs"))
m.Cmdy(nfs.DIR, kit.Select("./", arg, 0))
return true
}
return false
}
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
"walk": {Name: "walk", Help: "走遍世界", Value: kit.Data(kit.MDB_SHORT, "name", "path", "", "regs", ".*\\.csv")},
},
Commands: map[string]*ice.Command{
"walk": {Name: "walk path=@province auto", Help: "走遍世界", Meta: kit.Dict("display", "local/wiki/walk"), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 0 && arg[0] == "action" {
switch arg[1] {
case "保存":
m.Cmd("nfs.save", path.Join(m.Conf(cmd, "meta.path"), arg[2]), arg[3])
}
return
}
// 文件列表
m.Option("dir_root", m.Conf(cmd, "meta.path"))
m.Option("dir_reg", m.Conf(cmd, "meta.regs"))
m.Cmdy("nfs.dir", kit.Select("./", arg, 0))
m.Sort("time", "time_r")
if len(arg) == 0 || strings.HasSuffix(arg[0], "/") {
// 目录列表
m.Option("dir_reg", "")
m.Option("dir_type", "dir")
m.Cmdy("nfs.dir", kit.Select("./", arg, 0))
return
}
m.Option("title", "我走过的世界")
m.CSV(m.Result())
}},
"mind": {Name: "mind zone type name text", Help: "思考", List: kit.List(
kit.MDB_INPUT, "text", "name", "path", "action", "auto", "figure", "key",
kit.MDB_INPUT, "text", "name", "type", "figure", "key",
kit.MDB_INPUT, "text", "name", "name", "figure", "key",
kit.MDB_INPUT, "button", "name", "添加",
kit.MDB_INPUT, "textarea", "name", "text",
kit.MDB_INPUT, "text", "name", "location", "figure", "key", "cb", "location",
), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 0 && arg[0] == "action" {
switch arg[1] {
case "input":
// 输入补全
switch arg[2] {
case "type":
m.Push("type", []string{"spark", "order", "table", "label", "chain", "refer", "brief", "chapter", "section", "title"})
case "path":
m.Option("_refresh", "true")
reply(m, "word", arg[3:]...)
}
return
}
}
if len(arg) < 2 {
m.Cmdy("word", arg)
return
}
m.Cmd("word", "action", "追加", arg)
m.Option("scan_mode", "scan")
m.Cmdy(ssh.SOURCE, path.Join(m.Conf("word", "meta.path"), arg[0]))
}},
},
}, nil)
}

View File

@ -3,65 +3,18 @@ package wiki
import (
ice "github.com/shylinux/icebergs"
_ "github.com/shylinux/icebergs/base"
"github.com/shylinux/icebergs/base/nfs"
"github.com/shylinux/icebergs/base/ssh"
"github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/base/web"
kit "github.com/shylinux/toolkits"
"path"
"strings"
)
func _wiki_list(m *ice.Message, cmd, name string, arg ...string) bool {
m.Debug(name)
if strings.HasSuffix(name, "/") {
m.Option(nfs.DIR_ROOT, m.Conf(cmd, "meta.path"))
m.Option(nfs.DIR_TYPE, nfs.TYPE_DIR)
m.Cmdy(nfs.DIR, name, "time size path")
const WIKI = "wiki"
m.Option(nfs.DIR_TYPE, nfs.TYPE_FILE)
m.Option(nfs.DIR_REG, m.Conf(cmd, "meta.regs"))
m.Cmdy(nfs.DIR, name, "time size path")
return true
}
return false
}
func _wiki_show(m *ice.Message, cmd, name string, arg ...string) {
m.Cmdy(nfs.CAT, path.Join(m.Conf(cmd, "meta.path"), name))
}
func _wiki_save(m *ice.Message, cmd, name, text string, arg ...string) {
m.Cmd(nfs.SAVE, path.Join(m.Conf(cmd, "meta.path"), name), text)
}
func _wiki_upload(m *ice.Message, cmd string) {
m.Cmdy(web.CACHE, web.UPLOAD)
m.Cmdy(web.CACHE, web.WATCH, m.Option(web.DATA), path.Join(m.Conf(cmd, "meta.path"), m.Option("path"), m.Option("name")))
}
func reply(m *ice.Message, cmd string, arg ...string) bool {
// 文件列表
m.Option(nfs.DIR_ROOT, m.Conf(cmd, "meta.path"))
if len(arg) == 0 || strings.HasSuffix(arg[0], "/") {
m.Option("_display", "table")
// if m.Option(nfs.DIR_DEEP) == "true" {
// return true
// }
// 目录列表
m.Option(nfs.DIR_TYPE, nfs.DIR)
m.Cmdy(nfs.DIR, kit.Select("./", arg, 0))
// 文件列表
m.Option(nfs.DIR_TYPE, nfs.FILE)
m.Option(nfs.DIR_REG, m.Conf(cmd, "meta.regs"))
m.Cmdy(nfs.DIR, kit.Select("./", arg, 0))
return true
}
return false
}
var Index = &ice.Context{Name: "wiki", Help: "文档中心",
var Index = &ice.Context{Name: WIKI, Help: "文档中心",
Configs: map[string]*ice.Config{
"walk": {Name: "walk", Help: "走遍世界", Value: kit.Data(kit.MDB_SHORT, "name", "path", "", "regs", ".*\\.csv")},
WIKI: {Name: WIKI, Help: "文档中心", Value: kit.Data(
kit.MDB_FIELD, "time,hash,type,name,text",
)},
},
Commands: map[string]*ice.Command{
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
@ -69,62 +22,12 @@ var Index = &ice.Context{Name: "wiki", Help: "文档中心",
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
}},
"walk": {Name: "walk path=@province auto", Help: "走遍世界", Meta: kit.Dict("display", "local/wiki/walk"), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 0 && arg[0] == "action" {
switch arg[1] {
case "保存":
m.Cmd("nfs.save", path.Join(m.Conf(cmd, "meta.path"), arg[2]), arg[3])
}
return
}
// 文件列表
m.Option("dir_root", m.Conf(cmd, "meta.path"))
m.Option("dir_reg", m.Conf(cmd, "meta.regs"))
m.Cmdy("nfs.dir", kit.Select("./", arg, 0))
m.Sort("time", "time_r")
if len(arg) == 0 || strings.HasSuffix(arg[0], "/") {
// 目录列表
m.Option("dir_reg", "")
m.Option("dir_type", "dir")
m.Cmdy("nfs.dir", kit.Select("./", arg, 0))
return
}
m.Option("title", "我走过的世界")
m.CSV(m.Result())
WIKI: {Name: WIKI, Help: "文档中心", Action: map[string]*ice.Action{
mdb.CREATE: {Name: "create", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy(mdb.INSERT, m.Prefix(WIKI), "", mdb.HASH, arg)
}},
"mind": {Name: "mind zone type name text", Help: "思考", List: kit.List(
kit.MDB_INPUT, "text", "name", "path", "action", "auto", "figure", "key",
kit.MDB_INPUT, "text", "name", "type", "figure", "key",
kit.MDB_INPUT, "text", "name", "name", "figure", "key",
kit.MDB_INPUT, "button", "name", "添加",
kit.MDB_INPUT, "textarea", "name", "text",
kit.MDB_INPUT, "text", "name", "location", "figure", "key", "cb", "location",
), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 0 && arg[0] == "action" {
switch arg[1] {
case "input":
// 输入补全
switch arg[2] {
case "type":
m.Push("type", []string{"spark", "order", "table", "label", "chain", "refer", "brief", "chapter", "section", "title"})
case "path":
m.Option("_refresh", "true")
reply(m, "word", arg[3:]...)
}
return
}
}
if len(arg) < 2 {
m.Cmdy("word", arg)
return
}
m.Cmd("word", "action", "追加", arg)
m.Option("scan_mode", "scan")
m.Cmdy(ssh.SOURCE, path.Join(m.Conf("word", "meta.path"), arg[0]))
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmdy(mdb.SELECT, m.Prefix(WIKI), "", mdb.HASH, kit.MDB_HASH, arg)
}},
},
}

View File

@ -39,7 +39,7 @@ func init() {
}
if len(arg) > 1 && arg[0] == "inner" {
// 代码详情
arg[1] = path.Join("usr", arg[1])
arg[1] = kit.Select(path.Join("usr", arg[1]), arg[1], arg[1] == "action")
m.Cmdy("web.code.inner", arg[1:])
return
}