forked from peterq/pan-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pan-light.go
283 lines (249 loc) · 5.97 KB
/
pan-light.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
// 这个是为了方便开发写的脚本, 代码写的很糙, 见笑
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
)
func main() {
flag.Usage = func() {
println("用法: pan-light [cmd] [sub cmd]\n")
println("参数:\n")
flag.PrintDefaults()
println()
println("命令:\n")
for _, m := range []struct{ name, desc string }{
{"pc dev", "启动pc客户端, 开发模式, 用plugin加速编译, 不支持windows"},
{"pc start", "启动pc客户端"},
{"pc moc", "生成moc"},
{"server start", "启动server"},
} {
fmt.Printf(" %v%v%v\n", m.name, strings.Repeat(" ", 12-len(m.name)), m.desc)
}
println()
os.Exit(0)
}
flag.Parse()
os.Setenv("GOPROXY", "https://goproxy.io")
cmd := "pc"
cmd = flag.Arg(0)
switch cmd {
case "pc":
pcCmd()
case "demo":
demoCmd()
case "server":
serverCmd()
default:
flag.Usage()
}
}
func serverCmd() {
cmd := flag.Arg(1)
switch cmd {
case "start":
serverStart()
default:
flag.Usage()
}
}
func serverStart() {
os.Setenv("pan_light_server_conf", "pan-light-server.yaml")
runCmd("./server", "go", "run", "pan-light-server.go")
}
func demoCmd() {
cmd := flag.Arg(1)
switch cmd {
case "host":
demoHost()
case "build.docker.slave":
buildDockerSlave()
case "avatar":
demoAvatar()
default:
flag.Usage()
}
}
func demoAvatar() {
runCmd("./server/cmd", "go", "run", "main.go", "avatar")
}
func buildDockerSlave() {
runCmd("demo", "go", "build", "-o", "./slave/ubuntu16.04/root.pan-light/demo_instance_manager", "slave.go")
//runCmd("demo", "docker", "build", "-t", "pan-light-slave", "./slave/ubuntu16.04")
}
func demoHost() {
cmd(qtBin("rcc"), "-binary", "pc/gui/qml/qml.qrc", "-o", "demo/slave/ubuntu16.04/root.pan-light/lib/qml.rcc").Run()
runCmd("./demo", "go", "build", "-o", "./slave/ubuntu16.04/root.pan-light/demo_instance_manager", "slave.go")
runCmd("./demo", "go", "run", "host.go")
}
func pcCmd() {
cmd := flag.Arg(1)
switch cmd {
case "start":
pcStart()
case "moc":
pcMoc()
case "dev":
pcDev()
case "build":
pcBuild()
case "download-icon":
downloadIcon()
default:
flag.Usage()
}
}
func pcDev() {
var rebuildPlugin bool
flag.BoolVar(&rebuildPlugin, "rebuild", false, "重新编译plugin插件")
flag.Parse()
pluginPath := "./pc/gui/gui-plugin.so"
_, err := os.Stat(pluginPath)
if os.IsNotExist(err) || rebuildPlugin {
log.Println("编译gui插件...")
runCmd("./pc", "go", "build", "-p", strconv.Itoa(runtime.NumCPU()-1), "-v", "-tags=plugin", "--buildmode=plugin", "-o", "gui/gui-plugin.so", "gui/gui-plugin.go")
}
log.Println("打包qml...")
cmd(qtBin("rcc"), "-binary", "pc/gui/qml/qml.qrc", "-o", "pc/gui/qml/qml.rcc").Run()
log.Println("启动客户端...")
runCmd("./pc", "go", "run", "-tags=plugin", "pan-light-pc-dev.go")
}
func pcBuild() {
runCmd("./pc", "go", "run", "../qt/cmd/qtdeploy/main.go", "-fast", "build", "desktop")
// 正式包编译步骤
// cd pc
// go run ../qt/cmd/qtrcc/main.go desktop gui/qml
// go build -p 8 -v -ldflags=all="-s" -o deploy/linux/pc -tags="production"
}
func qtBin(name string) string {
v, ok1 := os.LookupEnv("QT_VERSION")
d, ok2 := os.LookupEnv("QT_DIR")
if !ok1 || !ok2 {
panic("请先配置qt环境变量")
}
return d + "/" + v + "/gcc_64/bin/" + name
}
func pcStart() {
// 打包qml
log.Println("打包qml...")
c := cmd("go", "run", "../qt/cmd/qtrcc/main.go", "desktop", "gui/qml")
c.Dir, _ = filepath.Abs("./pc")
c.Run()
// 启动
log.Println("启动pc客户端")
c = cmd("go", "run", "pan-light-pc.go")
c.Dir = "./pc"
c.Run()
}
// 清除svg的style font节点, 减小尺寸
func iconSimplify() {
dir := "./pc/gui/qml/assets/images/icons"
ls, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
reg := regexp.MustCompile(`\<defs\>(.|\n)*\</defs\>`)
for _, f := range ls {
if strings.Index(f.Name(), ".svg") == len(f.Name())-4 {
filename := dir + "/" + f.Name()
bin, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
content := string(bin)
target := reg.ReplaceAllString(content, "")
if target != content {
err = ioutil.WriteFile(filename, []byte(target), os.ModePerm)
if err != nil {
panic(err)
}
}
}
}
}
func pcMoc() {
log.Println("moc...")
c := cmd("go", "run", "../qt/cmd/qtmoc/main.go", "desktop", "gui/comp")
c.Dir, _ = filepath.Abs("./pc")
e := c.Run()
if e != nil {
log.Println(e)
}
}
func cmd(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd
}
func runCmd(path, name string, arg ...string) {
c := cmd(name, arg...)
c.Dir, _ = filepath.Abs(path)
c.Env = append(os.Environ(), envFile(c.Dir)...)
c.Run()
}
func envFile(path string) (env []string) {
bin, err := ioutil.ReadFile(path + "/.env")
if err != nil {
return
}
str := string(bin)
for _, ln := range strings.Split(str, "\n") {
ln = strings.Trim(ln, "\n")
if ln == "" {
continue
}
if ln[0] == '#' {
continue
}
env = append(env, ln)
}
return
}
type gson = map[string]interface{}
type binary = []byte
func downloadIcon() {
u := "https://www.iconfont.cn/api/collection/detail.json?id=2271"
r, e := http.Get(u)
panicIf(e)
bin, e := ioutil.ReadAll(r.Body)
panicIf(e)
var j gson
panicIf(json.Unmarshal(bin, &j))
reg := regexp.MustCompile("[a-z0-9]+$")
var imgs []string
for _, icon := range j["data"].(gson)["icons"].([]interface{}) {
s := icon.(gson)["show_svg"].(string)
name := icon.(gson)["name"].(string)
name = string(reg.Find(binary(name)))
if name == "" {
name = icon.(gson)["name"].(string)
if strings.Contains(name, "未知") {
name = "unknown"
} else {
continue
}
}
imgs = append(imgs, name)
e = ioutil.WriteFile("pc/gui/qml/assets/images/icons/file/"+name+".svg", binary(s), os.ModePerm)
panicIf(e)
}
bin, _ = json.Marshal(imgs)
log.Println(string(bin))
}
func panicIf(e error) {
if e != nil {
panic(e)
}
}