diff --git a/.gitignore b/.gitignore index e26379ab..0df12e54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ bin src/github.com src/golang.org -arduino-builder +./arduino-builder # Created by .ignore support plugin (hsz.mobi) ### Go template diff --git a/README.md b/README.md index 453fb958..779f3725 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ go get github.com/stretchr/testify go get github.com/jstemmer/go-junit-report go get golang.org/x/codereview/patch go get golang.org/x/tools/cmd/vet -go build +go build arduino.cc/arduino-builder ``` ### TDD diff --git a/fmt_fix_vet b/fmt_fix_vet index ffd5c649..219e0c5b 100755 --- a/fmt_fix_vet +++ b/fmt_fix_vet @@ -1,8 +1,8 @@ #!/bin/bash -x go fmt ./src/arduino.cc/builder/... -go fmt main.go +go fmt ./src/arduino.cc/arduino-builder/... go fix ./src/arduino.cc/builder/... -go fix main.go +go fix ./src/arduino.cc/arduino-builder/... go vet ./src/arduino.cc/builder/... -go vet main.go +go vet ./src/arduino.cc/arduino-builder/... diff --git a/main.go b/src/arduino.cc/arduino-builder/main.go similarity index 71% rename from main.go rename to src/arduino.cc/arduino-builder/main.go index 662b726d..365efd09 100644 --- a/main.go +++ b/src/arduino.cc/arduino-builder/main.go @@ -41,9 +41,10 @@ import ( "syscall" "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/gohasissues" "arduino.cc/builder/i18n" + "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "github.com/go-errors/errors" ) @@ -165,111 +166,99 @@ func main() { fmt.Println("See https://www.arduino.cc/ and https://github.com/arduino/arduino-builder/graphs/contributors") fmt.Println("This is free software; see the source for copying conditions. There is NO") fmt.Println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") - defer os.Exit(0) return } - context := make(map[string]interface{}) + ctx := &types.Context{} - buildOptions := make(map[string]string) if *buildOptionsFileFlag != "" { + buildOptions := make(props.PropertiesMap) if _, err := os.Stat(*buildOptionsFileFlag); err == nil { data, err := ioutil.ReadFile(*buildOptionsFileFlag) if err != nil { printCompleteError(err) - defer os.Exit(1) - return } err = json.Unmarshal(data, &buildOptions) if err != nil { printCompleteError(err) - defer os.Exit(1) - return } } + ctx.InjectBuildOptions(buildOptions) } - var err error - printStackTrace := false - err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, hardwareFoldersFlag, buildOptions, constants.CTX_HARDWARE_FOLDERS, FLAG_HARDWARE, true) - if err != nil { - printError(err, printStackTrace) - defer os.Exit(1) - return + // FLAG_HARDWARE + if hardwareFolders, err := toSliceOfUnquoted(hardwareFoldersFlag); err != nil { + printCompleteError(err) + } else if len(hardwareFolders) > 0 { + ctx.HardwareFolders = hardwareFolders } - - err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, toolsFoldersFlag, buildOptions, constants.CTX_TOOLS_FOLDERS, FLAG_TOOLS, true) - if err != nil { - printError(err, printStackTrace) - defer os.Exit(1) - return + if len(ctx.HardwareFolders) == 0 { + printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_HARDWARE + "' is mandatory")) } - err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, librariesFoldersFlag, buildOptions, constants.CTX_OTHER_LIBRARIES_FOLDERS, FLAG_LIBRARIES, false) - if err != nil { - printError(err, printStackTrace) - defer os.Exit(1) - return + // FLAG_TOOLS + if toolsFolders, err := toSliceOfUnquoted(toolsFoldersFlag); err != nil { + printCompleteError(err) + } else if len(toolsFolders) > 0 { + ctx.ToolsFolders = toolsFolders } - - err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, librariesBuiltInFoldersFlag, buildOptions, constants.CTX_BUILT_IN_LIBRARIES_FOLDERS, FLAG_BUILT_IN_LIBRARIES, false) - if err != nil { - printError(err, printStackTrace) - defer os.Exit(1) - return + if len(ctx.ToolsFolders) == 0 { + printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_TOOLS + "' is mandatory")) } - err, printStackTrace = setContextSliceKeyOrLoadItFromOptions(context, customBuildPropertiesFlag, buildOptions, constants.CTX_CUSTOM_BUILD_PROPERTIES, FLAG_PREFS, false) - if err != nil { - printError(err, printStackTrace) - defer os.Exit(1) - return + // FLAG_LIBRARIES + if librariesFolders, err := toSliceOfUnquoted(librariesFoldersFlag); err != nil { + printCompleteError(err) + } else if len(librariesFolders) > 0 { + ctx.OtherLibrariesFolders = librariesFolders } - fqbn, err := gohasissues.Unquote(*fqbnFlag) - if err != nil { + // FLAG_BUILT_IN_LIBRARIES + if librariesBuiltInFolders, err := toSliceOfUnquoted(librariesBuiltInFoldersFlag); err != nil { printCompleteError(err) - defer os.Exit(1) - return + } else if len(librariesBuiltInFolders) > 0 { + ctx.BuiltInLibrariesFolders = librariesBuiltInFolders } - if fqbn == "" { - fqbn = buildOptions[constants.CTX_FQBN] + // FLAG_PREFS + if customBuildProperties, err := toSliceOfUnquoted(customBuildPropertiesFlag); err != nil { + printCompleteError(err) + } else if len(customBuildProperties) > 0 { + ctx.CustomBuildProperties = customBuildProperties } - if fqbn == "" { + // FLAG_FQBN + if fqbn, err := gohasissues.Unquote(*fqbnFlag); err != nil { + printCompleteError(err) + } else if fqbn != "" { + ctx.FQBN = fqbn + } + if ctx.FQBN == "" { printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_FQBN + "' is mandatory")) - defer os.Exit(1) - return } - context[constants.CTX_FQBN] = fqbn + // FLAG_BUILD_PATH buildPath, err := gohasissues.Unquote(*buildPathFlag) if err != nil { printCompleteError(err) - defer os.Exit(1) - return } - if buildPath != "" { _, err := os.Stat(buildPath) if err != nil { fmt.Fprintln(os.Stderr, err) - defer os.Exit(1) - return + os.Exit(1) } err = utils.EnsureFolderExists(buildPath) if err != nil { printCompleteError(err) - defer os.Exit(1) - return } } - context[constants.CTX_BUILD_PATH] = buildPath + ctx.BuildPath = buildPath + // FLAG_VID_PID if *vidPidFlag != "" { - context[constants.CTX_VIDPID] = *vidPidFlag + ctx.USBVidPid = *vidPidFlag } if flag.NArg() > 0 { @@ -277,10 +266,8 @@ func main() { sketchLocation, err := gohasissues.Unquote(sketchLocation) if err != nil { printCompleteError(err) - defer os.Exit(1) - return } - context[constants.CTX_SKETCH_LOCATION] = sketchLocation + ctx.SketchLocation = sketchLocation } if *verboseFlag && *quietFlag { @@ -288,84 +275,58 @@ func main() { *quietFlag = false } - context[constants.CTX_VERBOSE] = *verboseFlag + ctx.Verbose = *verboseFlag - coreAPIVersion := "" - if utils.MapStringStringHas(buildOptions, constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION) { - coreAPIVersion = buildOptions[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] - } else { - // if deprecated 'ideVersionFlag' has been used... + // FLAG_IDE_VERSION + if ctx.ArduinoAPIVersion == "" { + // if deprecated "--ideVersionFlag" has been used... if *coreAPIVersionFlag == "10600" && *ideVersionFlag != "10600" { - coreAPIVersion = *ideVersionFlag + ctx.ArduinoAPIVersion = *ideVersionFlag } else { - coreAPIVersion = *coreAPIVersionFlag + ctx.ArduinoAPIVersion = *coreAPIVersionFlag } } - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = coreAPIVersion if *warningsLevelFlag != "" { - context[constants.CTX_WARNINGS_LEVEL] = *warningsLevelFlag + ctx.WarningsLevel = *warningsLevelFlag } if *debugLevelFlag > -1 { - context[constants.CTX_DEBUG_LEVEL] = *debugLevelFlag + ctx.DebugLevel = *debugLevelFlag } if *quietFlag { - context[constants.CTX_LOGGER] = i18n.NoopLogger{} + ctx.SetLogger(i18n.NoopLogger{}) } else if *loggerFlag == FLAG_LOGGER_MACHINE { - context[constants.CTX_LOGGER] = i18n.MachineLogger{} + ctx.SetLogger(i18n.MachineLogger{}) } else { - context[constants.CTX_LOGGER] = i18n.HumanLogger{} + ctx.SetLogger(i18n.HumanLogger{}) } if *dumpPrefsFlag { - err = builder.RunParseHardwareAndDumpBuildProperties(context) + err = builder.RunParseHardwareAndDumpBuildProperties(ctx) } else if *preprocessFlag { - err = builder.RunPreprocess(context) + err = builder.RunPreprocess(ctx) } else { if flag.NArg() == 0 { fmt.Fprintln(os.Stderr, "Last parameter must be the sketch to compile") flag.Usage() - defer os.Exit(1) - return + os.Exit(1) } - err = builder.RunBuilder(context) + err = builder.RunBuilder(ctx) } - exitCode := 0 if err != nil { - err = utils.WrapError(err) + err = i18n.WrapError(err) fmt.Fprintln(os.Stderr, err) - if utils.DebugLevel(context) >= 10 { + if ctx.DebugLevel >= 10 { fmt.Fprintln(os.Stderr, err.(*errors.Error).ErrorStack()) } - exitCode = toExitCode(err) + os.Exit(toExitCode(err)) } - - defer os.Exit(exitCode) -} - -func setContextSliceKeyOrLoadItFromOptions(context map[string]interface{}, cliFlag []string, buildOptions map[string]string, contextKey string, paramName string, mandatory bool) (error, bool) { - values, err := toSliceOfUnquoted(cliFlag) - if err != nil { - return err, true - } - - if len(values) == 0 && len(buildOptions[contextKey]) > 0 { - values = strings.Split(buildOptions[contextKey], ",") - } - - if mandatory && len(values) == 0 { - return errors.New("Parameter '" + paramName + "' is mandatory"), false - } - - context[contextKey] = values - - return nil, false } func toExitCode(err error) int { @@ -398,11 +359,13 @@ func printError(err error, printStackTrace bool) { } func printCompleteError(err error) { - err = utils.WrapError(err) + err = i18n.WrapError(err) fmt.Fprintln(os.Stderr, err.(*errors.Error).ErrorStack()) + os.Exit(1) } func printErrorMessageAndFlagUsage(err error) { fmt.Fprintln(os.Stderr, err) flag.Usage() + os.Exit(1) } diff --git a/src/arduino.cc/builder/add_additional_entries_to_context.go b/src/arduino.cc/builder/add_additional_entries_to_context.go index 742c0be1..a4250c88 100644 --- a/src/arduino.cc/builder/add_additional_entries_to_context.go +++ b/src/arduino.cc/builder/add_additional_entries_to_context.go @@ -31,58 +31,48 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "path/filepath" ) type AddAdditionalEntriesToContext struct{} -func (s *AddAdditionalEntriesToContext) Run(context map[string]interface{}) error { - if utils.MapHas(context, constants.CTX_BUILD_PATH) { - buildPath := context[constants.CTX_BUILD_PATH].(string) +func (s *AddAdditionalEntriesToContext) Run(ctx *types.Context) error { + if ctx.BuildPath != "" { + buildPath := ctx.BuildPath preprocPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_PREPROC)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } sketchBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_SKETCH)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } librariesBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_LIBRARIES)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } coreBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_CORE)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_PREPROC_PATH] = preprocPath - context[constants.CTX_SKETCH_BUILD_PATH] = sketchBuildPath - context[constants.CTX_LIBRARIES_BUILD_PATH] = librariesBuildPath - context[constants.CTX_CORE_BUILD_PATH] = coreBuildPath + ctx.PreprocPath = preprocPath + ctx.SketchBuildPath = sketchBuildPath + ctx.LibrariesBuildPath = librariesBuildPath + ctx.CoreBuildPath = coreBuildPath } - if !utils.MapHas(context, constants.CTX_WARNINGS_LEVEL) { - context[constants.CTX_WARNINGS_LEVEL] = DEFAULT_WARNINGS_LEVEL + if ctx.WarningsLevel == "" { + ctx.WarningsLevel = DEFAULT_WARNINGS_LEVEL } - if !utils.MapHas(context, constants.CTX_VERBOSE) { - context[constants.CTX_VERBOSE] = false - } - - if !utils.MapHas(context, constants.CTX_DEBUG_LEVEL) { - context[constants.CTX_DEBUG_LEVEL] = DEFAULT_DEBUG_LEVEL - } - - sourceFiles := &types.UniqueStringQueue{} - context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE] = sourceFiles - foldersWithSources := &types.UniqueSourceFolderQueue{} - context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE] = foldersWithSources + ctx.CollectedSourceFiles = &types.UniqueStringQueue{} + ctx.FoldersWithSourceFiles = &types.UniqueSourceFolderQueue{} - context[constants.CTX_LIBRARY_RESOLUTION_RESULTS] = make(map[string]types.LibraryResolutionResult) - context[constants.CTX_HARDWARE_REWRITE_RESULTS] = make(map[*types.Platform][]types.PlatforKeyRewrite) + ctx.LibrariesResolutionResults = make(map[string]types.LibraryResolutionResult) + ctx.HardwareRewriteResults = make(map[*types.Platform][]types.PlatforKeyRewrite) return nil } diff --git a/src/arduino.cc/builder/add_build_board_property_if_missing.go b/src/arduino.cc/builder/add_build_board_property_if_missing.go index b819a424..10cd0e2e 100644 --- a/src/arduino.cc/builder/add_build_board_property_if_missing.go +++ b/src/arduino.cc/builder/add_build_board_property_if_missing.go @@ -31,7 +31,6 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" "arduino.cc/builder/types" "os" "strings" @@ -39,9 +38,9 @@ import ( type AddBuildBoardPropertyIfMissing struct{} -func (s *AddBuildBoardPropertyIfMissing) Run(context map[string]interface{}) error { - packages := context[constants.CTX_HARDWARE].(*types.Packages) - logger := context[constants.CTX_LOGGER].(i18n.Logger) +func (s *AddBuildBoardPropertyIfMissing) Run(ctx *types.Context) error { + packages := ctx.Hardware + logger := ctx.GetLogger() for _, aPackage := range packages.Packages { for _, platform := range aPackage.Platforms { diff --git a/src/arduino.cc/builder/add_missing_build_properties_from_parent_platform_txt_files.go b/src/arduino.cc/builder/add_missing_build_properties_from_parent_platform_txt_files.go index bf268070..79a44552 100644 --- a/src/arduino.cc/builder/add_missing_build_properties_from_parent_platform_txt_files.go +++ b/src/arduino.cc/builder/add_missing_build_properties_from_parent_platform_txt_files.go @@ -30,23 +30,21 @@ package builder import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/props" "arduino.cc/builder/types" ) type AddMissingBuildPropertiesFromParentPlatformTxtFiles struct{} -func (s *AddMissingBuildPropertiesFromParentPlatformTxtFiles) Run(context map[string]interface{}) error { - packages := context[constants.CTX_HARDWARE].(*types.Packages) - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) +func (s *AddMissingBuildPropertiesFromParentPlatformTxtFiles) Run(ctx *types.Context) error { + packages := ctx.Hardware + targetPackage := ctx.TargetPackage + buildProperties := ctx.BuildProperties newBuildProperties := packages.Properties.Clone() newBuildProperties.Merge(targetPackage.Properties) newBuildProperties.Merge(buildProperties) - context[constants.CTX_BUILD_PROPERTIES] = newBuildProperties + ctx.BuildProperties = newBuildProperties return nil } diff --git a/src/arduino.cc/builder/additional_sketch_files_copier.go b/src/arduino.cc/builder/additional_sketch_files_copier.go index 2bceb723..490e961d 100644 --- a/src/arduino.cc/builder/additional_sketch_files_copier.go +++ b/src/arduino.cc/builder/additional_sketch_files_copier.go @@ -30,7 +30,7 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "bytes" @@ -40,13 +40,13 @@ import ( type AdditionalSketchFilesCopier struct{} -func (s *AdditionalSketchFilesCopier) Run(context map[string]interface{}) error { - sketch := context[constants.CTX_SKETCH].(*types.Sketch) - sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string) +func (s *AdditionalSketchFilesCopier) Run(ctx *types.Context) error { + sketch := ctx.Sketch + sketchBuildPath := ctx.SketchBuildPath err := utils.EnsureFolderExists(sketchBuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } sketchBasePath := filepath.Dir(sketch.MainFile.Name) @@ -54,24 +54,24 @@ func (s *AdditionalSketchFilesCopier) Run(context map[string]interface{}) error for _, file := range sketch.AdditionalFiles { relativePath, err := filepath.Rel(sketchBasePath, file.Name) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } targetFilePath := filepath.Join(sketchBuildPath, relativePath) err = utils.EnsureFolderExists(filepath.Dir(targetFilePath)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } bytes, err := ioutil.ReadFile(file.Name) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if targetFileChanged(bytes, targetFilePath) { err := utils.WriteFileBytes(targetFilePath, bytes) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } diff --git a/src/arduino.cc/builder/builder.go b/src/arduino.cc/builder/builder.go index 9fb4206f..7e9d08a9 100644 --- a/src/arduino.cc/builder/builder.go +++ b/src/arduino.cc/builder/builder.go @@ -31,9 +31,9 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/phases" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "os" "reflect" "strconv" @@ -66,10 +66,8 @@ const DEFAULT_BUILD_CORE = "arduino" type Builder struct{} -func (s *Builder) Run(context map[string]interface{}) error { +func (s *Builder) Run(ctx *types.Context) error { commands := []types.Command{ - &SetupHumanLoggerIfMissing{}, - &GenerateBuildPathIfMissing{}, &EnsureBuildPathExists{}, @@ -115,14 +113,14 @@ func (s *Builder) Run(context map[string]interface{}) error { &RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_POSTBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX}, } - mainErr := runCommands(context, commands, true) + mainErr := runCommands(ctx, commands, true) commands = []types.Command{ &PrintUsedAndNotUsedLibraries{}, &PrintUsedLibrariesIfVerbose{}, } - otherErr := runCommands(context, commands, false) + otherErr := runCommands(ctx, commands, false) if mainErr != nil { return mainErr @@ -133,10 +131,8 @@ func (s *Builder) Run(context map[string]interface{}) error { type Preprocess struct{} -func (s *Preprocess) Run(context map[string]interface{}) error { +func (s *Preprocess) Run(ctx *types.Context) error { commands := []types.Command{ - &SetupHumanLoggerIfMissing{}, - &GenerateBuildPathIfMissing{}, &EnsureBuildPathExists{}, @@ -157,15 +153,13 @@ func (s *Preprocess) Run(context map[string]interface{}) error { &PrintPreprocessedSource{}, } - return runCommands(context, commands, true) + return runCommands(ctx, commands, true) } type ParseHardwareAndDumpBuildProperties struct{} -func (s *ParseHardwareAndDumpBuildProperties) Run(context map[string]interface{}) error { +func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error { commands := []types.Command{ - &SetupHumanLoggerIfMissing{}, - &GenerateBuildPathIfMissing{}, &ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -173,57 +167,57 @@ func (s *ParseHardwareAndDumpBuildProperties) Run(context map[string]interface{} &DumpBuildProperties{}, } - return runCommands(context, commands, true) + return runCommands(ctx, commands, true) } -func runCommands(context map[string]interface{}, commands []types.Command, progressEnabled bool) error { +func runCommands(ctx *types.Context, commands []types.Command, progressEnabled bool) error { commandsLength := len(commands) progressForEachCommand := float32(100) / float32(commandsLength) progress := float32(0) for _, command := range commands { - PrintRingNameIfDebug(context, command) - printProgressIfProgressEnabledAndMachineLogger(progressEnabled, context, progress) - err := command.Run(context) + PrintRingNameIfDebug(ctx, command) + printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, progress) + err := command.Run(ctx) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } progress += progressForEachCommand } - printProgressIfProgressEnabledAndMachineLogger(progressEnabled, context, 100) + printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, 100) return nil } -func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, context map[string]interface{}, progress float32) { +func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, ctx *types.Context, progress float32) { if !progressEnabled { return } - log := utils.Logger(context) + log := ctx.GetLogger() if log.Name() == "machine" { log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(float64(progress), 'f', 2, 32)) } } -func PrintRingNameIfDebug(context map[string]interface{}, command types.Command) { - if utils.DebugLevel(context) >= 10 { - utils.Logger(context).Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_COMMAND, strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name()) +func PrintRingNameIfDebug(ctx *types.Context, command types.Command) { + if ctx.DebugLevel >= 10 { + ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_COMMAND, strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name()) } } -func RunBuilder(context map[string]interface{}) error { +func RunBuilder(ctx *types.Context) error { command := Builder{} - return command.Run(context) + return command.Run(ctx) } -func RunParseHardwareAndDumpBuildProperties(context map[string]interface{}) error { +func RunParseHardwareAndDumpBuildProperties(ctx *types.Context) error { command := ParseHardwareAndDumpBuildProperties{} - return command.Run(context) + return command.Run(ctx) } -func RunPreprocess(context map[string]interface{}) error { +func RunPreprocess(ctx *types.Context) error { command := Preprocess{} - return command.Run(context) + return command.Run(ctx) } diff --git a/src/arduino.cc/builder/builder_utils/utils.go b/src/arduino.cc/builder/builder_utils/utils.go index 9d0dc06d..af5aabdc 100644 --- a/src/arduino.cc/builder/builder_utils/utils.go +++ b/src/arduino.cc/builder/builder_utils/utils.go @@ -45,18 +45,18 @@ import ( func CompileFilesRecursive(objectFiles []string, sourcePath string, buildPath string, buildProperties props.PropertiesMap, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) { objectFiles, err := CompileFiles(objectFiles, sourcePath, false, buildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } folders, err := utils.ReadDirFiltered(sourcePath, utils.FilterDirs) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } for _, folder := range folders { objectFiles, err = CompileFilesRecursive(objectFiles, filepath.Join(sourcePath, folder.Name()), filepath.Join(buildPath, folder.Name()), buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } } @@ -66,15 +66,15 @@ func CompileFilesRecursive(objectFiles []string, sourcePath string, buildPath st func CompileFiles(objectFiles []string, sourcePath string, recurse bool, buildPath string, buildProperties props.PropertiesMap, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) { objectFiles, err := compileFilesWithExtensionWithRecipe(objectFiles, sourcePath, recurse, buildPath, buildProperties, includes, ".S", constants.RECIPE_S_PATTERN, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } objectFiles, err = compileFilesWithExtensionWithRecipe(objectFiles, sourcePath, recurse, buildPath, buildProperties, includes, ".c", constants.RECIPE_C_PATTERN, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } objectFiles, err = compileFilesWithExtensionWithRecipe(objectFiles, sourcePath, recurse, buildPath, buildProperties, includes, ".cpp", constants.RECIPE_CPP_PATTERN, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } return objectFiles, nil } @@ -82,7 +82,7 @@ func CompileFiles(objectFiles []string, sourcePath string, recurse bool, buildPa func compileFilesWithExtensionWithRecipe(objectFiles []string, sourcePath string, recurse bool, buildPath string, buildProperties props.PropertiesMap, includes []string, extension string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) { sources, err := findFilesInFolder(sourcePath, extension, recurse) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } return compileFilesWithRecipe(objectFiles, sourcePath, sources, buildPath, buildProperties, includes, recipe, verbose, warningsLevel, logger) } @@ -90,7 +90,7 @@ func compileFilesWithExtensionWithRecipe(objectFiles []string, sourcePath string func findFilesInFolder(sourcePath string, extension string, recurse bool) ([]string, error) { files, err := utils.ReadDirFiltered(sourcePath, utils.FilterFilesWithExtension(extension)) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } var sources []string for _, file := range files { @@ -100,13 +100,13 @@ func findFilesInFolder(sourcePath string, extension string, recurse bool) ([]str if recurse { folders, err := utils.ReadDirFiltered(sourcePath, utils.FilterDirs) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } for _, folder := range folders { otherSources, err := findFilesInFolder(filepath.Join(sourcePath, folder.Name()), extension, recurse) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } sources = append(sources, otherSources...) } @@ -119,7 +119,7 @@ func compileFilesWithRecipe(objectFiles []string, sourcePath string, sources []s for _, source := range sources { objectFile, err := compileFileWithRecipe(sourcePath, source, buildPath, buildProperties, includes, recipe, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } objectFiles = append(objectFiles, objectFile) @@ -134,24 +134,24 @@ func compileFileWithRecipe(sourcePath string, source string, buildPath string, b properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = source relativeSource, err := filepath.Rel(sourcePath, source) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = filepath.Join(buildPath, relativeSource+".o") err = utils.EnsureFolderExists(filepath.Dir(properties[constants.BUILD_PROPERTIES_OBJECT_FILE])) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } objIsUpToDate, err := ObjFileIsUpToDate(properties[constants.BUILD_PROPERTIES_SOURCE_FILE], properties[constants.BUILD_PROPERTIES_OBJECT_FILE], filepath.Join(buildPath, relativeSource+".d")) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } if !objIsUpToDate { _, err = ExecRecipe(properties, recipe, false, verbose, verbose, logger) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } } else if verbose { logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_PREVIOUS_COMPILED_FILE, properties[constants.BUILD_PROPERTIES_OBJECT_FILE]) @@ -167,7 +167,7 @@ func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile string) (bool, err sourceFileStat, err := os.Stat(sourceFile) if err != nil { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } objectFileStat, err := os.Stat(objectFile) @@ -175,7 +175,7 @@ func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile string) (bool, err if os.IsNotExist(err) { return false, nil } else { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } } @@ -184,7 +184,7 @@ func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile string) (bool, err if os.IsNotExist(err) { return false, nil } else { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } } @@ -197,7 +197,7 @@ func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile string) (bool, err rows, err := utils.ReadFileToRows(dependencyFile) if err != nil { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } rows = utils.Map(rows, removeEndingBackSlash) @@ -222,7 +222,7 @@ func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile string) (bool, err for _, row := range rows { depStat, err := os.Stat(row) if err != nil && !os.IsNotExist(err) { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } if os.IsNotExist(err) { return false, nil @@ -260,7 +260,7 @@ func ArchiveCompiledFiles(buildPath string, archiveFile string, objectFiles []st if _, err := os.Stat(archiveFilePath); err == nil { err = os.Remove(archiveFilePath) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } } @@ -272,7 +272,7 @@ func ArchiveCompiledFiles(buildPath string, archiveFile string, objectFiles []st _, err := ExecRecipe(properties, constants.RECIPE_AR_PATTERN, false, verbose, verbose, logger) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } } @@ -282,7 +282,7 @@ func ArchiveCompiledFiles(buildPath string, archiveFile string, objectFiles []st func ExecRecipe(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) ([]byte, error) { command, err := PrepareCommandForRecipe(properties, recipe, removeUnsetProperties, echoCommandLine, echoOutput, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } if echoOutput { @@ -293,17 +293,17 @@ func ExecRecipe(properties props.PropertiesMap, recipe string, removeUnsetProper if echoOutput { err := command.Run() - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } bytes, err := command.Output() - return bytes, utils.WrapError(err) + return bytes, i18n.WrapError(err) } func PrepareCommandForRecipe(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (*exec.Cmd, error) { pattern := properties[recipe] if pattern == constants.EMPTY_STRING { - return nil, utils.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe) + return nil, i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe) } var err error @@ -311,13 +311,13 @@ func PrepareCommandForRecipe(properties props.PropertiesMap, recipe string, remo if removeUnsetProperties { commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } } command, err := utils.PrepareCommand(commandLine, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } if echoCommandLine { @@ -330,7 +330,7 @@ func PrepareCommandForRecipe(properties props.PropertiesMap, recipe string, remo func ExecRecipeCollectStdErr(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (string, error) { command, err := PrepareCommandForRecipe(properties, recipe, removeUnsetProperties, echoCommandLine, echoOutput, logger) if err != nil { - return "", utils.WrapError(err) + return "", i18n.WrapError(err) } buffer := &bytes.Buffer{} diff --git a/src/arduino.cc/builder/coan_runner.go b/src/arduino.cc/builder/coan_runner.go deleted file mode 100644 index 01420d2f..00000000 --- a/src/arduino.cc/builder/coan_runner.go +++ /dev/null @@ -1,90 +0,0 @@ -/* - * This file is part of Arduino Builder. - * - * Arduino Builder is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - */ - -package builder - -import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" - "arduino.cc/builder/props" - "arduino.cc/builder/utils" - "fmt" - "path/filepath" - "regexp" -) - -var ALLOWED_ARG = regexp.MustCompile("^source$|\\-E$|\\-m$|\\-P$|\\-kb$|\\-D.*") - -type CoanRunner struct{} - -func (s *CoanRunner) Run(context map[string]interface{}) error { - source := context[constants.CTX_SOURCE].(string) - source += "\n" - verbose := context[constants.CTX_VERBOSE].(bool) - - preprocPath := context[constants.CTX_PREPROC_PATH].(string) - err := utils.EnsureFolderExists(preprocPath) - if err != nil { - return utils.WrapError(err) - } - - coanTargetFileName := filepath.Join(preprocPath, constants.FILE_COAN_TARGET) - err = utils.WriteFile(coanTargetFileName, source) - if err != nil { - return utils.WrapError(err) - } - - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - properties := buildProperties.Clone() - properties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.COAN)) - properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = coanTargetFileName - - pattern := properties[constants.BUILD_PROPERTIES_PATTERN] - if pattern == constants.EMPTY_STRING { - return utils.Errorf(context, constants.MSG_PATTERN_MISSING, constants.COAN) - } - - logger := context[constants.CTX_LOGGER].(i18n.Logger) - commandLine := properties.ExpandPropsInString(pattern) - command, err := utils.PrepareCommandFilteredArgs(commandLine, filterAllowedArg, logger) - - if verbose { - fmt.Println(commandLine) - } - - sourceBytes, _ := command.Output() - - context[constants.CTX_SOURCE] = string(sourceBytes) - - return nil -} - -func filterAllowedArg(idx int, part string, parts []string) bool { - return idx == len(parts)-1 || ALLOWED_ARG.MatchString(part) -} diff --git a/src/arduino.cc/builder/collect_all_source_files_from_folders_with_sources.go b/src/arduino.cc/builder/collect_all_source_files_from_folders_with_sources.go index d9b26c60..efd51c18 100644 --- a/src/arduino.cc/builder/collect_all_source_files_from_folders_with_sources.go +++ b/src/arduino.cc/builder/collect_all_source_files_from_folders_with_sources.go @@ -30,8 +30,8 @@ package builder import ( - "arduino.cc/builder/constants" "arduino.cc/builder/gohasissues" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "io/ioutil" @@ -42,9 +42,9 @@ import ( type CollectAllSourceFilesFromFoldersWithSources struct{} -func (s *CollectAllSourceFilesFromFoldersWithSources) Run(context map[string]interface{}) error { - foldersWithSources := context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE].(*types.UniqueSourceFolderQueue) - sourceFiles := context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE].(*types.UniqueStringQueue) +func (s *CollectAllSourceFilesFromFoldersWithSources) Run(ctx *types.Context) error { + foldersWithSources := ctx.FoldersWithSourceFiles + sourceFiles := ctx.CollectedSourceFiles filePaths := []string{} for !foldersWithSources.Empty() { @@ -56,7 +56,7 @@ func (s *CollectAllSourceFilesFromFoldersWithSources) Run(context map[string]int err = collectByReadDir(&filePaths, sourceFolder.Folder) } if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } @@ -75,7 +75,7 @@ func collectByWalk(filePaths *[]string, folder string) error { } walkFunc := utils.CollectAllReadableFiles(filePaths, checkExtensionFunc) err := gohasissues.Walk(folder, walkFunc) - return utils.WrapError(err) + return i18n.WrapError(err) } func collectByReadDir(filePaths *[]string, folder string) error { @@ -85,7 +85,7 @@ func collectByReadDir(filePaths *[]string, folder string) error { files, err := ioutil.ReadDir(folder) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { ext := strings.ToLower(filepath.Ext(file.Name())) diff --git a/src/arduino.cc/builder/collect_ctags_from_sketch_files.go b/src/arduino.cc/builder/collect_ctags_from_sketch_files.go index 471f3411..aee6935c 100644 --- a/src/arduino.cc/builder/collect_ctags_from_sketch_files.go +++ b/src/arduino.cc/builder/collect_ctags_from_sketch_files.go @@ -30,7 +30,6 @@ package builder import ( - "arduino.cc/builder/constants" "arduino.cc/builder/types" "arduino.cc/builder/utils" "strings" @@ -38,11 +37,10 @@ import ( type CollectCTagsFromSketchFiles struct{} -func (s *CollectCTagsFromSketchFiles) Run(context map[string]interface{}) error { - sketch := context[constants.CTX_SKETCH].(*types.Sketch) - sketchFileNames := collectSketchFileNamesFrom(sketch) +func (s *CollectCTagsFromSketchFiles) Run(ctx *types.Context) error { + sketchFileNames := collectSketchFileNamesFrom(ctx.Sketch) - allCtags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + allCtags := ctx.CTagsOfPreprocessedSource ctagsOfSketch := []*types.CTag{} for _, ctag := range allCtags { if utils.SliceContains(sketchFileNames, strings.Replace(ctag.Filename, "\\\\", "\\", -1)) { @@ -50,7 +48,7 @@ func (s *CollectCTagsFromSketchFiles) Run(context map[string]interface{}) error } } - context[constants.CTX_COLLECTED_CTAGS] = ctagsOfSketch + ctx.CTagsCollected = ctagsOfSketch return nil } diff --git a/src/arduino.cc/builder/compare_prototypes_from_source_and_preproc_source.go b/src/arduino.cc/builder/compare_prototypes_from_source_and_preproc_source.go index 7139da2d..0f19b7f1 100644 --- a/src/arduino.cc/builder/compare_prototypes_from_source_and_preproc_source.go +++ b/src/arduino.cc/builder/compare_prototypes_from_source_and_preproc_source.go @@ -29,16 +29,15 @@ package builder -import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/types" -) +// XXX: Obsolete? + +import "arduino.cc/builder/types" type ComparePrototypesFromSourceAndPreprocSource struct{} -func (s *ComparePrototypesFromSourceAndPreprocSource) Run(context map[string]interface{}) error { - ctagsOfSource := context[constants.CTX_CTAGS_OF_SOURCE].([]*types.CTag) - ctagsOfPreprocSource := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) +func (s *ComparePrototypesFromSourceAndPreprocSource) Run(ctx *types.Context) error { + ctagsOfSource := ctx.CTagsOfSource + ctagsOfPreprocSource := ctx.CTagsOfPreprocessedSource actualCTags := []*types.CTag{} for _, ctagOfPreprocSource := range ctagsOfPreprocSource { @@ -47,7 +46,7 @@ func (s *ComparePrototypesFromSourceAndPreprocSource) Run(context map[string]int } } - context[constants.CTX_COLLECTED_CTAGS] = actualCTags + ctx.CTagsCollected = actualCTags return nil } diff --git a/src/arduino.cc/builder/constants/constants.go b/src/arduino.cc/builder/constants/constants.go index 078ac08d..15d93824 100644 --- a/src/arduino.cc/builder/constants/constants.go +++ b/src/arduino.cc/builder/constants/constants.go @@ -66,79 +66,17 @@ const BUILD_PROPERTIES_RUNTIME_OS = "runtime.os" const BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH = "runtime.platform.path" const BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX = "runtime.tools." const BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX = ".path" +const BUILD_PROPERTIES_RUNTIME_IDE_VERSION = "runtime.ide.version" const BUILD_PROPERTIES_SOFTWARE = "software" const BUILD_PROPERTIES_SOURCE_FILE = "source_file" const BUILD_PROPERTIES_SOURCE_PATH = "build.source.path" const BUILD_PROPERTIES_TOOLS_KEY = "tools" const BUILD_PROPERTIES_VID = "vid" -const COAN = "coan" const CTAGS = "ctags" -const CTX_ACTUAL_PLATFORM = "actualPlatform" -const CTX_ARCHIVE_FILE_PATH_CORE = "archiveFileCore" -const CTX_BUILD_CORE = "buildCore" -const CTX_BUILD_OPTIONS = "buildOptions" -const CTX_BUILD_OPTIONS_JSON = "buildOptionsJson" -const CTX_BUILD_OPTIONS_PREVIOUS_JSON = "buildOptionsPreviousJson" -const CTX_BUILD_PATH = "buildPath" -const CTX_BUILD_PROPERTIES = "buildProperties" -const CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION = "runtime.ide.version" -const CTX_BUILT_IN_LIBRARIES_FOLDERS = "builtInLibrariesFolders" -const CTX_COLLECTED_CTAGS = "collectedCTags" -const CTX_COLLECTED_SOURCE_FILES_QUEUE = "collectedSourceFilesQueue" -const CTX_CORE_BUILD_PATH = "coreBuildPath" -const CTX_CTAGS_OF_PREPROC_SOURCE = "ctagsOfPreprocSource" -const CTX_CTAGS_OF_SOURCE = "ctagsOfSource" -const CTX_CTAGS_OUTPUT = "ctagsOutput" -const CTX_CTAGS_TEMP_FILE_PATH = "ctagsTempFilePath" -const CTX_CUSTOM_BUILD_PROPERTIES = "customBuildProperties" -const CTX_DEBUG_LEVEL = "debugLevel" -const CTX_DEBUG_PREPROCESSOR = "debugPreprocessor" -const CTX_FILE_PATH_TO_READ = "filePathToRead" -const CTX_FOLDERS_WITH_SOURCES_QUEUE = "foldersWithSourcesQueue" -const CTX_FQBN = "fqbn" -const CTX_GCC_MINUS_E_SOURCE = "gccMinusESource" -const CTX_GCC_MINUS_M_OUTPUT = "gccMinusMOutput" -const CTX_HARDWARE_FOLDERS = "hardwareFolders" -const CTX_HARDWARE = "hardware" -const CTX_HARDWARE_REWRITE_RESULTS = "hardwareRewriteResults" -const CTX_HEADER_TO_LIBRARIES = "headerToLibraries" -const CTX_IMPORTED_LIBRARIES = "importedLibraries" -const CTX_INCLUDE_FOLDERS = "includeFolders" -const CTX_INCLUDE_SECTION = "includeSection" -const CTX_INCLUDES = "includes" -const CTX_INCLUDES_JUST_FOUND = "includesJustFound" -const CTX_LIBRARIES_BUILD_PATH = "librariesBuildPath" -const CTX_LIBRARIES_FOLDERS = "librariesFolders" -const CTX_LIBRARIES = "libraries" -const CTX_LIBRARY_RESOLUTION_RESULTS = "libraryResolutionResults" -const CTX_LINE_OFFSET = "lineOffset" -const CTX_LINE_WHERE_TO_INSERT_PROTOTYPES = "lineWhereToInsertPrototypes" -const CTX_LOGGER = "logger" -const CTX_OBJECT_FILES_CORE = "objectFilesCore" -const CTX_OBJECT_FILES_LIBRARIES = "objectFilesLibraries" -const CTX_OBJECT_FILES_SKETCH = "objectFilesSketch" -const CTX_OTHER_LIBRARIES_FOLDERS = "otherLibrariesFolders" -const CTX_PLATFORM_KEYS_REWRITE = "platformKeysRewrite" -const CTX_PREPROC_PATH = "preprocPath" -const CTX_PROTOTYPE_SECTION = "prototypeSection" -const CTX_PROTOTYPES = "prototypes" -const CTX_SKETCH_BUILD_PATH = "sketchBuildPath" -const CTX_SKETCH_LOCATION = "sketchLocation" -const CTX_SKETCH = "sketch" -const CTX_SOURCE = "source" -const CTX_TARGET_BOARD = "targetBoard" -const CTX_TARGET_PACKAGE = "targetPackage" -const CTX_TARGET_PLATFORM = "targetPlatform" -const CTX_TOOLS_FOLDERS = "toolsFolders" -const CTX_TOOLS = "tools" -const CTX_VERBOSE = "verbose" -const CTX_VIDPID = "VIDPID" -const CTX_WARNINGS_LEVEL = "warningLevel" const EMPTY_STRING = "" const FILE_BOARDS_LOCAL_TXT = "boards.local.txt" const FILE_BOARDS_TXT = "boards.txt" const FILE_BUILTIN_TOOLS_VERSIONS_TXT = "builtin_tools_versions.txt" -const FILE_COAN_TARGET = "coan_target.cpp" const FILE_CTAGS_TARGET = "ctags_target.cpp" const FILE_CTAGS_TARGET_FOR_GCC_MINUS_E = "ctags_target_for_gcc_minus_e.cpp" const FILE_GCC_PREPROC_TARGET = "gcc_preproc_target.cpp" diff --git a/src/arduino.cc/builder/container_add_prototypes.go b/src/arduino.cc/builder/container_add_prototypes.go index 6c337af9..28ba1645 100644 --- a/src/arduino.cc/builder/container_add_prototypes.go +++ b/src/arduino.cc/builder/container_add_prototypes.go @@ -32,17 +32,17 @@ package builder import ( "arduino.cc/builder/constants" "arduino.cc/builder/ctags" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" ) type ContainerAddPrototypes struct{} -func (s *ContainerAddPrototypes) Run(context map[string]interface{}) error { +func (s *ContainerAddPrototypes) Run(ctx *types.Context) error { commands := []types.Command{ &GCCPreprocRunner{TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E}, - &ReadFileAndStoreInContext{TargetField: constants.CTX_GCC_MINUS_E_SOURCE}, - &CTagsTargetFileSaver{SourceField: constants.CTX_GCC_MINUS_E_SOURCE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E}, + &ReadFileAndStoreInContext{Target: &ctx.SourceGccMinusE}, + &CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E}, &ctags.CTagsRunner{}, &ctags.CTagsParser{}, &CollectCTagsFromSketchFiles{}, @@ -52,10 +52,10 @@ func (s *ContainerAddPrototypes) Run(context map[string]interface{}) error { } for _, command := range commands { - PrintRingNameIfDebug(context, command) - err := command.Run(context) + PrintRingNameIfDebug(ctx, command) + err := command.Run(ctx) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } diff --git a/src/arduino.cc/builder/container_build_options.go b/src/arduino.cc/builder/container_build_options.go index 168547a9..eff01468 100644 --- a/src/arduino.cc/builder/container_build_options.go +++ b/src/arduino.cc/builder/container_build_options.go @@ -30,13 +30,13 @@ package builder import ( + "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" ) type ContainerBuildOptions struct{} -func (s *ContainerBuildOptions) Run(context map[string]interface{}) error { +func (s *ContainerBuildOptions) Run(ctx *types.Context) error { commands := []types.Command{ &CreateBuildOptionsMap{}, &LoadPreviousBuildOptionsMap{}, @@ -45,10 +45,10 @@ func (s *ContainerBuildOptions) Run(context map[string]interface{}) error { } for _, command := range commands { - PrintRingNameIfDebug(context, command) - err := command.Run(context) + PrintRingNameIfDebug(ctx, command) + err := command.Run(ctx) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } diff --git a/src/arduino.cc/builder/container_find_includes.go b/src/arduino.cc/builder/container_find_includes.go index ae6a2396..e513747a 100644 --- a/src/arduino.cc/builder/container_find_includes.go +++ b/src/arduino.cc/builder/container_find_includes.go @@ -31,6 +31,7 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "path/filepath" @@ -38,23 +39,23 @@ import ( type ContainerFindIncludes struct{} -func (s *ContainerFindIncludes) Run(context map[string]interface{}) error { - err := runCommand(context, &IncludesToIncludeFolders{}) +func (s *ContainerFindIncludes) Run(ctx *types.Context) error { + err := runCommand(ctx, &IncludesToIncludeFolders{}) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string) - sketch := context[constants.CTX_SKETCH].(*types.Sketch) - err = findIncludesUntilDone(context, filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp")) + sketchBuildPath := ctx.SketchBuildPath + sketch := ctx.Sketch + err = findIncludesUntilDone(ctx, filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp")) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - foldersWithSources := context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE].(*types.UniqueSourceFolderQueue) - foldersWithSources.Push(types.SourceFolder{Folder: context[constants.CTX_SKETCH_BUILD_PATH].(string), Recurse: true}) - if utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) { - for _, library := range context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) { + foldersWithSources := ctx.FoldersWithSourceFiles + foldersWithSources.Push(types.SourceFolder{Folder: ctx.SketchBuildPath, Recurse: true}) + if len(ctx.ImportedLibraries) > 0 { + for _, library := range ctx.ImportedLibraries { sourceFolders := types.LibraryToSourceFolder(library) for _, sourceFolder := range sourceFolders { foldersWithSources.Push(sourceFolder) @@ -62,65 +63,65 @@ func (s *ContainerFindIncludes) Run(context map[string]interface{}) error { } } - err = runCommand(context, &CollectAllSourceFilesFromFoldersWithSources{}) + err = runCommand(ctx, &CollectAllSourceFilesFromFoldersWithSources{}) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - sourceFilePaths := context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE].(*types.UniqueStringQueue) + sourceFilePaths := ctx.CollectedSourceFiles for !sourceFilePaths.Empty() { - err = findIncludesUntilDone(context, sourceFilePaths.Pop().(string)) + err = findIncludesUntilDone(ctx, sourceFilePaths.Pop().(string)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - err := runCommand(context, &CollectAllSourceFilesFromFoldersWithSources{}) + err := runCommand(ctx, &CollectAllSourceFilesFromFoldersWithSources{}) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } - err = runCommand(context, &FailIfImportedLibraryIsWrong{}) + err = runCommand(ctx, &FailIfImportedLibraryIsWrong{}) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } return nil } -func runCommand(context map[string]interface{}, command types.Command) error { - PrintRingNameIfDebug(context, command) - err := command.Run(context) +func runCommand(ctx *types.Context, command types.Command) error { + PrintRingNameIfDebug(ctx, command) + err := command.Run(ctx) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } return nil } -func findIncludesUntilDone(context map[string]interface{}, sourceFilePath string) error { +func findIncludesUntilDone(ctx *types.Context, sourceFilePath string) error { targetFilePath := utils.NULLFile() - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries done := false for !done { commands := []types.Command{ &GCCPreprocRunnerForDiscoveringIncludes{SourceFilePath: sourceFilePath, TargetFilePath: targetFilePath}, - &IncludesFinderWithRegExp{ContextField: constants.CTX_GCC_MINUS_E_SOURCE}, + &IncludesFinderWithRegExp{Source: &ctx.SourceGccMinusE}, &IncludesToIncludeFolders{}, } for _, command := range commands { - err := runCommand(context, command) + err := runCommand(ctx, command) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } - if len(context[constants.CTX_INCLUDES_JUST_FOUND].([]string)) == 0 { + if len(ctx.IncludesJustFound) == 0 { done = true - } else if len(context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)) == len(importedLibraries) { - err := runCommand(context, &GCCPreprocRunner{TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E}) - return utils.WrapError(err) + } else if len(ctx.ImportedLibraries) == len(importedLibraries) { + err := runCommand(ctx, &GCCPreprocRunner{TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E}) + return i18n.WrapError(err) } - importedLibraries = context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) - context[constants.CTX_INCLUDES_JUST_FOUND] = []string{} + importedLibraries = ctx.ImportedLibraries + ctx.IncludesJustFound = []string{} } return nil } diff --git a/src/arduino.cc/builder/container_merge_copy_sketch_files.go b/src/arduino.cc/builder/container_merge_copy_sketch_files.go index 3dc8eec3..2d844b2f 100644 --- a/src/arduino.cc/builder/container_merge_copy_sketch_files.go +++ b/src/arduino.cc/builder/container_merge_copy_sketch_files.go @@ -30,13 +30,13 @@ package builder import ( + "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" ) type ContainerMergeCopySketchFiles struct{} -func (s *ContainerMergeCopySketchFiles) Run(context map[string]interface{}) error { +func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error { commands := []types.Command{ &SketchSourceMerger{}, &SketchSaver{}, @@ -44,10 +44,10 @@ func (s *ContainerMergeCopySketchFiles) Run(context map[string]interface{}) erro } for _, command := range commands { - PrintRingNameIfDebug(context, command) - err := command.Run(context) + PrintRingNameIfDebug(ctx, command) + err := command.Run(ctx) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } diff --git a/src/arduino.cc/builder/container_setup.go b/src/arduino.cc/builder/container_setup.go index 9c702b1d..e76bb500 100644 --- a/src/arduino.cc/builder/container_setup.go +++ b/src/arduino.cc/builder/container_setup.go @@ -30,13 +30,13 @@ package builder import ( + "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" ) type ContainerSetupHardwareToolsLibsSketchAndProps struct{} -func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(context map[string]interface{}) error { +func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) error { commands := []types.Command{ &AddAdditionalEntriesToContext{}, &FailIfBuildPathEqualsSketchPath{}, @@ -55,10 +55,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(context map[string]i } for _, command := range commands { - PrintRingNameIfDebug(context, command) - err := command.Run(context) + PrintRingNameIfDebug(ctx, command) + err := command.Run(ctx) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } diff --git a/src/arduino.cc/builder/create_build_options_map.go b/src/arduino.cc/builder/create_build_options_map.go index 3d05db6d..d9caa59f 100644 --- a/src/arduino.cc/builder/create_build_options_map.go +++ b/src/arduino.cc/builder/create_build_options_map.go @@ -30,54 +30,21 @@ package builder import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/utils" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "encoding/json" - "reflect" - "strings" ) type CreateBuildOptionsMap struct{} -func (s *CreateBuildOptionsMap) Run(context map[string]interface{}) error { - buildOptions := make(map[string]string) - - buildOptionsMapKeys := []string{ - constants.CTX_HARDWARE_FOLDERS, - constants.CTX_TOOLS_FOLDERS, - constants.CTX_BUILT_IN_LIBRARIES_FOLDERS, - constants.CTX_OTHER_LIBRARIES_FOLDERS, - constants.CTX_FQBN, - constants.CTX_SKETCH_LOCATION, - constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION, - constants.CTX_CUSTOM_BUILD_PROPERTIES, - } - - for _, key := range buildOptionsMapKeys { - if utils.MapHas(context, key) { - originalValue := context[key] - value := constants.EMPTY_STRING - kindOfValue := reflect.TypeOf(originalValue).Kind() - if kindOfValue == reflect.Slice { - value = strings.Join(originalValue.([]string), ",") - } else if kindOfValue == reflect.String { - value = originalValue.(string) - } else { - return utils.Errorf(context, constants.MSG_UNHANDLED_TYPE_IN_CONTEXT, kindOfValue.String(), key) - } - - buildOptions[key] = value - } - } - - context[constants.CTX_BUILD_OPTIONS] = buildOptions - +func (s *CreateBuildOptionsMap) Run(ctx *types.Context) error { + buildOptions := ctx.ExtractBuildOptions() bytes, err := json.MarshalIndent(buildOptions, "", " ") if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_BUILD_OPTIONS_JSON] = string(bytes) + ctx.BuildOptionsJson = string(bytes) return nil } diff --git a/src/arduino.cc/builder/ctags/ctags_parser.go b/src/arduino.cc/builder/ctags/ctags_parser.go index a4a6b0e0..55497155 100644 --- a/src/arduino.cc/builder/ctags/ctags_parser.go +++ b/src/arduino.cc/builder/ctags/ctags_parser.go @@ -32,7 +32,6 @@ package ctags import ( "arduino.cc/builder/constants" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "bufio" "os" "reflect" @@ -57,8 +56,8 @@ var KNOWN_TAG_KINDS = map[string]bool{ type CTagsParser struct{} -func (s *CTagsParser) Run(context map[string]interface{}) error { - rows := strings.Split(context[constants.CTX_CTAGS_OUTPUT].(string), "\n") +func (s *CTagsParser) Run(ctx *types.Context) error { + rows := strings.Split(ctx.CTagsOutput, "\n") rows = removeEmpty(rows) @@ -67,14 +66,14 @@ func (s *CTagsParser) Run(context map[string]interface{}) error { tags = append(tags, parseTag(row)) } - skipTagsWhere(tags, tagIsUnknown, context) - skipTagsWhere(tags, tagIsUnhandled, context) + skipTagsWhere(tags, tagIsUnknown, ctx) + skipTagsWhere(tags, tagIsUnhandled, ctx) addPrototypes(tags) - removeDefinedProtypes(tags, context) + removeDefinedProtypes(tags, ctx) removeDuplicate(tags) - skipTagsWhere(tags, prototypeAndCodeDontMatch, context) + skipTagsWhere(tags, prototypeAndCodeDontMatch, ctx) - context[constants.CTX_CTAGS_OF_PREPROC_SOURCE] = tags + ctx.CTagsOfPreprocessedSource = tags return nil } @@ -109,7 +108,7 @@ func addPrototype(tag *types.CTag) { tag.PrototypeModifiers = strings.TrimSpace(tag.PrototypeModifiers) } -func removeDefinedProtypes(tags []*types.CTag, context map[string]interface{}) { +func removeDefinedProtypes(tags []*types.CTag, ctx *types.Context) { definedPrototypes := make(map[string]bool) for _, tag := range tags { if tag.Kind == KIND_PROTOTYPE { @@ -119,8 +118,8 @@ func removeDefinedProtypes(tags []*types.CTag, context map[string]interface{}) { for _, tag := range tags { if definedPrototypes[tag.Prototype] { - if utils.DebugLevel(context) >= 10 { - utils.Logger(context).Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_ALREADY_DEFINED, tag.FunctionName) + if ctx.DebugLevel >= 10 { + ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_ALREADY_DEFINED, tag.FunctionName) } tag.SkipMe = true } @@ -141,12 +140,12 @@ func removeDuplicate(tags []*types.CTag) { type skipFuncType func(tag *types.CTag) bool -func skipTagsWhere(tags []*types.CTag, skipFunc skipFuncType, context map[string]interface{}) { +func skipTagsWhere(tags []*types.CTag, skipFunc skipFuncType, ctx *types.Context) { for _, tag := range tags { if !tag.SkipMe { skip := skipFunc(tag) - if skip && utils.DebugLevel(context) >= 10 { - utils.Logger(context).Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_WITH_REASON, tag.FunctionName, runtime.FuncForPC(reflect.ValueOf(skipFunc).Pointer()).Name()) + if skip && ctx.DebugLevel >= 10 { + ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_WITH_REASON, tag.FunctionName, runtime.FuncForPC(reflect.ValueOf(skipFunc).Pointer()).Name()) } tag.SkipMe = skip } diff --git a/src/arduino.cc/builder/ctags/ctags_runner.go b/src/arduino.cc/builder/ctags/ctags_runner.go index 4a603fd9..b58242d7 100644 --- a/src/arduino.cc/builder/ctags/ctags_runner.go +++ b/src/arduino.cc/builder/ctags/ctags_runner.go @@ -32,17 +32,17 @@ package ctags import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" - "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "fmt" ) type CTagsRunner struct{} -func (s *CTagsRunner) Run(context map[string]interface{}) error { - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - ctagsTargetFilePath := context[constants.CTX_CTAGS_TEMP_FILE_PATH].(string) - logger := context[constants.CTX_LOGGER].(i18n.Logger) +func (s *CTagsRunner) Run(ctx *types.Context) error { + buildProperties := ctx.BuildProperties + ctagsTargetFilePath := ctx.CTagsTargetFile + logger := ctx.GetLogger() properties := buildProperties.Clone() properties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS)) @@ -50,26 +50,26 @@ func (s *CTagsRunner) Run(context map[string]interface{}) error { pattern := properties[constants.BUILD_PROPERTIES_PATTERN] if pattern == constants.EMPTY_STRING { - return utils.Errorf(context, constants.MSG_PATTERN_MISSING, constants.CTAGS) + return i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, constants.CTAGS) } commandLine := properties.ExpandPropsInString(pattern) command, err := utils.PrepareCommand(commandLine, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - verbose := context[constants.CTX_VERBOSE].(bool) + verbose := ctx.Verbose if verbose { fmt.Println(commandLine) } sourceBytes, err := command.Output() if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_CTAGS_OUTPUT] = string(sourceBytes) + ctx.CTagsOutput = string(sourceBytes) return nil } diff --git a/src/arduino.cc/builder/ctags/ctags_to_prototypes.go b/src/arduino.cc/builder/ctags/ctags_to_prototypes.go index 73e05482..c6528fec 100644 --- a/src/arduino.cc/builder/ctags/ctags_to_prototypes.go +++ b/src/arduino.cc/builder/ctags/ctags_to_prototypes.go @@ -30,24 +30,21 @@ package ctags import ( - "arduino.cc/builder/constants" "arduino.cc/builder/types" "strings" ) type CTagsToPrototypes struct{} -func (s *CTagsToPrototypes) Run(context map[string]interface{}) error { - tags := context[constants.CTX_COLLECTED_CTAGS].([]*types.CTag) +func (s *CTagsToPrototypes) Run(ctx *types.Context) error { + tags := ctx.CTagsCollected lineWhereToInsertPrototypes := findLineWhereToInsertPrototypes(tags) if lineWhereToInsertPrototypes != -1 { - context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES] = lineWhereToInsertPrototypes + ctx.PrototypesLineWhereToInsert = lineWhereToInsertPrototypes } - prototypes := toPrototypes(tags) - context[constants.CTX_PROTOTYPES] = prototypes - + ctx.Prototypes = toPrototypes(tags) return nil } diff --git a/src/arduino.cc/builder/ctags_target_file_saver.go b/src/arduino.cc/builder/ctags_target_file_saver.go index f6fe259a..0f3277ca 100644 --- a/src/arduino.cc/builder/ctags_target_file_saver.go +++ b/src/arduino.cc/builder/ctags_target_file_saver.go @@ -30,32 +30,33 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "path/filepath" ) type CTagsTargetFileSaver struct { - SourceField string + Source *string TargetFileName string } -func (s *CTagsTargetFileSaver) Run(context map[string]interface{}) error { - source := context[s.SourceField].(string) +func (s *CTagsTargetFileSaver) Run(ctx *types.Context) error { + source := *s.Source - preprocPath := context[constants.CTX_PREPROC_PATH].(string) + preprocPath := ctx.PreprocPath err := utils.EnsureFolderExists(preprocPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } ctagsTargetFilePath := filepath.Join(preprocPath, s.TargetFileName) err = utils.WriteFile(ctagsTargetFilePath, source) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_CTAGS_TEMP_FILE_PATH] = ctagsTargetFilePath + ctx.CTagsTargetFile = ctagsTargetFilePath return nil } diff --git a/src/arduino.cc/builder/dump_build_properties.go b/src/arduino.cc/builder/dump_build_properties.go index 41db23f2..af20cc1b 100644 --- a/src/arduino.cc/builder/dump_build_properties.go +++ b/src/arduino.cc/builder/dump_build_properties.go @@ -30,8 +30,7 @@ package builder import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "fmt" "sort" @@ -39,8 +38,8 @@ import ( type DumpBuildProperties struct{} -func (s *DumpBuildProperties) Run(context map[string]interface{}) error { - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) +func (s *DumpBuildProperties) Run(ctx *types.Context) error { + buildProperties := ctx.BuildProperties keys := utils.KeysOfMapOfString(buildProperties) sort.Strings(keys) diff --git a/src/arduino.cc/builder/dump_context.go b/src/arduino.cc/builder/dump_context.go deleted file mode 100644 index 7cfc1a64..00000000 --- a/src/arduino.cc/builder/dump_context.go +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of Arduino Builder. - * - * Arduino Builder is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - */ - -package builder - -import ( - "arduino.cc/builder/utils" - "fmt" - "sort" -) - -type DumpContext struct{} - -func (s *DumpContext) Run(context map[string]interface{}) error { - keys := utils.KeysOfMapOfStringInterface(context) - sort.Strings(keys) - - for _, key := range keys { - fmt.Print(key + "=") - fmt.Println(context[key]) - } - - return nil -} diff --git a/src/arduino.cc/builder/ensure_buildpath_exists.go b/src/arduino.cc/builder/ensure_buildpath_exists.go index d3fc6dc5..39ab0156 100644 --- a/src/arduino.cc/builder/ensure_buildpath_exists.go +++ b/src/arduino.cc/builder/ensure_buildpath_exists.go @@ -30,18 +30,17 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "arduino.cc/builder/utils" ) type EnsureBuildPathExists struct{} -func (s *EnsureBuildPathExists) Run(context map[string]interface{}) error { - buildPath := context[constants.CTX_BUILD_PATH].(string) - - err := utils.EnsureFolderExists(buildPath) +func (s *EnsureBuildPathExists) Run(ctx *types.Context) error { + err := utils.EnsureFolderExists(ctx.BuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } return nil diff --git a/src/arduino.cc/builder/external_include_replacer.go b/src/arduino.cc/builder/external_include_replacer.go index 0e6ca0b8..fc3dcb43 100644 --- a/src/arduino.cc/builder/external_include_replacer.go +++ b/src/arduino.cc/builder/external_include_replacer.go @@ -30,28 +30,28 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "path/filepath" "strings" ) type ExternalIncludeReplacer struct { - SourceKey string - TargetKey string - From string - To string + Source *string + Target *string + From string + To string } -func (s *ExternalIncludeReplacer) Run(context map[string]interface{}) error { - source := context[s.SourceKey].(string) - nonAbsoluteIncludes := findNonAbsoluteIncludes(context[constants.CTX_INCLUDES].([]string)) +func (s *ExternalIncludeReplacer) Run(ctx *types.Context) error { + source := *s.Source + nonAbsoluteIncludes := findNonAbsoluteIncludes(ctx.Includes) for _, include := range nonAbsoluteIncludes { source = strings.Replace(source, s.From+"<"+include+">", s.To+"<"+include+">", -1) source = strings.Replace(source, s.From+"\""+include+"\"", s.To+"\""+include+"\"", -1) } - context[s.TargetKey] = source + *s.Target = source return nil } diff --git a/src/arduino.cc/builder/fail_if_buildpath_equals_sketchpath.go b/src/arduino.cc/builder/fail_if_buildpath_equals_sketchpath.go index d6bbff33..e42adee5 100644 --- a/src/arduino.cc/builder/fail_if_buildpath_equals_sketchpath.go +++ b/src/arduino.cc/builder/fail_if_buildpath_equals_sketchpath.go @@ -31,30 +31,31 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/utils" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "path/filepath" ) type FailIfBuildPathEqualsSketchPath struct{} -func (s *FailIfBuildPathEqualsSketchPath) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_BUILD_PATH) || !utils.MapHas(context, constants.CTX_SKETCH_LOCATION) { +func (s *FailIfBuildPathEqualsSketchPath) Run(ctx *types.Context) error { + if ctx.BuildPath == "" || ctx.SketchLocation == "" { return nil } - buildPath, err := filepath.Abs(context[constants.CTX_BUILD_PATH].(string)) + buildPath, err := filepath.Abs(ctx.BuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - sketchPath, err := filepath.Abs(context[constants.CTX_SKETCH_LOCATION].(string)) + sketchPath, err := filepath.Abs(ctx.SketchLocation) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } sketchPath = filepath.Dir(sketchPath) if buildPath == sketchPath { - return utils.Errorf(context, constants.MSG_SKETCH_CANT_BE_IN_BUILDPATH) + return i18n.ErrorfWithLogger(ctx.GetLogger(), constants.MSG_SKETCH_CANT_BE_IN_BUILDPATH) } return nil diff --git a/src/arduino.cc/builder/fail_if_imported_library_is_wrong.go b/src/arduino.cc/builder/fail_if_imported_library_is_wrong.go index ab261996..1c1c5ae1 100644 --- a/src/arduino.cc/builder/fail_if_imported_library_is_wrong.go +++ b/src/arduino.cc/builder/fail_if_imported_library_is_wrong.go @@ -33,34 +33,32 @@ import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "os" "path/filepath" ) type FailIfImportedLibraryIsWrong struct{} -func (s *FailIfImportedLibraryIsWrong) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) { +func (s *FailIfImportedLibraryIsWrong) Run(ctx *types.Context) error { + if len(ctx.ImportedLibraries) == 0 { return nil } - logger := context[constants.CTX_LOGGER].(i18n.Logger) - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + logger := ctx.GetLogger() - for _, library := range importedLibraries { + for _, library := range ctx.ImportedLibraries { if !library.IsLegacy { if stat, err := os.Stat(filepath.Join(library.Folder, constants.LIBRARY_FOLDER_ARCH)); err == nil && stat.IsDir() { - return utils.ErrorfWithLogger(logger, constants.MSG_ARCH_FOLDER_NOT_SUPPORTED) + return i18n.ErrorfWithLogger(logger, constants.MSG_ARCH_FOLDER_NOT_SUPPORTED) } for _, propName := range LIBRARY_MANDATORY_PROPERTIES { if _, ok := library.Properties[propName]; !ok { - return utils.ErrorfWithLogger(logger, constants.MSG_PROP_IN_LIBRARY, propName, library.Folder) + return i18n.ErrorfWithLogger(logger, constants.MSG_PROP_IN_LIBRARY, propName, library.Folder) } } if library.Layout == types.LIBRARY_RECURSIVE { if stat, err := os.Stat(filepath.Join(library.Folder, constants.LIBRARY_FOLDER_UTILITY)); err == nil && stat.IsDir() { - return utils.ErrorfWithLogger(logger, constants.MSG_LIBRARY_CAN_USE_SRC_AND_UTILITY_FOLDERS, library.Folder) + return i18n.ErrorfWithLogger(logger, constants.MSG_LIBRARY_CAN_USE_SRC_AND_UTILITY_FOLDERS, library.Folder) } } } diff --git a/src/arduino.cc/builder/gcc_minus_m_output_parser.go b/src/arduino.cc/builder/gcc_minus_m_output_parser.go index 757181ca..458d58de 100644 --- a/src/arduino.cc/builder/gcc_minus_m_output_parser.go +++ b/src/arduino.cc/builder/gcc_minus_m_output_parser.go @@ -30,23 +30,23 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "strings" ) type GCCMinusMOutputParser struct{} -func (s *GCCMinusMOutputParser) Run(context map[string]interface{}) error { - output := context[constants.CTX_GCC_MINUS_M_OUTPUT].(string) +func (s *GCCMinusMOutputParser) Run(ctx *types.Context) error { + output := ctx.OutputGccMinusM rows := strings.Split(output, "\n") includes := make([]string, 0) if len(rows) > 2 { for _, row := range rows[2:] { - if !strings.HasPrefix(row, constants.SPACE) { + if !strings.HasPrefix(row, " ") { row = strings.TrimSpace(row) - if row != constants.EMPTY_STRING { + if row != "" { row = strings.TrimSuffix(row, ":") row = strings.Replace(row, "\\ ", " ", -1) includes = append(includes, row) @@ -55,12 +55,7 @@ func (s *GCCMinusMOutputParser) Run(context map[string]interface{}) error { } } - if !utils.MapHas(context, constants.CTX_INCLUDES) { - context[constants.CTX_INCLUDES] = includes - return nil - } - - context[constants.CTX_INCLUDES] = utils.AddStringsToStringsSet(context[constants.CTX_INCLUDES].([]string), includes) + ctx.Includes = utils.AppendIfNotPresent(ctx.Includes, includes...) return nil } diff --git a/src/arduino.cc/builder/gcc_preproc_runner.go b/src/arduino.cc/builder/gcc_preproc_runner.go index 19f599c3..a35ba2b5 100644 --- a/src/arduino.cc/builder/gcc_preproc_runner.go +++ b/src/arduino.cc/builder/gcc_preproc_runner.go @@ -44,12 +44,12 @@ type GCCPreprocRunner struct { TargetFileName string } -func (s *GCCPreprocRunner) Run(context map[string]interface{}) error { - sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string) - sketch := context[constants.CTX_SKETCH].(*types.Sketch) - properties, targetFilePath, err := prepareGCCPreprocRecipeProperties(context, filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"), s.TargetFileName) +func (s *GCCPreprocRunner) Run(ctx *types.Context) error { + sketchBuildPath := ctx.SketchBuildPath + sketch := ctx.Sketch + properties, targetFilePath, err := prepareGCCPreprocRecipeProperties(ctx, filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"), s.TargetFileName) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if properties[constants.RECIPE_PREPROC_MACROS] == constants.EMPTY_STRING { @@ -57,14 +57,14 @@ func (s *GCCPreprocRunner) Run(context map[string]interface{}) error { properties[constants.RECIPE_PREPROC_MACROS] = GeneratePreprocPatternFromCompile(properties[constants.RECIPE_CPP_PATTERN]) } - verbose := context[constants.CTX_VERBOSE].(bool) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + verbose := ctx.Verbose + logger := ctx.GetLogger() _, err = builder_utils.ExecRecipe(properties, constants.RECIPE_PREPROC_MACROS, true, verbose, false, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_FILE_PATH_TO_READ] = targetFilePath + ctx.FileToRead = targetFilePath return nil } @@ -74,14 +74,14 @@ type GCCPreprocRunnerForDiscoveringIncludes struct { TargetFilePath string } -func (s *GCCPreprocRunnerForDiscoveringIncludes) Run(context map[string]interface{}) error { - properties, _, err := prepareGCCPreprocRecipeProperties(context, s.SourceFilePath, s.TargetFilePath) +func (s *GCCPreprocRunnerForDiscoveringIncludes) Run(ctx *types.Context) error { + properties, _, err := prepareGCCPreprocRecipeProperties(ctx, s.SourceFilePath, s.TargetFilePath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - verbose := context[constants.CTX_VERBOSE].(bool) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + verbose := ctx.Verbose + logger := ctx.GetLogger() if properties[constants.RECIPE_PREPROC_MACROS] == constants.EMPTY_STRING { //generate PREPROC_MACROS from RECIPE_CPP_PATTERN @@ -90,33 +90,29 @@ func (s *GCCPreprocRunnerForDiscoveringIncludes) Run(context map[string]interfac stderr, err := builder_utils.ExecRecipeCollectStdErr(properties, constants.RECIPE_PREPROC_MACROS, true, verbose, false, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_GCC_MINUS_E_SOURCE] = string(stderr) + ctx.SourceGccMinusE = string(stderr) return nil } -func prepareGCCPreprocRecipeProperties(context map[string]interface{}, sourceFilePath string, targetFilePath string) (props.PropertiesMap, string, error) { +func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath string, targetFilePath string) (props.PropertiesMap, string, error) { if targetFilePath != utils.NULLFile() { - preprocPath := context[constants.CTX_PREPROC_PATH].(string) + preprocPath := ctx.PreprocPath err := utils.EnsureFolderExists(preprocPath) if err != nil { - return nil, "", utils.WrapError(err) + return nil, "", i18n.WrapError(err) } targetFilePath = filepath.Join(preprocPath, targetFilePath) } - properties := make(props.PropertiesMap) - if p, ok := context[constants.CTX_BUILD_PROPERTIES]; ok { - properties = p.(props.PropertiesMap).Clone() - } - + properties := ctx.BuildProperties properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = sourceFilePath properties[constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH] = targetFilePath - includes := context[constants.CTX_INCLUDE_FOLDERS].([]string) + includes := ctx.IncludeFolders includes = utils.Map(includes, utils.WrapWithHyphenI) properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE) builder_utils.RemoveHyphenMDDFlagFromGCCCommandLine(properties) diff --git a/src/arduino.cc/builder/gcc_preproc_source_saver.go b/src/arduino.cc/builder/gcc_preproc_source_saver.go index 43d8e494..4eb1c083 100644 --- a/src/arduino.cc/builder/gcc_preproc_source_saver.go +++ b/src/arduino.cc/builder/gcc_preproc_source_saver.go @@ -29,23 +29,25 @@ package builder +// XXX: OBSOLETE? + import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "path/filepath" ) type GCCPreprocSourceSaver struct{} -func (s *GCCPreprocSourceSaver) Run(context map[string]interface{}) error { - preprocPath := context[constants.CTX_PREPROC_PATH].(string) +func (s *GCCPreprocSourceSaver) Run(ctx *types.Context) error { + preprocPath := ctx.PreprocPath err := utils.EnsureFolderExists(preprocPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - source := context[constants.CTX_SOURCE].(string) - - err = utils.WriteFile(filepath.Join(preprocPath, constants.FILE_GCC_PREPROC_TARGET), source) - return utils.WrapError(err) + err = utils.WriteFile(filepath.Join(preprocPath, constants.FILE_GCC_PREPROC_TARGET), ctx.Source) + return i18n.WrapError(err) } diff --git a/src/arduino.cc/builder/generate_buildpath_if_missing.go b/src/arduino.cc/builder/generate_buildpath_if_missing.go index 5a04272d..f165afaf 100644 --- a/src/arduino.cc/builder/generate_buildpath_if_missing.go +++ b/src/arduino.cc/builder/generate_buildpath_if_missing.go @@ -32,6 +32,7 @@ package builder import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "os" "path/filepath" @@ -40,26 +41,25 @@ import ( type GenerateBuildPathIfMissing struct{} -func (s *GenerateBuildPathIfMissing) Run(context map[string]interface{}) error { - if utils.MapHas(context, constants.CTX_BUILD_PATH) && context[constants.CTX_BUILD_PATH].(string) != constants.EMPTY_STRING { +func (s *GenerateBuildPathIfMissing) Run(ctx *types.Context) error { + if ctx.BuildPath != "" { return nil } - sketchLocation := context[constants.CTX_SKETCH_LOCATION].(string) - md5sum := utils.MD5Sum([]byte(sketchLocation)) + md5sum := utils.MD5Sum([]byte(ctx.SketchLocation)) buildPath := filepath.Join(os.TempDir(), "arduino-sketch-"+strings.ToUpper(md5sum)) _, err := os.Stat(buildPath) if err != nil && !os.IsNotExist(err) { - return utils.WrapError(err) + return i18n.WrapError(err) } - if utils.DebugLevel(context) > 5 { - logger := context[constants.CTX_LOGGER].(i18n.Logger) + if ctx.DebugLevel > 5 { + logger := ctx.GetLogger() logger.Fprintln(os.Stdout, constants.LOG_LEVEL_WARN, constants.MSG_SETTING_BUILD_PATH, buildPath) } - context[constants.CTX_BUILD_PATH] = buildPath + ctx.BuildPath = buildPath return nil } diff --git a/src/arduino.cc/builder/hardware_loader.go b/src/arduino.cc/builder/hardware_loader.go index 29dce302..02ccc996 100644 --- a/src/arduino.cc/builder/hardware_loader.go +++ b/src/arduino.cc/builder/hardware_loader.go @@ -41,37 +41,37 @@ import ( type HardwareLoader struct{} -func (s *HardwareLoader) Run(context map[string]interface{}) error { - logger := context[constants.CTX_LOGGER].(i18n.Logger) +func (s *HardwareLoader) Run(ctx *types.Context) error { + logger := ctx.GetLogger() packages := &types.Packages{} packages.Packages = make(map[string]*types.Package) packages.Properties = make(map[string]string) - folders := context[constants.CTX_HARDWARE_FOLDERS].([]string) + folders := ctx.HardwareFolders folders, err := utils.AbsolutizePaths(folders) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, folder := range folders { stat, err := os.Stat(folder) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if !stat.IsDir() { - return utils.Errorf(context, constants.MSG_MUST_BE_A_FOLDER, folder) + return i18n.ErrorfWithLogger(logger, constants.MSG_MUST_BE_A_FOLDER, folder) } hardwarePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } packages.Properties.Merge(hardwarePlatformTxt) subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } subfolders = utils.FilterOutFoldersByNames(subfolders, constants.FOLDER_TOOLS) @@ -86,13 +86,13 @@ func (s *HardwareLoader) Run(context map[string]interface{}) error { targetPackage := getOrCreatePackage(packages, packageId) err = loadPackage(targetPackage, subfolderPath, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } packages.Packages[packageId] = targetPackage } } - context[constants.CTX_HARDWARE] = packages + ctx.Hardware = packages return nil } @@ -113,13 +113,13 @@ func getOrCreatePackage(packages *types.Packages, packageId string) *types.Packa func loadPackage(targetPackage *types.Package, folder string, logger i18n.Logger) error { packagePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } targetPackage.Properties.Merge(packagePlatformTxt) subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } subfolders = utils.FilterOutFoldersByNames(subfolders, constants.FOLDER_TOOLS) @@ -133,7 +133,7 @@ func loadPackage(targetPackage *types.Package, folder string, logger i18n.Logger if err != nil && os.IsNotExist(err) { theOnlySubfolder, err := utils.TheOnlySubfolderOf(subfolderPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if theOnlySubfolder != constants.EMPTY_STRING { @@ -144,7 +144,7 @@ func loadPackage(targetPackage *types.Package, folder string, logger i18n.Logger platform := getOrCreatePlatform(platforms, platformId) err = loadPlatform(platform, targetPackage.PackageId, subfolderPath, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } platforms[platformId] = platform } @@ -169,7 +169,7 @@ func getOrCreatePlatform(platforms map[string]*types.Platform, platformId string func loadPlatform(targetPlatform *types.Platform, packageId string, folder string, logger i18n.Logger) error { _, err := os.Stat(filepath.Join(folder, constants.FILE_BOARDS_TXT)) if err != nil && !os.IsNotExist(err) { - return utils.WrapError(err) + return i18n.WrapError(err) } if os.IsNotExist(err) { @@ -180,19 +180,19 @@ func loadPlatform(targetPlatform *types.Platform, packageId string, folder strin err = loadBoards(targetPlatform.Boards, packageId, targetPlatform.PlatformId, folder, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } assignDefaultBoardToPlatform(targetPlatform) platformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } localPlatformProperties, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_LOCAL_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } targetPlatform.Properties = targetPlatform.Properties.Clone() @@ -201,7 +201,7 @@ func loadPlatform(targetPlatform *types.Platform, packageId string, folder strin programmersProperties, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PROGRAMMERS_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } targetPlatform.Programmers = props.MergeMapsOfProperties(make(map[string]props.PropertiesMap), targetPlatform.Programmers, programmersProperties.FirstLevelOf()) @@ -221,12 +221,12 @@ func assignDefaultBoardToPlatform(targetPlatform *types.Platform) { func loadBoards(boards map[string]*types.Board, packageId string, platformId string, folder string, logger i18n.Logger) error { properties, err := props.Load(filepath.Join(folder, constants.FILE_BOARDS_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } localProperties, err := props.SafeLoad(filepath.Join(folder, constants.FILE_BOARDS_LOCAL_TXT), logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } properties = properties.Merge(localProperties) diff --git a/src/arduino.cc/builder/i18n/errors.go b/src/arduino.cc/builder/i18n/errors.go new file mode 100644 index 00000000..604fa1e8 --- /dev/null +++ b/src/arduino.cc/builder/i18n/errors.go @@ -0,0 +1,20 @@ +package i18n + +import "arduino.cc/builder/constants" +import "github.com/go-errors/errors" +import "os" + +func ErrorfWithLogger(logger Logger, format string, a ...interface{}) *errors.Error { + if logger.Name() == "machine" { + logger.Fprintln(os.Stderr, constants.LOG_LEVEL_ERROR, format, a...) + return errors.Errorf("") + } + return errors.Errorf(Format(format, a...)) +} + +func WrapError(err error) error { + if err == nil { + return nil + } + return errors.Wrap(err, 0) +} diff --git a/src/arduino.cc/builder/includes_finder_with_gcc.go b/src/arduino.cc/builder/includes_finder_with_gcc.go index 55648e76..e4fe6a09 100644 --- a/src/arduino.cc/builder/includes_finder_with_gcc.go +++ b/src/arduino.cc/builder/includes_finder_with_gcc.go @@ -29,11 +29,13 @@ package builder +// XXX: Obsolete? + import ( "arduino.cc/builder/builder_utils" "arduino.cc/builder/constants" "arduino.cc/builder/i18n" - "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "strings" ) @@ -42,20 +44,13 @@ type IncludesFinderWithGCC struct { SourceFile string } -func (s *IncludesFinderWithGCC) Run(context map[string]interface{}) error { - buildProperties := make(props.PropertiesMap) - if p, ok := context[constants.CTX_BUILD_PROPERTIES]; ok { - buildProperties = p.(props.PropertiesMap).Clone() - } - verbose := context[constants.CTX_VERBOSE].(bool) - logger := context[constants.CTX_LOGGER].(i18n.Logger) +func (s *IncludesFinderWithGCC) Run(ctx *types.Context) error { + buildProperties := ctx.BuildProperties + verbose := ctx.Verbose + logger := ctx.GetLogger() - includesParams := constants.EMPTY_STRING - if utils.MapHas(context, constants.CTX_INCLUDE_FOLDERS) { - includes := context[constants.CTX_INCLUDE_FOLDERS].([]string) - includes = utils.Map(includes, utils.WrapWithHyphenI) - includesParams = strings.Join(includes, " ") - } + includes := utils.Map(ctx.IncludeFolders, utils.WrapWithHyphenI) + includesParams := strings.Join(includes, " ") properties := buildProperties.Clone() properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = s.SourceFile @@ -69,10 +64,10 @@ func (s *IncludesFinderWithGCC) Run(context map[string]interface{}) error { output, err := builder_utils.ExecRecipe(properties, constants.RECIPE_PREPROC_INCLUDES, true, verbose, false, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_GCC_MINUS_M_OUTPUT] = string(output) + ctx.OutputGccMinusM = string(output) return nil } diff --git a/src/arduino.cc/builder/includes_finder_with_regexp.go b/src/arduino.cc/builder/includes_finder_with_regexp.go index e83840f5..3f85a748 100644 --- a/src/arduino.cc/builder/includes_finder_with_regexp.go +++ b/src/arduino.cc/builder/includes_finder_with_regexp.go @@ -30,7 +30,7 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "regexp" "strings" @@ -39,18 +39,17 @@ import ( var INCLUDE_REGEXP = regexp.MustCompile("(?ms)^\\s*#[ \t]*include\\s*[<\"](\\S+)[\">]") type IncludesFinderWithRegExp struct { - ContextField string + Source *string } -func (s *IncludesFinderWithRegExp) Run(context map[string]interface{}) error { - source := context[s.ContextField].(string) +func (s *IncludesFinderWithRegExp) Run(ctx *types.Context) error { + source := *s.Source matches := INCLUDE_REGEXP.FindAllStringSubmatch(source, -1) includes := []string{} for _, match := range matches { includes = append(includes, strings.TrimSpace(match[1])) } - if len(includes) == 0 { include := findIncludesForOldCompilers(source) if include != "" { @@ -58,15 +57,8 @@ func (s *IncludesFinderWithRegExp) Run(context map[string]interface{}) error { } } - context[constants.CTX_INCLUDES_JUST_FOUND] = includes - - if !utils.MapHas(context, constants.CTX_INCLUDES) { - context[constants.CTX_INCLUDES] = includes - return nil - } - - context[constants.CTX_INCLUDES] = utils.AddStringsToStringsSet(context[constants.CTX_INCLUDES].([]string), includes) - + ctx.IncludesJustFound = includes + ctx.Includes = utils.AppendIfNotPresent(ctx.Includes, includes...) return nil } diff --git a/src/arduino.cc/builder/includes_to_include_folders.go b/src/arduino.cc/builder/includes_to_include_folders.go index 9760fb64..63c76f7f 100644 --- a/src/arduino.cc/builder/includes_to_include_folders.go +++ b/src/arduino.cc/builder/includes_to_include_folders.go @@ -31,6 +31,7 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/props" "arduino.cc/builder/types" "arduino.cc/builder/utils" @@ -40,30 +41,21 @@ import ( type IncludesToIncludeFolders struct{} -func (s *IncludesToIncludeFolders) Run(context map[string]interface{}) error { - includes := []string{} - if utils.MapHas(context, constants.CTX_INCLUDES) { - includes = context[constants.CTX_INCLUDES].([]string) - } - headerToLibraries := make(map[string][]*types.Library) - if utils.MapHas(context, constants.CTX_HEADER_TO_LIBRARIES) { - headerToLibraries = context[constants.CTX_HEADER_TO_LIBRARIES].(map[string][]*types.Library) - } +func (s *IncludesToIncludeFolders) Run(ctx *types.Context) error { + includes := ctx.Includes + headerToLibraries := ctx.HeaderToLibraries - platform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) - actualPlatform := context[constants.CTX_ACTUAL_PLATFORM].(*types.Platform) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + platform := ctx.TargetPlatform + actualPlatform := ctx.ActualPlatform + libraryResolutionResults := ctx.LibrariesResolutionResults + importedLibraries := ctx.ImportedLibraries - importedLibraries := []*types.Library{} - if utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) { - importedLibraries = context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) - } newlyImportedLibraries, err := resolveLibraries(includes, headerToLibraries, importedLibraries, []*types.Platform{actualPlatform, platform}, libraryResolutionResults) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - foldersWithSources := context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE].(*types.UniqueSourceFolderQueue) + foldersWithSources := ctx.FoldersWithSourceFiles for _, newlyImportedLibrary := range newlyImportedLibraries { if !sliceContainsLibrary(importedLibraries, newlyImportedLibrary) { @@ -75,12 +67,8 @@ func (s *IncludesToIncludeFolders) Run(context map[string]interface{}) error { } } - context[constants.CTX_IMPORTED_LIBRARIES] = importedLibraries - - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - verbose := context[constants.CTX_VERBOSE].(bool) - includeFolders := resolveIncludeFolders(newlyImportedLibraries, buildProperties, verbose) - context[constants.CTX_INCLUDE_FOLDERS] = includeFolders + ctx.ImportedLibraries = importedLibraries + ctx.IncludeFolders = resolveIncludeFolders(newlyImportedLibraries, ctx.BuildProperties, ctx.Verbose) return nil } diff --git a/src/arduino.cc/builder/libraries_loader.go b/src/arduino.cc/builder/libraries_loader.go index 0c043801..3ef57560 100644 --- a/src/arduino.cc/builder/libraries_loader.go +++ b/src/arduino.cc/builder/libraries_loader.go @@ -42,64 +42,57 @@ import ( type LibrariesLoader struct{} -func (s *LibrariesLoader) Run(context map[string]interface{}) error { - sortedLibrariesFolders := []string{} - - builtInLibrariesFolders := []string{} - if utils.MapHas(context, constants.CTX_BUILT_IN_LIBRARIES_FOLDERS) { - builtInLibrariesFolders = context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS].([]string) - } +func (s *LibrariesLoader) Run(ctx *types.Context) error { + builtInLibrariesFolders := ctx.BuiltInLibrariesFolders builtInLibrariesFolders, err := utils.AbsolutizePaths(builtInLibrariesFolders) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } + sortedLibrariesFolders := []string{} sortedLibrariesFolders = utils.AppendIfNotPresent(sortedLibrariesFolders, builtInLibrariesFolders...) - platform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) - debugLevel := utils.DebugLevel(context) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + platform := ctx.TargetPlatform + debugLevel := ctx.DebugLevel + logger := ctx.GetLogger() - actualPlatform := context[constants.CTX_ACTUAL_PLATFORM].(*types.Platform) + actualPlatform := ctx.ActualPlatform if actualPlatform != platform { sortedLibrariesFolders = appendPathToLibrariesFolders(sortedLibrariesFolders, filepath.Join(actualPlatform.Folder, constants.FOLDER_LIBRARIES)) } sortedLibrariesFolders = appendPathToLibrariesFolders(sortedLibrariesFolders, filepath.Join(platform.Folder, constants.FOLDER_LIBRARIES)) - librariesFolders := []string{} - if utils.MapHas(context, constants.CTX_OTHER_LIBRARIES_FOLDERS) { - librariesFolders = context[constants.CTX_OTHER_LIBRARIES_FOLDERS].([]string) - } + librariesFolders := ctx.OtherLibrariesFolders librariesFolders, err = utils.AbsolutizePaths(librariesFolders) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } sortedLibrariesFolders = utils.AppendIfNotPresent(sortedLibrariesFolders, librariesFolders...) - context[constants.CTX_LIBRARIES_FOLDERS] = sortedLibrariesFolders + ctx.LibrariesFolders = sortedLibrariesFolders var libraries []*types.Library for _, libraryFolder := range sortedLibrariesFolders { subFolders, err := utils.ReadDirFiltered(libraryFolder, utils.FilterDirs) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, subFolder := range subFolders { library, err := makeLibrary(filepath.Join(libraryFolder, subFolder.Name()), debugLevel, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } libraries = append(libraries, library) } } - context[constants.CTX_LIBRARIES] = libraries + ctx.Libraries = libraries headerToLibraries := make(map[string][]*types.Library) for _, library := range libraries { headers, err := utils.ReadDirFiltered(library.SrcFolder, utils.FilterFilesWithExtension(".h")) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, header := range headers { headerFileName := header.Name() @@ -107,7 +100,7 @@ func (s *LibrariesLoader) Run(context map[string]interface{}) error { } } - context[constants.CTX_HEADER_TO_LIBRARIES] = headerToLibraries + ctx.HeaderToLibraries = headerToLibraries return nil } @@ -122,7 +115,7 @@ func makeLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*typ func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*types.Library, error) { properties, err := props.Load(filepath.Join(libraryFolder, constants.LIBRARY_PROPERTIES), logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } if properties[constants.LIBRARY_MAINTAINER] == constants.EMPTY_STRING && properties[constants.LIBRARY_EMAIL] != constants.EMPTY_STRING { @@ -146,10 +139,10 @@ func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (* subFolders, err := utils.ReadDirFiltered(libraryFolder, utils.FilterDirs) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } - if debugLevel > 0 { + if debugLevel >= 0 { for _, subFolder := range subFolders { if utils.IsSCCSOrHiddenFile(subFolder) { if !utils.IsSCCSFile(subFolder) && utils.IsHiddenFile(subFolder) { diff --git a/src/arduino.cc/builder/load_previous_build_options.go b/src/arduino.cc/builder/load_previous_build_options.go index 3c884bff..2d051847 100644 --- a/src/arduino.cc/builder/load_previous_build_options.go +++ b/src/arduino.cc/builder/load_previous_build_options.go @@ -31,7 +31,8 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/utils" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "io/ioutil" "os" "path/filepath" @@ -39,25 +40,23 @@ import ( type LoadPreviousBuildOptionsMap struct{} -func (s *LoadPreviousBuildOptionsMap) Run(context map[string]interface{}) error { - buildPath := context[constants.CTX_BUILD_PATH].(string) - - buildOptionsFile := filepath.Join(buildPath, constants.BUILD_OPTIONS_FILE) +func (s *LoadPreviousBuildOptionsMap) Run(ctx *types.Context) error { + buildOptionsFile := filepath.Join(ctx.BuildPath, constants.BUILD_OPTIONS_FILE) _, err := os.Stat(buildOptionsFile) if err != nil { if os.IsNotExist(err) { return nil } - return utils.WrapError(err) + return i18n.WrapError(err) } bytes, err := ioutil.ReadFile(buildOptionsFile) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON] = string(bytes) + ctx.BuildOptionsJsonPrevious = string(bytes) return nil } diff --git a/src/arduino.cc/builder/load_vid_pid_specific_properties.go b/src/arduino.cc/builder/load_vid_pid_specific_properties.go index 3f7ccd2d..0b17ff8f 100644 --- a/src/arduino.cc/builder/load_vid_pid_specific_properties.go +++ b/src/arduino.cc/builder/load_vid_pid_specific_properties.go @@ -31,29 +31,30 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/props" - "arduino.cc/builder/utils" + "arduino.cc/builder/types" "strconv" "strings" ) type LoadVIDPIDSpecificProperties struct{} -func (s *LoadVIDPIDSpecificProperties) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_VIDPID) { +func (s *LoadVIDPIDSpecificProperties) Run(ctx *types.Context) error { + if ctx.USBVidPid == "" { return nil } - vidPid := context[constants.CTX_VIDPID].(string) + vidPid := ctx.USBVidPid vidPid = strings.ToLower(vidPid) vidPidParts := strings.Split(vidPid, "_") vid := vidPidParts[0] pid := vidPidParts[1] - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties VIDPIDIndex, err := findVIDPIDIndex(buildProperties, vid, pid) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if VIDPIDIndex < 0 { return nil diff --git a/src/arduino.cc/builder/merge_sketch_with_bootloader.go b/src/arduino.cc/builder/merge_sketch_with_bootloader.go index 85c5d015..34fa657b 100644 --- a/src/arduino.cc/builder/merge_sketch_with_bootloader.go +++ b/src/arduino.cc/builder/merge_sketch_with_bootloader.go @@ -32,7 +32,6 @@ package builder import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" - "arduino.cc/builder/props" "arduino.cc/builder/types" "arduino.cc/builder/utils" "os" @@ -42,16 +41,16 @@ import ( type MergeSketchWithBootloader struct{} -func (s *MergeSketchWithBootloader) Run(context map[string]interface{}) error { - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) +func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error { + buildProperties := ctx.BuildProperties if !utils.MapStringStringHas(buildProperties, constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK) && !utils.MapStringStringHas(buildProperties, constants.BUILD_PROPERTIES_BOOTLOADER_FILE) { return nil } - buildPath := context[constants.CTX_BUILD_PATH].(string) - sketch := context[constants.CTX_SKETCH].(*types.Sketch) + buildPath := ctx.BuildPath + sketch := ctx.Sketch sketchFileName := filepath.Base(sketch.MainFile.Name) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + logger := ctx.GetLogger() sketchInBuildPath := filepath.Join(buildPath, sketchFileName+".hex") sketchInSubfolder := filepath.Join(buildPath, constants.FOLDER_SKETCH, sketchFileName+".hex") @@ -131,13 +130,13 @@ func extractActualBootloader(bootloader []string) []string { func merge(builtSketchPath, bootloaderPath, mergedSketchPath string) error { sketch, err := utils.ReadFileToRows(builtSketchPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } sketch = sketch[:len(sketch)-2] bootloader, err := utils.ReadFileToRows(bootloaderPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } realBootloader := extractActualBootloader(bootloader) diff --git a/src/arduino.cc/builder/phases/core_builder.go b/src/arduino.cc/builder/phases/core_builder.go index d1b8cc9b..114143fb 100644 --- a/src/arduino.cc/builder/phases/core_builder.go +++ b/src/arduino.cc/builder/phases/core_builder.go @@ -34,30 +34,31 @@ import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" ) type CoreBuilder struct{} -func (s *CoreBuilder) Run(context map[string]interface{}) error { - coreBuildPath := context[constants.CTX_CORE_BUILD_PATH].(string) - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - verbose := context[constants.CTX_VERBOSE].(bool) - warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string) - logger := context[constants.CTX_LOGGER].(i18n.Logger) +func (s *CoreBuilder) Run(ctx *types.Context) error { + coreBuildPath := ctx.CoreBuildPath + buildProperties := ctx.BuildProperties + verbose := ctx.Verbose + warningsLevel := ctx.WarningsLevel + logger := ctx.GetLogger() err := utils.EnsureFolderExists(coreBuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } archiveFile, objectFiles, err := compileCore(coreBuildPath, buildProperties, verbose, warningsLevel, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_ARCHIVE_FILE_PATH_CORE] = archiveFile - context[constants.CTX_OBJECT_FILES_CORE] = objectFiles + ctx.CoreArchiveFilePath = archiveFile + ctx.CoreObjectsFiles = objectFiles return nil } @@ -79,18 +80,18 @@ func compileCore(buildPath string, buildProperties props.PropertiesMap, verbose if variantFolder != constants.EMPTY_STRING { variantObjectFiles, err = builder_utils.CompileFiles(variantObjectFiles, variantFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return "", nil, utils.WrapError(err) + return "", nil, i18n.WrapError(err) } } coreObjectFiles, err := builder_utils.CompileFiles([]string{}, coreFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return "", nil, utils.WrapError(err) + return "", nil, i18n.WrapError(err) } archiveFile, err := builder_utils.ArchiveCompiledFiles(buildPath, "core.a", coreObjectFiles, buildProperties, verbose, logger) if err != nil { - return "", nil, utils.WrapError(err) + return "", nil, i18n.WrapError(err) } return archiveFile, variantObjectFiles, nil diff --git a/src/arduino.cc/builder/phases/libraries_builder.go b/src/arduino.cc/builder/phases/libraries_builder.go index a26f5b2a..5ebd9c6b 100644 --- a/src/arduino.cc/builder/phases/libraries_builder.go +++ b/src/arduino.cc/builder/phases/libraries_builder.go @@ -42,27 +42,27 @@ import ( type LibrariesBuilder struct{} -func (s *LibrariesBuilder) Run(context map[string]interface{}) error { - librariesBuildPath := context[constants.CTX_LIBRARIES_BUILD_PATH].(string) - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - includes := context[constants.CTX_INCLUDE_FOLDERS].([]string) +func (s *LibrariesBuilder) Run(ctx *types.Context) error { + librariesBuildPath := ctx.LibrariesBuildPath + buildProperties := ctx.BuildProperties + includes := ctx.IncludeFolders includes = utils.Map(includes, utils.WrapWithHyphenI) - libraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) - verbose := context[constants.CTX_VERBOSE].(bool) - warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + libraries := ctx.ImportedLibraries + verbose := ctx.Verbose + warningsLevel := ctx.WarningsLevel + logger := ctx.GetLogger() err := utils.EnsureFolderExists(librariesBuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } objectFiles, err := compileLibraries(libraries, librariesBuildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_OBJECT_FILES_LIBRARIES] = objectFiles + ctx.LibrariesObjectFiles = objectFiles return nil } @@ -72,7 +72,7 @@ func compileLibraries(libraries []*types.Library, buildPath string, buildPropert for _, library := range libraries { libraryObjectFiles, err := compileLibrary(library, buildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } objectFiles = append(objectFiles, libraryObjectFiles...) } @@ -86,19 +86,19 @@ func compileLibrary(library *types.Library, buildPath string, buildProperties pr err := utils.EnsureFolderExists(libraryBuildPath) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } objectFiles := []string{} if library.Layout == types.LIBRARY_RECURSIVE { objectFiles, err = builder_utils.CompileFilesRecursive(objectFiles, library.SrcFolder, libraryBuildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } if library.DotALinkage { archiveFile, err := builder_utils.ArchiveCompiledFiles(libraryBuildPath, library.Name+".a", objectFiles, buildProperties, verbose, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } objectFiles = []string{archiveFile} } @@ -110,14 +110,14 @@ func compileLibrary(library *types.Library, buildPath string, buildProperties pr } objectFiles, err = builder_utils.CompileFiles(objectFiles, library.SrcFolder, false, libraryBuildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } if utilitySourcePathErr == nil { utilityBuildPath := filepath.Join(libraryBuildPath, constants.LIBRARY_FOLDER_UTILITY) objectFiles, err = builder_utils.CompileFiles(objectFiles, utilitySourcePath, false, utilityBuildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } } } diff --git a/src/arduino.cc/builder/phases/linker.go b/src/arduino.cc/builder/phases/linker.go index d2530d31..96ba5f88 100644 --- a/src/arduino.cc/builder/phases/linker.go +++ b/src/arduino.cc/builder/phases/linker.go @@ -34,6 +34,7 @@ import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "path/filepath" "strings" @@ -41,31 +42,31 @@ import ( type Linker struct{} -func (s *Linker) Run(context map[string]interface{}) error { - objectFilesSketch := context[constants.CTX_OBJECT_FILES_SKETCH].([]string) - objectFilesLibraries := context[constants.CTX_OBJECT_FILES_LIBRARIES].([]string) - objectFilesCore := context[constants.CTX_OBJECT_FILES_CORE].([]string) +func (s *Linker) Run(ctx *types.Context) error { + objectFilesSketch := ctx.SketchObjectFiles + objectFilesLibraries := ctx.LibrariesObjectFiles + objectFilesCore := ctx.CoreObjectsFiles var objectFiles []string objectFiles = append(objectFiles, objectFilesSketch...) objectFiles = append(objectFiles, objectFilesLibraries...) objectFiles = append(objectFiles, objectFilesCore...) - coreArchiveFilePath := context[constants.CTX_ARCHIVE_FILE_PATH_CORE].(string) - buildPath := context[constants.CTX_BUILD_PATH].(string) + coreArchiveFilePath := ctx.CoreArchiveFilePath + buildPath := ctx.BuildPath coreDotARelPath, err := filepath.Rel(buildPath, coreArchiveFilePath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - verbose := context[constants.CTX_VERBOSE].(bool) - warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + buildProperties := ctx.BuildProperties + verbose := ctx.Verbose + warningsLevel := ctx.WarningsLevel + logger := ctx.GetLogger() err = link(objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, verbose, warningsLevel, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } return nil diff --git a/src/arduino.cc/builder/phases/sketch_builder.go b/src/arduino.cc/builder/phases/sketch_builder.go index cf9befe7..dcdc4d47 100644 --- a/src/arduino.cc/builder/phases/sketch_builder.go +++ b/src/arduino.cc/builder/phases/sketch_builder.go @@ -31,35 +31,34 @@ package phases import ( "arduino.cc/builder/builder_utils" - "arduino.cc/builder/constants" "arduino.cc/builder/i18n" - "arduino.cc/builder/props" + "arduino.cc/builder/types" "arduino.cc/builder/utils" ) type SketchBuilder struct{} -func (s *SketchBuilder) Run(context map[string]interface{}) error { - sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string) - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - includes := context[constants.CTX_INCLUDE_FOLDERS].([]string) +func (s *SketchBuilder) Run(ctx *types.Context) error { + sketchBuildPath := ctx.SketchBuildPath + buildProperties := ctx.BuildProperties + includes := ctx.IncludeFolders includes = utils.Map(includes, utils.WrapWithHyphenI) - verbose := context[constants.CTX_VERBOSE].(bool) - warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + verbose := ctx.Verbose + warningsLevel := ctx.WarningsLevel + logger := ctx.GetLogger() err := utils.EnsureFolderExists(sketchBuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } var objectFiles []string objectFiles, err = builder_utils.CompileFiles(objectFiles, sketchBuildPath, true, sketchBuildPath, buildProperties, includes, verbose, warningsLevel, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_OBJECT_FILES_SKETCH] = objectFiles + ctx.SketchObjectFiles = objectFiles return nil } diff --git a/src/arduino.cc/builder/platform_keys_rewrite_loader.go b/src/arduino.cc/builder/platform_keys_rewrite_loader.go index 86e9c553..3112440c 100644 --- a/src/arduino.cc/builder/platform_keys_rewrite_loader.go +++ b/src/arduino.cc/builder/platform_keys_rewrite_loader.go @@ -44,13 +44,13 @@ import ( type PlatformKeysRewriteLoader struct{} -func (s *PlatformKeysRewriteLoader) Run(context map[string]interface{}) error { - logger := context[constants.CTX_LOGGER].(i18n.Logger) - folders := context[constants.CTX_HARDWARE_FOLDERS].([]string) +func (s *PlatformKeysRewriteLoader) Run(ctx *types.Context) error { + logger := ctx.GetLogger() + folders := ctx.HardwareFolders platformKeysRewriteTxtPath, err := findPlatformKeysRewriteTxt(folders) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if platformKeysRewriteTxtPath == constants.EMPTY_STRING { return nil @@ -68,7 +68,7 @@ func (s *PlatformKeysRewriteLoader) Run(context map[string]interface{}) error { if keyParts[0] == constants.PLATFORM_REWRITE_OLD { index, err := strconv.Atoi(keyParts[1]) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } rewriteKey := strings.Join(keyParts[2:], ".") oldValue := txt[key] @@ -79,7 +79,7 @@ func (s *PlatformKeysRewriteLoader) Run(context map[string]interface{}) error { } } - context[constants.CTX_PLATFORM_KEYS_REWRITE] = platformKeysRewrite + ctx.PlatformKeyRewrites = platformKeysRewrite return nil } @@ -92,7 +92,7 @@ func findPlatformKeysRewriteTxt(folders []string) (string, error) { return txtPath, nil } if !os.IsNotExist(err) { - return constants.EMPTY_STRING, utils.WrapError(err) + return constants.EMPTY_STRING, i18n.WrapError(err) } } diff --git a/src/arduino.cc/builder/print_preprocessed_source.go b/src/arduino.cc/builder/print_preprocessed_source.go index 5a444d10..b788cb72 100644 --- a/src/arduino.cc/builder/print_preprocessed_source.go +++ b/src/arduino.cc/builder/print_preprocessed_source.go @@ -30,16 +30,13 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "fmt" ) type PrintPreprocessedSource struct{} -func (s *PrintPreprocessedSource) Run(context map[string]interface{}) error { - source := context[constants.CTX_GCC_MINUS_E_SOURCE].(string) - - fmt.Println(source) - +func (s *PrintPreprocessedSource) Run(ctx *types.Context) error { + fmt.Println(ctx.SourceGccMinusE) return nil } diff --git a/src/arduino.cc/builder/print_used_and_not_used_libraries.go b/src/arduino.cc/builder/print_used_and_not_used_libraries.go index 7979e6b6..ae490a9f 100644 --- a/src/arduino.cc/builder/print_used_and_not_used_libraries.go +++ b/src/arduino.cc/builder/print_used_and_not_used_libraries.go @@ -31,22 +31,20 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "os" "time" ) type PrintUsedAndNotUsedLibraries struct{} -func (s *PrintUsedAndNotUsedLibraries) Run(context map[string]interface{}) error { - if utils.DebugLevel(context) <= 0 { +func (s *PrintUsedAndNotUsedLibraries) Run(ctx *types.Context) error { + if ctx.DebugLevel < 0 { return nil } - logger := context[constants.CTX_LOGGER].(i18n.Logger) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + logger := ctx.GetLogger() + libraryResolutionResults := ctx.LibrariesResolutionResults for header, libResResult := range libraryResolutionResults { if !libResResult.IsLibraryFromPlatform { diff --git a/src/arduino.cc/builder/print_used_libraries_if_verbose.go b/src/arduino.cc/builder/print_used_libraries_if_verbose.go index c3528a1a..afcf4eb4 100644 --- a/src/arduino.cc/builder/print_used_libraries_if_verbose.go +++ b/src/arduino.cc/builder/print_used_libraries_if_verbose.go @@ -31,25 +31,21 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "time" ) type PrintUsedLibrariesIfVerbose struct{} -func (s *PrintUsedLibrariesIfVerbose) Run(context map[string]interface{}) error { - verbose := context[constants.CTX_VERBOSE].(bool) - logger := context[constants.CTX_LOGGER].(i18n.Logger) +func (s *PrintUsedLibrariesIfVerbose) Run(ctx *types.Context) error { + verbose := ctx.Verbose + logger := ctx.GetLogger() - if !verbose || !utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) { + if !verbose || len(ctx.ImportedLibraries) == 0 { return nil } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) - - for _, library := range importedLibraries { + for _, library := range ctx.ImportedLibraries { legacy := constants.EMPTY_STRING if library.IsLegacy { legacy = constants.MSG_LIB_LEGACY diff --git a/src/arduino.cc/builder/props/properties.go b/src/arduino.cc/builder/props/properties.go index 68ef4861..e1132f3e 100644 --- a/src/arduino.cc/builder/props/properties.go +++ b/src/arduino.cc/builder/props/properties.go @@ -32,7 +32,6 @@ package props import ( "arduino.cc/builder/constants" "arduino.cc/builder/i18n" - "arduino.cc/builder/utils" "github.com/go-errors/errors" "io/ioutil" "os" @@ -61,7 +60,7 @@ func init() { func Load(filepath string, logger i18n.Logger) (PropertiesMap, error) { bytes, err := ioutil.ReadFile(filepath) if err != nil { - return nil, utils.WrapError(err) + return nil, err } text := string(bytes) @@ -73,7 +72,7 @@ func Load(filepath string, logger i18n.Logger) (PropertiesMap, error) { for _, line := range strings.Split(text, "\n") { err := properties.loadSingleLine(line) if err != nil { - return nil, utils.ErrorfWithLogger(logger, constants.MSG_WRONG_PROPERTIES_FILE, line, filepath) + return nil, i18n.ErrorfWithLogger(logger, constants.MSG_WRONG_PROPERTIES_FILE, line, filepath) } } @@ -86,7 +85,7 @@ func LoadFromSlice(lines []string, logger i18n.Logger) (PropertiesMap, error) { for _, line := range lines { err := properties.loadSingleLine(line) if err != nil { - return nil, utils.ErrorfWithLogger(logger, constants.MSG_WRONG_PROPERTIES, line) + return nil, i18n.ErrorfWithLogger(logger, constants.MSG_WRONG_PROPERTIES, line) } } @@ -119,7 +118,7 @@ func SafeLoad(filepath string, logger i18n.Logger) (PropertiesMap, error) { properties, err := Load(filepath, logger) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } return properties, nil } @@ -183,7 +182,7 @@ func MergeMapsOfProperties(target map[string]PropertiesMap, sources ...map[strin func DeleteUnexpandedPropsFromString(str string) (string, error) { rxp, err := regexp.Compile("\\{.+?\\}") if err != nil { - return constants.EMPTY_STRING, utils.WrapError(err) + return constants.EMPTY_STRING, i18n.WrapError(err) } return rxp.ReplaceAllString(str, constants.EMPTY_STRING), nil diff --git a/src/arduino.cc/builder/prototypes_adder.go b/src/arduino.cc/builder/prototypes_adder.go index 5fa95c65..9e05b109 100644 --- a/src/arduino.cc/builder/prototypes_adder.go +++ b/src/arduino.cc/builder/prototypes_adder.go @@ -32,7 +32,6 @@ package builder import ( "arduino.cc/builder/constants" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "fmt" "strconv" "strings" @@ -40,28 +39,24 @@ import ( type PrototypesAdder struct{} -func (s *PrototypesAdder) Run(context map[string]interface{}) error { - debugOutput := context[constants.CTX_DEBUG_PREPROCESSOR] != nil - source := context[constants.CTX_SOURCE].(string) +func (s *PrototypesAdder) Run(ctx *types.Context) error { + debugOutput := ctx.DebugPreprocessor + source := ctx.Source source = strings.Replace(source, "\r\n", "\n", -1) source = strings.Replace(source, "\r", "\n", -1) sourceRows := strings.Split(source, "\n") - if !utils.MapHas(context, constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES) { - return nil - } - - firstFunctionLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) + firstFunctionLine := ctx.PrototypesLineWhereToInsert if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) { return nil } - insertionLine := firstFunctionLine + context[constants.CTX_LINE_OFFSET].(int) - 1 + insertionLine := firstFunctionLine + ctx.LineOffset - 1 firstFunctionChar := len(strings.Join(sourceRows[:insertionLine], "\n")) + 1 - prototypeSection := composePrototypeSection(firstFunctionLine, context[constants.CTX_PROTOTYPES].([]*types.Prototype)) - context[constants.CTX_PROTOTYPE_SECTION] = prototypeSection + prototypeSection := composePrototypeSection(firstFunctionLine, ctx.Prototypes) + ctx.PrototypesSection = prototypeSection source = source[:firstFunctionChar] + prototypeSection + source[firstFunctionChar:] if debugOutput { @@ -79,7 +74,7 @@ func (s *PrototypesAdder) Run(context map[string]interface{}) error { } fmt.Println("#END OF PREPROCESSED SOURCE") } - context[constants.CTX_SOURCE] = source + ctx.Source = source return nil } diff --git a/src/arduino.cc/builder/read_file_and_store_in_context.go b/src/arduino.cc/builder/read_file_and_store_in_context.go index 03eaa2a9..bac49009 100644 --- a/src/arduino.cc/builder/read_file_and_store_in_context.go +++ b/src/arduino.cc/builder/read_file_and_store_in_context.go @@ -30,22 +30,22 @@ package builder import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/utils" + "arduino.cc/builder/i18n" + "arduino.cc/builder/types" "io/ioutil" ) type ReadFileAndStoreInContext struct { - TargetField string + Target *string } -func (s *ReadFileAndStoreInContext) Run(context map[string]interface{}) error { - bytes, err := ioutil.ReadFile(context[constants.CTX_FILE_PATH_TO_READ].(string)) +func (s *ReadFileAndStoreInContext) Run(ctx *types.Context) error { + bytes, err := ioutil.ReadFile(ctx.FileToRead) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[s.TargetField] = string(bytes) + *s.Target = string(bytes) return nil } diff --git a/src/arduino.cc/builder/recipe_runner.go b/src/arduino.cc/builder/recipe_runner.go index 8e656bd7..98f4632f 100644 --- a/src/arduino.cc/builder/recipe_runner.go +++ b/src/arduino.cc/builder/recipe_runner.go @@ -33,8 +33,7 @@ import ( "arduino.cc/builder/builder_utils" "arduino.cc/builder/constants" "arduino.cc/builder/i18n" - "arduino.cc/builder/props" - "arduino.cc/builder/utils" + "arduino.cc/builder/types" "os" "sort" "strings" @@ -45,28 +44,25 @@ type RecipeByPrefixSuffixRunner struct { Suffix string } -func (s *RecipeByPrefixSuffixRunner) Run(context map[string]interface{}) error { - logger := context[constants.CTX_LOGGER].(i18n.Logger) - if utils.DebugLevel(context) >= 10 { +func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error { + logger := ctx.GetLogger() + if ctx.DebugLevel >= 10 { logger.Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_LOOKING_FOR_RECIPES, s.Prefix, s.Suffix) } - buildProperties := make(props.PropertiesMap) - if p, ok := context[constants.CTX_BUILD_PROPERTIES]; ok { - buildProperties = p.(props.PropertiesMap).Clone() - } - verbose := context[constants.CTX_VERBOSE].(bool) + buildProperties := ctx.BuildProperties + verbose := ctx.Verbose recipes := findRecipes(buildProperties, s.Prefix, s.Suffix) properties := buildProperties.Clone() for _, recipe := range recipes { - if utils.DebugLevel(context) >= 10 { + if ctx.DebugLevel >= 10 { logger.Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_RECIPE, recipe) } _, err := builder_utils.ExecRecipe(properties, recipe, false, verbose, verbose, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } diff --git a/src/arduino.cc/builder/rewrite_hardware_keys.go b/src/arduino.cc/builder/rewrite_hardware_keys.go index b027165a..a245d309 100644 --- a/src/arduino.cc/builder/rewrite_hardware_keys.go +++ b/src/arduino.cc/builder/rewrite_hardware_keys.go @@ -32,19 +32,18 @@ package builder import ( "arduino.cc/builder/constants" "arduino.cc/builder/types" - "arduino.cc/builder/utils" ) type RewriteHardwareKeys struct{} -func (s *RewriteHardwareKeys) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_PLATFORM_KEYS_REWRITE) { +func (s *RewriteHardwareKeys) Run(ctx *types.Context) error { + if ctx.PlatformKeyRewrites.Empty() { return nil } - packages := context[constants.CTX_HARDWARE].(*types.Packages) - platformKeysRewrite := context[constants.CTX_PLATFORM_KEYS_REWRITE].(types.PlatforKeysRewrite) - hardwareRewriteResults := context[constants.CTX_HARDWARE_REWRITE_RESULTS].(map[*types.Platform][]types.PlatforKeyRewrite) + packages := ctx.Hardware + platformKeysRewrite := ctx.PlatformKeyRewrites + hardwareRewriteResults := ctx.HardwareRewriteResults for _, aPackage := range packages.Packages { for _, platform := range aPackage.Platforms { diff --git a/src/arduino.cc/builder/set_custom_build_properties.go b/src/arduino.cc/builder/set_custom_build_properties.go index ed908772..330a25cc 100644 --- a/src/arduino.cc/builder/set_custom_build_properties.go +++ b/src/arduino.cc/builder/set_custom_build_properties.go @@ -30,24 +30,19 @@ package builder import ( - "arduino.cc/builder/constants" "arduino.cc/builder/i18n" "arduino.cc/builder/props" - "arduino.cc/builder/utils" + "arduino.cc/builder/types" ) type SetCustomBuildProperties struct{} -func (s *SetCustomBuildProperties) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_CUSTOM_BUILD_PROPERTIES) { - return nil - } - - logger := context[constants.CTX_LOGGER].(i18n.Logger) - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - customBuildProperties, err := props.LoadFromSlice(context[constants.CTX_CUSTOM_BUILD_PROPERTIES].([]string), logger) +func (s *SetCustomBuildProperties) Run(ctx *types.Context) error { + logger := ctx.GetLogger() + buildProperties := ctx.BuildProperties + customBuildProperties, err := props.LoadFromSlice(ctx.CustomBuildProperties, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for key, value := range customBuildProperties { diff --git a/src/arduino.cc/builder/setup_build_properties.go b/src/arduino.cc/builder/setup_build_properties.go index 7e9332f7..9523a09d 100644 --- a/src/arduino.cc/builder/setup_build_properties.go +++ b/src/arduino.cc/builder/setup_build_properties.go @@ -42,33 +42,33 @@ import ( type SetupBuildProperties struct{} -func (s *SetupBuildProperties) Run(context map[string]interface{}) error { - packages := context[constants.CTX_HARDWARE].(*types.Packages) +func (s *SetupBuildProperties) Run(ctx *types.Context) error { + packages := ctx.Hardware - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) - actualPlatform := context[constants.CTX_ACTUAL_PLATFORM].(*types.Platform) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetPlatform := ctx.TargetPlatform + actualPlatform := ctx.ActualPlatform + targetBoard := ctx.TargetBoard buildProperties := make(props.PropertiesMap) buildProperties.Merge(actualPlatform.Properties) buildProperties.Merge(targetPlatform.Properties) buildProperties.Merge(targetBoard.Properties) - if utils.MapHas(context, constants.CTX_BUILD_PATH) { - buildProperties[constants.BUILD_PROPERTIES_BUILD_PATH] = context[constants.CTX_BUILD_PATH].(string) + if ctx.BuildPath != "" { + buildProperties[constants.BUILD_PROPERTIES_BUILD_PATH] = ctx.BuildPath } - if utils.MapHas(context, constants.CTX_SKETCH) { - buildProperties[constants.BUILD_PROPERTIES_BUILD_PROJECT_NAME] = filepath.Base(context[constants.CTX_SKETCH].(*types.Sketch).MainFile.Name) + if ctx.Sketch != nil { + buildProperties[constants.BUILD_PROPERTIES_BUILD_PROJECT_NAME] = filepath.Base(ctx.Sketch.MainFile.Name) } buildProperties[constants.BUILD_PROPERTIES_BUILD_ARCH] = strings.ToUpper(targetPlatform.PlatformId) - buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE] = context[constants.CTX_BUILD_CORE].(string) + buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE] = ctx.BuildCore buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH] = filepath.Join(actualPlatform.Folder, constants.FOLDER_CORES, buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE]) buildProperties[constants.BUILD_PROPERTIES_BUILD_SYSTEM_PATH] = filepath.Join(actualPlatform.Folder, constants.FOLDER_SYSTEM) buildProperties[constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH] = targetPlatform.Folder buildProperties[constants.BUILD_PROPERTIES_RUNTIME_HARDWARE_PATH] = filepath.Join(targetPlatform.Folder, "..") - buildProperties[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION].(string) - buildProperties[constants.IDE_VERSION] = context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION].(string) + buildProperties[constants.BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = ctx.ArduinoAPIVersion + buildProperties[constants.IDE_VERSION] = ctx.ArduinoAPIVersion buildProperties[constants.BUILD_PROPERTIES_RUNTIME_OS] = utils.PrettyOSName() variant := buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT] @@ -86,7 +86,7 @@ func (s *SetupBuildProperties) Run(context map[string]interface{}) error { buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH] = filepath.Join(variantPlatform.Folder, constants.FOLDER_VARIANTS, variant) } - tools := context[constants.CTX_TOOLS].([]*types.Tool) + tools := ctx.Tools for _, tool := range tools { buildProperties[constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX+tool.Name+constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX] = tool.Folder buildProperties[constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX+tool.Name+"-"+tool.Version+constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX] = tool.Folder @@ -96,8 +96,8 @@ func (s *SetupBuildProperties) Run(context map[string]interface{}) error { buildProperties[constants.BUILD_PROPERTIES_SOFTWARE] = DEFAULT_SOFTWARE } - if utils.MapHas(context, constants.CTX_SKETCH_LOCATION) { - sourcePath, err := filepath.Abs(context[constants.CTX_SKETCH_LOCATION].(string)) + if ctx.SketchLocation != "" { + sourcePath, err := filepath.Abs(ctx.SketchLocation) if err != nil { return err } @@ -111,7 +111,7 @@ func (s *SetupBuildProperties) Run(context map[string]interface{}) error { buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_ZONE] = strconv.Itoa(utils.TimezoneOffset()) buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_DST] = strconv.Itoa(utils.DaylightSavingsOffset(now)) - context[constants.CTX_BUILD_PROPERTIES] = buildProperties + ctx.BuildProperties = buildProperties return nil } diff --git a/src/arduino.cc/builder/setup_human_logger_if_missing.go b/src/arduino.cc/builder/setup_human_logger_if_missing.go deleted file mode 100644 index 4726dc32..00000000 --- a/src/arduino.cc/builder/setup_human_logger_if_missing.go +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of Arduino Builder. - * - * Arduino Builder is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - */ - -package builder - -import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" - "arduino.cc/builder/utils" -) - -type SetupHumanLoggerIfMissing struct{} - -func (s *SetupHumanLoggerIfMissing) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_LOGGER) { - context[constants.CTX_LOGGER] = i18n.HumanLogger{} - } - - return nil -} diff --git a/src/arduino.cc/builder/sketch_loader.go b/src/arduino.cc/builder/sketch_loader.go index 2c2ec10c..dba7ce3c 100644 --- a/src/arduino.cc/builder/sketch_loader.go +++ b/src/arduino.cc/builder/sketch_loader.go @@ -44,43 +44,45 @@ import ( type SketchLoader struct{} -func (s *SketchLoader) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_SKETCH_LOCATION) { +func (s *SketchLoader) Run(ctx *types.Context) error { + if ctx.SketchLocation == "" { return nil } - sketchLocation := context[constants.CTX_SKETCH_LOCATION].(string) + sketchLocation := ctx.SketchLocation sketchLocation, err := filepath.Abs(sketchLocation) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } mainSketchStat, err := os.Stat(sketchLocation) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if mainSketchStat.IsDir() { sketchLocation = filepath.Join(sketchLocation, mainSketchStat.Name()+".ino") } - context[constants.CTX_SKETCH_LOCATION] = sketchLocation + + ctx.SketchLocation = sketchLocation allSketchFilePaths, err := collectAllSketchFiles(filepath.Dir(sketchLocation)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } + logger := ctx.GetLogger() + if !utils.SliceContains(allSketchFilePaths, sketchLocation) { - return utils.Errorf(context, constants.MSG_CANT_FIND_SKETCH_IN_PATH, sketchLocation, filepath.Dir(sketchLocation)) + return i18n.ErrorfWithLogger(logger, constants.MSG_CANT_FIND_SKETCH_IN_PATH, sketchLocation, filepath.Dir(sketchLocation)) } - logger := context[constants.CTX_LOGGER].(i18n.Logger) sketch, err := makeSketch(sketchLocation, allSketchFilePaths, logger) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_SKETCH] = sketch + ctx.SketchLocation = sketchLocation + ctx.Sketch = sketch return nil } @@ -94,7 +96,7 @@ func collectAllSketchFiles(from string) ([]string, error) { } walkFunc := utils.CollectAllReadableFiles(&filePaths, checkExtensionFunc) err := gohasissues.Walk(from, walkFunc) - return filePaths, utils.WrapError(err) + return filePaths, i18n.WrapError(err) } func makeSketch(sketchLocation string, allSketchFilePaths []string, logger i18n.Logger) (*types.Sketch, error) { @@ -102,7 +104,7 @@ func makeSketch(sketchLocation string, allSketchFilePaths []string, logger i18n. for _, sketchFilePath := range allSketchFilePaths { source, err := ioutil.ReadFile(sketchFilePath) if err != nil { - return nil, utils.WrapError(err) + return nil, i18n.WrapError(err) } sketchFilesMap[sketchFilePath] = types.SketchFile{Name: sketchFilePath, Source: string(source)} } @@ -122,7 +124,7 @@ func makeSketch(sketchLocation string, allSketchFilePaths []string, logger i18n. } else if ADDITIONAL_FILE_VALID_EXTENSIONS[ext] { additionalFiles = append(additionalFiles, sketchFile) } else { - return nil, utils.ErrorfWithLogger(logger, constants.MSG_UNKNOWN_SKETCH_EXT, sketchFile.Name) + return nil, i18n.ErrorfWithLogger(logger, constants.MSG_UNKNOWN_SKETCH_EXT, sketchFile.Name) } } diff --git a/src/arduino.cc/builder/sketch_saver.go b/src/arduino.cc/builder/sketch_saver.go index 1a6bfb57..8737cc69 100644 --- a/src/arduino.cc/builder/sketch_saver.go +++ b/src/arduino.cc/builder/sketch_saver.go @@ -30,7 +30,7 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "path/filepath" @@ -38,16 +38,15 @@ import ( type SketchSaver struct{} -func (s *SketchSaver) Run(context map[string]interface{}) error { - sketch := context[constants.CTX_SKETCH].(*types.Sketch) - sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string) - source := context[constants.CTX_SOURCE].(string) +func (s *SketchSaver) Run(ctx *types.Context) error { + sketch := ctx.Sketch + sketchBuildPath := ctx.SketchBuildPath err := utils.EnsureFolderExists(sketchBuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } - err = utils.WriteFile(filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"), source) - return utils.WrapError(err) + err = utils.WriteFile(filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"), ctx.Source) + return i18n.WrapError(err) } diff --git a/src/arduino.cc/builder/sketch_source_merger.go b/src/arduino.cc/builder/sketch_source_merger.go index d4cc71a3..36c7c7a9 100644 --- a/src/arduino.cc/builder/sketch_source_merger.go +++ b/src/arduino.cc/builder/sketch_source_merger.go @@ -31,7 +31,6 @@ package builder import ( - "arduino.cc/builder/constants" "arduino.cc/builder/types" "regexp" "strings" @@ -39,8 +38,8 @@ import ( type SketchSourceMerger struct{} -func (s *SketchSourceMerger) Run(context map[string]interface{}) error { - sketch := context[constants.CTX_SKETCH].(*types.Sketch) +func (s *SketchSourceMerger) Run(ctx *types.Context) error { + sketch := ctx.Sketch lineOffset := 0 includeSection := "" @@ -50,7 +49,7 @@ func (s *SketchSourceMerger) Run(context map[string]interface{}) error { } includeSection += "#line 1 \"" + strings.Replace((&sketch.MainFile).Name, "\\", "\\\\", -1) + "\"\n" lineOffset++ - context[constants.CTX_INCLUDE_SECTION] = includeSection + ctx.IncludeSection = includeSection source := includeSection source += addSourceWrappedWithLineDirective(&sketch.MainFile) @@ -59,8 +58,8 @@ func (s *SketchSourceMerger) Run(context map[string]interface{}) error { source += addSourceWrappedWithLineDirective(&file) } - context[constants.CTX_LINE_OFFSET] = lineOffset - context[constants.CTX_SOURCE] = source + ctx.LineOffset = lineOffset + ctx.Source = source return nil } diff --git a/src/arduino.cc/builder/store_build_options_map.go b/src/arduino.cc/builder/store_build_options_map.go index 4e2172a4..a318e77f 100644 --- a/src/arduino.cc/builder/store_build_options_map.go +++ b/src/arduino.cc/builder/store_build_options_map.go @@ -31,17 +31,14 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "path/filepath" ) type StoreBuildOptionsMap struct{} -func (s *StoreBuildOptionsMap) Run(context map[string]interface{}) error { - buildPath := context[constants.CTX_BUILD_PATH].(string) - buildOptionsJson := context[constants.CTX_BUILD_OPTIONS_JSON].(string) - - utils.WriteFile(filepath.Join(buildPath, constants.BUILD_OPTIONS_FILE), buildOptionsJson) - +func (s *StoreBuildOptionsMap) Run(ctx *types.Context) error { + utils.WriteFile(filepath.Join(ctx.BuildPath, constants.BUILD_OPTIONS_FILE), ctx.BuildOptionsJson) return nil } diff --git a/src/arduino.cc/builder/target_board_resolver.go b/src/arduino.cc/builder/target_board_resolver.go index c1c58c4a..875c9455 100644 --- a/src/arduino.cc/builder/target_board_resolver.go +++ b/src/arduino.cc/builder/target_board_resolver.go @@ -31,6 +31,7 @@ package builder import ( "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "strings" @@ -38,34 +39,36 @@ import ( type TargetBoardResolver struct{} -func (s *TargetBoardResolver) Run(context map[string]interface{}) error { - fqbn := context[constants.CTX_FQBN].(string) +func (s *TargetBoardResolver) Run(ctx *types.Context) error { + fqbn := ctx.FQBN fqbnParts := strings.Split(fqbn, ":") targetPackageName := fqbnParts[0] targetPlatformName := fqbnParts[1] targetBoardName := fqbnParts[2] - packages := context[constants.CTX_HARDWARE].(*types.Packages) + packages := ctx.Hardware + + logger := ctx.GetLogger() targetPackage := packages.Packages[targetPackageName] if targetPackage == nil { - return utils.Errorf(context, constants.MSG_PACKAGE_UNKNOWN, targetPackageName) + return i18n.ErrorfWithLogger(logger, constants.MSG_PACKAGE_UNKNOWN, targetPackageName) } targetPlatform := targetPackage.Platforms[targetPlatformName] if targetPlatform == nil { - return utils.Errorf(context, constants.MSG_PLATFORM_UNKNOWN, targetPlatformName, targetPackageName) + return i18n.ErrorfWithLogger(logger, constants.MSG_PLATFORM_UNKNOWN, targetPlatformName, targetPackageName) } targetBoard := targetPlatform.Boards[targetBoardName] if targetBoard == nil { - return utils.Errorf(context, constants.MSG_BOARD_UNKNOWN, targetBoardName, targetPlatformName, targetPackageName) + return i18n.ErrorfWithLogger(logger, constants.MSG_BOARD_UNKNOWN, targetBoardName, targetPlatformName, targetPackageName) } - context[constants.CTX_TARGET_PACKAGE] = targetPackage - context[constants.CTX_TARGET_PLATFORM] = targetPlatform - context[constants.CTX_TARGET_BOARD] = targetBoard + ctx.TargetPackage = targetPackage + ctx.TargetPlatform = targetPlatform + ctx.TargetBoard = targetBoard if len(fqbnParts) > 3 { addAdditionalPropertiesToTargetBoard(targetBoard, fqbnParts[3]) @@ -81,7 +84,7 @@ func (s *TargetBoardResolver) Run(context map[string]interface{}) error { if len(coreParts) > 1 { core = coreParts[1] if packages.Packages[coreParts[0]] == nil { - return utils.Errorf(context, constants.MSG_MISSING_CORE_FOR_BOARD, coreParts[0]) + return i18n.ErrorfWithLogger(logger, constants.MSG_MISSING_CORE_FOR_BOARD, coreParts[0]) } corePlatform = packages.Packages[coreParts[0]].Platforms[targetPlatform.PlatformId] @@ -94,8 +97,8 @@ func (s *TargetBoardResolver) Run(context map[string]interface{}) error { actualPlatform = targetPlatform } - context[constants.CTX_BUILD_CORE] = core - context[constants.CTX_ACTUAL_PLATFORM] = actualPlatform + ctx.BuildCore = core + ctx.ActualPlatform = actualPlatform return nil } diff --git a/src/arduino.cc/builder/test/add_additional_entries_to_context_test.go b/src/arduino.cc/builder/test/add_additional_entries_to_context_test.go index 416f5335..a2bfbfc6 100644 --- a/src/arduino.cc/builder/test/add_additional_entries_to_context_test.go +++ b/src/arduino.cc/builder/test/add_additional_entries_to_context_test.go @@ -39,45 +39,40 @@ import ( ) func TestAddAdditionalEntriesToContextNoBuildPath(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} command := builder.AddAdditionalEntriesToContext{} - NoError(t, command.Run(context)) + NoError(t, command.Run(ctx)) - require.Nil(t, context[constants.CTX_PREPROC_PATH]) - require.Nil(t, context[constants.CTX_SKETCH_BUILD_PATH]) - require.Nil(t, context[constants.CTX_LIBRARIES_BUILD_PATH]) - require.Nil(t, context[constants.CTX_CORE_BUILD_PATH]) + require.Empty(t, ctx.PreprocPath) + require.Empty(t, ctx.SketchBuildPath) + require.Empty(t, ctx.LibrariesBuildPath) + require.Empty(t, ctx.CoreBuildPath) - require.NotNil(t, context[constants.CTX_WARNINGS_LEVEL]) - require.NotNil(t, context[constants.CTX_VERBOSE]) - require.NotNil(t, context[constants.CTX_DEBUG_LEVEL]) + require.NotNil(t, ctx.WarningsLevel) - require.True(t, context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE].(*types.UniqueStringQueue).Empty()) - require.True(t, context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE].(*types.UniqueSourceFolderQueue).Empty()) + require.True(t, ctx.CollectedSourceFiles.Empty()) + require.True(t, ctx.FoldersWithSourceFiles.Empty()) - require.Equal(t, 0, len(context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult))) + require.Equal(t, 0, len(ctx.LibrariesResolutionResults)) } func TestAddAdditionalEntriesToContextWithBuildPath(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_BUILD_PATH] = "folder" + ctx := &types.Context{} + ctx.BuildPath = "folder" command := builder.AddAdditionalEntriesToContext{} - NoError(t, command.Run(context)) + NoError(t, command.Run(ctx)) - require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_PREPROC)), context[constants.CTX_PREPROC_PATH]) - require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_SKETCH)), context[constants.CTX_SKETCH_BUILD_PATH]) - require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_LIBRARIES)), context[constants.CTX_LIBRARIES_BUILD_PATH]) - require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_CORE)), context[constants.CTX_CORE_BUILD_PATH]) + require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_PREPROC)), ctx.PreprocPath) + require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_SKETCH)), ctx.SketchBuildPath) + require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_LIBRARIES)), ctx.LibrariesBuildPath) + require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_CORE)), ctx.CoreBuildPath) - require.NotNil(t, context[constants.CTX_WARNINGS_LEVEL]) - require.NotNil(t, context[constants.CTX_VERBOSE]) - require.NotNil(t, context[constants.CTX_DEBUG_LEVEL]) + require.NotNil(t, ctx.WarningsLevel) - require.True(t, context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE].(*types.UniqueStringQueue).Empty()) - require.True(t, context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE].(*types.UniqueSourceFolderQueue).Empty()) + require.True(t, ctx.CollectedSourceFiles.Empty()) + require.True(t, ctx.FoldersWithSourceFiles.Empty()) - require.Equal(t, 0, len(context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult))) + require.Equal(t, 0, len(ctx.LibrariesResolutionResults)) } diff --git a/src/arduino.cc/builder/test/add_build_board_property_if_missing_test.go b/src/arduino.cc/builder/test/add_build_board_property_if_missing_test.go index 81450f89..8a38f08b 100644 --- a/src/arduino.cc/builder/test/add_build_board_property_if_missing_test.go +++ b/src/arduino.cc/builder/test/add_build_board_property_if_missing_test.go @@ -39,54 +39,54 @@ import ( ) func TestAddBuildBoardPropertyIfMissing(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:mymega" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + FQBN: "my_avr_platform:avr:mymega", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, &builder.AddBuildBoardPropertyIfMissing{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "my_avr_platform", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "mymega", targetBoard.BoardId) require.Equal(t, constants.EMPTY_STRING, targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) require.Equal(t, "AVR_MYMEGA", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_BOARD]) } func TestAddBuildBoardPropertyIfMissingNotMissing(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:mymega:cpu=atmega2560" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + FQBN: "my_avr_platform:avr:mymega:cpu=atmega2560", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, &builder.AddBuildBoardPropertyIfMissing{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "my_avr_platform", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "mymega", targetBoard.BoardId) require.Equal(t, "atmega2560", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) require.Equal(t, "AVR_MEGA2560", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_BOARD]) diff --git a/src/arduino.cc/builder/test/additional_sketch_files_copier_test.go b/src/arduino.cc/builder/test/additional_sketch_files_copier_test.go index 326f6d77..85d39cb9 100644 --- a/src/arduino.cc/builder/test/additional_sketch_files_copier_test.go +++ b/src/arduino.cc/builder/test/additional_sketch_files_copier_test.go @@ -55,22 +55,21 @@ func (s ByFileInfoName) Less(i, j int) bool { } func TestCopyOtherFiles(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{ + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.SketchLoader{}, &builder.AdditionalSketchFilesCopier{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -93,22 +92,21 @@ func TestCopyOtherFiles(t *testing.T) { } func TestCopyOtherFilesOnlyIfChanged(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{ + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.SketchLoader{}, &builder.AdditionalSketchFilesCopier{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -117,12 +115,13 @@ func TestCopyOtherFilesOnlyIfChanged(t *testing.T) { time.Sleep(2 * time.Second) - context = make(map[string]interface{}) - context[constants.CTX_BUILD_PATH] = buildPath - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") + ctx = &types.Context{ + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + BuildPath: buildPath, + } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } diff --git a/src/arduino.cc/builder/test/builder_test.go b/src/arduino.cc/builder/test/builder_test.go index 3ec2c459..5aeb8f4c 100644 --- a/src/arduino.cc/builder/test/builder_test.go +++ b/src/arduino.cc/builder/test/builder_test.go @@ -32,6 +32,7 @@ package test import ( "arduino.cc/builder" "arduino.cc/builder/constants" + "arduino.cc/builder/types" "github.com/stretchr/testify/require" "os" "os/exec" @@ -42,23 +43,24 @@ import ( func TestBuilderEmptySketch(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - context[constants.CTX_DEBUG_LEVEL] = 10 + ctx.DebugLevel = 10 command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_CORE, "HardwareSerial.cpp.o")) @@ -76,22 +78,22 @@ func TestBuilderEmptySketch(t *testing.T) { func TestBuilderBridge(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_CORE, "HardwareSerial.cpp.o")) @@ -111,21 +113,21 @@ func TestBuilderBridge(t *testing.T) { func TestBuilderSketchWithConfig(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_with_config", "sketch_with_config.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_config", "sketch_with_config.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_CORE, "HardwareSerial.cpp.o")) @@ -145,25 +147,25 @@ func TestBuilderSketchWithConfig(t *testing.T) { func TestBuilderBridgeTwice(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) command = builder.Builder{} - err = command.Run(context) + err = command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_CORE, "HardwareSerial.cpp.o")) @@ -183,22 +185,23 @@ func TestBuilderBridgeTwice(t *testing.T) { func TestBuilderBridgeSAM(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + FQBN: "arduino:sam:arduino_due_x_dbg", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:sam:arduino_due_x_dbg" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_WARNINGS_LEVEL] = "all" + ctx.WarningsLevel = "all" command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_CORE, "syscalls_sam3.c.o")) @@ -227,21 +230,21 @@ func TestBuilderBridgeSAM(t *testing.T) { func TestBuilderBridgeRedBearLab(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}, + ToolsFolders: []string{"downloaded_tools", "downloaded_board_manager_stuff"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + FQBN: "RedBearLab:avr:blend", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"} - context[constants.CTX_FQBN] = "RedBearLab:avr:blend" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_CORE, "HardwareSerial.cpp.o")) @@ -261,107 +264,107 @@ func TestBuilderBridgeRedBearLab(t *testing.T) { func TestBuilderSketchNoFunctions(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}, + ToolsFolders: []string{"downloaded_tools", "downloaded_board_manager_stuff"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_no_functions", "main.ino"), + FQBN: "RedBearLab:avr:blend", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"} - context[constants.CTX_FQBN] = "RedBearLab:avr:blend" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions", "main.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) require.Error(t, err) } func TestBuilderSketchWithBackup(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}, + ToolsFolders: []string{"downloaded_tools", "downloaded_board_manager_stuff"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_with_backup_files", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_backup_files", "sketch.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } func TestBuilderSketchWithOldLib(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_with_old_lib", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_old_lib", "sketch.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } func TestBuilderSketchWithSubfolders(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_with_subfolders", "sketch_with_subfolders.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_subfolders", "sketch_with_subfolders.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } func TestBuilderSketchBuildPathContainsUnusedPreviouslyCompiledLibrary(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + } + + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) NoError(t, os.MkdirAll(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "SPI"), os.FileMode(0755))) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - command := builder.Builder{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "SPI")) diff --git a/src/arduino.cc/builder/test/coan_runner_test.go b/src/arduino.cc/builder/test/coan_runner_test.go deleted file mode 100644 index 0393455b..00000000 --- a/src/arduino.cc/builder/test/coan_runner_test.go +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of Arduino Builder. - * - * Arduino Builder is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - * Copyright 2015 Matthijs Kooijman - */ - -package test - -import ( - "arduino.cc/builder" - "arduino.cc/builder/constants" - "arduino.cc/builder/types" - "github.com/stretchr/testify/require" - "os" - "path/filepath" - "strings" - "testing" -) - -func TestCoanRunner(t *testing.T) { - DownloadCoresAndToolsAndLibraries(t) - - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch2", "SketchWithIfDef.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, - &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.SketchSourceMerger{}, - &builder.CoanRunner{}, - } - - for _, command := range commands { - err := command.Run(context) - NoError(t, err) - } - - expectedSource := LoadAndInterpolate(t, filepath.Join("sketch2", "SketchWithIfDef.resolved.directives.txt"), context) - require.Equal(t, expectedSource, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) - -} diff --git a/src/arduino.cc/builder/test/collect_all_source_files_from_folders_with_sources_test.go b/src/arduino.cc/builder/test/collect_all_source_files_from_folders_with_sources_test.go index 34acde4a..6b92d840 100644 --- a/src/arduino.cc/builder/test/collect_all_source_files_from_folders_with_sources_test.go +++ b/src/arduino.cc/builder/test/collect_all_source_files_from_folders_with_sources_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "path/filepath" @@ -40,21 +39,20 @@ import ( ) func TestCollectAllSourceFilesFromFoldersWithSources(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} sourceFiles := &types.UniqueStringQueue{} - context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE] = sourceFiles + ctx.CollectedSourceFiles = sourceFiles foldersWithSources := &types.UniqueSourceFolderQueue{} foldersWithSources.Push(types.SourceFolder{Folder: Abs(t, "sketch_with_config"), Recurse: true}) - context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE] = foldersWithSources + ctx.FoldersWithSourceFiles = foldersWithSources commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.CollectAllSourceFilesFromFoldersWithSources{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -67,21 +65,20 @@ func TestCollectAllSourceFilesFromFoldersWithSources(t *testing.T) { } func TestCollectAllSourceFilesFromFoldersWithSourcesOfLibrary(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} sourceFiles := &types.UniqueStringQueue{} - context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE] = sourceFiles + ctx.CollectedSourceFiles = sourceFiles foldersWithSources := &types.UniqueSourceFolderQueue{} foldersWithSources.Push(types.SourceFolder{Folder: Abs(t, filepath.Join("downloaded_libraries", "Bridge")), Recurse: true}) - context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE] = foldersWithSources + ctx.FoldersWithSourceFiles = foldersWithSources commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.CollectAllSourceFilesFromFoldersWithSources{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -102,23 +99,22 @@ func TestCollectAllSourceFilesFromFoldersWithSourcesOfLibrary(t *testing.T) { } func TestCollectAllSourceFilesFromFoldersWithSourcesOfOldLibrary(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} sourceFiles := &types.UniqueStringQueue{} - context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE] = sourceFiles + ctx.CollectedSourceFiles = sourceFiles foldersWithSources := &types.UniqueSourceFolderQueue{} foldersWithSources.Push(types.SourceFolder{Folder: Abs(t, filepath.Join("libraries", "ShouldNotRecurseWithOldLibs")), Recurse: false}) foldersWithSources.Push(types.SourceFolder{Folder: Abs(t, filepath.Join("libraries", "ShouldNotRecurseWithOldLibs", "utility")), Recurse: false}) foldersWithSources.Push(types.SourceFolder{Folder: Abs(t, "non existent folder"), Recurse: false}) - context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE] = foldersWithSources + ctx.FoldersWithSourceFiles = foldersWithSources commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.CollectAllSourceFilesFromFoldersWithSources{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } diff --git a/src/arduino.cc/builder/test/create_build_options_map_test.go b/src/arduino.cc/builder/test/create_build_options_map_test.go index 08002c6f..f129025c 100644 --- a/src/arduino.cc/builder/test/create_build_options_map_test.go +++ b/src/arduino.cc/builder/test/create_build_options_map_test.go @@ -31,44 +31,36 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" - "arduino.cc/builder/utils" + "arduino.cc/builder/types" "github.com/stretchr/testify/require" "testing" ) func TestCreateBuildOptionsMap(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_BUILD_PATH] = "buildPath" - context[constants.CTX_HARDWARE_FOLDERS] = []string{"hardware", "hardware2"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"tools"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_FQBN] = "fqbn" - context[constants.CTX_SKETCH_LOCATION] = "sketchLocation" - context[constants.CTX_VERBOSE] = true - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "ideVersion" - context[constants.CTX_DEBUG_LEVEL] = 5 + ctx := &types.Context{ + HardwareFolders: []string{"hardware", "hardware2"}, + ToolsFolders: []string{"tools"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: "sketchLocation", + FQBN: "fqbn", + ArduinoAPIVersion: "ideVersion", + Verbose: true, + BuildPath: "buildPath", + DebugLevel: 5, + } create := builder.CreateBuildOptionsMap{} - err := create.Run(context) + err := create.Run(ctx) NoError(t, err) - buildOptions := context[constants.CTX_BUILD_OPTIONS].(map[string]string) - require.Equal(t, 6, len(utils.KeysOfMapOfString(buildOptions))) - require.Equal(t, "hardware,hardware2", buildOptions[constants.CTX_HARDWARE_FOLDERS]) - require.Equal(t, "tools", buildOptions[constants.CTX_TOOLS_FOLDERS]) - require.Equal(t, "libraries", buildOptions[constants.CTX_OTHER_LIBRARIES_FOLDERS]) - require.Equal(t, "fqbn", buildOptions[constants.CTX_FQBN]) - require.Equal(t, "sketchLocation", buildOptions[constants.CTX_SKETCH_LOCATION]) - require.Equal(t, "ideVersion", buildOptions[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION]) - require.Equal(t, "{\n"+ + " \"builtInLibrariesFolders\": \"\",\n"+ + " \"customBuildProperties\": \"\",\n"+ " \"fqbn\": \"fqbn\",\n"+ " \"hardwareFolders\": \"hardware,hardware2\",\n"+ " \"otherLibrariesFolders\": \"libraries\",\n"+ " \"runtime.ide.version\": \"ideVersion\",\n"+ " \"sketchLocation\": \"sketchLocation\",\n"+ " \"toolsFolders\": \"tools\"\n"+ - "}", context[constants.CTX_BUILD_OPTIONS_JSON].(string)) + "}", ctx.BuildOptionsJson) } diff --git a/src/arduino.cc/builder/test/ctags_parser_test.go b/src/arduino.cc/builder/test/ctags_parser_test.go index 9f671308..45f246af 100644 --- a/src/arduino.cc/builder/test/ctags_parser_test.go +++ b/src/arduino.cc/builder/test/ctags_parser_test.go @@ -30,7 +30,6 @@ package test import ( - "arduino.cc/builder/constants" "arduino.cc/builder/ctags" "arduino.cc/builder/types" "github.com/stretchr/testify/require" @@ -40,17 +39,17 @@ import ( ) func TestCTagsParserShouldListPrototypes(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListPrototypes.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 8, len(tags)) idx := 0 @@ -88,17 +87,17 @@ func TestCTagsParserShouldListPrototypes(t *testing.T) { } func TestCTagsParserShouldListTemplates(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 3, len(tags)) idx := 0 @@ -116,17 +115,17 @@ func TestCTagsParserShouldListTemplates(t *testing.T) { } func TestCTagsParserShouldListTemplates2(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates2.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 4, len(tags)) idx := 0 @@ -146,17 +145,17 @@ func TestCTagsParserShouldListTemplates2(t *testing.T) { } func TestCTagsParserShouldDealWithClasses(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithClasses.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 2, len(tags)) idx := 0 @@ -168,17 +167,17 @@ func TestCTagsParserShouldDealWithClasses(t *testing.T) { } func TestCTagsParserShouldDealWithStructs(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithStructs.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 5, len(tags)) idx := 0 @@ -200,17 +199,17 @@ func TestCTagsParserShouldDealWithStructs(t *testing.T) { } func TestCTagsParserShouldDealWithMacros(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithMacros.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 8, len(tags)) idx := 0 @@ -240,17 +239,17 @@ func TestCTagsParserShouldDealWithMacros(t *testing.T) { } func TestCTagsParserShouldDealFunctionWithDifferentSignatures(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 3, len(tags)) idx := 0 @@ -265,17 +264,17 @@ func TestCTagsParserShouldDealFunctionWithDifferentSignatures(t *testing.T) { } func TestCTagsParserClassMembersAreFilteredOut(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserClassMembersAreFilteredOut.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 5, len(tags)) idx := 0 @@ -299,17 +298,17 @@ func TestCTagsParserClassMembersAreFilteredOut(t *testing.T) { } func TestCTagsParserStructWithFunctions(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStructWithFunctions.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 8, len(tags)) idx := 0 @@ -341,17 +340,17 @@ func TestCTagsParserStructWithFunctions(t *testing.T) { } func TestCTagsParserDefaultArguments(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserDefaultArguments.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 3, len(tags)) idx := 0 @@ -367,17 +366,17 @@ func TestCTagsParserDefaultArguments(t *testing.T) { } func TestCTagsParserNamespace(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserNamespace.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 3, len(tags)) idx := 0 @@ -393,17 +392,17 @@ func TestCTagsParserNamespace(t *testing.T) { } func TestCTagsParserStatic(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStatic.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 3, len(tags)) idx := 0 @@ -418,17 +417,17 @@ func TestCTagsParserStatic(t *testing.T) { } func TestCTagsParserFunctionPointer(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointer.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 4, len(tags)) idx := 0 @@ -446,17 +445,17 @@ func TestCTagsParserFunctionPointer(t *testing.T) { } func TestCTagsParserFunctionPointers(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointers.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(context) + ctagsParser.Run(ctx) - tags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag) + tags := ctx.CTagsOfPreprocessedSource require.Equal(t, 5, len(tags)) idx := 0 diff --git a/src/arduino.cc/builder/test/ctags_runner_test.go b/src/arduino.cc/builder/test/ctags_runner_test.go index 8cb77b9e..d7d96ef4 100644 --- a/src/arduino.cc/builder/test/ctags_runner_test.go +++ b/src/arduino.cc/builder/test/ctags_runner_test.go @@ -44,23 +44,23 @@ import ( func TestCTagsRunner(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + sketchLocation := Abs(t, filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino")) - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - sketchLocation := Abs(t, filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino")) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -70,12 +70,12 @@ func TestCTagsRunner(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{SourceField: constants.CTX_SOURCE, TargetFileName: constants.FILE_CTAGS_TARGET}, + &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, &ctags.CTagsRunner{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -88,29 +88,29 @@ func TestCTagsRunner(t *testing.T) { "analogCommand " + sketchLocation + " /^void analogCommand(BridgeClient client) {$/;\" kind:function line:109 signature:(BridgeClient client) returntype:void\n" + "modeCommand " + sketchLocation + " /^void modeCommand(BridgeClient client) {$/;\" kind:function line:149 signature:(BridgeClient client) returntype:void\n" - require.Equal(t, expectedOutput, strings.Replace(context[constants.CTX_CTAGS_OUTPUT].(string), "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithClass(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + sketchLocation := Abs(t, filepath.Join("sketch_with_class", "sketch.ino")) - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - sketchLocation := Abs(t, filepath.Join("sketch_with_class", "sketch.ino")) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -120,12 +120,12 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{SourceField: constants.CTX_SOURCE, TargetFileName: constants.FILE_CTAGS_TARGET}, + &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, &ctags.CTagsRunner{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -136,29 +136,29 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { "setup\t" + sketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:13\tsignature:()\treturntype:void\n" + "loop\t" + sketchLocation + "\t/^void loop() {$/;\"\tkind:function\tline:17\tsignature:()\treturntype:void\n" - require.Equal(t, expectedOutput, strings.Replace(context[constants.CTX_CTAGS_OUTPUT].(string), "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithTypename(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + sketchLocation := Abs(t, filepath.Join("sketch_with_typename", "sketch.ino")) - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - sketchLocation := Abs(t, filepath.Join("sketch_with_typename", "sketch.ino")) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -168,12 +168,12 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{SourceField: constants.CTX_SOURCE, TargetFileName: constants.FILE_CTAGS_TARGET}, + &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, &ctags.CTagsRunner{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -183,29 +183,29 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { "loop\t" + sketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" + "func\t" + sketchLocation + "\t/^typename Foo::Bar func(){$/;\"\tkind:function\tline:12\tsignature:()\treturntype:Foo::Bar\n" - require.Equal(t, expectedOutput, strings.Replace(context[constants.CTX_CTAGS_OUTPUT].(string), "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithNamespace(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + sketchLocation := Abs(t, filepath.Join("sketch_with_namespace", "sketch.ino")) - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - sketchLocation := Abs(t, filepath.Join("sketch_with_namespace", "sketch.ino")) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -215,12 +215,12 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{SourceField: constants.CTX_SOURCE, TargetFileName: constants.FILE_CTAGS_TARGET}, + &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, &ctags.CTagsRunner{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -229,29 +229,29 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { "setup\t" + sketchLocation + "\t/^void setup() {}$/;\"\tkind:function\tline:7\tsignature:()\treturntype:void\n" + "loop\t" + sketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:8\tsignature:()\treturntype:void\n" - require.Equal(t, expectedOutput, strings.Replace(context[constants.CTX_CTAGS_OUTPUT].(string), "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithTemplates(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + sketchLocation := Abs(t, filepath.Join("sketch_with_templates_and_shift", "template_and_shift.cpp")) - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - sketchLocation := Abs(t, filepath.Join("sketch_with_templates_and_shift", "template_and_shift.cpp")) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -261,12 +261,12 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{SourceField: constants.CTX_SOURCE, TargetFileName: constants.FILE_CTAGS_TARGET}, + &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, &ctags.CTagsRunner{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -276,5 +276,5 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { "aVar\t" + sketchLocation + "\t/^c< 1<<8 > aVar;$/;\"\tkind:variable\tline:16\n" + "func\t" + sketchLocation + "\t/^template func( c< 1< & aParam) {$/;\"\tkind:function\tline:18\tsignature:( c< 1< & aParam)\treturntype:template\n" - require.Equal(t, expectedOutput, strings.Replace(context[constants.CTX_CTAGS_OUTPUT].(string), "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) } diff --git a/src/arduino.cc/builder/test/ctags_to_prototypes_test.go b/src/arduino.cc/builder/test/ctags_to_prototypes_test.go index 29597646..ca1a2a6b 100644 --- a/src/arduino.cc/builder/test/ctags_to_prototypes_test.go +++ b/src/arduino.cc/builder/test/ctags_to_prototypes_test.go @@ -30,7 +30,6 @@ package test import ( - "arduino.cc/builder/constants" "arduino.cc/builder/ctags" "arduino.cc/builder/types" "github.com/stretchr/testify/require" @@ -39,26 +38,33 @@ import ( "testing" ) +type CollectCtagsFromPreprocSource struct{} + +func (*CollectCtagsFromPreprocSource) Run(ctx *types.Context) error { + ctx.CTagsCollected = ctx.CTagsOfPreprocessedSource + return nil +} + func TestCTagsToPrototypesShouldListPrototypes(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListPrototypes.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 5, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) @@ -68,30 +74,29 @@ func TestCTagsToPrototypesShouldListPrototypes(t *testing.T) { require.Equal(t, "void analogCommand(YunClient client);", prototypes[3].Prototype) require.Equal(t, "void modeCommand(YunClient client);", prototypes[4].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 33, prototypeLine) + require.Equal(t, 33, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesShouldListTemplates(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 3, len(prototypes)) require.Equal(t, "template T minimum (T a, T b);", prototypes[0].Prototype) @@ -99,30 +104,29 @@ func TestCTagsToPrototypesShouldListTemplates(t *testing.T) { require.Equal(t, "void setup();", prototypes[1].Prototype) require.Equal(t, "void loop();", prototypes[2].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 2, prototypeLine) + require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesShouldListTemplates2(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates2.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 4, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) @@ -131,57 +135,55 @@ func TestCTagsToPrototypesShouldListTemplates2(t *testing.T) { require.Equal(t, "template int SRAM_writeAnything(int ee, const T& value);", prototypes[2].Prototype) require.Equal(t, "template int SRAM_readAnything(int ee, T& value);", prototypes[3].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 1, prototypeLine) + require.Equal(t, 1, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesShouldDealWithClasses(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithClasses.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 0, len(prototypes)) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 8, prototypeLine) + require.Equal(t, 8, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesShouldDealWithStructs(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithStructs.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 3, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) @@ -189,30 +191,29 @@ func TestCTagsToPrototypesShouldDealWithStructs(t *testing.T) { require.Equal(t, "void loop();", prototypes[1].Prototype) require.Equal(t, "void dostuff(A_NEW_TYPE * bar);", prototypes[2].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 9, prototypeLine) + require.Equal(t, 9, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesShouldDealWithMacros(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithMacros.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 5, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) @@ -222,119 +223,115 @@ func TestCTagsToPrototypesShouldDealWithMacros(t *testing.T) { require.Equal(t, "void disabledIsDefined();", prototypes[3].Prototype) require.Equal(t, "int useMyType(MyType type);", prototypes[4].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 18, prototypeLine) + require.Equal(t, 18, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesShouldDealFunctionWithDifferentSignatures(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 1, len(prototypes)) require.Equal(t, "boolean getBytes( byte addr, int amount );", prototypes[0].Prototype) require.Equal(t, "/tmp/test260613593/preproc/ctags_target.cpp", prototypes[0].File) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 5031, prototypeLine) + require.Equal(t, 5031, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesClassMembersAreFilteredOut(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserClassMembersAreFilteredOut.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 2, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) require.Equal(t, "/tmp/test834438754/preproc/ctags_target.cpp", prototypes[0].File) require.Equal(t, "void loop();", prototypes[1].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 14, prototypeLine) + require.Equal(t, 14, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesStructWithFunctions(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStructWithFunctions.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 2, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) require.Equal(t, "/tmp/build7315640391316178285.tmp/preproc/ctags_target.cpp", prototypes[0].File) require.Equal(t, "void loop();", prototypes[1].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 16, prototypeLine) + require.Equal(t, 16, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesDefaultArguments(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserDefaultArguments.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 3, len(prototypes)) require.Equal(t, "void test(int x = 1);", prototypes[0].Prototype) @@ -342,60 +339,58 @@ func TestCTagsToPrototypesDefaultArguments(t *testing.T) { require.Equal(t, "/tmp/test179252494/preproc/ctags_target.cpp", prototypes[1].File) require.Equal(t, "void loop();", prototypes[2].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 2, prototypeLine) + require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesNamespace(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserNamespace.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 2, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) require.Equal(t, "/tmp/test030883150/preproc/ctags_target.cpp", prototypes[0].File) require.Equal(t, "void loop();", prototypes[1].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 8, prototypeLine) + require.Equal(t, 8, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesStatic(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStatic.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 3, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) @@ -404,30 +399,29 @@ func TestCTagsToPrototypesStatic(t *testing.T) { require.Equal(t, "void doStuff();", prototypes[2].Prototype) require.Equal(t, "static", prototypes[2].Modifiers) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 2, prototypeLine) + require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesFunctionPointer(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointer.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) + prototypes := ctx.Prototypes require.Equal(t, 3, len(prototypes)) require.Equal(t, "void t1Callback();", prototypes[0].Prototype) @@ -435,36 +429,33 @@ func TestCTagsToPrototypesFunctionPointer(t *testing.T) { require.Equal(t, "void setup();", prototypes[1].Prototype) require.Equal(t, "void loop();", prototypes[2].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 2, prototypeLine) + require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) } func TestCTagsToPrototypesFunctionPointers(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointers.txt")) NoError(t, err) - context[constants.CTX_CTAGS_OUTPUT] = string(bytes) + ctx.CTagsOutput = string(bytes) commands := []types.Command{ &ctags.CTagsParser{}, - &CopyContextKeys{From: constants.CTX_CTAGS_OF_PREPROC_SOURCE, To: constants.CTX_COLLECTED_CTAGS}, + &CollectCtagsFromPreprocSource{}, &ctags.CTagsToPrototypes{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype) - + prototypes := ctx.Prototypes require.Equal(t, 2, len(prototypes)) require.Equal(t, "void setup();", prototypes[0].Prototype) require.Equal(t, "/tmp/test907446433/preproc/ctags_target.cpp", prototypes[0].File) require.Equal(t, "void loop();", prototypes[1].Prototype) - prototypeLine := context[constants.CTX_LINE_WHERE_TO_INSERT_PROTOTYPES].(int) - require.Equal(t, 2, prototypeLine) + require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) } diff --git a/src/arduino.cc/builder/test/external_include_replacer_test.go b/src/arduino.cc/builder/test/external_include_replacer_test.go index ad8a5250..e75b6597 100644 --- a/src/arduino.cc/builder/test/external_include_replacer_test.go +++ b/src/arduino.cc/builder/test/external_include_replacer_test.go @@ -31,7 +31,7 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "github.com/stretchr/testify/require" "testing" ) @@ -71,27 +71,27 @@ var sourceWithPragmas = "#line 1 \"sketch_with_config.ino\"\n" + "}\n" func TestExternalIncludeReplacerIncludeToPragma(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SOURCE] = sourceWithIncludes - context[constants.CTX_INCLUDES] = []string{"/tmp/test184599776/sketch/config.h", "/tmp/test184599776/sketch/includes/de bug.h", "Bridge.h"} + ctx := &types.Context{} + ctx.Source = sourceWithIncludes + ctx.Includes = []string{"/tmp/test184599776/sketch/config.h", "/tmp/test184599776/sketch/includes/de bug.h", "Bridge.h"} - replacer := builder.ExternalIncludeReplacer{SourceKey: constants.CTX_SOURCE, TargetKey: constants.CTX_GCC_MINUS_E_SOURCE, From: "#include ", To: "#pragma ___MY_INCLUDE___ "} - err := replacer.Run(context) + replacer := builder.ExternalIncludeReplacer{Source: &ctx.Source, Target: &ctx.SourceGccMinusE, From: "#include ", To: "#pragma ___MY_INCLUDE___ "} + err := replacer.Run(ctx) NoError(t, err) - require.Equal(t, sourceWithIncludes, context[constants.CTX_SOURCE].(string)) - require.Equal(t, sourceWithPragmas, context[constants.CTX_GCC_MINUS_E_SOURCE].(string)) + require.Equal(t, sourceWithIncludes, ctx.Source) + require.Equal(t, sourceWithPragmas, ctx.SourceGccMinusE) } func TestExternalIncludeReplacerPragmaToInclude(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_GCC_MINUS_E_SOURCE] = sourceWithPragmas - context[constants.CTX_INCLUDES] = []string{"/tmp/test184599776/sketch/config.h", "/tmp/test184599776/sketch/includes/de bug.h", "Bridge.h"} + ctx := &types.Context{} + ctx.SourceGccMinusE = sourceWithPragmas + ctx.Includes = []string{"/tmp/test184599776/sketch/config.h", "/tmp/test184599776/sketch/includes/de bug.h", "Bridge.h"} - replacer := builder.ExternalIncludeReplacer{SourceKey: constants.CTX_GCC_MINUS_E_SOURCE, TargetKey: constants.CTX_GCC_MINUS_E_SOURCE, From: "#pragma ___MY_INCLUDE___ ", To: "#include "} - err := replacer.Run(context) + replacer := builder.ExternalIncludeReplacer{Source: &ctx.SourceGccMinusE, Target: &ctx.SourceGccMinusE, From: "#pragma ___MY_INCLUDE___ ", To: "#include "} + err := replacer.Run(ctx) NoError(t, err) - require.Nil(t, context[constants.CTX_SOURCE]) - require.Equal(t, sourceWithIncludes, context[constants.CTX_GCC_MINUS_E_SOURCE].(string)) + require.Empty(t, ctx.Source) + require.Equal(t, sourceWithIncludes, ctx.SourceGccMinusE) } diff --git a/src/arduino.cc/builder/test/fail_if_buildpath_equals_sketchpath_test.go b/src/arduino.cc/builder/test/fail_if_buildpath_equals_sketchpath_test.go index 2f02bb8b..04da3ae7 100644 --- a/src/arduino.cc/builder/test/fail_if_buildpath_equals_sketchpath_test.go +++ b/src/arduino.cc/builder/test/fail_if_buildpath_equals_sketchpath_test.go @@ -31,27 +31,27 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "github.com/stretchr/testify/require" "testing" ) func TestFailIfBuildPathEqualsSketchPath(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_BUILD_PATH] = "buildPath" - context[constants.CTX_SKETCH_LOCATION] = "buildPath/sketch.ino" + ctx := &types.Context{ + SketchLocation: "buildPath/sketch.ino", + BuildPath: "buildPath", + } command := builder.FailIfBuildPathEqualsSketchPath{} - require.Error(t, command.Run(context)) + require.Error(t, command.Run(ctx)) } func TestFailIfBuildPathEqualsSketchPathSketchPathDiffers(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_BUILD_PATH] = "buildPath" - context[constants.CTX_SKETCH_LOCATION] = "sketchPath/sketch.ino" + ctx := &types.Context{ + SketchLocation: "sketchPath/sketch.ino", + BuildPath: "buildPath", + } command := builder.FailIfBuildPathEqualsSketchPath{} - NoError(t, command.Run(context)) + NoError(t, command.Run(ctx)) } diff --git a/src/arduino.cc/builder/test/gcc_minus_m_output_parser_test.go b/src/arduino.cc/builder/test/gcc_minus_m_output_parser_test.go index 17917709..df848fcc 100644 --- a/src/arduino.cc/builder/test/gcc_minus_m_output_parser_test.go +++ b/src/arduino.cc/builder/test/gcc_minus_m_output_parser_test.go @@ -31,13 +31,13 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "github.com/stretchr/testify/require" "testing" ) func TestGCCMinusMOutputParser(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} output := "sketch_with_config.o: sketch_with_config.ino config.h de\\ bug.h Bridge.h\n" + "\n" + @@ -47,14 +47,13 @@ func TestGCCMinusMOutputParser(t *testing.T) { "\n" + "Bridge.h:\n" - context[constants.CTX_GCC_MINUS_M_OUTPUT] = output + ctx.OutputGccMinusM = output parser := builder.GCCMinusMOutputParser{} - err := parser.Run(context) + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 3, len(includes)) require.Equal(t, "config.h", includes[0]) require.Equal(t, "de bug.h", includes[1]) @@ -62,23 +61,22 @@ func TestGCCMinusMOutputParser(t *testing.T) { } func TestGCCMinusMOutputParserEmptyOutput(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} output := "sketch.ino.o: /tmp/test699709208/sketch/sketch.ino.cpp" - context[constants.CTX_GCC_MINUS_M_OUTPUT] = output + ctx.OutputGccMinusM = output parser := builder.GCCMinusMOutputParser{} - err := parser.Run(context) + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 0, len(includes)) } func TestGCCMinusMOutputParserFirstLineOnMultipleLines(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} output := "sketch_with_config.ino.o: \\\n" + " /tmp/test097286304/sketch/sketch_with_config.ino.cpp \\\n" + @@ -91,14 +89,13 @@ func TestGCCMinusMOutputParserFirstLineOnMultipleLines(t *testing.T) { "\n" + "Bridge.h:\n" - context[constants.CTX_GCC_MINUS_M_OUTPUT] = output + ctx.OutputGccMinusM = output parser := builder.GCCMinusMOutputParser{} - err := parser.Run(context) + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 3, len(includes)) require.Equal(t, "/tmp/test097286304/sketch/config.h", includes[0]) require.Equal(t, "/tmp/test097286304/sketch/includes/de bug.h", includes[1]) diff --git a/src/arduino.cc/builder/test/generate_buildpath_if_missing_test.go b/src/arduino.cc/builder/test/generate_buildpath_if_missing_test.go index 48830a98..e1ec6edc 100644 --- a/src/arduino.cc/builder/test/generate_buildpath_if_missing_test.go +++ b/src/arduino.cc/builder/test/generate_buildpath_if_missing_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "os" @@ -40,50 +39,48 @@ import ( ) func TestGenerateBuildPathIfMissing(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_SKETCH_LOCATION] = "test" + ctx := &types.Context{ + SketchLocation: "test", + } command := builder.GenerateBuildPathIfMissing{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) - require.Equal(t, filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6"), context[constants.CTX_BUILD_PATH]) + require.Equal(t, filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6"), ctx.BuildPath) _, err = os.Stat(filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6")) require.True(t, os.IsNotExist(err)) } func TestGenerateBuildPathIfEmpty(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_SKETCH_LOCATION] = "test" - context[constants.CTX_BUILD_PATH] = constants.EMPTY_STRING + ctx := &types.Context{ + SketchLocation: "test", + } createBuildPathIfMissing := builder.GenerateBuildPathIfMissing{} - err := createBuildPathIfMissing.Run(context) + err := createBuildPathIfMissing.Run(ctx) NoError(t, err) - require.Equal(t, filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6"), context[constants.CTX_BUILD_PATH]) + require.Equal(t, filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6"), ctx.BuildPath) _, err = os.Stat(filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6")) require.True(t, os.IsNotExist(err)) } func TestDontGenerateBuildPathIfPresent(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_BUILD_PATH] = "test" + ctx := &types.Context{} + ctx.BuildPath = "test" createBuildPathIfMissing := builder.GenerateBuildPathIfMissing{} - err := createBuildPathIfMissing.Run(context) + err := createBuildPathIfMissing.Run(ctx) NoError(t, err) - require.Equal(t, "test", context[constants.CTX_BUILD_PATH]) + require.Equal(t, "test", ctx.BuildPath) } func TestGenerateBuildPathAndEnsureItExists(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_SKETCH_LOCATION] = "test" + ctx := &types.Context{ + SketchLocation: "test", + } commands := []types.Command{ &builder.GenerateBuildPathIfMissing{}, @@ -91,13 +88,13 @@ func TestGenerateBuildPathAndEnsureItExists(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } defer os.RemoveAll(filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6")) - require.Equal(t, filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6"), context[constants.CTX_BUILD_PATH]) + require.Equal(t, filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6"), ctx.BuildPath) _, err := os.Stat(filepath.Join(os.TempDir(), "arduino-sketch-098F6BCD4621D373CADE4E832627B4F6")) NoError(t, err) } diff --git a/src/arduino.cc/builder/test/hardware/platform.txt b/src/arduino.cc/builder/test/hardware/platform.txt index 25054c03..8b137891 100644 --- a/src/arduino.cc/builder/test/hardware/platform.txt +++ b/src/arduino.cc/builder/test/hardware/platform.txt @@ -1,6 +1 @@ -# coan -# ------------------------------ -tools.coan.path={runtime.tools.coan.path} -tools.coan.cmd.path={path}/coan -tools.coan.pattern="{cmd.path}" source -m -E -P -kb {compiler.c.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.c.extra_flags} {build.extra_flags} "{source_file}" diff --git a/src/arduino.cc/builder/test/hardware_loader_test.go b/src/arduino.cc/builder/test/hardware_loader_test.go index 443204d2..c9ea1d52 100644 --- a/src/arduino.cc/builder/test/hardware_loader_test.go +++ b/src/arduino.cc/builder/test/hardware_loader_test.go @@ -41,20 +41,20 @@ import ( ) func TestLoadHardware(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{"downloaded_hardware", filepath.Join("..", "hardware"), "hardware"} + ctx := &types.Context{ + HardwareFolders: []string{"downloaded_hardware", filepath.Join("..", "hardware"), "hardware"}, + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - packages := context[constants.CTX_HARDWARE].(*types.Packages) + packages := ctx.Hardware require.Equal(t, 2, len(packages.Packages)) require.NotNil(t, packages.Packages["arduino"]) require.Equal(t, 2, len(packages.Packages["arduino"].Platforms)) @@ -85,11 +85,11 @@ func TestLoadHardware(t *testing.T) { } func TestLoadHardwareMixingUserHardwareFolder(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{"downloaded_hardware", filepath.Join("..", "hardware"), "hardware", "user_hardware"} + ctx := &types.Context{ + HardwareFolders: []string{"downloaded_hardware", filepath.Join("..", "hardware"), "hardware", "user_hardware"}, + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.PlatformKeysRewriteLoader{}, @@ -97,11 +97,11 @@ func TestLoadHardwareMixingUserHardwareFolder(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - packages := context[constants.CTX_HARDWARE].(*types.Packages) + packages := ctx.Hardware if runtime.GOOS == "windows" { //a package is a symlink, and windows does not support them @@ -155,20 +155,20 @@ func TestLoadHardwareMixingUserHardwareFolder(t *testing.T) { } func TestLoadHardwareWithBoardManagerFolderStructure(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{"downloaded_board_manager_stuff"} + ctx := &types.Context{ + HardwareFolders: []string{"downloaded_board_manager_stuff"}, + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - packages := context[constants.CTX_HARDWARE].(*types.Packages) + packages := ctx.Hardware require.Equal(t, 3, len(packages.Packages)) require.NotNil(t, packages.Packages["arduino"]) require.Equal(t, 1, len(packages.Packages["arduino"].Platforms)) @@ -203,21 +203,20 @@ func TestLoadHardwareWithBoardManagerFolderStructure(t *testing.T) { } func TestLoadLotsOfHardware(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{"downloaded_board_manager_stuff", "downloaded_hardware", filepath.Join("..", "hardware"), "hardware", "user_hardware"} + ctx := &types.Context{ + HardwareFolders: []string{"downloaded_board_manager_stuff", "downloaded_hardware", filepath.Join("..", "hardware"), "hardware", "user_hardware"}, + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - packages := context[constants.CTX_HARDWARE].(*types.Packages) + packages := ctx.Hardware if runtime.GOOS == "windows" { //a package is a symlink, and windows does not support them diff --git a/src/arduino.cc/builder/test/helper.go b/src/arduino.cc/builder/test/helper.go index 0bdffa51..3e83d337 100644 --- a/src/arduino.cc/builder/test/helper.go +++ b/src/arduino.cc/builder/test/helper.go @@ -44,7 +44,7 @@ import ( "text/template" ) -func LoadAndInterpolate(t *testing.T, filename string, context map[string]interface{}) string { +func LoadAndInterpolate(t *testing.T, filename string, ctx *types.Context) string { funcsMap := template.FuncMap{ "EscapeBackSlashes": func(s string) string { return strings.Replace(s, "\\", "\\\\", -1) @@ -55,7 +55,9 @@ func LoadAndInterpolate(t *testing.T, filename string, context map[string]interf NoError(t, err) var buf bytes.Buffer - err = tpl.Execute(&buf, context) + data := make(map[string]interface{}) + data["sketch"] = ctx.Sketch + err = tpl.Execute(&buf, data) NoError(t, err) return buf.String() @@ -77,10 +79,10 @@ func NoError(t *testing.T, err error, msgAndArgs ...interface{}) { } } -func SetupBuildPath(t *testing.T, context map[string]interface{}) string { +func SetupBuildPath(t *testing.T, ctx *types.Context) string { buildPath, err := ioutil.TempDir(constants.EMPTY_STRING, "test") NoError(t, err) - context[constants.CTX_BUILD_PATH] = buildPath + ctx.BuildPath = buildPath return buildPath } @@ -95,13 +97,3 @@ func (s ByLibraryName) Swap(i, j int) { func (s ByLibraryName) Less(i, j int) bool { return s[i].Name < s[j].Name } - -type CopyContextKeys struct { - From string - To string -} - -func (s *CopyContextKeys) Run(context map[string]interface{}) error { - context[s.To] = context[s.From] - return nil -} diff --git a/src/arduino.cc/builder/test/helper_tools_downloader.go b/src/arduino.cc/builder/test/helper_tools_downloader.go index 0517951e..e2f1f2fe 100644 --- a/src/arduino.cc/builder/test/helper_tools_downloader.go +++ b/src/arduino.cc/builder/test/helper_tools_downloader.go @@ -105,13 +105,6 @@ func DownloadCoresAndToolsAndLibraries(t *testing.T) { Tool{Name: "avrdude", Version: "6.0.1-arduino5"}, Tool{Name: "avr-gcc", Version: "4.8.1-arduino5"}, Tool{Name: "arm-none-eabi-gcc", Version: "4.8.3-2014q1"}, - Tool{Name: "coan", Version: "5.2", OsUrls: []OsUrl{ - OsUrl{Os: "i686-pc-linux-gnu", Url: "http://downloads.arduino.cc/tools/coan-5.2-i686-pc-linux-gnu.tar.bz2"}, - OsUrl{Os: "x86_64-pc-linux-gnu", Url: "http://downloads.arduino.cc/tools/coan-5.2-x86_64-pc-linux-gnu.tar.bz2"}, - OsUrl{Os: "i686-mingw32", Url: "http://downloads.arduino.cc/tools/coan-5.2-i686-mingw32.zip"}, - OsUrl{Os: "x86_64-apple-darwin", Url: "http://downloads.arduino.cc/tools/coan-5.2-x86_64-apple-darwin.zip"}, - OsUrl{Os: "arm-linux-gnueabihf", Url: "http://downloads.arduino.cc/tools/coan-5.2-arm-linux-gnueabihf.tar.bz2"}, - }}, Tool{Name: "ctags", Version: "5.8-arduino10", OsUrls: []OsUrl{ OsUrl{Os: "i686-pc-linux-gnu", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino10-i686-pc-linux-gnu.tar.bz2"}, @@ -237,11 +230,11 @@ func downloadCores(cores []Core, index map[string]interface{}) error { for _, core := range cores { url, err := findCoreUrl(index, core) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = downloadAndUnpackCore(core, url, HARDWARE_FOLDER) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } return nil @@ -251,11 +244,11 @@ func downloadBoardManagerCores(cores []Core, index map[string]interface{}) error for _, core := range cores { url, err := findCoreUrl(index, core) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = downloadAndUnpackBoardManagerCore(core, url, BOARD_MANAGER_FOLDER) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } return nil @@ -288,11 +281,11 @@ func downloadTools(tools []Tool, index map[string]interface{}) error { for _, tool := range tools { url, err := findToolUrl(index, tool, host) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = downloadAndUnpackTool(tool, url, TOOLS_FOLDER, true) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } @@ -308,7 +301,7 @@ func downloadToolsMultipleVersions(tools []Tool, index map[string]interface{}) e if err == nil { err = os.RemoveAll(filepath.Join(TOOLS_FOLDER, tool.Name)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } @@ -317,11 +310,11 @@ func downloadToolsMultipleVersions(tools []Tool, index map[string]interface{}) e for _, tool := range tools { url, err := findToolUrl(index, tool, host) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = downloadAndUnpackTool(tool, url, TOOLS_FOLDER, false) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } @@ -334,11 +327,11 @@ func downloadBoardsManagerTools(tools []Tool, index map[string]interface{}) erro for _, tool := range tools { url, err := findToolUrl(index, tool, host) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = downloadAndUnpackBoardsManagerTool(tool, url, BOARD_MANAGER_FOLDER) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } @@ -363,7 +356,7 @@ func allCoresAlreadyDownloadedAndUnpacked(targetPath string, cores []Core) (bool for _, core := range cores { alreadyDownloaded, err := coreAlreadyDownloadedAndUnpacked(targetPath, core) if err != nil { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } if !alreadyDownloaded { return false, nil @@ -381,12 +374,12 @@ func coreAlreadyDownloadedAndUnpacked(targetPath string, core Core) (bool, error } platform, err := props.Load(filepath.Join(corePath, "platform.txt"), i18n.HumanLogger{}) if err != nil { - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } if core.Version != platform["version"] { err := os.RemoveAll(corePath) - return false, utils.WrapError(err) + return false, i18n.WrapError(err) } return true, nil @@ -445,7 +438,7 @@ func libraryAlreadyDownloadedAndUnpacked(targetPath string, library Library) boo func downloadAndUnpackCore(core Core, url string, targetPath string) error { alreadyDownloaded, err := coreAlreadyDownloadedAndUnpacked(targetPath, core) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if alreadyDownloaded { return nil @@ -453,12 +446,12 @@ func downloadAndUnpackCore(core Core, url string, targetPath string) error { targetPath, err = filepath.Abs(targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } unpackFolder, files, err := downloadAndUnpack(url) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer os.RemoveAll(unpackFolder) @@ -466,28 +459,28 @@ func downloadAndUnpackCore(core Core, url string, targetPath string) error { if err == nil { err = os.RemoveAll(filepath.Join(targetPath, core.Maintainer, core.Arch)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } if len(files) == 1 && files[0].IsDir() { err = utils.EnsureFolderExists(filepath.Join(targetPath, core.Maintainer)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, core.Maintainer, core.Arch)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else { err = utils.EnsureFolderExists(filepath.Join(targetPath, core.Maintainer, core.Arch)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, core.Maintainer, core.Arch, file.Name())) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } @@ -502,12 +495,12 @@ func downloadAndUnpackBoardManagerCore(core Core, url string, targetPath string) targetPath, err := filepath.Abs(targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } unpackFolder, files, err := downloadAndUnpack(url) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer os.RemoveAll(unpackFolder) @@ -515,28 +508,28 @@ func downloadAndUnpackBoardManagerCore(core Core, url string, targetPath string) if err == nil { err = os.RemoveAll(filepath.Join(targetPath, core.Maintainer, "hardware", core.Arch)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } if len(files) == 1 && files[0].IsDir() { err = utils.EnsureFolderExists(filepath.Join(targetPath, core.Maintainer, "hardware", core.Arch)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, core.Maintainer, "hardware", core.Arch, core.Version)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else { err = utils.EnsureFolderExists(filepath.Join(targetPath, core.Maintainer, "hardware", core.Arch, core.Version)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, core.Maintainer, "hardware", core.Arch, core.Version, file.Name())) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } @@ -551,33 +544,33 @@ func downloadAndUnpackBoardsManagerTool(tool Tool, url string, targetPath string targetPath, err := filepath.Abs(targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } unpackFolder, files, err := downloadAndUnpack(url) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer os.RemoveAll(unpackFolder) if len(files) == 1 && files[0].IsDir() { err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else { err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version, file.Name())) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } @@ -592,12 +585,12 @@ func downloadAndUnpackTool(tool Tool, url string, targetPath string, deleteIfMis targetPath, err := filepath.Abs(targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } unpackFolder, files, err := downloadAndUnpack(url) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer os.RemoveAll(unpackFolder) @@ -606,7 +599,7 @@ func downloadAndUnpackTool(tool Tool, url string, targetPath string, deleteIfMis if err == nil { err = os.RemoveAll(filepath.Join(targetPath, tool.Name)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } @@ -614,21 +607,21 @@ func downloadAndUnpackTool(tool Tool, url string, targetPath string, deleteIfMis if len(files) == 1 && files[0].IsDir() { err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Name)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, tool.Name, tool.Version)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else { err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Name, tool.Version)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, tool.Name, tool.Version, file.Name())) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } @@ -641,7 +634,7 @@ func downloadAndUnpack(url string) (string, []os.FileInfo, error) { unpackFolder, err := ioutil.TempDir(constants.EMPTY_STRING, "arduino-builder-tool") if err != nil { - return constants.EMPTY_STRING, nil, utils.WrapError(err) + return constants.EMPTY_STRING, nil, i18n.WrapError(err) } urlParts := strings.Split(url, "/") @@ -650,12 +643,12 @@ func downloadAndUnpack(url string) (string, []os.FileInfo, error) { res, err := http.Get(url) if err != nil { - return constants.EMPTY_STRING, nil, utils.WrapError(err) + return constants.EMPTY_STRING, nil, i18n.WrapError(err) } bytes, err := ioutil.ReadAll(res.Body) if err != nil { - return constants.EMPTY_STRING, nil, utils.WrapError(err) + return constants.EMPTY_STRING, nil, i18n.WrapError(err) } res.Body.Close() @@ -664,7 +657,7 @@ func downloadAndUnpack(url string) (string, []os.FileInfo, error) { cmd := buildUnpackCmd(archiveFilePath) out, err := cmd.CombinedOutput() if err != nil { - return constants.EMPTY_STRING, nil, utils.WrapError(err) + return constants.EMPTY_STRING, nil, i18n.WrapError(err) } if len(out) > 0 { fmt.Println(string(out)) @@ -674,7 +667,7 @@ func downloadAndUnpack(url string) (string, []os.FileInfo, error) { files, err := gohasissues.ReadDir(unpackFolder) if err != nil { - return constants.EMPTY_STRING, nil, utils.WrapError(err) + return constants.EMPTY_STRING, nil, i18n.WrapError(err) } return unpackFolder, files, nil @@ -746,11 +739,11 @@ func downloadLibraries(libraries []Library, index map[string]interface{}) error for _, library := range libraries { url, err := findLibraryUrl(index, library) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = downloadAndUnpackLibrary(library, url, LIBRARIES_FOLDER) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } @@ -779,12 +772,12 @@ func downloadAndUnpackLibrary(library Library, url string, targetPath string) er targetPath, err := filepath.Abs(targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } unpackFolder, files, err := downloadAndUnpack(url) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer os.RemoveAll(unpackFolder) @@ -792,13 +785,13 @@ func downloadAndUnpackLibrary(library Library, url string, targetPath string) er if err == nil { err = os.RemoveAll(filepath.Join(targetPath, strings.Replace(library.Name, " ", "_", -1))) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, strings.Replace(library.Name, " ", "_", -1))) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } return nil @@ -812,42 +805,42 @@ func copyRecursive(from, to string) error { rel, err := filepath.Rel(from, currentPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } targetPath := filepath.Join(to, rel) if info.IsDir() { err := os.MkdirAll(targetPath, info.Mode()) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else if info.Mode().IsRegular() { fromFile, err := os.Open(currentPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer fromFile.Close() targetFile, err := os.Create(targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } defer targetFile.Close() _, err = io.Copy(targetFile, fromFile) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } err = os.Chmod(targetPath, info.Mode()) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else if info.Mode()&os.ModeSymlink == os.ModeSymlink { linkedFile, err := os.Readlink(currentPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } fromFile := filepath.Join(filepath.Dir(targetPath), linkedFile) err = os.Symlink(fromFile, targetPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else { return errors.Errorf("unable to copy file " + currentPath) @@ -856,5 +849,5 @@ func copyRecursive(from, to string) error { return nil } err := gohasissues.Walk(from, copyFunc) - return utils.WrapError(err) + return i18n.WrapError(err) } diff --git a/src/arduino.cc/builder/test/includes_finder_with_gcc_test.go b/src/arduino.cc/builder/test/includes_finder_with_gcc_test.go index 1fc613bd..903161f1 100644 --- a/src/arduino.cc/builder/test/includes_finder_with_gcc_test.go +++ b/src/arduino.cc/builder/test/includes_finder_with_gcc_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "arduino.cc/builder/utils" "github.com/stretchr/testify/require" @@ -44,21 +43,19 @@ import ( func TestIncludesFinderWithGCC(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + SketchLocation: filepath.Join("sketch2", "SketchWithIfDef.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch2", "SketchWithIfDef.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, - &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -67,32 +64,30 @@ func TestIncludesFinderWithGCC(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - - require.Nil(t, context[constants.CTX_INCLUDES]) + require.Nil(t, ctx.Includes) } func TestIncludesFinderWithGCCSketchWithConfig(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"dependent_libraries", "libraries"}, + SketchLocation: filepath.Join("sketch_with_config", "sketch_with_config.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"dependent_libraries", "libraries"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_config", "sketch_with_config.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -102,16 +97,15 @@ func TestIncludesFinderWithGCCSketchWithConfig(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 1, len(includes)) require.True(t, utils.SliceContains(includes, "Bridge.h")) - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "Bridge", importedLibraries[0].Name) } @@ -119,21 +113,20 @@ func TestIncludesFinderWithGCCSketchWithConfig(t *testing.T) { func TestIncludesFinderWithGCCSketchWithDependendLibraries(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + OtherLibrariesFolders: []string{"dependent_libraries"}, + SketchLocation: filepath.Join("sketch_with_dependend_libraries", "sketch.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"dependent_libraries"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_dependend_libraries", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -143,12 +136,11 @@ func TestIncludesFinderWithGCCSketchWithDependendLibraries(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 4, len(includes)) sort.Strings(includes) @@ -157,7 +149,7 @@ func TestIncludesFinderWithGCCSketchWithDependendLibraries(t *testing.T) { require.Equal(t, "library3.h", includes[2]) require.Equal(t, "library4.h", includes[3]) - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries require.Equal(t, 4, len(importedLibraries)) sort.Sort(ByLibraryName(importedLibraries)) @@ -170,22 +162,21 @@ func TestIncludesFinderWithGCCSketchWithDependendLibraries(t *testing.T) { func TestIncludesFinderWithGCCSketchWithThatChecksIfSPIHasTransactions(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"dependent_libraries", "libraries"}, + SketchLocation: filepath.Join("sketch_that_checks_if_SPI_has_transactions", "sketch.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"dependent_libraries", "libraries"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_that_checks_if_SPI_has_transactions", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -195,16 +186,15 @@ func TestIncludesFinderWithGCCSketchWithThatChecksIfSPIHasTransactions(t *testin } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 1, len(includes)) require.Equal(t, "SPI.h", includes[0]) - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "SPI", importedLibraries[0].Name) } @@ -212,22 +202,21 @@ func TestIncludesFinderWithGCCSketchWithThatChecksIfSPIHasTransactions(t *testin func TestIncludesFinderWithGCCSketchWithThatChecksIfSPIHasTransactionsAndIncludesMissingLib(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"dependent_libraries", "libraries"}, + SketchLocation: filepath.Join("sketch_that_checks_if_SPI_has_transactions_and_includes_missing_Ethernet", "sketch.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"dependent_libraries", "libraries"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_that_checks_if_SPI_has_transactions_and_includes_missing_Ethernet", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -235,22 +224,21 @@ func TestIncludesFinderWithGCCSketchWithThatChecksIfSPIHasTransactionsAndInclude } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } command := &builder.ContainerFindIncludes{} - err := command.Run(context) + err := command.Run(ctx) require.Error(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 2, len(includes)) sort.Strings(includes) require.Equal(t, "Inexistent.h", includes[0]) require.Equal(t, "SPI.h", includes[1]) - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "SPI", importedLibraries[0].Name) } diff --git a/src/arduino.cc/builder/test/includes_finder_with_regexp_test.go b/src/arduino.cc/builder/test/includes_finder_with_regexp_test.go index b87de9db..1a8fb456 100644 --- a/src/arduino.cc/builder/test/includes_finder_with_regexp_test.go +++ b/src/arduino.cc/builder/test/includes_finder_with_regexp_test.go @@ -31,108 +31,62 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" - "os" - "path/filepath" "sort" "testing" ) -func TestIncludesFinderWithRegExpCoanOutput(t *testing.T) { - DownloadCoresAndToolsAndLibraries(t) - - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch2", "SketchWithIfDef.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_VERBOSE] = true - - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, - - &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - - &builder.ContainerMergeCopySketchFiles{}, - - &builder.CoanRunner{}, - - &builder.IncludesFinderWithRegExp{ContextField: constants.CTX_SOURCE}, - } - - for _, command := range commands { - err := command.Run(context) - NoError(t, err) - } - - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) - require.Equal(t, 3, len(includes)) - require.Equal(t, "Arduino.h", includes[0]) - require.Equal(t, "empty_1.h", includes[1]) - require.Equal(t, "empty_2.h", includes[2]) -} - func TestIncludesFinderWithRegExp(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} output := "/some/path/sketch.ino:1:17: fatal error: SPI.h: No such file or directory\n" + "#include \n" + "^\n" + "compilation terminated." - context["source"] = output + ctx.Source = output - parser := builder.IncludesFinderWithRegExp{ContextField: "source"} - err := parser.Run(context) + parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source} + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 1, len(includes)) require.Equal(t, "SPI.h", includes[0]) } func TestIncludesFinderWithRegExpEmptyOutput(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} output := "" - context["source"] = output + ctx.Source = output - parser := builder.IncludesFinderWithRegExp{ContextField: "source"} - err := parser.Run(context) + parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source} + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 0, len(includes)) } func TestIncludesFinderWithRegExpPreviousIncludes(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_INCLUDES] = []string{"test.h"} + ctx := &types.Context{ + Includes: []string{"test.h"}, + } output := "/some/path/sketch.ino:1:17: fatal error: SPI.h: No such file or directory\n" + "#include \n" + "^\n" + "compilation terminated." - context["source"] = output + ctx.Source = output - parser := builder.IncludesFinderWithRegExp{ContextField: "source"} - err := parser.Run(context) + parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source} + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 2, len(includes)) sort.Strings(includes) require.Equal(t, "SPI.h", includes[0]) @@ -140,65 +94,56 @@ func TestIncludesFinderWithRegExpPreviousIncludes(t *testing.T) { } func TestIncludesFinderWithRegExpPaddedIncludes(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_INCLUDES] = []string{} + ctx := &types.Context{} output := "/some/path/sketch.ino:1:33: fatal error: Wire.h: No such file or directory\n" + " # include \n" + " ^\n" + "compilation terminated.\n" - context["source"] = output + ctx.Source = output - parser := builder.IncludesFinderWithRegExp{ContextField: "source"} - err := parser.Run(context) + parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source} + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 1, len(includes)) sort.Strings(includes) require.Equal(t, "Wire.h", includes[0]) } func TestIncludesFinderWithRegExpPaddedIncludes2(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_INCLUDES] = []string{} + ctx := &types.Context{} output := "/some/path/sketch.ino:1:33: fatal error: Wire.h: No such file or directory\n" + " #\t\t\tinclude \n" + " ^\n" + "compilation terminated.\n" - context["source"] = output + ctx.Source = output - parser := builder.IncludesFinderWithRegExp{ContextField: "source"} - err := parser.Run(context) + parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source} + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 1, len(includes)) sort.Strings(includes) require.Equal(t, "Wire.h", includes[0]) } func TestIncludesFinderWithRegExpPaddedIncludes3(t *testing.T) { - context := make(map[string]interface{}) - - context[constants.CTX_INCLUDES] = []string{} + ctx := &types.Context{} output := "/some/path/sketch.ino:1:33: fatal error: SPI.h: No such file or directory\n" + "compilation terminated.\n" - context["source"] = output + ctx.Source = output - parser := builder.IncludesFinderWithRegExp{ContextField: "source"} - err := parser.Run(context) + parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source} + err := parser.Run(ctx) NoError(t, err) - require.NotNil(t, context[constants.CTX_INCLUDES]) - includes := context[constants.CTX_INCLUDES].([]string) + includes := ctx.Includes require.Equal(t, 1, len(includes)) sort.Strings(includes) require.Equal(t, "SPI.h", includes[0]) diff --git a/src/arduino.cc/builder/test/includes_to_include_folders_test.go b/src/arduino.cc/builder/test/includes_to_include_folders_test.go index 8d29018b..a0fb431f 100644 --- a/src/arduino.cc/builder/test/includes_to_include_folders_test.go +++ b/src/arduino.cc/builder/test/includes_to_include_folders_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "os" @@ -43,22 +42,21 @@ import ( func TestIncludesToIncludeFolders(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -68,15 +66,15 @@ func TestIncludesToIncludeFolders(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "Bridge", importedLibraries[0].Name) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) require.False(t, libraryResolutionResults["Bridge.h"].IsLibraryFromPlatform) } @@ -84,22 +82,21 @@ func TestIncludesToIncludeFolders(t *testing.T) { func TestIncludesToIncludeFoldersSketchWithIfDef(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch2", "SketchWithIfDef.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch2", "SketchWithIfDef.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -109,36 +106,35 @@ func TestIncludesToIncludeFoldersSketchWithIfDef(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries require.Equal(t, 0, len(importedLibraries)) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) } func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch9", "sketch.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch9", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -148,17 +144,17 @@ func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 2, len(importedLibraries)) require.Equal(t, "Bridge", importedLibraries[0].Name) require.Equal(t, "IRremote", importedLibraries[1].Name) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) require.False(t, libraryResolutionResults["Bridge.h"].IsLibraryFromPlatform) require.False(t, libraryResolutionResults["IRremote.h"].IsLibraryFromPlatform) @@ -167,22 +163,21 @@ func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch10", "sketch.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch10", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -192,17 +187,17 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 2, len(importedLibraries)) require.Equal(t, "ANewLibrary-master", importedLibraries[0].Name) require.Equal(t, "IRremote", importedLibraries[1].Name) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) require.False(t, libraryResolutionResults["anewlibrary.h"].IsLibraryFromPlatform) require.False(t, libraryResolutionResults["IRremote.h"].IsLibraryFromPlatform) @@ -211,21 +206,20 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + SketchLocation: filepath.Join("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino"), + FQBN: "my_avr_platform:avr:custom_yun", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:custom_yun" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -235,17 +229,17 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "SPI", importedLibraries[0].Name) require.Equal(t, Abs(t, filepath.Join("user_hardware", "my_avr_platform", "avr", "libraries", "SPI")), importedLibraries[0].SrcFolder) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) require.True(t, libraryResolutionResults["SPI.h"].IsLibraryFromPlatform) } @@ -253,22 +247,21 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatform(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino"), + FQBN: "my_avr_platform:avr:custom_yun", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:custom_yun" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -278,17 +271,17 @@ func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatfo } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "SPI", importedLibraries[0].Name) require.Equal(t, Abs(t, filepath.Join("libraries", "SPI")), importedLibraries[0].SrcFolder) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) require.False(t, libraryResolutionResults["SPI.h"].IsLibraryFromPlatform) } @@ -296,22 +289,21 @@ func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatfo func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}, + ToolsFolders: []string{"downloaded_tools", "downloaded_board_manager_stuff"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_usbhost", "sketch_usbhost.ino"), + FQBN: "arduino:samd:arduino_zero_native", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"} - context[constants.CTX_FQBN] = "arduino:samd:arduino_zero_native" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_usbhost", "sketch_usbhost.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -321,17 +313,17 @@ func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) + importedLibraries := ctx.ImportedLibraries sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "USBHost", importedLibraries[0].Name) require.Equal(t, Abs(t, filepath.Join("libraries", "USBHost", "src")), importedLibraries[0].SrcFolder) - libraryResolutionResults := context[constants.CTX_LIBRARY_RESOLUTION_RESULTS].(map[string]types.LibraryResolutionResult) + libraryResolutionResults := ctx.LibrariesResolutionResults require.NotNil(t, libraryResolutionResults) require.False(t, libraryResolutionResults["Usb.h"].IsLibraryFromPlatform) } diff --git a/src/arduino.cc/builder/test/libraries_loader_test.go b/src/arduino.cc/builder/test/libraries_loader_test.go index c0ab45bf..217603aa 100644 --- a/src/arduino.cc/builder/test/libraries_loader_test.go +++ b/src/arduino.cc/builder/test/libraries_loader_test.go @@ -42,15 +42,14 @@ import ( func TestLoadLibrariesAVR(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + FQBN: "arduino:avr:leonardo", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, @@ -58,17 +57,17 @@ func TestLoadLibrariesAVR(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - librariesFolders := context[constants.CTX_LIBRARIES_FOLDERS].([]string) + librariesFolders := ctx.LibrariesFolders require.Equal(t, 3, len(librariesFolders)) require.Equal(t, Abs(t, filepath.Join("downloaded_libraries")), librariesFolders[0]) require.Equal(t, Abs(t, filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), librariesFolders[1]) require.Equal(t, Abs(t, filepath.Join("libraries")), librariesFolders[2]) - libraries := context[constants.CTX_LIBRARIES].([]*types.Library) + libraries := ctx.Libraries require.Equal(t, 20, len(libraries)) sort.Sort(ByLibraryName(libraries)) @@ -131,7 +130,7 @@ func TestLoadLibrariesAVR(t *testing.T) { idx++ require.Equal(t, "Wire", libraries[idx].Name) - headerToLibraries := context[constants.CTX_HEADER_TO_LIBRARIES].(map[string][]*types.Library) + headerToLibraries := ctx.HeaderToLibraries require.Equal(t, 2, len(headerToLibraries["Audio.h"])) require.Equal(t, "Audio", headerToLibraries["Audio.h"][0].Name) require.Equal(t, "FakeAudio", headerToLibraries["Audio.h"][1].Name) @@ -152,15 +151,14 @@ func TestLoadLibrariesAVR(t *testing.T) { func TestLoadLibrariesSAM(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:sam:arduino_due_x_dbg" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + FQBN: "arduino:sam:arduino_due_x_dbg", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, @@ -168,17 +166,17 @@ func TestLoadLibrariesSAM(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - librariesFolders := context[constants.CTX_LIBRARIES_FOLDERS].([]string) + librariesFolders := ctx.LibrariesFolders require.Equal(t, 3, len(librariesFolders)) require.Equal(t, Abs(t, filepath.Join("downloaded_libraries")), librariesFolders[0]) require.Equal(t, Abs(t, filepath.Join("downloaded_hardware", "arduino", "sam", "libraries")), librariesFolders[1]) require.Equal(t, Abs(t, filepath.Join("libraries")), librariesFolders[2]) - libraries := context[constants.CTX_LIBRARIES].([]*types.Library) + libraries := ctx.Libraries require.Equal(t, 18, len(libraries)) sort.Sort(ByLibraryName(libraries)) @@ -218,7 +216,7 @@ func TestLoadLibrariesSAM(t *testing.T) { idx++ require.Equal(t, "Wire", libraries[idx].Name) - headerToLibraries := context[constants.CTX_HEADER_TO_LIBRARIES].(map[string][]*types.Library) + headerToLibraries := ctx.HeaderToLibraries require.Equal(t, 2, len(headerToLibraries["Audio.h"])) libraries = headerToLibraries["Audio.h"] @@ -236,15 +234,14 @@ func TestLoadLibrariesSAM(t *testing.T) { func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")} + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")}, + FQBN: "arduino:avr:leonardo", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, @@ -252,11 +249,11 @@ func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - librariesFolders := context[constants.CTX_LIBRARIES_FOLDERS].([]string) + librariesFolders := ctx.LibrariesFolders require.Equal(t, 3, len(librariesFolders)) require.Equal(t, Abs(t, filepath.Join("downloaded_libraries")), librariesFolders[0]) require.Equal(t, Abs(t, filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), librariesFolders[1]) @@ -266,15 +263,14 @@ func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { func TestLoadLibrariesMyAVRPlatform(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "user_hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:custom_yun" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")} + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "user_hardware", "downloaded_hardware"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")}, + FQBN: "my_avr_platform:avr:custom_yun", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, @@ -282,11 +278,11 @@ func TestLoadLibrariesMyAVRPlatform(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - librariesFolders := context[constants.CTX_LIBRARIES_FOLDERS].([]string) + librariesFolders := ctx.LibrariesFolders require.Equal(t, 4, len(librariesFolders)) require.Equal(t, Abs(t, filepath.Join("downloaded_libraries")), librariesFolders[0]) require.Equal(t, Abs(t, filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), librariesFolders[1]) diff --git a/src/arduino.cc/builder/test/load_previous_build_options_map_test.go b/src/arduino.cc/builder/test/load_previous_build_options_map_test.go index 295e6e3c..772d1c04 100644 --- a/src/arduino.cc/builder/test/load_previous_build_options_map_test.go +++ b/src/arduino.cc/builder/test/load_previous_build_options_map_test.go @@ -32,6 +32,7 @@ package test import ( "arduino.cc/builder" "arduino.cc/builder/constants" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "github.com/stretchr/testify/require" "os" @@ -40,30 +41,30 @@ import ( ) func TestLoadPreviousBuildOptionsMap(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) err := utils.WriteFile(filepath.Join(buildPath, constants.BUILD_OPTIONS_FILE), "test") NoError(t, err) command := builder.LoadPreviousBuildOptionsMap{} - err = command.Run(context) + err = command.Run(ctx) NoError(t, err) - require.Equal(t, "test", context[constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON]) + require.Equal(t, "test", ctx.BuildOptionsJsonPrevious) } func TestLoadPreviousBuildOptionsMapMissingFile(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) command := builder.LoadPreviousBuildOptionsMap{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) - require.False(t, utils.MapHas(context, constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON)) + require.Empty(t, ctx.BuildOptionsJsonPrevious) } diff --git a/src/arduino.cc/builder/test/load_vid_pid_specific_properties_test.go b/src/arduino.cc/builder/test/load_vid_pid_specific_properties_test.go index f4ef6ff5..b0857b23 100644 --- a/src/arduino.cc/builder/test/load_vid_pid_specific_properties_test.go +++ b/src/arduino.cc/builder/test/load_vid_pid_specific_properties_test.go @@ -31,8 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" - "arduino.cc/builder/props" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "os" @@ -43,28 +41,27 @@ import ( func TestLoadVIDPIDSpecificPropertiesWhenNoVIDPIDAreProvided(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools", "./tools_builtin"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:micro", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "./tools_builtin"} - context[constants.CTX_FQBN] = "arduino:avr:micro" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties require.Equal(t, "0x0037", buildProperties["pid.0"]) require.Equal(t, "\"Genuino Micro\"", buildProperties["vid.4.build.usb_product"]) @@ -74,29 +71,29 @@ func TestLoadVIDPIDSpecificPropertiesWhenNoVIDPIDAreProvided(t *testing.T) { func TestLoadVIDPIDSpecificProperties(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools", "./tools_builtin"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:micro", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "./tools_builtin"} - context[constants.CTX_FQBN] = "arduino:avr:micro" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_VIDPID] = "0x2341_0x0237" + ctx.USBVidPid = "0x2341_0x0237" commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties require.Equal(t, "0x0037", buildProperties["pid.0"]) require.Equal(t, "\"Genuino Micro\"", buildProperties["vid.4.build.usb_product"]) diff --git a/src/arduino.cc/builder/test/merge_sketch_with_bootloader_test.go b/src/arduino.cc/builder/test/merge_sketch_with_bootloader_test.go index 727ab5c9..ae0c91bc 100644 --- a/src/arduino.cc/builder/test/merge_sketch_with_bootloader_test.go +++ b/src/arduino.cc/builder/test/merge_sketch_with_bootloader_test.go @@ -32,7 +32,6 @@ package test import ( "arduino.cc/builder" "arduino.cc/builder/constants" - "arduino.cc/builder/props" "arduino.cc/builder/types" "arduino.cc/builder/utils" "github.com/stretchr/testify/require" @@ -46,19 +45,19 @@ import ( func TestMergeSketchWithBootloader(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - err := utils.EnsureFolderExists(filepath.Join(buildPath, "sketch")) NoError(t, err) @@ -68,13 +67,12 @@ func TestMergeSketchWithBootloader(t *testing.T) { NoError(t, err) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.MergeSketchWithBootloader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -89,19 +87,19 @@ func TestMergeSketchWithBootloader(t *testing.T) { func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - err := utils.EnsureFolderExists(filepath.Join(buildPath, "sketch")) NoError(t, err) @@ -111,13 +109,12 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { NoError(t, err) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.MergeSketchWithBootloader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -132,35 +129,34 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties delete(buildProperties, constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK) delete(buildProperties, constants.BUILD_PROPERTIES_BOOTLOADER_FILE) command := &builder.MergeSketchWithBootloader{} - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(buildPath, "sketch.ino.with_bootloader.hex")) @@ -171,19 +167,19 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "my_avr_platform:avr:mymega:cpu=atmega2560", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:mymega:cpu=atmega2560" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - err := utils.EnsureFolderExists(filepath.Join(buildPath, "sketch")) NoError(t, err) @@ -193,13 +189,12 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { NoError(t, err) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.MergeSketchWithBootloader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } diff --git a/src/arduino.cc/builder/test/platform_keys_rewrite_loader_test.go b/src/arduino.cc/builder/test/platform_keys_rewrite_loader_test.go index 694d33af..47ff86a4 100644 --- a/src/arduino.cc/builder/test/platform_keys_rewrite_loader_test.go +++ b/src/arduino.cc/builder/test/platform_keys_rewrite_loader_test.go @@ -39,20 +39,20 @@ import ( ) func TestLoadPlatformKeysRewrite(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{"downloaded_hardware", filepath.Join("..", "hardware"), "hardware"} + ctx := &types.Context{ + HardwareFolders: []string{"downloaded_hardware", filepath.Join("..", "hardware"), "hardware"}, + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.PlatformKeysRewriteLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - platformKeysRewrite := context[constants.CTX_PLATFORM_KEYS_REWRITE].(types.PlatforKeysRewrite) + platformKeysRewrite := ctx.PlatformKeyRewrites require.Equal(t, 13, len(platformKeysRewrite.Rewrites)) require.Equal(t, constants.BUILD_PROPERTIES_COMPILER_PATH, platformKeysRewrite.Rewrites[0].Key) diff --git a/src/arduino.cc/builder/test/prototypes_adder_test.go b/src/arduino.cc/builder/test/prototypes_adder_test.go index 6976b289..a6f46a45 100644 --- a/src/arduino.cc/builder/test/prototypes_adder_test.go +++ b/src/arduino.cc/builder/test/prototypes_adder_test.go @@ -32,7 +32,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "os" @@ -44,26 +43,26 @@ import ( func TestPrototypesAdderBridgeExample(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - context[constants.CTX_DEBUG_LEVEL] = 10 + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) + + ctx.DebugLevel = 10 commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -78,33 +77,32 @@ func TestPrototypesAdderBridgeExample(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 33 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 46 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 62 \""+absoluteSketchLocation+"\"\nvoid process(BridgeClient client);\n#line 82 \""+absoluteSketchLocation+"\"\nvoid digitalCommand(BridgeClient client);\n#line 109 \""+absoluteSketchLocation+"\"\nvoid analogCommand(BridgeClient client);\n#line 149 \""+absoluteSketchLocation+"\"\nvoid modeCommand(BridgeClient client);\n#line 33 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 33 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 46 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 62 \""+absoluteSketchLocation+"\"\nvoid process(BridgeClient client);\n#line 82 \""+absoluteSketchLocation+"\"\nvoid digitalCommand(BridgeClient client);\n#line 109 \""+absoluteSketchLocation+"\"\nvoid analogCommand(BridgeClient client);\n#line 149 \""+absoluteSketchLocation+"\"\nvoid modeCommand(BridgeClient client);\n#line 33 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) } func TestPrototypesAdderSketchWithIfDef(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch2", "SketchWithIfDef.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch2", "SketchWithIfDef.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -119,33 +117,32 @@ func TestPrototypesAdderSketchWithIfDef(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch2", "SketchWithIfDef.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch2", "SketchWithIfDef.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderBaladuino(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch3", "Baladuino.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch3", "Baladuino.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -160,33 +157,32 @@ func TestPrototypesAdderBaladuino(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch3", "Baladuino.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch3", "Baladuino.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch4", "CharWithEscapedDoubleQuote.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch4", "CharWithEscapedDoubleQuote.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -201,33 +197,32 @@ func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch4", "CharWithEscapedDoubleQuote.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch4", "CharWithEscapedDoubleQuote.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch5", "IncludeBetweenMultilineComment.ino"), + FQBN: "arduino:sam:arduino_due_x_dbg", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:sam:arduino_due_x_dbg" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch5", "IncludeBetweenMultilineComment.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -242,33 +237,32 @@ func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch5", "IncludeBetweenMultilineComment.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch5", "IncludeBetweenMultilineComment.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderLineContinuations(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch6", "/LineContinuations.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch6", "/LineContinuations.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -283,33 +277,32 @@ func TestPrototypesAdderLineContinuations(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch6", "LineContinuations.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch6", "LineContinuations.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderStringWithComment(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch7", "StringWithComment.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch7", "StringWithComment.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -324,33 +317,32 @@ func TestPrototypesAdderStringWithComment(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch7", "StringWithComment.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch7", "StringWithComment.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchWithStruct(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch8", "SketchWithStruct.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch8", "SketchWithStruct.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -365,12 +357,12 @@ func TestPrototypesAdderSketchWithStruct(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch8", "SketchWithStruct.preprocessed.txt"), context) - obtained := strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch8", "SketchWithStruct.preprocessed.txt"), ctx) + obtained := strings.Replace(ctx.Source, "\r\n", "\n", -1) // ctags based preprocessing removes the space after "dostuff", but this is still OK // TODO: remove this exception when moving to a more powerful parser preprocessed = strings.Replace(preprocessed, "void dostuff (A_NEW_TYPE * bar);", "void dostuff(A_NEW_TYPE * bar);", 1) @@ -381,25 +373,24 @@ func TestPrototypesAdderSketchWithStruct(t *testing.T) { func TestPrototypesAdderSketchWithConfig(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_config", "sketch_with_config.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -414,39 +405,38 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 13 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 17 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 13 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 13 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 17 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 13 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) - preprocessed := LoadAndInterpolate(t, filepath.Join("sketch_with_config", "sketch_with_config.preprocessed.txt"), context) - require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + preprocessed := LoadAndInterpolate(t, filepath.Join("sketch_with_config", "sketch_with_config.preprocessed.txt"), ctx) + require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_no_functions_two_files", "main.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions_two_files", "main.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_no_functions_two_files", "main.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -461,36 +451,35 @@ func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Nil(t, context[constants.CTX_PROTOTYPE_SECTION]) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "", ctx.PrototypesSection) } func TestPrototypesAdderSketchNoFunctions(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("sketch_no_functions", "main.ino"), + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) sketchLocation := filepath.Join("sketch_no_functions", "main.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions", "main.ino") - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -505,36 +494,35 @@ func TestPrototypesAdderSketchNoFunctions(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Nil(t, context[constants.CTX_PROTOTYPE_SECTION]) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "", ctx.PrototypesSection) } func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_default_args", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -549,36 +537,35 @@ func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 4 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 7 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 4 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 7 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) } func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_inline_function", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -593,14 +580,14 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) expected := "#line 1 \"" + absoluteSketchLocation + "\"\nvoid setup();\n#line 2 \"" + absoluteSketchLocation + "\"\nvoid loop();\n#line 4 \"" + absoluteSketchLocation + "\"\nshort unsigned int testInt();\n#line 8 \"" + absoluteSketchLocation + "\"\nstatic int8_t testInline();\n#line 12 \"" + absoluteSketchLocation + "\"\n__attribute__((always_inline)) uint8_t testAttribute();\n#line 1 \"" + absoluteSketchLocation + "\"\n" - obtained := context[constants.CTX_PROTOTYPE_SECTION].(string) + obtained := ctx.PrototypesSection // ctags based preprocessing removes "inline" but this is still OK // TODO: remove this exception when moving to a more powerful parser expected = strings.Replace(expected, "static inline int8_t testInline();", "static int8_t testInline();", -1) @@ -615,25 +602,24 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_function_signature_inside_ifdef", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -648,36 +634,35 @@ func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 1 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 3 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 15 \""+absoluteSketchLocation+"\"\nint8_t adalight();\n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 1 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 3 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 15 \""+absoluteSketchLocation+"\"\nint8_t adalight();\n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) } func TestPrototypesAdderSketchWithUSBCON(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_usbcon", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + OtherLibrariesFolders: []string{"libraries"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -692,35 +677,34 @@ func TestPrototypesAdderSketchWithUSBCON(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 5 \""+absoluteSketchLocation+"\"\nvoid ciao();\n#line 10 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 15 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 5 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 5 \""+absoluteSketchLocation+"\"\nvoid ciao();\n#line 10 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 15 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 5 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) } func TestPrototypesAdderSketchWithTypename(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_typename", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + LibrariesFolders: []string{"libraries", "downloaded_libraries"}, + ToolsFolders: []string{"downloaded_tools"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -735,13 +719,13 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) expected := "#line 6 \"" + absoluteSketchLocation + "\"\nvoid setup();\n#line 10 \"" + absoluteSketchLocation + "\"\nvoid loop();\n#line 12 \"" + absoluteSketchLocation + "\"\ntypename Foo::Bar func();\n#line 6 \"" + absoluteSketchLocation + "\"\n" - obtained := context[constants.CTX_PROTOTYPE_SECTION].(string) + obtained := ctx.PrototypesSection // ctags based preprocessing ignores line with typename // TODO: remove this exception when moving to a more powerful parser expected = strings.Replace(expected, "#line 12 \""+absoluteSketchLocation+"\"\ntypename Foo::Bar func();\n", "", -1) @@ -752,25 +736,24 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) { func TestPrototypesAdderSketchWithIfDef2(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_ifdef", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:yun" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:yun", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -785,39 +768,38 @@ func TestPrototypesAdderSketchWithIfDef2(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 5 \""+absoluteSketchLocation+"\"\nvoid elseBranch();\n#line 9 \""+absoluteSketchLocation+"\"\nvoid f1();\n#line 10 \""+absoluteSketchLocation+"\"\nvoid f2();\n#line 12 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 14 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 5 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 5 \""+absoluteSketchLocation+"\"\nvoid elseBranch();\n#line 9 \""+absoluteSketchLocation+"\"\nvoid f1();\n#line 10 \""+absoluteSketchLocation+"\"\nvoid f2();\n#line 12 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 14 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 5 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) - expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.txt"), context) - require.Equal(t, expectedSource, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.txt"), ctx) + require.Equal(t, expectedSource, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_ifdef", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:sam:arduino_due_x_dbg" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:sam:arduino_due_x_dbg", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -832,39 +814,38 @@ func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 2 \""+absoluteSketchLocation+"\"\nvoid ifBranch();\n#line 9 \""+absoluteSketchLocation+"\"\nvoid f1();\n#line 10 \""+absoluteSketchLocation+"\"\nvoid f2();\n#line 12 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 14 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 2 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 2 \""+absoluteSketchLocation+"\"\nvoid ifBranch();\n#line 9 \""+absoluteSketchLocation+"\"\nvoid f1();\n#line 10 \""+absoluteSketchLocation+"\"\nvoid f2();\n#line 12 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 14 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 2 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) - expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.SAM.txt"), context) - require.Equal(t, expectedSource, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1)) + expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.SAM.txt"), ctx) + require.Equal(t, expectedSource, strings.Replace(ctx.Source, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchWithConst(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("sketch_with_const", "sketch.ino") absoluteSketchLocation := strings.Replace(Abs(t, sketchLocation), "\\", "\\\\", -1) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: sketchLocation, + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + Verbose: true, + } + + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -879,35 +860,32 @@ func TestPrototypesAdderSketchWithConst(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_INCLUDE_SECTION].(string)) - require.Equal(t, "#line 1 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 2 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 4 \""+absoluteSketchLocation+"\"\nconst __FlashStringHelper* test();\n#line 6 \""+absoluteSketchLocation+"\"\nconst int test3();\n#line 8 \""+absoluteSketchLocation+"\"\nvolatile __FlashStringHelper* test2();\n#line 10 \""+absoluteSketchLocation+"\"\nvolatile int test4();\n#line 1 \""+absoluteSketchLocation+"\"\n", context[constants.CTX_PROTOTYPE_SECTION].(string)) + require.Equal(t, "#include \n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.IncludeSection) + require.Equal(t, "#line 1 \""+absoluteSketchLocation+"\"\nvoid setup();\n#line 2 \""+absoluteSketchLocation+"\"\nvoid loop();\n#line 4 \""+absoluteSketchLocation+"\"\nconst __FlashStringHelper* test();\n#line 6 \""+absoluteSketchLocation+"\"\nconst int test3();\n#line 8 \""+absoluteSketchLocation+"\"\nvolatile __FlashStringHelper* test2();\n#line 10 \""+absoluteSketchLocation+"\"\nvolatile int test4();\n#line 1 \""+absoluteSketchLocation+"\"\n", ctx.PrototypesSection) } func TestPrototypesAdderSketchWithDosEol(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: filepath.Join("eol_processing", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + Verbose: true, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - sketchLocation := filepath.Join("eol_processing", "sketch.ino") - - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = sketchLocation - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -922,7 +900,7 @@ func TestPrototypesAdderSketchWithDosEol(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } // only requires no error as result diff --git a/src/arduino.cc/builder/test/read_file_and_store_in_context_test.go b/src/arduino.cc/builder/test/read_file_and_store_in_context_test.go index 2734725a..0fb9db49 100644 --- a/src/arduino.cc/builder/test/read_file_and_store_in_context_test.go +++ b/src/arduino.cc/builder/test/read_file_and_store_in_context_test.go @@ -31,7 +31,7 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "arduino.cc/builder/utils" "github.com/stretchr/testify/require" "io/ioutil" @@ -46,12 +46,12 @@ func TestReadFileAndStoreInContext(t *testing.T) { utils.WriteFile(file.Name(), "test test\nciao") - context := make(map[string]interface{}) - context[constants.CTX_FILE_PATH_TO_READ] = file.Name() + ctx := &types.Context{} + ctx.FileToRead = file.Name() - command := &builder.ReadFileAndStoreInContext{TargetField: constants.CTX_GCC_MINUS_E_SOURCE} - err = command.Run(context) + command := &builder.ReadFileAndStoreInContext{Target: &ctx.SourceGccMinusE} + err = command.Run(ctx) NoError(t, err) - require.Equal(t, "test test\nciao", context[constants.CTX_GCC_MINUS_E_SOURCE].(string)) + require.Equal(t, "test test\nciao", ctx.SourceGccMinusE) } diff --git a/src/arduino.cc/builder/test/recipe_runner_test.go b/src/arduino.cc/builder/test/recipe_runner_test.go index d4d51f64..5519ca5e 100644 --- a/src/arduino.cc/builder/test/recipe_runner_test.go +++ b/src/arduino.cc/builder/test/recipe_runner_test.go @@ -43,20 +43,19 @@ import ( // and that allows to test if the recipe is actually run // So this test is pretty useless func TestRecipeRunner(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} buildProperties := make(props.PropertiesMap) - context[constants.CTX_BUILD_PROPERTIES] = buildProperties + ctx.BuildProperties = buildProperties buildProperties["recipe.hooks.prebuild.1.pattern"] = "echo" commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_PREBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } } diff --git a/src/arduino.cc/builder/test/rewrite_hardware_keys_test.go b/src/arduino.cc/builder/test/rewrite_hardware_keys_test.go index 6ad301fb..cab44675 100644 --- a/src/arduino.cc/builder/test/rewrite_hardware_keys_test.go +++ b/src/arduino.cc/builder/test/rewrite_hardware_keys_test.go @@ -38,7 +38,7 @@ import ( ) func TestRewriteHardwareKeys(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} packages := &types.Packages{} packages.Packages = make(map[string]*types.Package) @@ -52,20 +52,19 @@ func TestRewriteHardwareKeys(t *testing.T) { platform.Properties[constants.PLATFORM_NAME] = "A test platform" platform.Properties[constants.BUILD_PROPERTIES_COMPILER_PATH] = "{runtime.ide.path}/hardware/tools/avr/bin/" - context[constants.CTX_HARDWARE] = packages + ctx.Hardware = packages rewrite := types.PlatforKeyRewrite{Key: constants.BUILD_PROPERTIES_COMPILER_PATH, OldValue: "{runtime.ide.path}/hardware/tools/avr/bin/", NewValue: "{runtime.tools.avr-gcc.path}/bin/"} platformKeysRewrite := types.PlatforKeysRewrite{Rewrites: []types.PlatforKeyRewrite{rewrite}} - context[constants.CTX_PLATFORM_KEYS_REWRITE] = platformKeysRewrite + ctx.PlatformKeyRewrites = platformKeysRewrite commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.RewriteHardwareKeys{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -73,7 +72,7 @@ func TestRewriteHardwareKeys(t *testing.T) { } func TestRewriteHardwareKeysWithRewritingDisabled(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} packages := &types.Packages{} packages.Packages = make(map[string]*types.Package) @@ -88,21 +87,20 @@ func TestRewriteHardwareKeysWithRewritingDisabled(t *testing.T) { platform.Properties[constants.BUILD_PROPERTIES_COMPILER_PATH] = "{runtime.ide.path}/hardware/tools/avr/bin/" platform.Properties[constants.REWRITING] = constants.REWRITING_DISABLED - context[constants.CTX_HARDWARE] = packages + ctx.Hardware = packages rewrite := types.PlatforKeyRewrite{Key: constants.BUILD_PROPERTIES_COMPILER_PATH, OldValue: "{runtime.ide.path}/hardware/tools/avr/bin/", NewValue: "{runtime.tools.avr-gcc.path}/bin/"} platformKeysRewrite := types.PlatforKeysRewrite{Rewrites: []types.PlatforKeyRewrite{rewrite}} - context[constants.CTX_PLATFORM_KEYS_REWRITE] = platformKeysRewrite + ctx.PlatformKeyRewrites = platformKeysRewrite commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.RewriteHardwareKeys{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } diff --git a/src/arduino.cc/builder/test/setup_build_properties_test.go b/src/arduino.cc/builder/test/setup_build_properties_test.go index b933f8a1..592bccd5 100644 --- a/src/arduino.cc/builder/test/setup_build_properties_test.go +++ b/src/arduino.cc/builder/test/setup_build_properties_test.go @@ -32,7 +32,6 @@ package test import ( "arduino.cc/builder" "arduino.cc/builder/constants" - "arduino.cc/builder/props" "arduino.cc/builder/types" "arduino.cc/builder/utils" "github.com/stretchr/testify/require" @@ -44,19 +43,18 @@ import ( func TestSetupBuildProperties(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + ToolsFolders: []string{"downloaded_tools", "./tools_builtin"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "./tools_builtin"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.ToolsLoader{}, @@ -66,11 +64,11 @@ func TestSetupBuildProperties(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties require.Equal(t, "ARDUINO", buildProperties[constants.BUILD_PROPERTIES_SOFTWARE]) @@ -82,7 +80,7 @@ func TestSetupBuildProperties(t *testing.T) { require.Equal(t, Abs(t, "downloaded_hardware/arduino/avr"), buildProperties[constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH]) require.Equal(t, Abs(t, "downloaded_hardware/arduino"), buildProperties[constants.BUILD_PROPERTIES_RUNTIME_HARDWARE_PATH]) - require.Equal(t, "10600", buildProperties[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION]) + require.Equal(t, "10600", buildProperties[constants.BUILD_PROPERTIES_RUNTIME_IDE_VERSION]) require.NotEmpty(t, buildProperties[constants.BUILD_PROPERTIES_RUNTIME_OS]) require.Equal(t, Abs(t, "./downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1"), buildProperties["runtime.tools.arm-none-eabi-gcc.path"]) @@ -106,20 +104,20 @@ func TestSetupBuildProperties(t *testing.T) { func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + ToolsFolders: []string{"downloaded_tools", "./tools_builtin"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "arduino:avr:uno", + ArduinoAPIVersion: "10600", - buildPath := SetupBuildPath(t, context) - defer os.RemoveAll(buildPath) + CustomBuildProperties: []string{"name=fake name", "tools.avrdude.config.path=non existent path with space and a ="}, + } - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "./tools_builtin"} - context[constants.CTX_FQBN] = "arduino:avr:uno" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - context[constants.CTX_CUSTOM_BUILD_PROPERTIES] = []string{"name=fake name", "tools.avrdude.config.path=non existent path with space and a ="} + buildPath := SetupBuildPath(t, ctx) + defer os.RemoveAll(buildPath) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.ToolsLoader{}, @@ -130,11 +128,11 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties require.Equal(t, "ARDUINO", buildProperties[constants.BUILD_PROPERTIES_SOFTWARE]) @@ -147,19 +145,18 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { func TestSetupBuildPropertiesUserHardware(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + ToolsFolders: []string{"downloaded_tools", "./tools_builtin"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "my_avr_platform:avr:custom_yun", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "./tools_builtin"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:custom_yun" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.AddAdditionalEntriesToContext{}, &builder.HardwareLoader{}, &builder.ToolsLoader{}, @@ -169,11 +166,11 @@ func TestSetupBuildPropertiesUserHardware(t *testing.T) { } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties require.Equal(t, "ARDUINO", buildProperties[constants.BUILD_PROPERTIES_SOFTWARE]) @@ -186,28 +183,27 @@ func TestSetupBuildPropertiesUserHardware(t *testing.T) { func TestSetupBuildPropertiesWithMissingPropsFromParentPlatformTxtFiles(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + ToolsFolders: []string{"downloaded_tools", "./tools_builtin"}, + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + FQBN: "my_avr_platform:avr:custom_yun", + ArduinoAPIVersion: "10600", + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600" - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "./tools_builtin"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:custom_yun" - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") - commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) + buildProperties := ctx.BuildProperties require.Equal(t, "ARDUINO", buildProperties[constants.BUILD_PROPERTIES_SOFTWARE]) @@ -217,13 +213,9 @@ func TestSetupBuildPropertiesWithMissingPropsFromParentPlatformTxtFiles(t *testi require.Equal(t, "\"{compiler.path}{compiler.c.cmd}\" {compiler.c.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.c.extra_flags} {build.extra_flags} {includes} \"{source_file}\" -o \"{object_file}\"", buildProperties["recipe.c.o.pattern"]) require.Equal(t, "{path}/etc/avrdude.conf", buildProperties["tools.avrdude.config.path"]) - coanProps := buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.COAN) - require.Equal(t, "{path}/coan", coanProps["cmd.path"]) - require.Equal(t, "\"{cmd.path}\" source -m -E -P -kb {compiler.c.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.c.extra_flags} {build.extra_flags} \"{source_file}\"", coanProps[constants.BUILD_PROPERTIES_PATTERN]) - require.Equal(t, Abs(t, "user_hardware/my_avr_platform/avr"), buildProperties[constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH]) require.Equal(t, Abs(t, "user_hardware/my_avr_platform"), buildProperties[constants.BUILD_PROPERTIES_RUNTIME_HARDWARE_PATH]) - require.Equal(t, "10600", buildProperties[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION]) + require.Equal(t, "10600", buildProperties[constants.BUILD_PROPERTIES_RUNTIME_IDE_VERSION]) require.NotEmpty(t, buildProperties[constants.BUILD_PROPERTIES_RUNTIME_OS]) require.Equal(t, Abs(t, "./downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1"), buildProperties["runtime.tools.arm-none-eabi-gcc.path"]) diff --git a/src/arduino.cc/builder/test/setup_human_logger_if_missing_test.go b/src/arduino.cc/builder/test/setup_human_logger_if_missing_test.go deleted file mode 100644 index 8644a567..00000000 --- a/src/arduino.cc/builder/test/setup_human_logger_if_missing_test.go +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of Arduino Builder. - * - * Arduino Builder is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - */ - -package test - -import ( - "arduino.cc/builder" - "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" - "github.com/stretchr/testify/require" - "reflect" - "testing" -) - -func TestSetupHumanLogger(t *testing.T) { - context := make(map[string]interface{}) - - command := &builder.SetupHumanLoggerIfMissing{} - err := command.Run(context) - NoError(t, err) - - logger := context[constants.CTX_LOGGER].(i18n.Logger) - require.Equal(t, "HumanLogger", reflect.TypeOf(logger).Name()) -} diff --git a/src/arduino.cc/builder/test/sketch_loader_test.go b/src/arduino.cc/builder/test/sketch_loader_test.go index 120c68f1..1ae3560e 100644 --- a/src/arduino.cc/builder/test/sketch_loader_test.go +++ b/src/arduino.cc/builder/test/sketch_loader_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "path/filepath" @@ -39,54 +38,44 @@ import ( ) func TestLoadSketchWithFolder(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = "sketch1" - - loggerCommand := builder.SetupHumanLoggerIfMissing{} - err := loggerCommand.Run(context) - NoError(t, err) + ctx := &types.Context{ + SketchLocation: "sketch1", + } loader := builder.SketchLoader{} - err = loader.Run(context) + err := loader.Run(ctx) require.Error(t, err) - - sketch := context[constants.CTX_SKETCH] - require.Nil(t, sketch) + require.Nil(t, ctx.Sketch) } func TestLoadSketchNonExistentPath(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = "asdasd78128123981723981273asdasd" - - loggerCommand := builder.SetupHumanLoggerIfMissing{} - err := loggerCommand.Run(context) - NoError(t, err) + ctx := &types.Context{ + SketchLocation: "asdasd78128123981723981273asdasd", + } loader := builder.SketchLoader{} - err = loader.Run(context) + err := loader.Run(ctx) require.Error(t, err) - - sketch := context[constants.CTX_SKETCH] - require.Nil(t, sketch) + require.Nil(t, ctx.Sketch) } func TestLoadSketch(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") + ctx := &types.Context{ + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.SketchLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - sketch := context[constants.CTX_SKETCH].(*types.Sketch) + sketch := ctx.Sketch require.NotNil(t, sketch) require.Contains(t, sketch.MainFile.Name, "sketch.ino") @@ -102,36 +91,31 @@ func TestLoadSketch(t *testing.T) { } func TestFailToLoadSketchFromFolder(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = "./sketch1" - - loggerCommand := builder.SetupHumanLoggerIfMissing{} - err := loggerCommand.Run(context) - NoError(t, err) + ctx := &types.Context{ + SketchLocation: "./sketch1", + } loader := builder.SketchLoader{} - err = loader.Run(context) + err := loader.Run(ctx) require.Error(t, err) - - sketch := context[constants.CTX_SKETCH] - require.Nil(t, sketch) + require.Nil(t, ctx.Sketch) } func TestLoadSketchFromFolder(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = "sketch_with_subfolders" + ctx := &types.Context{ + SketchLocation: "sketch_with_subfolders", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.SketchLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - sketch := context[constants.CTX_SKETCH].(*types.Sketch) + sketch := ctx.Sketch require.NotNil(t, sketch) require.Contains(t, sketch.MainFile.Name, "sketch_with_subfolders.ino") @@ -144,20 +128,20 @@ func TestLoadSketchFromFolder(t *testing.T) { } func TestLoadSketchWithBackup(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_backup_files", "sketch.ino") + ctx := &types.Context{ + SketchLocation: filepath.Join("sketch_with_backup_files", "sketch.ino"), + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.SketchLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - sketch := context[constants.CTX_SKETCH].(*types.Sketch) + sketch := ctx.Sketch require.NotNil(t, sketch) require.Contains(t, sketch.MainFile.Name, "sketch.ino") @@ -167,20 +151,20 @@ func TestLoadSketchWithBackup(t *testing.T) { } func TestLoadSketchWithMacOSXGarbage(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_with_macosx_garbage", "sketch.ino") + ctx := &types.Context{ + SketchLocation: filepath.Join("sketch_with_macosx_garbage", "sketch.ino"), + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.SketchLoader{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - sketch := context[constants.CTX_SKETCH].(*types.Sketch) + sketch := ctx.Sketch require.NotNil(t, sketch) require.Contains(t, sketch.MainFile.Name, "sketch.ino") diff --git a/src/arduino.cc/builder/test/sketch_source_merger_test.go b/src/arduino.cc/builder/test/sketch_source_merger_test.go index c0b50698..6cb68dc0 100644 --- a/src/arduino.cc/builder/test/sketch_source_merger_test.go +++ b/src/arduino.cc/builder/test/sketch_source_merger_test.go @@ -32,7 +32,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "path/filepath" @@ -41,22 +40,22 @@ import ( ) func TestMergeSketch(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino") + ctx := &types.Context{ + SketchLocation: filepath.Join("sketch1", "sketch.ino"), + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.SketchLoader{}, &builder.SketchSourceMerger{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - source := context[constants.CTX_SOURCE].(string) + source := ctx.Source - expected_source := LoadAndInterpolate(t, filepath.Join("sketch1", "merged_sketch.txt"), context) + expected_source := LoadAndInterpolate(t, filepath.Join("sketch1", "merged_sketch.txt"), ctx) require.Equal(t, expected_source, strings.Replace(source, "\r\n", "\n", -1)) } diff --git a/src/arduino.cc/builder/test/store_build_options_map_test.go b/src/arduino.cc/builder/test/store_build_options_map_test.go index 110bdd6b..5ff02564 100644 --- a/src/arduino.cc/builder/test/store_build_options_map_test.go +++ b/src/arduino.cc/builder/test/store_build_options_map_test.go @@ -41,28 +41,29 @@ import ( ) func TestStoreBuildOptionsMap(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{ + HardwareFolders: []string{"hardware"}, + ToolsFolders: []string{"tools"}, + BuiltInLibrariesFolders: []string{"built-in libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + SketchLocation: "sketchLocation", + FQBN: "fqbn", + ArduinoAPIVersion: "ideVersion", + CustomBuildProperties: []string{"custom=prop"}, + Verbose: true, + DebugLevel: 5, + } - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = "hardware" - context[constants.CTX_TOOLS_FOLDERS] = "tools" - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = "built-in libraries" - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = "libraries" - context[constants.CTX_FQBN] = "fqbn" - context[constants.CTX_SKETCH_LOCATION] = "sketchLocation" - context[constants.CTX_VERBOSE] = true - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "ideVersion" - context[constants.CTX_DEBUG_LEVEL] = 5 - commands := []types.Command{ &builder.CreateBuildOptionsMap{}, &builder.StoreBuildOptionsMap{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -74,6 +75,7 @@ func TestStoreBuildOptionsMap(t *testing.T) { require.Equal(t, "{\n"+ " \"builtInLibrariesFolders\": \"built-in libraries\",\n"+ + " \"customBuildProperties\": \"custom=prop\",\n"+ " \"fqbn\": \"fqbn\",\n"+ " \"hardwareFolders\": \"hardware\",\n"+ " \"otherLibrariesFolders\": \"libraries\",\n"+ diff --git a/src/arduino.cc/builder/test/target_board_resolver_test.go b/src/arduino.cc/builder/test/target_board_resolver_test.go index 19268b26..274eb92c 100644 --- a/src/arduino.cc/builder/test/target_board_resolver_test.go +++ b/src/arduino.cc/builder/test/target_board_resolver_test.go @@ -39,155 +39,155 @@ import ( ) func TestTargetBoardResolverUno(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:avr:uno" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + FQBN: "arduino:avr:uno", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "arduino", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "uno", targetBoard.BoardId) require.Equal(t, "atmega328p", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) } func TestTargetBoardResolverDue(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:sam:arduino_due_x" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + FQBN: "arduino:sam:arduino_due_x", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "arduino", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "sam", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "arduino_due_x", targetBoard.BoardId) require.Equal(t, "cortex-m3", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) } func TestTargetBoardResolverMega1280(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:avr:mega:cpu=atmega1280" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + FQBN: "arduino:avr:mega:cpu=atmega1280", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "arduino", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "mega", targetBoard.BoardId) require.Equal(t, "atmega1280", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) require.Equal(t, "AVR_MEGA", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_BOARD]) } func TestTargetBoardResolverMega2560(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"} - context[constants.CTX_FQBN] = "arduino:avr:mega:cpu=atmega2560" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}, + FQBN: "arduino:avr:mega:cpu=atmega2560", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "arduino", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "mega", targetBoard.BoardId) require.Equal(t, "atmega2560", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) require.Equal(t, "AVR_MEGA2560", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_BOARD]) } func TestTargetBoardResolverCustomYun(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_FQBN] = "my_avr_platform:avr:custom_yun" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + FQBN: "my_avr_platform:avr:custom_yun", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "my_avr_platform", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "custom_yun", targetBoard.BoardId) require.Equal(t, "atmega32u4", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU]) require.Equal(t, "AVR_YUN", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_BOARD]) } func TestTargetBoardResolverCustomCore(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"} - context[constants.CTX_FQBN] = "watterott:avr:attiny841:core=spencekonde,info=info" + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"}, + FQBN: "watterott:avr:attiny841:core=spencekonde,info=info", + } commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.HardwareLoader{}, &builder.TargetBoardResolver{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } - targetPackage := context[constants.CTX_TARGET_PACKAGE].(*types.Package) + targetPackage := ctx.TargetPackage require.Equal(t, "watterott", targetPackage.PackageId) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) + targetPlatform := ctx.TargetPlatform require.Equal(t, "avr", targetPlatform.PlatformId) - targetBoard := context[constants.CTX_TARGET_BOARD].(*types.Board) + targetBoard := ctx.TargetBoard require.Equal(t, "attiny841", targetBoard.BoardId) - require.Equal(t, "tiny841", context[constants.CTX_BUILD_CORE].(string)) + require.Equal(t, "tiny841", ctx.BuildCore) require.Equal(t, "tiny14", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_VARIANT]) } diff --git a/src/arduino.cc/builder/test/tools_loader_test.go b/src/arduino.cc/builder/test/tools_loader_test.go index 07965598..2c85a942 100644 --- a/src/arduino.cc/builder/test/tools_loader_test.go +++ b/src/arduino.cc/builder/test/tools_loader_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "sort" @@ -56,14 +55,15 @@ func (s ByToolIDAndVersion) Less(i, j int) bool { func TestLoadTools(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "tools_builtin"} + ctx := &types.Context{ + ToolsFolders: []string{"downloaded_tools", "tools_builtin"}, + } loader := builder.ToolsLoader{} - err := loader.Run(context) + err := loader.Run(ctx) NoError(t, err) - tools := context[constants.CTX_TOOLS].([]*types.Tool) + tools := ctx.Tools require.Equal(t, 7, len(tools)) sort.Sort(ByToolIDAndVersion(tools)) @@ -93,14 +93,15 @@ func TestLoadTools(t *testing.T) { func TestLoadToolsWithBoardManagerFolderStructure(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_board_manager_stuff"} + ctx := &types.Context{ + ToolsFolders: []string{"downloaded_board_manager_stuff"}, + } loader := builder.ToolsLoader{} - err := loader.Run(context) + err := loader.Run(ctx) NoError(t, err) - tools := context[constants.CTX_TOOLS].([]*types.Tool) + tools := ctx.Tools require.Equal(t, 3, len(tools)) sort.Sort(ByToolIDAndVersion(tools)) @@ -118,14 +119,15 @@ func TestLoadToolsWithBoardManagerFolderStructure(t *testing.T) { func TestLoadLotsOfTools(t *testing.T) { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "tools_builtin", "downloaded_board_manager_stuff"} + ctx := &types.Context{ + ToolsFolders: []string{"downloaded_tools", "tools_builtin", "downloaded_board_manager_stuff"}, + } loader := builder.ToolsLoader{} - err := loader.Run(context) + err := loader.Run(ctx) NoError(t, err) - tools := context[constants.CTX_TOOLS].([]*types.Tool) + tools := ctx.Tools require.Equal(t, 9, len(tools)) require.Equal(t, "arm-none-eabi-gcc", tools[0].Name) diff --git a/src/arduino.cc/builder/test/try_build_of_problematic_sketch_test.go b/src/arduino.cc/builder/test/try_build_of_problematic_sketch_test.go index 37bfb131..1140cb48 100644 --- a/src/arduino.cc/builder/test/try_build_of_problematic_sketch_test.go +++ b/src/arduino.cc/builder/test/try_build_of_problematic_sketch_test.go @@ -32,7 +32,7 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" + "arduino.cc/builder/types" "os" "path/filepath" "testing" @@ -117,9 +117,9 @@ func TestTryBuild019(t *testing.T) { } func TestTryBuild020(t *testing.T) { - context := makeDefaultContext(t) - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"dependent_libraries", "libraries"} - tryPreprocessWithContext(t, context, "sketch_with_dependend_libraries", "sketch.ino") + ctx := makeDefaultContext(t) + ctx.OtherLibrariesFolders = []string{"dependent_libraries", "libraries"} + tryPreprocessWithContext(t, ctx, "sketch_with_dependend_libraries", "sketch.ino") } func TestTryBuild021(t *testing.T) { @@ -127,9 +127,9 @@ func TestTryBuild021(t *testing.T) { } func TestTryBuild022(t *testing.T) { - context := makeDefaultContext(t) - context[constants.CTX_FQBN] = "arduino:samd:arduino_zero_native" - tryBuildWithContext(t, context, "sketch_usbhost", "sketch_usbhost.ino") + ctx := makeDefaultContext(t) + ctx.FQBN = "arduino:samd:arduino_zero_native" + tryBuildWithContext(t, ctx, "sketch_usbhost", "sketch_usbhost.ino") } func TestTryBuild023(t *testing.T) { @@ -186,9 +186,9 @@ func TestTryBuild035(t *testing.T) { } func TestTryBuild036(t *testing.T) { - context := makeDefaultContext(t) - context[constants.CTX_FQBN] = "arduino:samd:arduino_zero_native" - tryBuildWithContext(t, context, "sketch11", "sketch_fastleds.ino") + ctx := makeDefaultContext(t) + ctx.FQBN = "arduino:samd:arduino_zero_native" + tryBuildWithContext(t, ctx, "sketch11", "sketch_fastleds.ino") } func TestTryBuild037(t *testing.T) { @@ -199,47 +199,47 @@ func TestTryBuild038(t *testing.T) { tryBuild(t, "sketch_with_multiline_prototypes", "sketch_with_multiline_prototypes.ino") } -func makeDefaultContext(t *testing.T) map[string]interface{} { +func makeDefaultContext(t *testing.T) *types.Context { DownloadCoresAndToolsAndLibraries(t) - context := make(map[string]interface{}) - buildPath := SetupBuildPath(t, context) + ctx := &types.Context{ + HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}, + ToolsFolders: []string{"downloaded_tools", "downloaded_board_manager_stuff"}, + BuiltInLibrariesFolders: []string{"downloaded_libraries"}, + OtherLibrariesFolders: []string{"libraries"}, + FQBN: "arduino:avr:leonardo", + ArduinoAPIVersion: "10607", + Verbose: true, + DebugPreprocessor: true, + } + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"} - context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"} - context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"} - context[constants.CTX_FQBN] = "arduino:avr:leonardo" - context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10607" - context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"} - context[constants.CTX_VERBOSE] = true - context[constants.CTX_DEBUG_PREPROCESSOR] = true - - return context + return ctx } func tryBuild(t *testing.T, sketchPath ...string) { - context := makeDefaultContext(t) - tryBuildWithContext(t, context, sketchPath...) + ctx := makeDefaultContext(t) + tryBuildWithContext(t, ctx, sketchPath...) } -func tryBuildWithContext(t *testing.T, context map[string]interface{}, sketchPath ...string) { +func tryBuildWithContext(t *testing.T, ctx *types.Context, sketchPath ...string) { sketchLocation := filepath.Join(sketchPath...) - context[constants.CTX_SKETCH_LOCATION] = sketchLocation + ctx.SketchLocation = sketchLocation - err := builder.RunBuilder(context) + err := builder.RunBuilder(ctx) NoError(t, err, "Build error for "+sketchLocation) } func tryPreprocess(t *testing.T, sketchPath ...string) { - context := makeDefaultContext(t) - tryPreprocessWithContext(t, context, sketchPath...) + ctx := makeDefaultContext(t) + tryPreprocessWithContext(t, ctx, sketchPath...) } -func tryPreprocessWithContext(t *testing.T, context map[string]interface{}, sketchPath ...string) { +func tryPreprocessWithContext(t *testing.T, ctx *types.Context, sketchPath ...string) { sketchLocation := filepath.Join(sketchPath...) - context[constants.CTX_SKETCH_LOCATION] = sketchLocation + ctx.SketchLocation = sketchLocation - err := builder.RunPreprocess(context) + err := builder.RunPreprocess(ctx) NoError(t, err, "Build error for "+sketchLocation) } diff --git a/src/arduino.cc/builder/test/unused_compiled_libraries_remover_test.go b/src/arduino.cc/builder/test/unused_compiled_libraries_remover_test.go index d82af8c0..98ca24a9 100644 --- a/src/arduino.cc/builder/test/unused_compiled_libraries_remover_test.go +++ b/src/arduino.cc/builder/test/unused_compiled_libraries_remover_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/types" "github.com/stretchr/testify/require" "io/ioutil" @@ -49,12 +48,12 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { NoError(t, os.MkdirAll(filepath.Join(temp, "Bridge"), os.FileMode(0755))) NoError(t, ioutil.WriteFile(filepath.Join(temp, "dummy_file"), []byte{}, os.FileMode(0644))) - context := make(map[string]interface{}) - context[constants.CTX_LIBRARIES_BUILD_PATH] = temp - context[constants.CTX_IMPORTED_LIBRARIES] = []*types.Library{&types.Library{Name: "Bridge"}} + ctx := &types.Context{} + ctx.LibrariesBuildPath = temp + ctx.ImportedLibraries = []*types.Library{&types.Library{Name: "Bridge"}} cmd := builder.UnusedCompiledLibrariesRemover{} - err = cmd.Run(context) + err = cmd.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(temp, "SPI")) @@ -67,12 +66,12 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { } func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { - context := make(map[string]interface{}) - context[constants.CTX_LIBRARIES_BUILD_PATH] = filepath.Join(os.TempDir(), "test") - context[constants.CTX_IMPORTED_LIBRARIES] = []*types.Library{&types.Library{Name: "Bridge"}} + ctx := &types.Context{} + ctx.LibrariesBuildPath = filepath.Join(os.TempDir(), "test") + ctx.ImportedLibraries = []*types.Library{&types.Library{Name: "Bridge"}} cmd := builder.UnusedCompiledLibrariesRemover{} - err := cmd.Run(context) + err := cmd.Run(ctx) NoError(t, err) } @@ -85,12 +84,12 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { NoError(t, os.MkdirAll(filepath.Join(temp, "Bridge"), os.FileMode(0755))) NoError(t, ioutil.WriteFile(filepath.Join(temp, "dummy_file"), []byte{}, os.FileMode(0644))) - context := make(map[string]interface{}) - context[constants.CTX_LIBRARIES_BUILD_PATH] = temp - context[constants.CTX_IMPORTED_LIBRARIES] = []*types.Library{} + ctx := &types.Context{} + ctx.LibrariesBuildPath = temp + ctx.ImportedLibraries = []*types.Library{} cmd := builder.UnusedCompiledLibrariesRemover{} - err = cmd.Run(context) + err = cmd.Run(ctx) NoError(t, err) _, err = os.Stat(filepath.Join(temp, "SPI")) diff --git a/src/arduino.cc/builder/test/wipeout_build_path_if_build_options_changed_test.go b/src/arduino.cc/builder/test/wipeout_build_path_if_build_options_changed_test.go index d72b05b2..b6bd6c4a 100644 --- a/src/arduino.cc/builder/test/wipeout_build_path_if_build_options_changed_test.go +++ b/src/arduino.cc/builder/test/wipeout_build_path_if_build_options_changed_test.go @@ -31,7 +31,6 @@ package test import ( "arduino.cc/builder" - "arduino.cc/builder/constants" "arduino.cc/builder/gohasissues" "arduino.cc/builder/types" "arduino.cc/builder/utils" @@ -42,23 +41,22 @@ import ( ) func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON] = "old" - context[constants.CTX_BUILD_OPTIONS_JSON] = "new" + ctx.BuildOptionsJsonPrevious = "old" + ctx.BuildOptionsJson = "new" utils.TouchFile(filepath.Join(buildPath, "should_be_deleted.txt")) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.WipeoutBuildPathIfBuildOptionsChanged{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -74,22 +72,21 @@ func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { } func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_OPTIONS_JSON] = "new" + ctx.BuildOptionsJson = "new" utils.TouchFile(filepath.Join(buildPath, "should_not_be_deleted.txt")) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.WipeoutBuildPathIfBuildOptionsChanged{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } @@ -105,23 +102,22 @@ func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing. } func TestWipeoutBuildPathIfBuildOptionsChangedBuildOptionsMatch(t *testing.T) { - context := make(map[string]interface{}) + ctx := &types.Context{} - buildPath := SetupBuildPath(t, context) + buildPath := SetupBuildPath(t, ctx) defer os.RemoveAll(buildPath) - context[constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON] = "options" - context[constants.CTX_BUILD_OPTIONS_JSON] = "options" + ctx.BuildOptionsJsonPrevious = "options" + ctx.BuildOptionsJson = "options" utils.TouchFile(filepath.Join(buildPath, "should_not_be_deleted.txt")) commands := []types.Command{ - &builder.SetupHumanLoggerIfMissing{}, &builder.WipeoutBuildPathIfBuildOptionsChanged{}, } for _, command := range commands { - err := command.Run(context) + err := command.Run(ctx) NoError(t, err) } diff --git a/src/arduino.cc/builder/tools_loader.go b/src/arduino.cc/builder/tools_loader.go index 9da9e727..621a5391 100644 --- a/src/arduino.cc/builder/tools_loader.go +++ b/src/arduino.cc/builder/tools_loader.go @@ -32,6 +32,7 @@ package builder import ( "arduino.cc/builder/constants" "arduino.cc/builder/gohasissues" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "os" @@ -41,37 +42,37 @@ import ( type ToolsLoader struct{} -func (s *ToolsLoader) Run(context map[string]interface{}) error { - folders := context[constants.CTX_TOOLS_FOLDERS].([]string) +func (s *ToolsLoader) Run(ctx *types.Context) error { + folders := ctx.ToolsFolders tools := []*types.Tool{} for _, folder := range folders { builtinToolsVersionsFile, err := findBuiltinToolsVersionsFile(folder) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if builtinToolsVersionsFile != constants.EMPTY_STRING { err = loadToolsFrom(&tools, builtinToolsVersionsFile) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } else { subfolders, err := collectAllToolsFolders(folder) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, subfolder := range subfolders { err = loadToolsFromFolderStructure(&tools, subfolder) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } } - context[constants.CTX_TOOLS] = tools + ctx.Tools = tools return nil } @@ -89,7 +90,7 @@ func collectAllToolsFolders(from string) ([]string, error) { rel, err := filepath.Rel(from, currentPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } depth := len(strings.Split(rel, string(os.PathSeparator))) @@ -107,7 +108,7 @@ func collectAllToolsFolders(from string) ([]string, error) { folders = append(folders, from) } - return folders, utils.WrapError(err) + return folders, i18n.WrapError(err) } func toolsSliceContains(tools *[]*types.Tool, name, version string) bool { @@ -122,12 +123,12 @@ func toolsSliceContains(tools *[]*types.Tool, name, version string) bool { func loadToolsFrom(tools *[]*types.Tool, builtinToolsVersionsFilePath string) error { rows, err := utils.ReadFileToRows(builtinToolsVersionsFilePath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } folder, err := filepath.Abs(filepath.Dir(builtinToolsVersionsFilePath)) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, row := range rows { @@ -161,23 +162,23 @@ func findBuiltinToolsVersionsFile(folder string) (string, error) { return nil } err := gohasissues.Walk(folder, findBuiltInToolsVersionsTxt) - return builtinToolsVersionsFilePath, utils.WrapError(err) + return builtinToolsVersionsFilePath, i18n.WrapError(err) } func loadToolsFromFolderStructure(tools *[]*types.Tool, folder string) error { toolsNames, err := utils.ReadDirFiltered(folder, utils.FilterDirs) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, toolName := range toolsNames { toolVersions, err := utils.ReadDirFiltered(filepath.Join(folder, toolName.Name()), utils.FilterDirs) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, toolVersion := range toolVersions { toolFolder, err := filepath.Abs(filepath.Join(folder, toolName.Name(), toolVersion.Name())) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } if !toolsSliceContains(tools, toolName.Name(), toolVersion.Name()) { *tools = append(*tools, &types.Tool{Name: toolName.Name(), Version: toolVersion.Name(), Folder: toolFolder}) diff --git a/src/arduino.cc/builder/types/context.go b/src/arduino.cc/builder/types/context.go new file mode 100644 index 00000000..09546536 --- /dev/null +++ b/src/arduino.cc/builder/types/context.go @@ -0,0 +1,125 @@ +package types + +import "strings" +import "arduino.cc/builder/i18n" +import "arduino.cc/builder/props" + +// Context structure +type Context struct { + // Build options + HardwareFolders []string + ToolsFolders []string + LibrariesFolders []string + BuiltInLibrariesFolders []string + OtherLibrariesFolders []string + SketchLocation string + ArduinoAPIVersion string + FQBN string + + // Build options are serialized here + BuildOptionsJson string + BuildOptionsJsonPrevious string + + Hardware *Packages + Tools []*Tool + TargetBoard *Board + TargetPackage *Package + TargetPlatform *Platform + ActualPlatform *Platform + USBVidPid string + + PlatformKeyRewrites PlatforKeysRewrite + HardwareRewriteResults map[*Platform][]PlatforKeyRewrite + + BuildProperties props.PropertiesMap + BuildCore string + BuildPath string + SketchBuildPath string + CoreBuildPath string + CoreArchiveFilePath string + CoreObjectsFiles []string + LibrariesBuildPath string + LibrariesObjectFiles []string + PreprocPath string + SketchObjectFiles []string + + CollectedSourceFiles *UniqueStringQueue + FoldersWithSourceFiles *UniqueSourceFolderQueue + + Sketch *Sketch + Source string + SourceGccMinusE string + + WarningsLevel string + + // Libraries handling + Includes []string + Libraries []*Library + HeaderToLibraries map[string][]*Library + ImportedLibraries []*Library + LibrariesResolutionResults map[string]LibraryResolutionResult + IncludesJustFound []string + IncludeFolders []string + OutputGccMinusM string + + // C++ Parsing + CTagsOutput string + CTagsTargetFile string + CTagsOfSource []*CTag + CTagsOfPreprocessedSource []*CTag + CTagsCollected []*CTag + IncludeSection string + LineOffset int + PrototypesSection string + PrototypesLineWhereToInsert int + Prototypes []*Prototype + + // Verbosity settings + Verbose bool + DebugPreprocessor bool + + // Contents of a custom build properties file (line by line) + CustomBuildProperties []string + + // Logging + logger i18n.Logger + DebugLevel int + + // ReadFileAndStoreInContext command + FileToRead string +} + +func (ctx *Context) ExtractBuildOptions() props.PropertiesMap { + opts := make(props.PropertiesMap) + opts["hardwareFolders"] = strings.Join(ctx.HardwareFolders, ",") + opts["toolsFolders"] = strings.Join(ctx.ToolsFolders, ",") + opts["builtInLibrariesFolders"] = strings.Join(ctx.BuiltInLibrariesFolders, ",") + opts["otherLibrariesFolders"] = strings.Join(ctx.OtherLibrariesFolders, ",") + opts["sketchLocation"] = ctx.SketchLocation + opts["fqbn"] = ctx.FQBN + opts["runtime.ide.version"] = ctx.ArduinoAPIVersion + opts["customBuildProperties"] = strings.Join(ctx.CustomBuildProperties, ",") + return opts +} + +func (ctx *Context) InjectBuildOptions(opts props.PropertiesMap) { + ctx.HardwareFolders = strings.Split(opts["hardwareFolders"], ",") + ctx.ToolsFolders = strings.Split(opts["toolsFolders"], ",") + ctx.BuiltInLibrariesFolders = strings.Split(opts["builtInLibrariesFolders"], ",") + ctx.OtherLibrariesFolders = strings.Split(opts["otherLibrariesFolders"], ",") + ctx.SketchLocation = opts["sketchLocation"] + ctx.FQBN = opts["fqbn"] + ctx.ArduinoAPIVersion = opts["runtime.ide.version"] + ctx.CustomBuildProperties = strings.Split(opts["customBuildProperties"], ",") +} + +func (ctx *Context) GetLogger() i18n.Logger { + if ctx.logger == nil { + return &i18n.HumanLogger{} + } + return ctx.logger +} + +func (ctx *Context) SetLogger(l i18n.Logger) { + ctx.logger = l +} diff --git a/src/arduino.cc/builder/types/types.go b/src/arduino.cc/builder/types/types.go index 2eebaf8e..b9750197 100644 --- a/src/arduino.cc/builder/types/types.go +++ b/src/arduino.cc/builder/types/types.go @@ -140,6 +140,10 @@ type PlatforKeysRewrite struct { Rewrites []PlatforKeyRewrite } +func (p *PlatforKeysRewrite) Empty() bool { + return len(p.Rewrites) == 0 +} + type PlatforKeyRewrite struct { Key string OldValue string @@ -151,10 +155,6 @@ type KeyValuePair struct { Value string } -type Command interface { - Run(context map[string]interface{}) error -} - type Prototype struct { FunctionName string File string @@ -205,3 +205,7 @@ func LibraryToSourceFolder(library *Library) []SourceFolder { } return sourceFolders } + +type Command interface { + Run(ctx *Context) error +} diff --git a/src/arduino.cc/builder/unused_compiled_libraries_remover.go b/src/arduino.cc/builder/unused_compiled_libraries_remover.go index cd743b04..07b89da8 100644 --- a/src/arduino.cc/builder/unused_compiled_libraries_remover.go +++ b/src/arduino.cc/builder/unused_compiled_libraries_remover.go @@ -30,7 +30,7 @@ package builder import ( - "arduino.cc/builder/constants" + "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" "io/ioutil" @@ -40,27 +40,26 @@ import ( type UnusedCompiledLibrariesRemover struct{} -func (s *UnusedCompiledLibrariesRemover) Run(context map[string]interface{}) error { - librariesBuildPath := context[constants.CTX_LIBRARIES_BUILD_PATH].(string) - libraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) +func (s *UnusedCompiledLibrariesRemover) Run(ctx *types.Context) error { + librariesBuildPath := ctx.LibrariesBuildPath _, err := os.Stat(librariesBuildPath) if err != nil && os.IsNotExist(err) { return nil } - libraryNames := toLibraryNames(libraries) + libraryNames := toLibraryNames(ctx.ImportedLibraries) files, err := ioutil.ReadDir(librariesBuildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { if file.IsDir() { if !utils.SliceContains(libraryNames, file.Name()) { err := os.RemoveAll(filepath.Join(librariesBuildPath, file.Name())) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } } } diff --git a/src/arduino.cc/builder/utils/utils.go b/src/arduino.cc/builder/utils/utils.go index 068d212c..16a11d18 100644 --- a/src/arduino.cc/builder/utils/utils.go +++ b/src/arduino.cc/builder/utils/utils.go @@ -35,12 +35,10 @@ import ( "arduino.cc/builder/i18n" "crypto/md5" "encoding/hex" - "github.com/go-errors/errors" "io/ioutil" "os" "os/exec" "path/filepath" - "reflect" "runtime" "strings" ) @@ -61,23 +59,6 @@ func KeysOfMapOfString(input map[string]string) []string { return keys } -func KeysOfMapOfStringBool(input map[string]bool) []string { - var keys []string - for key, _ := range input { - keys = append(keys, key) - } - return keys -} - -func MergeMapsOfStringBool(target map[string]bool, sources ...map[string]bool) map[string]bool { - for _, source := range sources { - for key, value := range source { - target[key] = value - } - } - return target -} - func PrettyOSName() string { switch osName := runtime.GOOS; osName { case "darwin": @@ -126,7 +107,7 @@ func ParseCommandLine(input string, logger i18n.Logger) ([]string, error) { } if escapingChar != constants.EMPTY_STRING { - return nil, ErrorfWithLogger(logger, constants.MSG_INVALID_QUOTING, escapingChar) + return nil, i18n.ErrorfWithLogger(logger, constants.MSG_INVALID_QUOTING, escapingChar) } return parts, nil @@ -137,7 +118,7 @@ type filterFiles func([]os.FileInfo) []os.FileInfo func ReadDirFiltered(folder string, fn filterFiles) ([]os.FileInfo, error) { files, err := gohasissues.ReadDir(folder) if err != nil { - return nil, WrapError(err) + return nil, i18n.WrapError(err) } return fn(files), nil } @@ -234,7 +215,7 @@ type argFilterFunc func(int, string, []string) bool func PrepareCommandFilteredArgs(pattern string, filter argFilterFunc, logger i18n.Logger) (*exec.Cmd, error) { parts, err := ParseCommandLine(pattern, logger) if err != nil { - return nil, WrapError(err) + return nil, i18n.WrapError(err) } command := parts[0] parts = parts[1:] @@ -256,33 +237,6 @@ func PrepareCommand(pattern string, logger i18n.Logger) (*exec.Cmd, error) { return PrepareCommandFilteredArgs(pattern, filterEmptyArg, logger) } -func WrapError(err error) error { - if err == nil { - return nil - } - return errors.Wrap(err, 0) -} - -func Logger(context map[string]interface{}) i18n.Logger { - if MapHas(context, constants.CTX_LOGGER) { - return context[constants.CTX_LOGGER].(i18n.Logger) - } - return i18n.HumanLogger{} -} - -func Errorf(context map[string]interface{}, format string, a ...interface{}) *errors.Error { - log := Logger(context) - return ErrorfWithLogger(log, format, a...) -} - -func ErrorfWithLogger(log i18n.Logger, format string, a ...interface{}) *errors.Error { - if log.Name() == "machine" { - log.Fprintln(os.Stderr, constants.LOG_LEVEL_ERROR, format, a...) - return errors.Errorf(constants.EMPTY_STRING) - } - return errors.Errorf(i18n.Format(format, a...)) -} - func MapHas(aMap map[string]interface{}, key string) bool { _, ok := aMap[key] return ok @@ -293,13 +247,6 @@ func MapStringStringHas(aMap map[string]string, key string) bool { return ok } -func DebugLevel(context map[string]interface{}) int { - if MapHas(context, constants.CTX_DEBUG_LEVEL) && reflect.TypeOf(context[constants.CTX_DEBUG_LEVEL]).Kind() == reflect.Int { - return context[constants.CTX_DEBUG_LEVEL].(int) - } - return 0 -} - func SliceToMapStringBool(keys []string, value bool) map[string]bool { aMap := make(map[string]bool) for _, key := range keys { @@ -312,7 +259,7 @@ func AbsolutizePaths(files []string) ([]string, error) { for idx, file := range files { absFile, err := filepath.Abs(file) if err != nil { - return nil, WrapError(err) + return nil, i18n.WrapError(err) } files[idx] = absFile } @@ -323,7 +270,7 @@ func AbsolutizePaths(files []string) ([]string, error) { func ReadFileToRows(file string) ([]string, error) { bytes, err := ioutil.ReadFile(file) if err != nil { - return nil, WrapError(err) + return nil, i18n.WrapError(err) } txt := string(bytes) txt = strings.Replace(txt, "\r\n", "\n", -1) @@ -334,7 +281,7 @@ func ReadFileToRows(file string) ([]string, error) { func TheOnlySubfolderOf(folder string) (string, error) { subfolders, err := ReadDirFiltered(folder, FilterDirs) if err != nil { - return constants.EMPTY_STRING, WrapError(err) + return constants.EMPTY_STRING, i18n.WrapError(err) } if len(subfolders) != 1 { @@ -392,15 +339,6 @@ func AppendIfNotPresent(target []string, elements ...string) []string { return target } -func AddStringsToStringsSet(accumulator []string, stringsToAdd []string) []string { - previousStringsSet := SliceToMapStringBool(accumulator, true) - stringsSetToAdd := SliceToMapStringBool(stringsToAdd, true) - - newStringsSet := MergeMapsOfStringBool(previousStringsSet, stringsSetToAdd) - - return KeysOfMapOfStringBool(newStringsSet) -} - func EnsureFolderExists(folder string) error { return os.MkdirAll(folder, os.FileMode(0755)) } diff --git a/src/arduino.cc/builder/warn_about_arch_incompatible_libraries.go b/src/arduino.cc/builder/warn_about_arch_incompatible_libraries.go index 8ca59447..7f716c00 100644 --- a/src/arduino.cc/builder/warn_about_arch_incompatible_libraries.go +++ b/src/arduino.cc/builder/warn_about_arch_incompatible_libraries.go @@ -31,24 +31,21 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" - "arduino.cc/builder/props" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "os" "strings" ) type WarnAboutArchIncompatibleLibraries struct{} -func (s *WarnAboutArchIncompatibleLibraries) Run(context map[string]interface{}) error { - if utils.DebugLevel(context) <= 0 { +func (s *WarnAboutArchIncompatibleLibraries) Run(ctx *types.Context) error { + if ctx.DebugLevel < 0 { return nil } - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) - buildProperties := context[constants.CTX_BUILD_PROPERTIES].(props.PropertiesMap) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + targetPlatform := ctx.TargetPlatform + buildProperties := ctx.BuildProperties + logger := ctx.GetLogger() archs := []string{} archs = append(archs, targetPlatform.PlatformId) @@ -60,8 +57,7 @@ func (s *WarnAboutArchIncompatibleLibraries) Run(context map[string]interface{}) } } - importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) - for _, importedLibrary := range importedLibraries { + for _, importedLibrary := range ctx.ImportedLibraries { if !importedLibrary.SupportsArchitectures(archs) { logger.Fprintln(os.Stdout, constants.LOG_LEVEL_WARN, constants.MSG_LIBRARY_INCOMPATIBLE_ARCH, importedLibrary.Name, importedLibrary.Archs, archs) } diff --git a/src/arduino.cc/builder/warn_about_platform_rewrites.go b/src/arduino.cc/builder/warn_about_platform_rewrites.go index a6be00ab..123057a7 100644 --- a/src/arduino.cc/builder/warn_about_platform_rewrites.go +++ b/src/arduino.cc/builder/warn_about_platform_rewrites.go @@ -31,24 +31,21 @@ package builder import ( "arduino.cc/builder/constants" - "arduino.cc/builder/i18n" "arduino.cc/builder/types" - "arduino.cc/builder/utils" "os" ) type WarnAboutPlatformRewrites struct{} -func (s *WarnAboutPlatformRewrites) Run(context map[string]interface{}) error { - warn := utils.DebugLevel(context) > 0 - if !warn { +func (s *WarnAboutPlatformRewrites) Run(ctx *types.Context) error { + if ctx.DebugLevel < 0 { return nil } - logger := context[constants.CTX_LOGGER].(i18n.Logger) - hardwareRewriteResults := context[constants.CTX_HARDWARE_REWRITE_RESULTS].(map[*types.Platform][]types.PlatforKeyRewrite) - targetPlatform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform) - actualPlatform := context[constants.CTX_ACTUAL_PLATFORM].(*types.Platform) + logger := ctx.GetLogger() + hardwareRewriteResults := ctx.HardwareRewriteResults + targetPlatform := ctx.TargetPlatform + actualPlatform := ctx.ActualPlatform platforms := []*types.Platform{targetPlatform} if actualPlatform != targetPlatform { diff --git a/src/arduino.cc/builder/wipeout_build_path_if_build_options_changed.go b/src/arduino.cc/builder/wipeout_build_path_if_build_options_changed.go index a974164e..e60d6472 100644 --- a/src/arduino.cc/builder/wipeout_build_path_if_build_options_changed.go +++ b/src/arduino.cc/builder/wipeout_build_path_if_build_options_changed.go @@ -33,7 +33,7 @@ import ( "arduino.cc/builder/constants" "arduino.cc/builder/gohasissues" "arduino.cc/builder/i18n" - "arduino.cc/builder/utils" + "arduino.cc/builder/types" "os" "path/filepath" "regexp" @@ -41,19 +41,19 @@ import ( type WipeoutBuildPathIfBuildOptionsChanged struct{} -func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(context map[string]interface{}) error { - if !utils.MapHas(context, constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON) { +func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error { + if ctx.BuildOptionsJsonPrevious == "" { return nil } - buildOptionsJson := context[constants.CTX_BUILD_OPTIONS_JSON].(string) - previousBuildOptionsJson := context[constants.CTX_BUILD_OPTIONS_PREVIOUS_JSON].(string) - logger := context[constants.CTX_LOGGER].(i18n.Logger) + buildOptionsJson := ctx.BuildOptionsJson + previousBuildOptionsJson := ctx.BuildOptionsJsonPrevious + logger := ctx.GetLogger() if buildOptionsJson == previousBuildOptionsJson { return nil } - re := regexp.MustCompile("(?m)^.*" + constants.CTX_SKETCH_LOCATION + ".*$[\r\n]+") + re := regexp.MustCompile("(?m)^.*" + ctx.SketchLocation + ".*$[\r\n]+") buildOptionsJson = re.ReplaceAllString(buildOptionsJson, "") previousBuildOptionsJson = re.ReplaceAllString(previousBuildOptionsJson, "") @@ -64,10 +64,10 @@ func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(context map[string]interface logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_BUILD_OPTIONS_CHANGED) - buildPath := context[constants.CTX_BUILD_PATH].(string) + buildPath := ctx.BuildPath files, err := gohasissues.ReadDir(buildPath) if err != nil { - return utils.WrapError(err) + return i18n.WrapError(err) } for _, file := range files { os.RemoveAll(filepath.Join(buildPath, file.Name()))