Skip to content

Commit 1ea7518

Browse files
authored
Added --clean flag in 'compile' command (#1019)
1 parent 27ec121 commit 1ea7518

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

Diff for: cli/compile/compile.go

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var (
5151
libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
5252
optimizeForDebug bool // Optimize compile output for debug, not for release
5353
programmer string // Use the specified programmer to upload
54+
clean bool // Cleanup the build folder and do not use any cached build
5455
)
5556

5657
// NewCommand created a new `compile` command
@@ -86,6 +87,7 @@ func NewCommand() *cobra.Command {
8687
"List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.")
8788
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")
8889
command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.")
90+
command.Flags().BoolVar(&clean, "clean", false, "Optional, cleanup the build folder and do not use any cached build.")
8991

9092
return command
9193
}
@@ -121,6 +123,7 @@ func run(cmd *cobra.Command, args []string) {
121123
DryRun: dryRun,
122124
Libraries: libraries,
123125
OptimizeForDebug: optimizeForDebug,
126+
Clean: clean,
124127
}, os.Stdout, os.Stderr, viper.GetString("logging.level") == "debug")
125128

126129
if err != nil {

Diff for: commands/compile/compile.go

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
5959
"exportDir": telemetry.Sanitize(req.GetExportDir()),
6060
"jobs": strconv.FormatInt(int64(req.Jobs), 10),
6161
"libraries": strings.Join(req.Libraries, ","),
62+
"clean": strconv.FormatBool(req.GetClean()),
6263
}
6364

6465
if req.GetExportFile() != "" {
@@ -188,6 +189,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
188189
builderCtx.ExecStdout = outStream
189190
builderCtx.ExecStderr = errStream
190191
builderCtx.SetLogger(i18n.LoggerToCustomStreams{Stdout: outStream, Stderr: errStream})
192+
builderCtx.Clean = req.GetClean()
191193

192194
// if --preprocess or --show-properties were passed, we can stop here
193195
if req.GetShowProperties() {

Diff for: legacy/builder/phases/core_builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
9494
archivedCoreName := GetCachedCoreArchiveFileName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN),
9595
buildProperties.Get("compiler.optimization_flags"), realCoreFolder)
9696
targetArchivedCore = buildCachePath.Join(archivedCoreName)
97-
canUseArchivedCore := !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore)
97+
canUseArchivedCore := !ctx.Clean && !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore)
9898

9999
if canUseArchivedCore {
100100
// use archived core

Diff for: legacy/builder/types/context.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Context struct {
7070
ArduinoAPIVersion string
7171
FQBN *cores.FQBN
7272
CodeCompleteAt string
73+
Clean bool
7374

7475
// Build options are serialized here
7576
BuildOptionsJson string

Diff for: legacy/builder/wipeout_build_path_if_build_options_changed.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ import (
2121

2222
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
2323
"github.com/arduino/arduino-cli/legacy/builder/constants"
24-
"github.com/arduino/arduino-cli/legacy/builder/gohasissues"
2524
"github.com/arduino/arduino-cli/legacy/builder/types"
25+
"github.com/arduino/go-paths-helper"
2626
properties "github.com/arduino/go-properties-orderedmap"
2727
"github.com/pkg/errors"
2828
)
2929

3030
type WipeoutBuildPathIfBuildOptionsChanged struct{}
3131

3232
func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error {
33+
if ctx.Clean {
34+
return doCleanup(ctx.BuildPath)
35+
}
3336
if ctx.BuildOptionsJsonPrevious == "" {
3437
return nil
3538
}
@@ -64,18 +67,22 @@ func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error {
6467
}
6568
}
6669

70+
return doCleanup(ctx.BuildPath)
71+
}
72+
73+
func doCleanup(buildPath *paths.Path) error {
6774
// FIXME: this should go outside legacy and behind a `logrus` call so users can
6875
// control when this should be printed.
6976
// logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_BUILD_OPTIONS_CHANGED)
7077

71-
buildPath := ctx.BuildPath
72-
files, err := gohasissues.ReadDir(buildPath.String())
73-
if err != nil {
74-
return errors.WithStack(err)
75-
}
76-
for _, file := range files {
77-
buildPath.Join(file.Name()).RemoveAll()
78+
if files, err := buildPath.ReadDir(); err != nil {
79+
return errors.WithMessage(err, "cleaning build path")
80+
} else {
81+
for _, file := range files {
82+
if err := file.RemoveAll(); err != nil {
83+
return errors.WithMessage(err, "cleaning build path")
84+
}
85+
}
7886
}
79-
8087
return nil
8188
}

Diff for: rpc/commands/compile.pb.go

+20-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/commands/compile.proto

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ message CompileReq {
4040
bool optimizeForDebug = 16; // Optimize compile output for debug, not for release.
4141
bool dryRun = 17; // When set to `true` the compiled binary will not be copied to the export directory.
4242
string export_dir = 18; // Optional: save the build artifacts in this directory, the directory must exist.
43+
bool clean = 19; // Optional: cleanup the build folder and do not use any previously cached build
4344
}
4445

4546
message CompileResp {

0 commit comments

Comments
 (0)