Skip to content

Commit 0b72f7b

Browse files
author
rsora
committed
add build-path fallback POC in upload
1 parent c076ad3 commit 0b72f7b

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

commands/upload/upload.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/arduino/arduino-cli/arduino/builder"
2829
"github.com/arduino/arduino-cli/arduino/cores"
2930
"github.com/arduino/arduino-cli/arduino/sketches"
3031
"github.com/arduino/arduino-cli/cli/feedback"
@@ -156,6 +157,7 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
156157

157158
var importPath *paths.Path
158159
var importFile string
160+
// If no importFile is passed, use sketch path
159161
if req.GetImportFile() == "" {
160162
importPath = sketch.FullPath
161163
importFile = sketch.Name + "." + fqbnSuffix
@@ -169,19 +171,52 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
169171
if !ok {
170172
return nil, fmt.Errorf("property 'recipe.output.tmp_file' not defined")
171173
}
174+
172175
ext := filepath.Ext(outputTmpFile)
173176
if strings.HasSuffix(importFile, ext) {
174177
importFile = importFile[:len(importFile)-len(ext)]
175178
}
176179

180+
// Check if the file ext we calculate is the same that is needed by the upload recipe
181+
recipet := uploadProperties.Get("upload.pattern")
182+
cmdLinet := uploadProperties.ExpandPropsInString(recipet)
183+
cmdArgst, err := properties.SplitQuotedString(cmdLinet, `"'`, false)
184+
var tPath *paths.Path
185+
if err != nil {
186+
return nil, fmt.Errorf("invalid recipe '%s': %s", recipet, err)
187+
}
188+
for _, t := range cmdArgst {
189+
if strings.Contains(t, "build.project_name") {
190+
tPath = paths.New(t)
191+
}
192+
}
193+
194+
if ext != tPath.Ext() {
195+
ext = tPath.Ext()
196+
}
197+
//uploadRecipeInputFileExt :=
177198
uploadProperties.SetPath("build.path", importPath)
178199
uploadProperties.Set("build.project_name", importFile)
179200
uploadFile := importPath.Join(importFile + ext)
180201
if _, err := uploadFile.Stat(); err != nil {
181-
if os.IsNotExist(err) {
182-
return nil, fmt.Errorf("compiled sketch %s not found", uploadFile.String())
202+
if !os.IsNotExist(err) {
203+
return nil, fmt.Errorf("cannot open sketch: %s", err)
183204
}
184-
return nil, fmt.Errorf("cannot open sketch: %s", err)
205+
// Built sketch not found in the provided path, let's fallback to the temp compile path
206+
fallbackBuildPath := builder.GenBuildPath(sketchPath)
207+
logrus.Warnf("Built sketch not found in %s, let's fallback to %s", uploadFile, fallbackBuildPath)
208+
uploadProperties.SetPath("build.path", fallbackBuildPath)
209+
// If we search inside the build.path, compile artifact do not have the fqbnSuffix in the filename
210+
uploadFile = fallbackBuildPath.Join(sketch.Name + ".ino" + ext)
211+
if _, err := uploadFile.Stat(); err != nil {
212+
if os.IsNotExist(err) {
213+
return nil, fmt.Errorf("compiled sketch %s not found", uploadFile.String())
214+
}
215+
return nil, fmt.Errorf("cannot open sketch: %s", err)
216+
}
217+
// Clean from extension
218+
uploadProperties.Set("build.project_name", sketch.Name+".ino")
219+
185220
}
186221

187222
// Perform reset via 1200bps touch if requested

0 commit comments

Comments
 (0)