forked from mijia/gobuildweb
/
main.go
121 lines (107 loc) · 3.46 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"runtime"
"sync"
"github.com/BurntSushi/toml"
"github.com/agtorre/gocolorize"
"github.com/mijia/gobuildweb/assets"
"github.com/mijia/gobuildweb/loggers"
"strings"
)
type ProjectConfig struct {
sync.RWMutex
Package *PackageConfig
Assets *assets.Config
Distribution *DistributionConfig
}
func (pc ProjectConfig) getAssetEntry(entryName string) (assets.Entry, bool) {
pc.RLock()
defer pc.RUnlock()
return assets.GetEntryConfig(*pc.Assets, entryName)
}
type PackageConfig struct {
Name string
Version string
Authors []string
Dependencies []string `toml:"deps"`
Builder string `toml:builder`
BuildOpts []string `toml:"build_opts"`
OmitTests []string `toml:"omit_tests"`
IsGraceful bool `toml:"is_graceful"`
}
type DistributionConfig struct {
BuildOpts []string `toml:"build_opts"`
PackExtras []string `toml:"pack_extras"`
CrossTargets [][2]string `toml:"cross_targets"`
ExtraCmd []string `toml:"extra_cmd"`
}
func usage() {
fmt.Println("Usage:")
fmt.Println(" run Will watch your file changes and run the application")
fmt.Println(" dist Build your web application")
fmt.Println(" -specified-entries entry1,entry2 run -debug Will watch your file changes and run the application, just compile the specified entry")
os.Exit(1)
}
//传入参数specified-entries,用逗号分隔
func getAvailableEntries(specifiedEntries string, entries []assets.Entry) []assets.Entry {
var availableEntries []assets.Entry
specifiedEntries = strings.TrimSpace(specifiedEntries)
if len(specifiedEntries) == 0 {
return entries
}
specifiedEntryList := strings.Split(specifiedEntries, ",")
specifiedEntryMap := map[string]bool{}
for _, specifiedEntry := range specifiedEntryList {
specifiedEntryMap[specifiedEntry] = true
}
for _, entry := range entries {
if _, ok := specifiedEntryMap[entry.Name]; ok {
availableEntries = append(availableEntries, entry)
loggers.Info("the available entry is %+v", entry)
}
}
return availableEntries
}
func main() {
loggers.IsDebug = os.Getenv("GBW_DEBUG") == "1"
fmt.Println(gocolorize.NewColor("magenta").Paint("gobuildweb > Build a Golang web application.\n"))
cmds := map[string]Command{
"run": commandRun,
"dist": commandDist,
}
specifiedEntries := flag.String("specified-entries", "", "the specified entries name,用逗号分隔")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
usage()
}
if cmd, ok := cmds[args[0]]; !ok {
usage()
} else {
if fi, err := os.Stat("project.toml"); os.IsNotExist(err) {
loggers.ERROR.Fatalf("Please provide a project.toml for web project.")
} else if err != nil {
loggers.ERROR.Fatalf("Accessing project.toml file error, %v.", err)
} else if fi.IsDir() {
loggers.ERROR.Fatalf("project.toml cannot be a directory.")
}
if _, err := toml.DecodeFile("project.toml", &rootConfig); err != nil {
loggers.ERROR.Fatalf("Cannot decode the project.toml into TOML format, %v", err)
}
if len(*specifiedEntries) > 0 && rootConfig.Assets != nil {
availableEntries := getAvailableEntries(*specifiedEntries, rootConfig.Assets.Entries)
rootConfig.Assets.Entries = availableEntries
}
loggers.SUCC.Printf("Loaded project.toml... %s", rootConfig.Package.Name)
if err := cmd(args[1:]); err != nil {
loggers.ERROR.Fatalf("Executing command [%v] error, %v", args[0], err)
}
}
}
var rootConfig ProjectConfig
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}