Skip to content

Commit 6321095

Browse files
refactor WipeoutBuildPathIfBuildOptionsChanged in a function
1 parent 8512ca3 commit 6321095

3 files changed

+43
-40
lines changed

Diff for: legacy/builder/container_build_options.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ func (s *ContainerBuildOptions) Run(ctx *types.Context) error {
3939
}
4040
ctx.BuildOptionsJsonPrevious = buildOptionsJsonPrevious
4141

42-
commands := []types.Command{&WipeoutBuildPathIfBuildOptionsChanged{}}
43-
for _, command := range commands {
44-
PrintRingNameIfDebug(ctx, command)
45-
err := command.Run(ctx)
46-
if err != nil {
47-
return errors.WithStack(err)
48-
}
42+
infoOut, err := WipeoutBuildPathIfBuildOptionsChanged(
43+
ctx.Clean,
44+
ctx.BuildPath,
45+
ctx.BuildOptionsJson,
46+
ctx.BuildOptionsJsonPrevious,
47+
ctx.BuildProperties,
48+
)
49+
if err != nil {
50+
return errors.WithStack(err)
51+
}
52+
if infoOut != "" {
53+
ctx.Info(infoOut)
4954
}
5055

5156
return StoreBuildOptionsMap(ctx.BuildPath, ctx.BuildOptionsJson)

Diff for: legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) {
3434

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

37-
commands := []types.Command{
38-
&builder.WipeoutBuildPathIfBuildOptionsChanged{},
39-
}
40-
41-
for _, command := range commands {
42-
err := command.Run(ctx)
43-
require.NoError(t, err)
44-
}
37+
_, err := builder.WipeoutBuildPathIfBuildOptionsChanged(
38+
ctx.Clean,
39+
ctx.BuildPath,
40+
ctx.BuildOptionsJson,
41+
ctx.BuildOptionsJsonPrevious,
42+
ctx.BuildProperties,
43+
)
44+
require.NoError(t, err)
4545

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

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

69-
commands := []types.Command{
70-
&builder.WipeoutBuildPathIfBuildOptionsChanged{},
71-
}
72-
73-
for _, command := range commands {
74-
err := command.Run(ctx)
75-
require.NoError(t, err)
76-
}
69+
_, err := builder.WipeoutBuildPathIfBuildOptionsChanged(
70+
ctx.Clean,
71+
ctx.BuildPath,
72+
ctx.BuildOptionsJson,
73+
ctx.BuildOptionsJsonPrevious,
74+
ctx.BuildProperties,
75+
)
76+
require.NoError(t, err)
7777

7878
exist, err := buildPath.ExistCheck()
7979
require.NoError(t, err)

Diff for: legacy/builder/wipeout_build_path_if_build_options_changed.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,32 @@ import (
2121

2222
"github.com/arduino/arduino-cli/arduino/builder/utils"
2323
"github.com/arduino/arduino-cli/legacy/builder/constants"
24-
"github.com/arduino/arduino-cli/legacy/builder/types"
2524
"github.com/arduino/go-paths-helper"
2625
properties "github.com/arduino/go-properties-orderedmap"
2726
"github.com/pkg/errors"
2827
)
2928

30-
type WipeoutBuildPathIfBuildOptionsChanged struct{}
31-
32-
func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error {
33-
if ctx.Clean {
34-
return doCleanup(ctx.BuildPath)
29+
func WipeoutBuildPathIfBuildOptionsChanged(
30+
clean bool,
31+
buildPath *paths.Path,
32+
buildOptionsJson, buildOptionsJsonPrevious string,
33+
buildProperties *properties.Map,
34+
) (string, error) {
35+
if clean {
36+
return "", doCleanup(buildPath)
3537
}
36-
if ctx.BuildOptionsJsonPrevious == "" {
37-
return nil
38+
if buildOptionsJsonPrevious == "" {
39+
return "", nil
3840
}
39-
buildOptionsJson := ctx.BuildOptionsJson
40-
previousBuildOptionsJson := ctx.BuildOptionsJsonPrevious
4141

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

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

5352
// If SketchLocation path is different but filename is the same, consider it equal
@@ -61,21 +60,20 @@ func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error {
6160
// check if any of the files contained in the core folders has changed
6261
// since the json was generated - like platform.txt or similar
6362
// if so, trigger a "safety" wipe
64-
buildProperties := ctx.BuildProperties
6563
targetCoreFolder := buildProperties.GetPath("runtime.platform.path")
6664
coreFolder := buildProperties.GetPath("build.core.path")
6765
realCoreFolder := coreFolder.Parent().Parent()
68-
jsonPath := ctx.BuildPath.Join(constants.BUILD_OPTIONS_FILE)
66+
jsonPath := buildPath.Join(constants.BUILD_OPTIONS_FILE)
6967
coreUnchanged, _ := utils.DirContentIsOlderThan(realCoreFolder, jsonPath, ".txt")
7068
if coreUnchanged && targetCoreFolder != nil && !realCoreFolder.EqualsTo(targetCoreFolder) {
7169
coreUnchanged, _ = utils.DirContentIsOlderThan(targetCoreFolder, jsonPath, ".txt")
7270
}
7371
if coreUnchanged {
74-
return nil
72+
return "", nil
7573
}
7674
}
7775

78-
return doCleanup(ctx.BuildPath)
76+
return "", doCleanup(buildPath)
7977
}
8078

8179
func doCleanup(buildPath *paths.Path) error {

0 commit comments

Comments
 (0)