From 7fc0034b790eb94b3b8ee12a3d7dcc6a3e98a84b Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Fri, 8 Sep 2023 15:46:15 +0200 Subject: [PATCH] refactor ContainerBuildOptions in a function --- legacy/builder/builder.go | 27 ++++++++++++- legacy/builder/container_build_options.go | 47 ++++++++++++----------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 5f9c29a3e1c..93b8f2df70c 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -41,7 +41,7 @@ func (s *Builder) Run(ctx *types.Context) error { var _err, mainErr error commands := []types.Command{ - &ContainerBuildOptions{}, + containerBuildOptions(ctx), types.BareCommand(func(ctx *types.Context) error { return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.prebuild", ".pattern", false) @@ -286,7 +286,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { var _err error commands := []types.Command{ - &ContainerBuildOptions{}, + containerBuildOptions(ctx), types.BareCommand(func(ctx *types.Context) error { return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.prebuild", ".pattern", false) @@ -383,3 +383,26 @@ func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipI func(msg string) { ctx.Info(msg) }, ) } + +func containerBuildOptions(ctx *types.Context) types.BareCommand { + return types.BareCommand(func(ctx *types.Context) error { + // TODO here we can pass only the properties we're reading from the + // ctx.BuildProperties + buildOptionsJSON, buildOptionsJSONPrevious, infoMessage, err := ContainerBuildOptions( + ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, + ctx.BuiltInLibrariesDirs, ctx.BuildPath, ctx.Sketch, ctx.CustomBuildProperties, + ctx.FQBN.String(), ctx.Clean, ctx.BuildProperties, + ) + if infoMessage != "" { + ctx.Info(infoMessage) + } + if err != nil { + return err + } + + ctx.BuildOptionsJson = buildOptionsJSON + ctx.BuildOptionsJsonPrevious = buildOptionsJSONPrevious + + return nil + }) +} diff --git a/legacy/builder/container_build_options.go b/legacy/builder/container_build_options.go index b71a6ef82fe..6302bc558f7 100644 --- a/legacy/builder/container_build_options.go +++ b/legacy/builder/container_build_options.go @@ -16,42 +16,45 @@ package builder import ( - "github.com/arduino/arduino-cli/legacy/builder/types" + "github.com/arduino/arduino-cli/arduino/sketch" + "github.com/arduino/go-paths-helper" + properties "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) -type ContainerBuildOptions struct{} - -func (s *ContainerBuildOptions) Run(ctx *types.Context) error { +func ContainerBuildOptions( + hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, + builtInLibrariesDirs, buildPath *paths.Path, + sketch *sketch.Sketch, + customBuildProperties []string, + fqbn string, + clean bool, + buildProperties *properties.Map, +) (string, string, string, error) { buildOptionsJSON, err := CreateBuildOptionsMap( - ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, ctx.Sketch, ctx.CustomBuildProperties, - ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"), + hardwareDirs, builtInToolsDirs, otherLibrariesDirs, + builtInLibrariesDirs, sketch, customBuildProperties, + fqbn, buildProperties.Get("compiler.optimization_flags"), ) if err != nil { - return errors.WithStack(err) + return "", "", "", errors.WithStack(err) } - ctx.BuildOptionsJson = buildOptionsJSON - buildOptionsJsonPrevious, err := LoadPreviousBuildOptionsMap(ctx.BuildPath) + buildOptionsJSONPrevious, err := LoadPreviousBuildOptionsMap(buildPath) if err != nil { - return errors.WithStack(err) + return "", "", "", errors.WithStack(err) } - ctx.BuildOptionsJsonPrevious = buildOptionsJsonPrevious infoOut, err := WipeoutBuildPathIfBuildOptionsChanged( - ctx.Clean, - ctx.BuildPath, - ctx.BuildOptionsJson, - ctx.BuildOptionsJsonPrevious, - ctx.BuildProperties, + clean, + buildPath, + buildOptionsJSON, + buildOptionsJSONPrevious, + buildProperties, ) if err != nil { - return errors.WithStack(err) - } - if infoOut != "" { - ctx.Info(infoOut) + return "", "", "", errors.WithStack(err) } - return StoreBuildOptionsMap(ctx.BuildPath, ctx.BuildOptionsJson) + return buildOptionsJSON, buildOptionsJSONPrevious, infoOut, StoreBuildOptionsMap(buildPath, buildOptionsJSON) }