/
main.go
135 lines (118 loc) · 2.96 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"github.com/davecgh/go-spew/spew"
)
var configFilePath string
var logFilePath string
var cmd string
var field string
// -logFile의 인자로는 3가지가 가능하다
// -logFile 없음 : 유니티 기본 로그 파일
// -logFile이 있지만 공백 : stdout, https://answers.unity.com/answers/1139749/view.html
// -logFile이 있음 : 로그 파일 경로
// golang에서는 string에 nil할당이 불가능하고
// 그렇다고 옵션을 뜯어고치긴 귀찮아서 임의의 파일명을 기본값으로 사용
const defaultLogFile = "_DEFAULT_LOG_"
func init() {
flag.StringVar(&configFilePath, "config", "", "config file path")
flag.StringVar(&logFilePath, "log", defaultLogFile, "unity log file path")
flag.StringVar(&cmd, "cmd", "dump", "command")
flag.StringVar(&field, "field", "", "field to show")
}
func main() {
flag.Parse()
config, err := loadConfig(configFilePath, logFilePath)
if err != nil {
log.Fatalf("Failed to load config: %s", err)
return
}
switch cmd {
case "dump":
cmdDump(&config)
case "show":
cmdShow(&config)
case "build":
cmdBuild(&config)
default:
log.Fatalf("Unknown command: %s", cmd)
}
}
func cmdDump(c *Config) {
spew.Dump(c)
fmt.Println("")
fmt.Println("BuildPath\t:", c.MakeBuildPath())
fmt.Println("ConfigPath\t:", c.FilePath)
fmt.Println("LogFilePath\t:", c.LogFilePath())
fmt.Println("UnityPath\t:", c.MakeUnityPath())
fmt.Println("ProjectPath\t:", c.MakeProjectPath())
fmt.Println("Args\t:", c.Args())
}
func cmdShow(c *Config) {
fmt.Println(getShowString(c))
}
func getShowString(c *Config) string {
switch field {
case "build_path":
return c.MakeBuildPath()
case "config_path":
return c.FilePath
case "log_file_path":
return c.LogFilePath()
case "unity_path":
return c.MakeUnityPath()
case "project_path":
return c.MakeProjectPath()
case "minamo_version":
tmpfile, err := ioutil.TempFile("", "minamo")
if err != nil {
panic(err)
}
fp := tmpfile.Name()
defer os.Remove(tmpfile.Name())
tmpfile.Close()
c.logFilePath = tmpfile.Name()
method := "Assets.Minamo.Editor.EntryPoint.VersionName"
_, _, err = c.ExecuteMethod(method)
if err != nil {
panic(err)
}
data, err := ioutil.ReadFile(fp)
if err != nil {
panic(err)
}
re := regexp.MustCompile(`MinamoVersion=(\d+.\d+.\d+)`)
founds := re.FindAllSubmatch(data, -1)
if len(founds) > 0 {
f := founds[0][1]
v := string(f)
return v
}
return ""
case "version_code":
code := NewOutputContext(c, c.Now).VersionCode
return strconv.Itoa(code)
case "version_name":
return NewOutputContext(c, c.Now).VersionName
case "package_name":
return NewOutputContext(c, c.Now).PackageName
default:
log.Fatalf("unknown field: %s", field)
return ""
}
}
func cmdBuild(c *Config) {
output, buildTime, err := c.Execute()
if err != nil {
fmt.Println(output)
panic(err)
}
fmt.Println(output)
fmt.Println("BuildTime\t:", buildTime)
}