1
0
forked from x/icebergs
icebergs/template.sh
2020-03-09 12:48:56 +08:00

165 lines
4.0 KiB
Bash
Executable File

#! /bin/sh
ice_sh="bin/ice.sh"
ice_bin="bin/ice.bin"
ice_mod="${PWD##**/}"
init_shy="etc/init.shy"
local_shy="etc/local.shy"
exit_shy="etc/exit.shy"
main_go="src/main.go"
readme="README.md"
prepare() {
[ -f ${readme} ] || cat >> ${readme} <<END
hello ice world
END
[ -d src ] || mkdir src
[ -f ${main_go} ] || cat >> ${main_go} <<END
package main
import (
"github.com/shylinux/icebergs"
_ "github.com/shylinux/icebergs/base"
_ "github.com/shylinux/icebergs/core"
_ "github.com/shylinux/icebergs/misc"
)
func main() {
println(ice.Run())
}
END
[ -f go.mod ] || go mod init ${ice_mod}
[ -f Makefile ] || cat >> Makefile <<END
all:
@echo && date
export CGO_ENABLED=0
export GOPRIVATE=github.com
export GOPROXY=https://goproxy.cn
go build -o ${ice_bin} ${main_go} && chmod u+x ${ice_bin} && ./${ice_sh} restart
END
[ -d etc ] || mkdir etc
[ -f ${init_shy} ] || cat >> ${init_shy} <<END
~web
# web.code.tmux.init
# web.code.git.init
~ssh
source etc/local.shy
END
[ -f ${local_shy} ] || cat >> ${local_shy} <<END
~web
END
[ -f ${exit_shy} ] || cat >> "${exit_shy}" <<END
END
[ -d bin ] || mkdir bin
[ -f ${ice_sh} ] || cat >> ${ice_sh} <<END
#! /bin/sh
export PATH=\${PWD}/bin:\${PWD}:\$PATH
export ctx_pid=\${ctx_pid:=var/run/ice.pid}
export ctx_log=\${ctx_log:=bin/boot.log}
export ctx_mod=\${ctx_mod:="gdb,log,ssh,ctx"}
prepare() {
[ -d bin ] || mkdir bin
[ -e ${ice_sh} ] || curl \$ctx_dev/publish/ice.sh -o ${ice_sh} && chmod u+x ${ice_sh}
[ -e ${ice_bin} ] && chmod u+x ${ice_bin} && return
bin="ice"
case \`uname -s\` in
Darwin) bin=\${bin}.darwin ;;
Linux) bin=\${bin}.linux ;;
*) bin=\${bin}.windows ;;
esac
case \`uname -m\` in
x86_64) bin=\${bin}.amd64 ;;
i686) bin=\${bin}.386 ;;
arm*) bin=\${bin}.arm ;;
esac
curl \$ctx_dev/publish/\${bin} -o ${ice_bin} && chmod u+x ${ice_bin}
}
start() {
trap HUP hup && while true; do
date && ice.bin \$@ 2>\$ctx_log && echo -e "\n\nrestarting..." || break
done
}
restart() {
[ -e \$ctx_pid ] && kill -2 \`cat \$ctx_pid\` || echo
}
shutdown() {
[ -e \$ctx_pid ] && kill -3 \`cat \$ctx_pid\` || echo
}
serve() {
prepare && shutdown && start \$@
}
cmd=\$1 && [ -n "\$cmd" ] && shift || cmd=serve
\$cmd \$*
END
chmod u+x ${ice_sh}
}
build() {
export CGO_ENABLED=0
export GOPRIVATE=github.com
export GOPROXY=https://goproxy.cn
miss=./ && [ "$1" != "" ] && miss=$1 && shift && mkdir $miss
cd $miss && prepare && go build -o ${ice_bin} ${main_go} && chmod u+x ${ice_bin} && ./${ice_sh} start serve
}
tutor() {
[ -d $1 ] || mkdir $1
[ -f "$1/$1.js" ] || cat >> "$1/$1.js" <<END
Volcanos("onimport", {help: "导入数据", list: [],
"init": function(can, msg, cb, output, action, option) {},
})
Volcanos("onaction", {help: "控件菜单", list: []})
Volcanos("onchoice", {help: "控件交互", list: ["刷新"]
"刷新": function(event, can, value, cmd, target) {},
})
Volcanos("ondetail", {help: "控件详情", list: []})
Volcanos("onexport", {help: "导出数据", list: []})
END
[ -f "$1/$1.shy" ] || cat >> "$1/$1.shy" <<END
title "$1"
END
[ -f "$1/$1.go" ] || cat >> "$1/$1.go" <<END
package $1
import (
"github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/core/wiki"
"github.com/shylinux/toolkits"
)
var Index = &ice.Context{Name: "$1", Help: "$1",
Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{
"$1": {Name: "$1", Help: "$1", Value: kit.Data(kit.MDB_SHORT, "name")},
},
Commands: map[string]*ice.Command{
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
"$1": {Name: "$1", Help: "$1", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Echo("hello world")
}},
},
}
func init() { wiki.Index.Register(Index, nil) }
END
ls -l $1
}
cmd=build && [ "$1" != "" ] && cmd=$1 && shift
$cmd $*