Skip to content

Commit

Permalink
refactor WipeoutBuildPathIfBuildOptionsChanged 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 ceb5982 commit 7c8e73c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
19 changes: 12 additions & 7 deletions legacy/builder/container_build_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ func (s *ContainerBuildOptions) Run(ctx *types.Context) error {
}
ctx.BuildOptionsJsonPrevious = buildOptionsJsonPrevious

commands := []types.Command{&WipeoutBuildPathIfBuildOptionsChanged{}}
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
return errors.WithStack(err)
}
infoOut, err := WipeoutBuildPathIfBuildOptionsChanged(
ctx.Clean,
ctx.BuildPath,
ctx.BuildOptionsJson,
ctx.BuildOptionsJsonPrevious,
ctx.BuildProperties,
)
if err != nil {
return errors.WithStack(err)
}
if infoOut != "" {
ctx.Info(infoOut)
}

return StoreBuildOptionsMap(ctx.BuildPath, ctx.BuildOptionsJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) {

buildPath.Join("should_be_deleted.txt").Truncate()

commands := []types.Command{
&builder.WipeoutBuildPathIfBuildOptionsChanged{},
}

for _, command := range commands {
err := command.Run(ctx)
require.NoError(t, err)
}
_, err := builder.WipeoutBuildPathIfBuildOptionsChanged(
ctx.Clean,
ctx.BuildPath,
ctx.BuildOptionsJson,
ctx.BuildOptionsJsonPrevious,
ctx.BuildProperties,
)
require.NoError(t, err)

exist, err := buildPath.ExistCheck()
require.NoError(t, err)
Expand All @@ -66,14 +66,14 @@ func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing.

require.NoError(t, buildPath.Join("should_not_be_deleted.txt").Truncate())

commands := []types.Command{
&builder.WipeoutBuildPathIfBuildOptionsChanged{},
}

for _, command := range commands {
err := command.Run(ctx)
require.NoError(t, err)
}
_, err := builder.WipeoutBuildPathIfBuildOptionsChanged(
ctx.Clean,
ctx.BuildPath,
ctx.BuildOptionsJson,
ctx.BuildOptionsJsonPrevious,
ctx.BuildProperties,
)
require.NoError(t, err)

exist, err := buildPath.ExistCheck()
require.NoError(t, err)
Expand Down
32 changes: 15 additions & 17 deletions legacy/builder/wipeout_build_path_if_build_options_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,32 @@ import (

"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)

type WipeoutBuildPathIfBuildOptionsChanged struct{}

func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error {
if ctx.Clean {
return doCleanup(ctx.BuildPath)
func WipeoutBuildPathIfBuildOptionsChanged(
clean bool,
buildPath *paths.Path,
buildOptionsJson, buildOptionsJsonPrevious string,
buildProperties *properties.Map,
) (string, error) {
if clean {
return "", doCleanup(buildPath)
}
if ctx.BuildOptionsJsonPrevious == "" {
return nil
if buildOptionsJsonPrevious == "" {
return "", nil
}
buildOptionsJson := ctx.BuildOptionsJson
previousBuildOptionsJson := ctx.BuildOptionsJsonPrevious

var opts *properties.Map
if err := json.Unmarshal([]byte(buildOptionsJson), &opts); err != nil || opts == nil {
panic(constants.BUILD_OPTIONS_FILE + " is invalid")
}

var prevOpts *properties.Map
if err := json.Unmarshal([]byte(previousBuildOptionsJson), &prevOpts); err != nil || prevOpts == nil {
ctx.Info(tr("%[1]s invalid, rebuilding all", constants.BUILD_OPTIONS_FILE))
return doCleanup(ctx.BuildPath)
if err := json.Unmarshal([]byte(buildOptionsJsonPrevious), &prevOpts); err != nil || prevOpts == nil {
return tr("%[1]s invalid, rebuilding all", constants.BUILD_OPTIONS_FILE), doCleanup(buildPath)
}

// If SketchLocation path is different but filename is the same, consider it equal
Expand All @@ -61,21 +60,20 @@ func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error {
// check if any of the files contained in the core folders has changed
// since the json was generated - like platform.txt or similar
// if so, trigger a "safety" wipe
buildProperties := ctx.BuildProperties
targetCoreFolder := buildProperties.GetPath(constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH)
coreFolder := buildProperties.GetPath("build.core.path")
realCoreFolder := coreFolder.Parent().Parent()
jsonPath := ctx.BuildPath.Join(constants.BUILD_OPTIONS_FILE)
jsonPath := buildPath.Join(constants.BUILD_OPTIONS_FILE)
coreUnchanged, _ := utils.DirContentIsOlderThan(realCoreFolder, jsonPath, ".txt")
if coreUnchanged && targetCoreFolder != nil && !realCoreFolder.EqualsTo(targetCoreFolder) {
coreUnchanged, _ = utils.DirContentIsOlderThan(targetCoreFolder, jsonPath, ".txt")
}
if coreUnchanged {
return nil
return "", nil
}
}

return doCleanup(ctx.BuildPath)
return "", doCleanup(buildPath)
}

func doCleanup(buildPath *paths.Path) error {
Expand Down

0 comments on commit 7c8e73c

Please sign in to comment.