Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b1a8b93

Browse files
author
rsora
committedApr 9, 2020
Refactor smart built sketch search
1 parent 7289221 commit b1a8b93

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed
 

‎commands/upload/upload.go

+62-52
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"fmt"
2121
"io"
2222
"net/url"
23-
"os"
24-
"path/filepath"
2523
"strings"
2624
"time"
2725

@@ -150,6 +148,18 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
150148
uploadProperties.Set("upload.verify", uploadProperties.Get("upload.params.noverify"))
151149
}
152150

151+
// Extract the final file extension from the upload recipe
152+
ext, err := getExtensionFromRecipe(uploadProperties, err)
153+
if err != nil {
154+
return nil, fmt.Errorf("recipe parsing unsuccessful: %s", err)
155+
}
156+
157+
// Start smart fetch process for the built sketch
158+
var uploadPaths []*paths.Path
159+
160+
// Search for built sketch to upload in CLI param importFile (if passed)...
161+
// ...or in the Export path (the Sketch Path)
162+
153163
// Set path to compiled binary
154164
// Make the filename without the FQBN configs part
155165
fqbn.Configs = properties.NewMap()
@@ -166,64 +176,42 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
166176
importFile = paths.New(req.GetImportFile()).Base()
167177
}
168178

169-
outputTmpFile, ok := uploadProperties.GetOk("recipe.output.tmp_file")
170-
outputTmpFile = uploadProperties.ExpandPropsInString(outputTmpFile)
171-
if !ok {
172-
return nil, fmt.Errorf("property 'recipe.output.tmp_file' not defined")
179+
// Remove file extension if any from input
180+
importFileExt := paths.New(importFile).Ext()
181+
if strings.HasSuffix(importFile, importFileExt) {
182+
importFile = importFile[:len(importFile)-len(importFileExt)]
173183
}
174184

175-
ext := filepath.Ext(outputTmpFile)
176-
if strings.HasSuffix(importFile, ext) {
177-
importFile = importFile[:len(importFile)-len(ext)]
178-
}
179-
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-
}
185+
uploadPaths = append(uploadPaths, importPath.Join(importFile+ext))
193186

194-
if ext != tPath.Ext() {
195-
ext = tPath.Ext()
187+
// Try fetch the file to upload using CLI param buildPath
188+
if req.GetBuildPath() != "" {
189+
buildPath := paths.New(req.GetBuildPath())
190+
// If we search inside the build.path, compile artifact do not have the fqbnSuffix in the filename
191+
uploadPaths = append(uploadPaths, buildPath.Join(sketch.Name+".ino"+ext))
196192
}
197-
//uploadRecipeInputFileExt :=
198-
uploadProperties.SetPath("build.path", importPath)
199-
uploadProperties.Set("build.project_name", importFile)
200-
uploadFile := importPath.Join(importFile + ext)
201-
if _, err := uploadFile.Stat(); err != nil {
202-
if !os.IsNotExist(err) {
203-
return nil, fmt.Errorf("cannot open sketch: %s", err)
204-
}
205-
// Built sketch not found in the provided path, let's fallback to the temp compile path
206-
var fallbackBuildPath *paths.Path
207-
if req.GetBuildPath() != "" {
208-
fallbackBuildPath = paths.New(req.GetBuildPath())
209-
} else {
210193

211-
fallbackBuildPath = builder.GenBuildPath(sketchPath)
212-
}
194+
// Try fetch the file to upload using builder.GenBuildPath(sketchPath)
195+
fallbackBuildPath := builder.GenBuildPath(sketchPath)
196+
// If we search inside the build.path, compile artifact do not have the fqbnSuffix in the filename
197+
uploadPaths = append(uploadPaths, fallbackBuildPath.Join(sketch.Name+".ino"+ext))
198+
for _, p := range uploadPaths {
199+
if _, err := p.Stat(); err == nil {
200+
uploadProperties.SetPath("build.path", p.Parent())
201+
name := p.Base()
202+
name = name[:len(name)-len(ext)]
203+
uploadProperties.Set("build.project_name", name)
213204

214-
logrus.Warnf("Built sketch not found in %s, let's fallback to %s", uploadFile, fallbackBuildPath)
215-
uploadProperties.SetPath("build.path", fallbackBuildPath)
216-
// If we search inside the build.path, compile artifact do not have the fqbnSuffix in the filename
217-
uploadFile = fallbackBuildPath.Join(sketch.Name + ".ino" + ext)
218-
if _, err := uploadFile.Stat(); err != nil {
219-
if os.IsNotExist(err) {
220-
return nil, fmt.Errorf("compiled sketch %s not found", uploadFile.String())
221-
}
222-
return nil, fmt.Errorf("cannot open sketch: %s", err)
205+
} else {
206+
logrus.Warnf("Built sketch opening error in %s: %s", p, err)
223207
}
224-
// Clean from extension
225-
uploadProperties.Set("build.project_name", sketch.Name+".ino")
208+
}
226209

210+
if n, nOK := uploadProperties.GetOk("build.project_name"); n == "" || !nOK {
211+
return nil, fmt.Errorf("cannot find file to upload")
212+
}
213+
if p, pOK := uploadProperties.GetOk("build.path"); p == "" || !pOK {
214+
return nil, fmt.Errorf("cannot find file to upload")
227215
}
228216

229217
// Perform reset via 1200bps touch if requested
@@ -305,6 +293,28 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
305293
return &rpc.UploadResp{}, nil
306294
}
307295

296+
func getExtensionFromRecipe(uploadProperties *properties.Map, err error) (string, error) {
297+
recipe := uploadProperties.Get("upload.pattern")
298+
cmdLine := uploadProperties.ExpandPropsInString(recipe)
299+
cmdArgs, err := properties.SplitQuotedString(cmdLine, `"'`, false)
300+
if err != nil {
301+
return "", err
302+
}
303+
304+
var uploadInputPath *paths.Path
305+
for _, t := range cmdArgs {
306+
if strings.Contains(t, "build.project_name") {
307+
uploadInputPath = paths.New(t)
308+
}
309+
}
310+
311+
if uploadInputPath == nil {
312+
return "", fmt.Errorf("cannot find upload file extension in upload recipe")
313+
}
314+
315+
return uploadInputPath.Ext(), nil
316+
}
317+
308318
func touchSerialPortAt1200bps(port string) error {
309319
logrus.Infof("Touching port %s at 1200bps", port)
310320

0 commit comments

Comments
 (0)
Please sign in to comment.