From fe118b95989e2c23f73008c66e6d0467f4ccf432 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 9 Aug 2020 13:47:43 +0800 Subject: [PATCH] add binpack --- .gitignore | 1 + base/ssh/ssh.go | 12 ++++ base/web/serve.go | 9 +++ core/code/pack.go | 176 ++++++++++++++++++++++++++++++++++++++++++++++ go.sum | 1 + misc.go | 2 + misc/wx/wx.shy | 5 ++ 7 files changed, 206 insertions(+) create mode 100644 core/code/pack.go diff --git a/.gitignore b/.gitignore index f69473aa..180e8921 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Binaries for programs and plugins +pack/ *.exe *.exe~ *.dll diff --git a/base/ssh/ssh.go b/base/ssh/ssh.go index 71984c6d..d56905ff 100644 --- a/base/ssh/ssh.go +++ b/base/ssh/ssh.go @@ -280,6 +280,18 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { m.Option(ice.MSG_USERZONE, "boot") aaa.UserRoot(m) default: + if b, ok := ice.BinPack[arg[0]]; ok { + m.Debug("binpack %v %v", arg[0], len(b)) + buf := bytes.NewBuffer(make([]byte, 0, 4096)) + defer func() { m.Echo(buf.String()) }() + + // 脚本解析 + f.source = arg[0] + r, f.stdout = bytes.NewReader(b), buf + m.Cap(ice.CTX_STREAM, arg[0]) + f.target = m.Source() + break + } if s, e := os.Open(arg[0]); !m.Warn(e != nil, "%s", e) { defer s.Close() diff --git a/base/web/serve.go b/base/web/serve.go index 0f40cf7c..34051d23 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -8,6 +8,7 @@ import ( "github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/tcp" kit "github.com/shylinux/toolkits" + log "github.com/shylinux/toolkits/logs" "encoding/json" "net/http" @@ -207,6 +208,14 @@ func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool { } else if r.URL.Path == "/share" && r.Method == "GET" { http.ServeFile(w, r, m.Conf(SERVE, "meta.page.share")) } else { + if b, ok := ice.BinPack[r.URL.Path]; ok { + log.Info("BinPack %v %v", r.URL.Path, len(b)) + if strings.HasSuffix(r.URL.Path, ".css") { + w.Header().Set("Content-Type", "text/css; charset=utf-8") + } + w.Write(b) + return false + } return true } return false diff --git a/core/code/pack.go b/core/code/pack.go new file mode 100644 index 00000000..ca5613a4 --- /dev/null +++ b/core/code/pack.go @@ -0,0 +1,176 @@ +package code + +import ( + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/nfs" + kit "github.com/shylinux/toolkits" + + "fmt" + "io/ioutil" + "os" + "path" + "strings" +) + +func _pack_volcanos(m *ice.Message, pack *os.File) { + m.Option(nfs.DIR_ROOT, "usr/volcanos") + m.Option(nfs.DIR_DEEP, "true") + m.Option(nfs.DIR_TYPE, nfs.FILE) + + for _, k := range []string{"favicon.ico", "index.html", "index.css", "index.js", "proto.js", "frame.js", "cache.js", "cache.css"} { + what := "" + if f, e := os.Open("usr/volcanos/" + k); e == nil { + defer f.Close() + if b, e := ioutil.ReadAll(f); e == nil { + what = fmt.Sprintf("%v", b) + } + } + if k == "index.html" { + k = "" + } + what = strings.ReplaceAll(what, " ", ",") + pack.WriteString(fmt.Sprintf(` "%s": []byte{%v},`+"\n", "/"+k, what[1:len(what)-1])) + } + for _, k := range []string{"lib", "pane", "plugin"} { + m.Cmd(nfs.DIR, k).Table(func(index int, value map[string]string, head []string) { + what := "" + if f, e := os.Open("usr/volcanos/" + value["path"]); e == nil { + defer f.Close() + if b, e := ioutil.ReadAll(f); e == nil { + what = fmt.Sprintf("%v", b) + } + } + what = strings.ReplaceAll(what, " ", ",") + pack.WriteString(fmt.Sprintf(` "%s": []byte{%v},`+"\n", "/"+value["path"], what[1:len(what)-1])) + }) + } + pack.WriteString("\n") +} +func _pack_learning(m *ice.Message, pack *os.File) { + m.Option(nfs.DIR_ROOT, "usr/learning") + m.Option(nfs.DIR_DEEP, "true") + m.Option(nfs.DIR_TYPE, nfs.FILE) + + m.Cmd(nfs.DIR, "./").Table(func(index int, value map[string]string, head []string) { + what := "" + if f, e := os.Open("usr/learning/" + value["path"]); e == nil { + defer f.Close() + if b, e := ioutil.ReadAll(f); e == nil { + what = fmt.Sprintf("%v", b) + } + } + what = strings.ReplaceAll(what, " ", ",") + pack.WriteString(fmt.Sprintf(` "%s": []byte{%v},`+"\n", "usr/learning/"+value["path"], what[1:len(what)-1])) + }) + pack.WriteString("\n") +} +func _pack_icebergs(m *ice.Message, pack *os.File) { + m.Option(nfs.DIR_ROOT, "usr/icebergs") + m.Option(nfs.DIR_DEEP, "true") + m.Option(nfs.DIR_TYPE, nfs.FILE) + + m.Cmd(nfs.DIR, "./").Table(func(index int, value map[string]string, head []string) { + what := "" + if strings.HasPrefix(value["path"], "pack") { + return + } + if f, e := os.Open("usr/icebergs/" + value["path"]); e == nil { + defer f.Close() + if b, e := ioutil.ReadAll(f); e == nil { + what = fmt.Sprintf("%v", b) + } + } + what = strings.ReplaceAll(what, " ", ",") + pack.WriteString(fmt.Sprintf(` "%s": []byte{%v},`+"\n", "usr/icebergs/"+value["path"], what[1:len(what)-1])) + }) + pack.WriteString("\n") +} +func _pack_intshell(m *ice.Message, pack *os.File) { + m.Option(nfs.DIR_ROOT, "usr/intshell") + m.Option(nfs.DIR_DEEP, "true") + m.Option(nfs.DIR_TYPE, nfs.FILE) + + m.Cmd(nfs.DIR, "./").Table(func(index int, value map[string]string, head []string) { + if strings.HasPrefix(value["path"], "pluged") { + return + } + what := "" + if f, e := os.Open("usr/intshell/" + value["path"]); e != nil { + return + } else { + defer f.Close() + if b, e := ioutil.ReadAll(f); e != nil { + return + } else { + what = fmt.Sprintf("%v", b) + } + } + what = strings.ReplaceAll(what, " ", ",") + pack.WriteString(fmt.Sprintf(` "%s": []byte{%v},`+"\n", "usr/intshell/"+value["path"], what[1:len(what)-1])) + }) +} + +const ( + WEBPACK = "webpack" + BINPACK = "binpack" +) + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + WEBPACK: {Name: "webpack", Help: "打包", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Option(nfs.DIR_ROOT, "usr/volcanos") + m.Option(nfs.DIR_DEEP, "true") + m.Option(nfs.DIR_TYPE, nfs.FILE) + + js, p, e := kit.Create("usr/volcanos/cache.js") + m.Assert(e) + defer js.Close() + + css, _, e := kit.Create("usr/volcanos/cache.css") + m.Assert(e) + defer css.Close() + + for _, k := range []string{"lib", "pane", "plugin"} { + m.Cmd(nfs.DIR, k).Table(func(index int, value map[string]string, head []string) { + if strings.HasSuffix(value["path"], ".css") { + js.WriteString(`Volcanos.meta.cache["` + path.Join("/", value["path"]) + "\"] = []\n") + css.WriteString(m.Cmdx(nfs.CAT, "usr/volcanos/"+value["path"])) + } + if strings.HasSuffix(value["path"], ".js") { + js.WriteString(`_can_name = "` + path.Join("/", value["path"]) + "\"\n") + js.WriteString(m.Cmdx(nfs.CAT, "usr/volcanos/"+value["path"])) + } + }) + } + for _, k := range []string{"frame.js"} { + js.WriteString(`_can_name = "` + path.Join("/", k) + "\"\n") + js.WriteString(m.Cmdx(nfs.CAT, "usr/volcanos/"+k)) + } + + js.WriteString(`_can_name = ""` + "\n") + m.Echo(p) + }}, + BINPACK: {Name: "binpack", Help: "打包", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + pack, p, e := kit.Create("usr/icebergs/pack/binpack.go") + m.Assert(e) + defer pack.Close() + + pack.WriteString(`package pack` + "\n\n") + pack.WriteString(`import "github.com/shylinux/icebergs"` + "\n\n") + pack.WriteString(`func init() {` + "\n") + pack.WriteString(` ice.BinPack = map[string][]byte{` + "\n") + + _pack_volcanos(m, pack) + _pack_learning(m, pack) + _pack_icebergs(m, pack) + _pack_intshell(m, pack) + + pack.WriteString(` }` + "\n") + pack.WriteString(`}` + "\n") + m.Echo(p) + }}, + }, + }, nil) + +} diff --git a/go.sum b/go.sum index 03858d7c..4a7b24a1 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,7 @@ github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4 h1:u9jwvcKbQpghIXgNl/EOL github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4/go.mod h1:kcRFpEzolcEklV6rD7W95mG49/sbdX/PlFmd7ni3RvA= github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 h1:Yp0zFEufLz0H7jzffb4UPXijavlyqlYeOg7dcyVUNnQ= github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369/go.mod h1:aFJ1ZwLjvHN4yEzE5Bkz8rD8/d8Vlj3UIuvz2yfET7I= +github.com/shylinux/contexts v1.1.1 h1:15+xC/IN0ycE+H8FGto13Em/MQph/Nfk6qcEw0ffvaU= github.com/shylinux/toolkits v0.1.6 h1:x+Bs+oijuS11TWGAsdCEgwDbCS35uJvnP4jYDETJqig= github.com/shylinux/toolkits v0.1.6/go.mod h1:Y68Ot6xOmo1bun67YvqC3chDGeU2gDxtsUnvVDGJm4g= github.com/shylinux/toolkits v0.1.7 h1:RDUpZNTgnob6vaKJvJgNVaDE2UZRURjLG4StpSaAWF8= diff --git a/misc.go b/misc.go index 36a1ac83..f6995b5f 100644 --- a/misc.go +++ b/misc.go @@ -74,3 +74,5 @@ func (m *Message) PushAction(list ...interface{}) { func (m *Message) PushDetail(value interface{}, arg ...interface{}) *Message { return m.Push("detail", value, arg...) } + +var BinPack = map[string][]byte{} diff --git a/misc/wx/wx.shy b/misc/wx/wx.shy index 5b3bd0d5..38ca8727 100644 --- a/misc/wx/wx.shy +++ b/misc/wx/wx.shy @@ -7,7 +7,12 @@ refer "" ` ` image qrcode `https://weixin.qq.com` +chapter "技术" +field webpack web.code.webpack +field binpack web.code.binpack + chapter "应用" + field "阅读器" web.code.inner args `[ src/ main.shy ]` field "编辑器" web.code.vimer args `[ src/ main.go ]`