Skip to content

Commit 22f8a57

Browse files
committed
refactor: rename MainSketchPath to mainSketchPath and update related methods
1 parent 3f54506 commit 22f8a57

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

internal/orchestrator/app/app.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
type ArduinoApp struct {
3131
Name string
3232
MainPythonFile *paths.Path
33-
MainSketchPath *paths.Path
33+
mainSketchPath *paths.Path
3434
FullPath *paths.Path // FullPath is the path to the App folder
3535
Descriptor AppDescriptor
3636
}
@@ -76,10 +76,10 @@ func Load(appPath *paths.Path) (ArduinoApp, error) {
7676

7777
if appPath.Join("sketch", "sketch.ino").Exist() {
7878
// TODO: check sketch casing?
79-
app.MainSketchPath = appPath.Join("sketch")
79+
app.mainSketchPath = appPath.Join("sketch")
8080
}
8181

82-
if app.MainPythonFile == nil && app.MainSketchPath == nil {
82+
if app.MainPythonFile == nil && app.mainSketchPath == nil {
8383
return ArduinoApp{}, errors.New("main python file and sketch file missing from app")
8484
}
8585

@@ -91,6 +91,13 @@ func Load(appPath *paths.Path) (ArduinoApp, error) {
9191
return app, nil
9292
}
9393

94+
func (a *ArduinoApp) GetSketchPath() (*paths.Path, bool) {
95+
if a == nil || a.mainSketchPath == nil {
96+
return nil, false
97+
}
98+
return a.mainSketchPath, true
99+
}
100+
94101
// GetDescriptorPath returns the path to the app descriptor file (app.yaml or app.yml)
95102
func (a *ArduinoApp) GetDescriptorPath() *paths.Path {
96103
descriptorFile := a.FullPath.Join("app.yaml")

internal/orchestrator/app/app_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,22 @@ func TestLoad(t *testing.T) {
5858

5959
assert.NotNil(t, app.MainPythonFile)
6060
assert.Equal(t, f.Must(filepath.Abs("testdata/AppSimple/python/main.py")), app.MainPythonFile.String())
61+
sketchPath, ok := app.GetSketchPath()
62+
assert.True(t, ok)
63+
assert.NotNil(t, sketchPath)
64+
assert.Equal(t, f.Must(filepath.Abs("testdata/AppSimple/sketch")), sketchPath.String())
65+
})
66+
67+
t.Run("it loads an app with misssing sketch folder", func(t *testing.T) {
68+
app, err := Load(paths.New("testdata/MissingSketch"))
69+
assert.NoError(t, err)
70+
assert.NotEmpty(t, app)
71+
72+
assert.NotNil(t, app.MainPythonFile)
6173

62-
assert.NotNil(t, app.MainSketchPath)
63-
assert.Equal(t, f.Must(filepath.Abs("testdata/AppSimple/sketch")), app.MainSketchPath.String())
74+
sketchPath, ok := app.GetSketchPath()
75+
assert.False(t, ok)
76+
assert.Nil(t, sketchPath)
6477
})
6578
}
6679

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: "An app with only python"
2+
description: "An app with only python
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
print("Hello world!")

internal/orchestrator/orchestrator.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func StartApp(
154154
if !yield(StreamMessage{progress: &Progress{Name: "preparing", Progress: 0.0}}) {
155155
return
156156
}
157-
if appToStart.MainSketchPath != nil {
157+
158+
if _, ok := appToStart.GetSketchPath(); ok {
158159
if !yield(StreamMessage{progress: &Progress{Name: "sketch compiling and uploading", Progress: 0.0}}) {
159160
return
160161
}
@@ -175,7 +176,7 @@ func StartApp(
175176
return
176177
}
177178
provisionStartProgress := float32(0.0)
178-
if appToStart.MainSketchPath != nil {
179+
if _, ok := appToStart.GetSketchPath(); ok {
179180
provisionStartProgress = 10.0
180181
}
181182

@@ -402,7 +403,7 @@ func stopAppWithCmd(ctx context.Context, docker command.Cli, app app.ArduinoApp,
402403
}
403404
})
404405

405-
if app.MainSketchPath != nil {
406+
if _, ok := app.GetSketchPath(); ok {
406407
// Before stopping the microcontroller we want to make sure that the app was running.
407408
appStatus, err := getAppStatus(ctx, docker, app)
408409
if err != nil {
@@ -1162,9 +1163,12 @@ func compileUploadSketch(
11621163
defer func() {
11631164
_, _ = srv.Destroy(ctx, &rpc.DestroyRequest{Instance: inst})
11641165
}()
1165-
sketchPath := arduinoApp.MainSketchPath.String()
1166+
sketchPath, ok := arduinoApp.GetSketchPath()
1167+
if !ok {
1168+
return fmt.Errorf("no sketch path found in the Arduino app")
1169+
}
11661170
buildPath := arduinoApp.SketchBuildPath().String()
1167-
sketchResp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath})
1171+
sketchResp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
11681172
if err != nil {
11691173
return err
11701174
}
@@ -1175,7 +1179,7 @@ func compileUploadSketch(
11751179
}
11761180
initReq := &rpc.InitRequest{
11771181
Instance: inst,
1178-
SketchPath: sketchPath,
1182+
SketchPath: sketchPath.String(),
11791183
Profile: profile,
11801184
}
11811185

@@ -1215,7 +1219,7 @@ func compileUploadSketch(
12151219
compileReq := rpc.CompileRequest{
12161220
Instance: inst,
12171221
Fqbn: "arduino:zephyr:unoq",
1218-
SketchPath: sketchPath,
1222+
SketchPath: sketchPath.String(),
12191223
BuildPath: buildPath,
12201224
Jobs: 2,
12211225
}
@@ -1241,12 +1245,12 @@ func compileUploadSketch(
12411245
slog.Info("Used library " + lib.GetName() + " (" + lib.GetVersion() + ") in " + lib.GetInstallDir())
12421246
}
12431247

1244-
if err := uploadSketchInRam(ctx, w, srv, inst, sketchPath, buildPath); err != nil {
1248+
if err := uploadSketchInRam(ctx, w, srv, inst, sketchPath.String(), buildPath); err != nil {
12451249
slog.Warn("failed to upload in ram mode, trying to configure the board in ram mode, and retry", slog.String("error", err.Error()))
12461250
if err := configureMicroInRamMode(ctx, w, srv, inst); err != nil {
12471251
return err
12481252
}
1249-
return uploadSketchInRam(ctx, w, srv, inst, sketchPath, buildPath)
1253+
return uploadSketchInRam(ctx, w, srv, inst, sketchPath.String(), buildPath)
12501254
}
12511255
return nil
12521256
}

internal/orchestrator/sketch_libs.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package orchestrator
1717

1818
import (
1919
"context"
20+
"errors"
2021
"log/slog"
2122
"time"
2223

@@ -30,6 +31,11 @@ import (
3031
const indexUpdateInterval = 10 * time.Minute
3132

3233
func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID, addDeps bool) ([]LibraryReleaseID, error) {
34+
sketchPath, ok := app.GetSketchPath()
35+
if !ok {
36+
return []LibraryReleaseID{}, errors.New("cannot add a library. Missing sketch folder")
37+
}
38+
3339
srv := commands.NewArduinoCoreServer()
3440
var inst *rpc.Instance
3541
if res, err := srv.Create(ctx, &rpc.CreateRequest{}); err != nil {
@@ -58,7 +64,7 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel
5864

5965
resp, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{
6066
Instance: inst,
61-
SketchPath: app.MainSketchPath.String(),
67+
SketchPath: sketchPath.String(),
6268
Library: &rpc.SketchProfileLibraryReference{
6369
Library: &rpc.SketchProfileLibraryReference_IndexLibrary_{
6470
IndexLibrary: &rpc.SketchProfileLibraryReference_IndexLibrary{
@@ -77,6 +83,10 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel
7783
}
7884

7985
func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID) (LibraryReleaseID, error) {
86+
sketchPath, ok := app.GetSketchPath()
87+
if !ok {
88+
return LibraryReleaseID{}, errors.New("cannot remove a library. Missing sketch folder")
89+
}
8090
srv := commands.NewArduinoCoreServer()
8191
var inst *rpc.Instance
8292
if res, err := srv.Create(ctx, &rpc.CreateRequest{}); err != nil {
@@ -102,7 +112,7 @@ func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef Library
102112
},
103113
},
104114
},
105-
SketchPath: app.MainSketchPath.String(),
115+
SketchPath: sketchPath.String(),
106116
})
107117
if err != nil {
108118
return LibraryReleaseID{}, err
@@ -111,10 +121,15 @@ func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef Library
111121
}
112122

113123
func ListSketchLibraries(ctx context.Context, app app.ArduinoApp) ([]LibraryReleaseID, error) {
124+
sketchPath, ok := app.GetSketchPath()
125+
if !ok {
126+
return []LibraryReleaseID{}, errors.New("cannot list libraries. Missing sketch folder")
127+
}
128+
114129
srv := commands.NewArduinoCoreServer()
115130

116131
resp, err := srv.ProfileLibList(ctx, &rpc.ProfileLibListRequest{
117-
SketchPath: app.MainSketchPath.String(),
132+
SketchPath: sketchPath.String(),
118133
})
119134
if err != nil {
120135
return nil, err

0 commit comments

Comments
 (0)