forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
82 lines (68 loc) · 1.93 KB
/
app.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
package app
import (
jsoniter "github.com/json-iterator/go"
l "github.com/yaoapp/gou/lang"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/data"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/xfs"
)
// Load Application
func Load(cfg config.Config) {
// Set language pack
share.App.L = map[string]string{}
if l.Default != nil {
share.App.L = l.Default.Global
}
// Load Info
LoadInfo(cfg.Root)
}
// L 语言包
func L(word string) string {
if trans, has := share.App.L[word]; has {
return trans
}
return word
}
// LoadInfo 应用信息
func LoadInfo(root string) {
info := defaultInfo()
fs := xfs.New(root)
if fs.MustExists("/app.json") {
err := jsoniter.Unmarshal(fs.MustReadFile("/app.json"), &info)
if err != nil {
exception.New("解析应用失败 %s", 500, err).Throw()
}
}
if has, _ := fs.Exists("/yao/icons/icon.icns"); has {
info.Icons.Set("icns", xfs.Encode(fs.MustReadFile("/yao/icons/icon.icns")))
}
if has, _ := fs.Exists("/yao/icons/icon.ico"); has {
info.Icons.Set("ico", xfs.Encode(fs.MustReadFile("/yao/icons/icon.ico")))
}
if has, _ := fs.Exists("/yao/icons/icon.png"); has {
info.Icons.Set("png", xfs.Encode(fs.MustReadFile("/yao/icons/icon.png")))
}
info.L = share.App.L
share.App = info
}
// LoadLangBuildIn 从制品中读取
func LoadLangBuildIn(dir string) error {
return nil
}
// defaultInfo 读取默认应用信息
func defaultInfo() share.AppInfo {
info := share.AppInfo{
Icons: maps.MakeSync(),
}
err := jsoniter.Unmarshal(data.MustAsset("yao/data/app.json"), &info)
if err != nil {
exception.New("解析默认应用失败 %s", 500, err).Throw()
}
info.Icons.Set("icns", xfs.Encode(data.MustAsset("yao/data/icons/icon.icns")))
info.Icons.Set("ico", xfs.Encode(data.MustAsset("yao/data/icons/icon.ico")))
info.Icons.Set("png", xfs.Encode(data.MustAsset("yao/data/icons/icon.png")))
return info
}