Skip to content

Commit d2aea4a

Browse files
committed
Do not store sketch build-path but generate it on demand
1 parent 03901e9 commit d2aea4a

File tree

8 files changed

+20
-30
lines changed

8 files changed

+20
-30
lines changed

arduino/sketch/sketch.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ import (
3232
type Sketch struct {
3333
Name string
3434
MainFile *paths.Path
35-
FullPath *paths.Path // FullPath is the path to the Sketch folder
36-
BuildPath *paths.Path
35+
FullPath *paths.Path // FullPath is the path to the Sketch folder
3736
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
3837
AdditionalFiles paths.PathList
3938
RootFolderFiles paths.PathList // All files that are in the Sketch root
@@ -81,7 +80,6 @@ func New(path *paths.Path) (*Sketch, error) {
8180
Name: path.Base(),
8281
MainFile: mainFile,
8382
FullPath: path,
84-
BuildPath: GenBuildPath(path),
8583
OtherSketchFiles: paths.PathList{},
8684
AdditionalFiles: paths.PathList{},
8785
RootFolderFiles: paths.PathList{},
@@ -293,14 +291,15 @@ func CheckForPdeFiles(sketch *paths.Path) []*paths.Path {
293291
return files
294292
}
295293

296-
// GenBuildPath generates a suitable name for the build folder.
297-
// The sketchPath, if not nil, is also used to furhter differentiate build paths.
298-
func GenBuildPath(sketchPath *paths.Path) *paths.Path {
299-
path := ""
300-
if sketchPath != nil {
301-
path = sketchPath.String()
302-
}
294+
// DefaultBuildPath generates the default build directory for a given sketch.
295+
// The build path is in a temporary directory and is unique for each sketch.
296+
func (s *Sketch) DefaultBuildPath() *paths.Path {
297+
return paths.TempDir().Join("arduino", "sketch-"+s.Hash())
298+
}
299+
300+
// Hash generate a unique hash for the given sketch.
301+
func (s *Sketch) Hash() string {
302+
path := s.FullPath.String()
303303
md5SumBytes := md5.Sum([]byte(path))
304-
md5Sum := strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))
305-
return paths.TempDir().Join("arduino", "sketch-"+md5Sum)
304+
return strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))
306305
}

arduino/sketch/sketch_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,8 @@ func TestNewSketchFolderSymlink(t *testing.T) {
287287

288288
func TestGenBuildPath(t *testing.T) {
289289
want := paths.TempDir().Join("arduino", "sketch-ACBD18DB4CC2F85CEDEF654FCCC4A4D8")
290-
assert.True(t, GenBuildPath(paths.New("foo")).EquivalentTo(want))
291-
292-
want = paths.TempDir().Join("arduino", "sketch-D41D8CD98F00B204E9800998ECF8427E")
293-
assert.True(t, GenBuildPath(nil).EquivalentTo(want))
290+
assert.True(t, (&Sketch{FullPath: paths.New("foo")}).DefaultBuildPath().EquivalentTo(want))
291+
assert.Equal(t, "ACBD18DB4CC2F85CEDEF654FCCC4A4D8", (&Sketch{FullPath: paths.New("foo")}).Hash())
294292
}
295293

296294
func TestCheckForPdeFiles(t *testing.T) {

commands/compile/compile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
128128
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
129129
builderCtx.LibraryDirs = paths.NewPathList(req.Library...)
130130
if req.GetBuildPath() == "" {
131-
builderCtx.BuildPath = sk.BuildPath
131+
builderCtx.BuildPath = sk.DefaultBuildPath()
132132
} else {
133133
builderCtx.BuildPath = paths.New(req.GetBuildPath()).Canonical()
134134
}

commands/debug/debug_info.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ func getDebugProperties(req *debug.DebugConfigRequest, pme *packagemanager.Explo
115115
}
116116
}
117117

118-
importPath := sk.BuildPath
118+
var importPath *paths.Path
119119
if importDir := req.GetImportDir(); importDir != "" {
120120
importPath = paths.New(importDir)
121+
} else {
122+
importPath = sk.DefaultBuildPath()
121123
}
122124
if !importPath.Exist() {
123125
return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}

commands/upload/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sk *sketch.Sk
575575

576576
// Case 4: only sketch specified. In this case we use the generated build path
577577
// and the given sketch name.
578-
return sk.BuildPath, sk.Name + sk.MainFile.Ext(), nil
578+
return sk.DefaultBuildPath(), sk.Name + sk.MainFile.Ext(), nil
579579
}
580580

581581
func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {

commands/upload/upload_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
7979
// 03: error: used both importPath and importFile
8080
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", ""},
8181
// 04: only sketch without FQBN
82-
{"", "", blonk, nil, sketch.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"},
82+
{"", "", blonk, nil, blonk.DefaultBuildPath().String(), "Blonk.ino"},
8383
// 05: use importFile to detect build.path and project_name, sketch is ignored.
8484
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino"},
8585
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
@@ -95,7 +95,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
9595
// 11: error: used both importPath and importFile
9696
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", ""},
9797
// 12: use sketch to determine project name and sketch+fqbn to determine build path
98-
{"", "", blonk, fqbn, sketch.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"},
98+
{"", "", blonk, fqbn, blonk.DefaultBuildPath().String(), "Blonk.ino"},
9999
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
100100
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
101101
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn

legacy/builder/builder.go

-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"reflect"
2020
"time"
2121

22-
"github.com/arduino/arduino-cli/arduino/sketch"
2322
"github.com/arduino/arduino-cli/i18n"
2423
"github.com/arduino/arduino-cli/legacy/builder/phases"
2524
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -129,10 +128,6 @@ func (s *PreprocessSketch) Run(ctx *types.Context) error {
129128
type Preprocess struct{}
130129

131130
func (s *Preprocess) Run(ctx *types.Context) error {
132-
if ctx.BuildPath == nil {
133-
ctx.BuildPath = sketch.GenBuildPath(ctx.SketchLocation)
134-
}
135-
136131
if err := ctx.BuildPath.MkdirAll(); err != nil {
137132
return err
138133
}
@@ -188,9 +183,6 @@ func RunBuilder(ctx *types.Context) error {
188183
}
189184

190185
func RunParseHardware(ctx *types.Context) error {
191-
if ctx.BuildPath == nil {
192-
ctx.BuildPath = sketch.GenBuildPath(ctx.SketchLocation)
193-
}
194186
commands := []types.Command{
195187
&ContainerSetupHardwareToolsLibsSketchAndProps{},
196188
}

legacy/builder/container_setup.go

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
6060
if err != nil {
6161
return errors.WithStack(err)
6262
}
63-
sketch.BuildPath = ctx.BuildPath
6463
ctx.SketchLocation = sketch.MainFile
6564
ctx.Sketch = sketch
6665
}

0 commit comments

Comments
 (0)