1
+ //
2
+ // Copyright (c) 2018, Brian Frank and Andy Frank
3
+ // Licensed under the Academic Free License version 3.0
4
+ //
5
+ // History:
6
+ // 17 Sep 2018 Andy Frank Creation
7
+ //
8
+
9
+ **************************************************************************
10
+ ** Command line tool support for build framework
11
+ **************************************************************************
12
+
13
+ internal class Main
14
+ {
15
+ Int main()
16
+ {
17
+ cmd := Env.cur.args.first?. lower
18
+ switch (cmd)
19
+ {
20
+ case "init" : return InitCmd().run
21
+ default :
22
+ echo("usage: fan build init <podName>" )
23
+ return 1
24
+ }
25
+ }
26
+ }
27
+
28
+ **************************************************************************
29
+ ** InitCmd
30
+ **************************************************************************
31
+
32
+ internal class InitCmd
33
+ {
34
+ new make()
35
+ {
36
+ this .envDir = Env.cur.workDir
37
+ this .srcDir = envDir + `src/`
38
+ this .curDir = File.os(Env.cur.vars["user.dir" ])
39
+ this .hasEnv = curDir.pathStr.contains(envDir.pathStr)
40
+ }
41
+
42
+ ** Run init command.
43
+ Int run()
44
+ {
45
+ try
46
+ {
47
+ parsePath
48
+ createEnv
49
+ createPod
50
+ echo("INIT SUCCESS!" )
51
+ return 0
52
+ }
53
+ catch (Err err)
54
+ {
55
+ err.trace
56
+ Env.cur.err.printLine("INIT FAILED!" )
57
+ return 1
58
+ }
59
+ }
60
+
61
+ ** Parse pod name optional path.
62
+ private Void parsePath()
63
+ {
64
+ // validate args
65
+ arg := Env.cur.args.getSafe(1 )
66
+ if (arg == null ) throw Err("Missing 'podName' argument" )
67
+
68
+ // parse path
69
+ if (! arg.endsWith("/" )) arg += "/"
70
+ this .podPath = Uri .fromStr(arg)
71
+ this .podName = podPath.path.last
72
+
73
+ // validate pod name
74
+ err := compiler:: InitInput.isValidPodName(podName)
75
+ if (err != null ) throw Err(err)
76
+ }
77
+
78
+ ** Create a new env.
79
+ private Void createEnv()
80
+ {
81
+ // short-circuit if already created
82
+ if (hasEnv) return
83
+
84
+ // check if dir exists
85
+ if ((curDir + podPath).exists)
86
+ throw Err("Cannot create env - directory already exists '$podPath '" )
87
+
88
+ // create new env using pod name; collapse podPath if specified
89
+ this .envDir = (curDir + podPath).create
90
+ this .srcDir = envDir + `src/`
91
+ this .podPath = `${podName} /`
92
+
93
+ // stub env dirs
94
+ envDir.createDir("etc" )
95
+ envDir.createDir("lib" )
96
+ envDir.createDir("src" )
97
+ envDir.createFile("fan.props" ).out.print("" ).sync.close
98
+ build := (envDir + `src/` ).createFile("build.fan" )
99
+ build.out.printLine(buildGroup.replace("{{podName}}" , podName)).sync.close
100
+ build- > executable = true
101
+
102
+ echo("Created new env '${envDir.uri.relTo(curDir.uri)} '" )
103
+ }
104
+
105
+ ** Create new pod source tree.
106
+ private Void createPod()
107
+ {
108
+ // find podDir
109
+ podDir := this .srcDir + this .podPath
110
+
111
+ // verify pod does not already exist
112
+ if (podDir.exists) throw Err("Pod '$podName ' already exists" )
113
+
114
+ // stub pod dirs
115
+ podDir.create
116
+ podDir.createDir("fan" )
117
+ podDir.createDir("test" )
118
+ build := podDir.createFile("build.fan" )
119
+ build.out.printLine(buildPod.replace("{{podName}}" , podName)).sync.close
120
+ build- > executable = true
121
+
122
+ echo("Created new pod '${podDir.uri.relTo(envDir.uri)} '" )
123
+ if (hasEnv) echo("Remember to add '${podDir.uri.relTo(srcDir.uri)} build.fan' to 'src/build.fan'!" )
124
+ }
125
+
126
+ ** Build group script template.
127
+ private static const Str buildGroup :=
128
+ """#! /usr/bin/env fan
129
+
130
+ using build
131
+
132
+ class Build : BuildGroup
133
+ {
134
+ new make()
135
+ {
136
+ childrenScripts =
137
+ [
138
+ `{{podName}}/build.fan`,
139
+ ]
140
+ }
141
+ }"""
142
+
143
+ ** Build pod script template.
144
+ private static const Str buildPod :=
145
+ """#! /usr/bin/env fan
146
+
147
+ using build
148
+
149
+ class Build : build::BuildPod
150
+ {
151
+ new make()
152
+ {
153
+ podName = "{{podName}}"
154
+ summary = "Description of this pod"
155
+ version = Version("1.0")
156
+ // These values are optional, but recommended
157
+ // See: http://fantom.org/doc/docLang/Pods#meta
158
+ // meta = [
159
+ // "org.name": "My Org",
160
+ // "org.uri": "http://myorg.org/",
161
+ // "proj.name": "My Project",
162
+ // "proj.uri": "http://myproj.org/",
163
+ // "license.name": "Apache License 2.0",
164
+ // "vcs.name": "Git",
165
+ // "vcs.uri": "https://github.com/myorg/myproj"
166
+ // ]
167
+ depends = ["sys 1.0"]
168
+ srcDirs = [`fan/`]
169
+ // resDirs = [,]
170
+ // javaDirs = [,]
171
+ // jsDirs = [,]
172
+ // docApi = false // defaults to 'true'
173
+ // docSrc = true // defaults to 'false'
174
+ }
175
+ }"""
176
+
177
+ private File envDir // current Fantom env dir
178
+ private File srcDir // envDir + src/
179
+ private File curDir // current shell working dir
180
+ private Bool hasEnv // are we under an existing PathEnv
181
+ private Str ? podName // pod name to create
182
+ private Uri ? podPath // path to pod including podName
183
+ }
0 commit comments