Skip to content

Commit

Permalink
refactor ContainerBuildOptions in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 8, 2023
1 parent 6321095 commit 7fc0034
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
27 changes: 25 additions & 2 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
}
47 changes: 25 additions & 22 deletions legacy/builder/container_build_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 7fc0034

Please sign in to comment.