Skip to content

Commit fa317a6

Browse files
committed
Added buildOptions refactoring to main.go
main.go has been moved into module arduino.cc/arduino-builder Signed-off-by: Cristian Maglie <[email protected]>
1 parent d5ae87c commit fa317a6

File tree

4 files changed

+60
-71
lines changed

4 files changed

+60
-71
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
bin
22
src/github.com
33
src/golang.org
4-
arduino-builder
4+
./arduino-builder
55

66
# Created by .ignore support plugin (hsz.mobi)
77
### Go template

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ go get github.com/stretchr/testify
6969
go get github.com/jstemmer/go-junit-report
7070
go get golang.org/x/codereview/patch
7171
go get golang.org/x/tools/cmd/vet
72-
go build
72+
go build arduino.cc/arduino-builder
7373
```
7474

7575
### TDD

fmt_fix_vet

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash -x
22

33
go fmt ./src/arduino.cc/builder/...
4-
go fmt main.go
4+
go fmt ./src/arduino.cc/arduino-builder/...
55
go fix ./src/arduino.cc/builder/...
6-
go fix main.go
6+
go fix ./src/arduino.cc/arduino-builder/...
77
go vet ./src/arduino.cc/builder/...
8-
go vet main.go
8+
go vet ./src/arduino.cc/arduino-builder/...

main.go renamed to src/arduino.cc/arduino-builder/main.go

+55-66
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import (
4444
"arduino.cc/builder/constants"
4545
"arduino.cc/builder/gohasissues"
4646
"arduino.cc/builder/i18n"
47+
"arduino.cc/builder/props"
48+
"arduino.cc/builder/types"
4749
"arduino.cc/builder/utils"
4850
"github.com/go-errors/errors"
4951
)
@@ -170,85 +172,77 @@ func main() {
170172
}
171173

172174
context := make(map[string]interface{})
175+
ctx := &types.Context{}
173176

174-
buildOptions := make(map[string]string)
175177
if *buildOptionsFileFlag != "" {
178+
buildOptions := make(props.PropertiesMap)
176179
if _, err := os.Stat(*buildOptionsFileFlag); err == nil {
177180
data, err := ioutil.ReadFile(*buildOptionsFileFlag)
178181
if err != nil {
179182
printCompleteError(err)
180-
defer os.Exit(1)
181-
return
182183
}
183184
err = json.Unmarshal(data, &buildOptions)
184185
if err != nil {
185186
printCompleteError(err)
186-
defer os.Exit(1)
187-
return
188187
}
189188
}
189+
ctx.InjectBuildOptions(buildOptions)
190190
}
191191

192-
var err error
193-
printStackTrace := false
194-
err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, hardwareFoldersFlag, buildOptions, constants.CTX_HARDWARE_FOLDERS, FLAG_HARDWARE, true)
195-
if err != nil {
196-
printError(err, printStackTrace)
197-
defer os.Exit(1)
198-
return
192+
// FLAG_HARDWARE
193+
if hardwareFolders, err := toSliceOfUnquoted(hardwareFoldersFlag); err != nil {
194+
printCompleteError(err)
195+
} else if len(hardwareFolders) > 0 {
196+
ctx.HardwareFolders = hardwareFolders
199197
}
200-
201-
err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, toolsFoldersFlag, buildOptions, constants.CTX_TOOLS_FOLDERS, FLAG_TOOLS, true)
202-
if err != nil {
203-
printError(err, printStackTrace)
204-
defer os.Exit(1)
205-
return
198+
if len(ctx.HardwareFolders) == 0 {
199+
printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_HARDWARE + "' is mandatory"))
206200
}
207201

208-
err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, librariesFoldersFlag, buildOptions, constants.CTX_OTHER_LIBRARIES_FOLDERS, FLAG_LIBRARIES, false)
209-
if err != nil {
210-
printError(err, printStackTrace)
211-
defer os.Exit(1)
212-
return
202+
// FLAG_TOOLS
203+
if toolsFolders, err := toSliceOfUnquoted(toolsFoldersFlag); err != nil {
204+
printCompleteError(err)
205+
} else if len(toolsFolders) > 0 {
206+
ctx.ToolsFolders = toolsFolders
213207
}
214-
215-
err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, librariesBuiltInFoldersFlag, buildOptions, constants.CTX_BUILT_IN_LIBRARIES_FOLDERS, FLAG_BUILT_IN_LIBRARIES, false)
216-
if err != nil {
217-
printError(err, printStackTrace)
218-
defer os.Exit(1)
219-
return
208+
if len(ctx.ToolsFolders) == 0 {
209+
printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_TOOLS + "' is mandatory"))
220210
}
221211

222-
err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, customBuildPropertiesFlag, buildOptions, constants.CTX_CUSTOM_BUILD_PROPERTIES, FLAG_PREFS, false)
223-
if err != nil {
224-
printError(err, printStackTrace)
225-
defer os.Exit(1)
226-
return
212+
// FLAG_LIBRARIES
213+
if librariesFolders, err := toSliceOfUnquoted(librariesFoldersFlag); err != nil {
214+
printCompleteError(err)
215+
} else if len(librariesFolders) > 0 {
216+
ctx.OtherLibrariesFolders = librariesFolders
227217
}
228218

229-
fqbn, err := gohasissues.Unquote(*fqbnFlag)
230-
if err != nil {
219+
// FLAG_BUILT_IN_LIBRARIES
220+
if librariesBuiltInFolders, err := toSliceOfUnquoted(librariesBuiltInFoldersFlag); err != nil {
231221
printCompleteError(err)
232-
defer os.Exit(1)
233-
return
222+
} else if len(librariesBuiltInFolders) > 0 {
223+
ctx.BuiltInLibrariesFolders = librariesBuiltInFolders
234224
}
235225

236-
if fqbn == "" {
237-
fqbn = buildOptions[constants.CTX_FQBN]
226+
// FLAG_PREFS
227+
if customBuildProperties, err := toSliceOfUnquoted(customBuildPropertiesFlag); err != nil {
228+
printCompleteError(err)
229+
} else if len(customBuildProperties) > 0 {
230+
ctx.CustomBuildProperties = customBuildProperties
238231
}
239232

240-
if fqbn == "" {
233+
// FLAG_FQBN
234+
if fqbn, err := gohasissues.Unquote(*fqbnFlag); err != nil {
235+
printCompleteError(err)
236+
} else if fqbn != "" {
237+
ctx.FQBN = fqbn
238+
}
239+
if ctx.FQBN == "" {
241240
printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_FQBN + "' is mandatory"))
242-
defer os.Exit(1)
243-
return
244241
}
245-
context[constants.CTX_FQBN] = fqbn
246242

247243
buildPath, err := gohasissues.Unquote(*buildPathFlag)
248244
if err != nil {
249245
printCompleteError(err)
250-
defer os.Exit(1)
251-
return
252246
}
253247

254248
if buildPath != "" {
@@ -262,8 +256,6 @@ func main() {
262256
err = utils.EnsureFolderExists(buildPath)
263257
if err != nil {
264258
printCompleteError(err)
265-
defer os.Exit(1)
266-
return
267259
}
268260
}
269261
context[constants.CTX_BUILD_PATH] = buildPath
@@ -277,31 +269,26 @@ func main() {
277269
sketchLocation, err := gohasissues.Unquote(sketchLocation)
278270
if err != nil {
279271
printCompleteError(err)
280-
defer os.Exit(1)
281-
return
282272
}
283-
context[constants.CTX_SKETCH_LOCATION] = sketchLocation
273+
ctx.SketchLocation = sketchLocation
284274
}
285275

286276
if *verboseFlag && *quietFlag {
287277
*verboseFlag = false
288278
*quietFlag = false
289279
}
290280

291-
context[constants.CTX_VERBOSE] = *verboseFlag
281+
ctx.Verbose = *verboseFlag
292282

293-
coreAPIVersion := ""
294-
if utils.MapStringStringHas(buildOptions, constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION) {
295-
coreAPIVersion = buildOptions[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION]
296-
} else {
297-
// if deprecated 'ideVersionFlag' has been used...
283+
// FLAG_IDE_VERSION
284+
if ctx.ArduinoAPIVersion == "" {
285+
// if deprecated "--ideVersionFlag" has been used...
298286
if *coreAPIVersionFlag == "10600" && *ideVersionFlag != "10600" {
299-
coreAPIVersion = *ideVersionFlag
287+
ctx.ArduinoAPIVersion = *ideVersionFlag
300288
} else {
301-
coreAPIVersion = *coreAPIVersionFlag
289+
ctx.ArduinoAPIVersion = *coreAPIVersionFlag
302290
}
303291
}
304-
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = coreAPIVersion
305292

306293
if *warningsLevelFlag != "" {
307294
context[constants.CTX_WARNINGS_LEVEL] = *warningsLevelFlag
@@ -312,25 +299,25 @@ func main() {
312299
}
313300

314301
if *quietFlag {
315-
context[constants.CTX_LOGGER] = i18n.NoopLogger{}
302+
ctx.SetLogger(i18n.NoopLogger{})
316303
} else if *loggerFlag == FLAG_LOGGER_MACHINE {
317-
context[constants.CTX_LOGGER] = i18n.MachineLogger{}
304+
ctx.SetLogger(i18n.MachineLogger{})
318305
} else {
319-
context[constants.CTX_LOGGER] = i18n.HumanLogger{}
306+
ctx.SetLogger(i18n.HumanLogger{})
320307
}
321308

322309
if *dumpPrefsFlag {
323-
err = builder.RunParseHardwareAndDumpBuildProperties(context)
310+
err = builder.RunParseHardwareAndDumpBuildProperties(context, ctx)
324311
} else if *preprocessFlag {
325-
err = builder.RunPreprocess(context)
312+
err = builder.RunPreprocess(context, ctx)
326313
} else {
327314
if flag.NArg() == 0 {
328315
fmt.Fprintln(os.Stderr, "Last parameter must be the sketch to compile")
329316
flag.Usage()
330317
defer os.Exit(1)
331318
return
332319
}
333-
err = builder.RunBuilder(context)
320+
err = builder.RunBuilder(context, ctx)
334321
}
335322

336323
exitCode := 0
@@ -400,9 +387,11 @@ func printError(err error, printStackTrace bool) {
400387
func printCompleteError(err error) {
401388
err = i18n.WrapError(err)
402389
fmt.Fprintln(os.Stderr, err.(*errors.Error).ErrorStack())
390+
os.Exit(1)
403391
}
404392

405393
func printErrorMessageAndFlagUsage(err error) {
406394
fmt.Fprintln(os.Stderr, err)
407395
flag.Usage()
396+
os.Exit(1)
408397
}

0 commit comments

Comments
 (0)