forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_pkg_create.go
251 lines (215 loc) · 5.05 KB
/
cmd_pkg_create.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
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"text/template"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
func hwaf_make_cmd_pkg_create() *commander.Command {
cmd := &commander.Command{
Run: hwaf_run_cmd_pkg_create,
UsageLine: "create [options] <pkg-full-path>",
Short: "create a new package in the current workarea",
Long: `
create creates a new package in the current workarea.
ex:
$ hwaf pkg create MyPath/MyPackage
`,
Flag: *flag.NewFlagSet("hwaf-pkg-create", flag.ExitOnError),
}
cmd.Flag.Bool("v", false, "enable verbose output")
cmd.Flag.String("script", "hscript", "type of the hwaf script to use (hscript|wscript)")
cmd.Flag.String("authors", "", "comma-separated list of authors for the new package")
return cmd
}
func hwaf_run_cmd_pkg_create(cmd *commander.Command, args []string) {
var err error
n := "hwaf-pkg-" + cmd.Name()
pkgpath := ""
switch len(args) {
case 1:
pkgpath = args[0]
default:
err = fmt.Errorf("%s: you need to give a package (full) path", n)
handle_err(err)
}
script := cmd.Flag.Lookup("script").Value.Get().(string)
switch script {
case "hscript", "wscript":
// ok
default:
err = fmt.Errorf("%s: script type is either 'hscript' or 'wscript' (got: %q)", n, script)
handle_err(err)
}
verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
authors := func() []string {
authors := cmd.Flag.Lookup("authors").Value.Get().(string)
out := make([]string, 0, 1)
for _, s := range strings.Split(authors, ",") {
s = strings.Trim(s, " ")
if s == "" {
continue
}
out = append(out, s)
}
return out
}()
if len(authors) == 0 {
usr, err := user.Current()
handle_err(err)
//fmt.Printf(">>>>> %v\n", usr)
usrname := usr.Name
if usrname == "" {
usrname = usr.Username
}
authors = []string{usrname}
}
if verbose {
fmt.Printf("%s: create package [%s]...\n", n, pkgpath)
}
cfg, err := g_ctx.LocalCfg()
handle_err(err)
pkgdir := "src"
if cfg.HasOption("hwaf-cfg", "pkgdir") {
pkgdir, err = cfg.String("hwaf-cfg", "pkgdir")
handle_err(err)
}
dir := filepath.Join(pkgdir, pkgpath)
if path_exists(dir) {
err = fmt.Errorf("%s: directory [%s] already exists on filesystem", n, dir)
handle_err(err)
}
err = os.MkdirAll(dir, 0755)
handle_err(err)
if g_ctx.PkgDb.HasPkg(dir) {
err = fmt.Errorf("%s: a package with name [%s] already exists", n, dir)
handle_err(err)
}
pkgname := filepath.Base(pkgpath)
const w_txt = `# -*- python -*-
# automatically generated wscript
import waflib.Logs as msg
PACKAGE = {
'name': '{{.FullName}}',
'author': [{{.Authors | printlst }}],
}
def pkg_deps(ctx):
# put your package dependencies here.
# e.g.:
# ctx.use_pkg('AtlasPolicy')
return
def configure(ctx):
msg.debug('[configure] package name: '+PACKAGE['name'])
return
def build(ctx):
# build artifacts
# e.g.:
# ctx.build_complib(
# name = '{{.Name}}',
# source = 'src/*.cxx src/components/*.cxx',
# use = ['lib1', 'lib2', 'ROOT', 'boost', ...],
# )
# ctx.install_headers()
# ctx.build_pymodule(source=['python/*.py'])
# ctx.install_joboptions(source=['share/*.py'])
return
`
const h_txt = `# -*- yaml -*-
# automatically generated hscript
package: {
name: "{{.FullName}}",
authors: [{{.Authors | printlst}}],
## dependencies of this package
deps: {
public: [
],
private: [
],
# specify runtime dependencies
# e.g: python modules for scripts installed by this package
# binaries used by scripts installed by this package
runtime: [
],
},
}
options: {}
configure: {}
build: {
# build artifacts
# e.g.:
# {{.Name}}: {
# source: "src/*.cxx src/components/*.cxx",
# use: ["lib1", "lib2", "ROOT", "boost", ...],
# }
}
## EOF ##
`
txt := h_txt
fname := "hscript.yml"
switch script {
case "hscript":
txt = h_txt
fname = "hscript.yml"
case "wscript":
txt = w_txt
fname = "wscript"
}
// create generic structure...
for _, d := range []string{
//"cmt",
pkgname,
"src",
} {
err = os.MkdirAll(filepath.Join(dir, d), 0755)
handle_err(err)
}
wscript, err := os.Create(filepath.Join(dir, fname))
handle_err(err)
defer func() {
err = wscript.Sync()
handle_err(err)
err = wscript.Close()
handle_err(err)
}()
/* fill the template */
pkg := struct {
FullName string
Name string
Authors []string
}{
FullName: pkgpath,
Name: pkgname,
Authors: authors,
}
tmpl := template.New(script).Funcs(template.FuncMap{
"printlst": func(lst []string) string {
out := []string{}
for idx, s := range lst {
s = strings.Trim(s, " ")
if s == "" {
continue
}
comma := ","
if idx+1 == len(lst) {
comma = ""
}
out = append(out, fmt.Sprintf("%q%s", s, comma))
}
return strings.Join(out, " ")
},
})
tmpl, err = tmpl.Parse(txt)
handle_err(err)
err = tmpl.Execute(wscript, &pkg)
handle_err(err)
err = g_ctx.PkgDb.Add("local", "", dir)
handle_err(err)
if verbose {
fmt.Printf("%s: create package [%s]... [ok]\n", n, pkgpath)
}
}
// EOF