forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_init.go
201 lines (171 loc) · 4.07 KB
/
cmd_init.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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/hwaf/gas"
)
func hwaf_make_cmd_init() *commander.Command {
cmd := &commander.Command{
Run: hwaf_run_cmd_init,
UsageLine: "init [options] <workarea>",
Short: "initialize a new workarea",
Long: `
init initializes a new workarea.
ex:
$ hwaf init
$ hwaf init .
$ hwaf init my-work-area
`,
Flag: *flag.NewFlagSet("hwaf-init", flag.ExitOnError),
}
cmd.Flag.Bool("v", false, "enable verbose output")
return cmd
}
func hwaf_run_cmd_init(cmd *commander.Command, args []string) {
var err error
n := "hwaf-" + cmd.Name()
dirname := ""
switch len(args) {
case 0:
dirname = "."
case 1:
dirname = args[0]
default:
err = fmt.Errorf("%s: you need to give a directory name", n)
handle_err(err)
}
dirname = os.ExpandEnv(dirname)
dirname = filepath.Clean(dirname)
verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
proj_name := dirname
if proj_name == "." {
pwd, err := os.Getwd()
handle_err(err)
proj_name = filepath.Base(pwd)
} else {
proj_name = filepath.Base(dirname)
}
if verbose {
fmt.Printf("%s: creating workarea [%s]...\n", n, dirname)
}
if !path_exists(dirname) {
err = os.MkdirAll(dirname, 0700)
handle_err(err)
}
pwd, err := os.Getwd()
handle_err(err)
defer os.Chdir(pwd)
err = os.Chdir(dirname)
handle_err(err)
// setup hep-waf-tools
if verbose {
fmt.Printf("%s: add .hwaf/tools...\n", n)
}
hwaf_tools_dir := ""
switch g_ctx.Root {
default:
hwaf_tools_dir = filepath.Join(g_ctx.Root, "share", "hwaf", "tools")
case "":
hwaf_tools_dir, err = gas.Abs("github.com/hwaf/hwaf/py-hwaftools")
handle_err(err)
}
hwaf_tools_dir = os.ExpandEnv(hwaf_tools_dir)
if verbose {
fmt.Printf("%s: using hwaf/tools from [%s]...\n", n, hwaf_tools_dir)
}
if !path_exists(hwaf_tools_dir) {
err = fmt.Errorf("no such directory [%s]", hwaf_tools_dir)
handle_err(err)
}
if !path_exists(".hwaf") {
err = os.MkdirAll(".hwaf", 0700)
handle_err(err)
}
// add waf-bin
{
if verbose {
fmt.Printf("%s: add .hwaf/bin...\n", n)
}
hwaf_bin_dir := ""
switch g_ctx.Root {
default:
hwaf_bin_dir = filepath.Join(g_ctx.Root, "bin")
case "":
hwaf_bin_dir, err = gas.Abs("github.com/hwaf/hwaf")
handle_err(err)
}
hwaf_bin_dir = os.ExpandEnv(hwaf_bin_dir)
if verbose {
fmt.Printf("%s: using hwaf-bin from [%s]...\n", n, hwaf_bin_dir)
}
if !path_exists(hwaf_bin_dir) {
err = fmt.Errorf("no such hwaf-bin dir [%s]", hwaf_bin_dir)
handle_err(err)
}
src_waf, err := os.Open(filepath.Join(hwaf_bin_dir, "waf"))
handle_err(err)
defer src_waf.Close()
if !path_exists(".hwaf/bin") {
err = os.MkdirAll(".hwaf/bin", 0700)
handle_err(err)
}
waf_bin, err := os.Create(filepath.Join(".hwaf", "bin", "waf"))
handle_err(err)
defer waf_bin.Close()
if runtime.GOOS != "windows" {
err = waf_bin.Chmod(0755)
handle_err(err)
}
_, err = io.Copy(waf_bin, src_waf)
handle_err(err)
err = waf_bin.Sync()
handle_err(err)
}
// add pkgdb
err = ioutil.WriteFile(
filepath.Join(".hwaf", "pkgdb.json"),
[]byte("{}\n"),
0755,
)
handle_err(err)
// add template wscript
if verbose {
fmt.Printf("%s: add top-level wscript...\n", n)
}
if !path_exists("wscript") {
wscript_tmpl, err := os.Open(filepath.Join(hwaf_tools_dir, "hwaf-wscript"))
handle_err(err)
defer wscript_tmpl.Close()
wscript_b, err := ioutil.ReadAll(wscript_tmpl)
handle_err(err)
// replace 'hwaf-workarea' with workarea name
wscript_s := strings.Replace(
string(wscript_b),
"APPNAME = 'hwaf-workarea'",
fmt.Sprintf("APPNAME = '%s'", proj_name),
-1)
wscript, err := os.Create("wscript")
handle_err(err)
defer wscript.Close()
_, err = io.WriteString(wscript, wscript_s)
handle_err(err)
handle_err(wscript.Sync())
handle_err(wscript.Close())
}
// create 'src' directory
if !path_exists("src") {
err = os.MkdirAll("src", 0700)
handle_err(err)
}
if verbose {
fmt.Printf("%s: creating workarea [%s]... [ok]\n", n, dirname)
}
}
// EOF