Skip to content

Commit e6d5328

Browse files
committed
Improved pattern matching subroutine to find multi-step recipe.
1 parent a93cccc commit e6d5328

File tree

5 files changed

+69
-34
lines changed

5 files changed

+69
-34
lines changed

commands/service_compile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
386386
exportBinaries = false
387387
}
388388
if exportBinaries {
389-
if err := sketchBuilder.RunRecipe("recipe.hooks.savehex.presavehex", ".pattern", false); err != nil {
389+
if err := sketchBuilder.RunRecipe("recipe.hooks.savehex.presavehex.pattern", false); err != nil {
390390
return err
391391
}
392392

@@ -423,7 +423,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
423423
}
424424
}
425425

426-
if err = sketchBuilder.RunRecipe("recipe.hooks.savehex.postsavehex", ".pattern", false); err != nil {
426+
if err = sketchBuilder.RunRecipe("recipe.hooks.savehex.postsavehex.pattern", false); err != nil {
427427
return err
428428
}
429429
}

internal/arduino/builder/builder.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (b *Builder) preprocess() error {
301301
}
302302
b.Progress.CompleteStep()
303303

304-
if err := b.RunRecipe("recipe.hooks.prebuild", ".pattern", false); err != nil {
304+
if err := b.RunRecipe("recipe.hooks.prebuild.pattern", false); err != nil {
305305
return err
306306
}
307307
b.Progress.CompleteStep()
@@ -382,7 +382,7 @@ func (b *Builder) Build() error {
382382
// Build fixdoc
383383
func (b *Builder) build() error {
384384
b.logIfVerbose(false, i18n.Tr("Compiling sketch..."))
385-
if err := b.RunRecipe("recipe.hooks.sketch.prebuild", ".pattern", false); err != nil {
385+
if err := b.RunRecipe("recipe.hooks.sketch.prebuild.pattern", false); err != nil {
386386
return err
387387
}
388388
b.Progress.CompleteStep()
@@ -392,13 +392,13 @@ func (b *Builder) build() error {
392392
}
393393
b.Progress.CompleteStep()
394394

395-
if err := b.RunRecipe("recipe.hooks.sketch.postbuild", ".pattern", true); err != nil {
395+
if err := b.RunRecipe("recipe.hooks.sketch.postbuild.pattern", true); err != nil {
396396
return err
397397
}
398398
b.Progress.CompleteStep()
399399

400400
b.logIfVerbose(false, i18n.Tr("Compiling libraries..."))
401-
if err := b.RunRecipe("recipe.hooks.libraries.prebuild", ".pattern", false); err != nil {
401+
if err := b.RunRecipe("recipe.hooks.libraries.prebuild.pattern", false); err != nil {
402402
return err
403403
}
404404
b.Progress.CompleteStep()
@@ -413,13 +413,13 @@ func (b *Builder) build() error {
413413
}
414414
b.Progress.CompleteStep()
415415

416-
if err := b.RunRecipe("recipe.hooks.libraries.postbuild", ".pattern", true); err != nil {
416+
if err := b.RunRecipe("recipe.hooks.libraries.postbuild.pattern", true); err != nil {
417417
return err
418418
}
419419
b.Progress.CompleteStep()
420420

421421
b.logIfVerbose(false, i18n.Tr("Compiling core..."))
422-
if err := b.RunRecipe("recipe.hooks.core.prebuild", ".pattern", false); err != nil {
422+
if err := b.RunRecipe("recipe.hooks.core.prebuild.pattern", false); err != nil {
423423
return err
424424
}
425425
b.Progress.CompleteStep()
@@ -429,13 +429,13 @@ func (b *Builder) build() error {
429429
}
430430
b.Progress.CompleteStep()
431431

432-
if err := b.RunRecipe("recipe.hooks.core.postbuild", ".pattern", true); err != nil {
432+
if err := b.RunRecipe("recipe.hooks.core.postbuild.pattern", true); err != nil {
433433
return err
434434
}
435435
b.Progress.CompleteStep()
436436

437437
b.logIfVerbose(false, i18n.Tr("Linking everything together..."))
438-
if err := b.RunRecipe("recipe.hooks.linking.prelink", ".pattern", false); err != nil {
438+
if err := b.RunRecipe("recipe.hooks.linking.prelink.pattern", false); err != nil {
439439
return err
440440
}
441441
b.Progress.CompleteStep()
@@ -445,22 +445,22 @@ func (b *Builder) build() error {
445445
}
446446
b.Progress.CompleteStep()
447447

448-
if err := b.RunRecipe("recipe.hooks.linking.postlink", ".pattern", true); err != nil {
448+
if err := b.RunRecipe("recipe.hooks.linking.postlink.pattern", true); err != nil {
449449
return err
450450
}
451451
b.Progress.CompleteStep()
452452

453-
if err := b.RunRecipe("recipe.hooks.objcopy.preobjcopy", ".pattern", false); err != nil {
453+
if err := b.RunRecipe("recipe.hooks.objcopy.preobjcopy.pattern", false); err != nil {
454454
return err
455455
}
456456
b.Progress.CompleteStep()
457457

458-
if err := b.RunRecipe("recipe.objcopy", ".pattern", true); err != nil {
458+
if err := b.RunRecipe("recipe.objcopy.pattern", true); err != nil {
459459
return err
460460
}
461461
b.Progress.CompleteStep()
462462

463-
if err := b.RunRecipe("recipe.hooks.objcopy.postobjcopy", ".pattern", true); err != nil {
463+
if err := b.RunRecipe("recipe.hooks.objcopy.postobjcopy.pattern", true); err != nil {
464464
return err
465465
}
466466
b.Progress.CompleteStep()
@@ -470,7 +470,7 @@ func (b *Builder) build() error {
470470
}
471471
b.Progress.CompleteStep()
472472

473-
if err := b.RunRecipe("recipe.hooks.postbuild", ".pattern", true); err != nil {
473+
if err := b.RunRecipe("recipe.hooks.postbuild.pattern", true); err != nil {
474474
return err
475475
}
476476
b.Progress.CompleteStep()

internal/arduino/builder/linker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ func (b *Builder) link() error {
9090
properties.Set("archive_file_path", b.buildArtifacts.coreArchiveFilePath.String())
9191
properties.Set("object_files", objectFileList)
9292

93-
return b.RunRecipeWithProps("recipe.c.combine", ".pattern", properties, false)
93+
return b.RunRecipeWithProps("recipe.c.combine.pattern", properties, false)
9494
}

internal/arduino/builder/recipe.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package builder
1717

1818
import (
1919
"sort"
20+
"strconv"
2021
"strings"
2122

2223
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
@@ -26,14 +27,14 @@ import (
2627
)
2728

2829
// RunRecipe fixdoc
29-
func (b *Builder) RunRecipe(prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase bool) error {
30-
return b.RunRecipeWithProps(prefix, suffix, b.buildProperties, skipIfOnlyUpdatingCompilationDatabase)
30+
func (b *Builder) RunRecipe(pattern string, skipIfOnlyUpdatingCompilationDatabase bool) error {
31+
return b.RunRecipeWithProps(pattern, b.buildProperties, skipIfOnlyUpdatingCompilationDatabase)
3132
}
3233

33-
func (b *Builder) RunRecipeWithProps(prefix, suffix string, buildProperties *properties.Map, skipIfOnlyUpdatingCompilationDatabase bool) error {
34-
logrus.Debugf("Looking for recipes like %s", prefix+"*"+suffix)
34+
func (b *Builder) RunRecipeWithProps(pattern string, buildProperties *properties.Map, skipIfOnlyUpdatingCompilationDatabase bool) error {
35+
logrus.Debugf("Looking for recipes like %s", pattern)
3536

36-
recipes := findRecipes(buildProperties, prefix, suffix)
37+
recipes := findRecipes(buildProperties, pattern)
3738

3839
for _, recipe := range recipes {
3940
logrus.Debugf("Running recipe: %s", recipe)
@@ -58,23 +59,47 @@ func (b *Builder) RunRecipeWithProps(prefix, suffix string, buildProperties *pro
5859
return nil
5960
}
6061

61-
func findRecipes(buildProperties *properties.Map, patternPrefix string, patternSuffix string) []string {
62+
// findRecipes extracts all the recipes counting if the given pattern is present in the numbered form.
63+
// For example, if the pattern is "upload.recipe.cmd", it will return all the recipes mathching
64+
// "upload.recipe.1.cmd", "upload.recipe.2.cmd", etc. if they exist.
65+
// Otherwise, it will return the recipe with the exact pattern "upload.recipe.cmd" if it exists.
66+
func findRecipes(buildProperties *properties.Map, pattern string) []string {
6267
var recipes []string
6368

64-
exactKey := patternPrefix + patternSuffix
69+
// Check if the numbered pattern is present
70+
if split := strings.Split(pattern, "."); len(split) >= 2 {
71+
l := len(split)
72+
prefix := strings.Join(split[:l-1], ".") + "."
73+
suffix := "." + split[l-1]
74+
75+
// Extract all the keys that match the "indexed" pattern
76+
indexedRecipes := map[int]string{}
77+
for _, key := range buildProperties.Keys() {
78+
if strings.HasPrefix(key, prefix) && strings.HasSuffix(key, suffix) && buildProperties.Get(key) != "" {
79+
// Check if the key is numbered
80+
idx := strings.TrimSuffix(strings.TrimPrefix(key, prefix), suffix)
81+
if n, err := strconv.Atoi(idx); err == nil {
82+
// Deduplicate recipes with the same index (like "05" and "5")
83+
indexedRecipes[n] = key
84+
}
85+
}
86+
}
6587

66-
for _, key := range buildProperties.Keys() {
67-
if key != exactKey && strings.HasPrefix(key, patternPrefix) && strings.HasSuffix(key, patternSuffix) && buildProperties.Get(key) != "" {
68-
recipes = append(recipes, key)
88+
// Sort the keys by index and add them to the recipes list
89+
var indices []int
90+
for idx := range indexedRecipes {
91+
indices = append(indices, idx)
92+
}
93+
sort.Ints(indices)
94+
for _, idx := range indices {
95+
recipes = append(recipes, indexedRecipes[idx])
6996
}
7097
}
7198

7299
// If no recipes were found, check if the exact key exists
73-
if len(recipes) == 0 && buildProperties.Get(exactKey) != "" {
74-
recipes = append(recipes, exactKey)
100+
if len(recipes) == 0 && buildProperties.ContainsKey(pattern) {
101+
recipes = append(recipes, pattern)
75102
}
76103

77-
sort.Strings(recipes)
78-
79104
return recipes
80105
}

internal/arduino/builder/recipe_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,41 @@ func TestRecipeFinder(t *testing.T) {
1313
buildProperties.Set("recipe.test", "test")
1414
buildProperties.Set("recipe.1.test", "test2")
1515
buildProperties.Set("recipe.2.test", "test3")
16-
recipes := findRecipes(buildProperties, "recipe", ".test")
16+
recipes := findRecipes(buildProperties, "recipe.test")
1717
require.Equal(t, []string{"recipe.1.test", "recipe.2.test"}, recipes)
1818
})
1919
t.Run("NumberedRecipesWithGaps", func(t *testing.T) {
2020
buildProperties := properties.NewMap()
2121
buildProperties.Set("recipe.test", "test")
2222
buildProperties.Set("recipe.2.test", "test3")
2323
buildProperties.Set("recipe.0.test", "test2")
24-
recipes := findRecipes(buildProperties, "recipe", ".test")
24+
recipes := findRecipes(buildProperties, "recipe.test")
2525
require.Equal(t, []string{"recipe.0.test", "recipe.2.test"}, recipes)
2626
})
2727
t.Run("NumberedRecipesWithGapsAndDifferentLenghtNumbers", func(t *testing.T) {
2828
buildProperties := properties.NewMap()
2929
buildProperties.Set("recipe.test", "test")
3030
buildProperties.Set("recipe.12.test", "test3")
3131
buildProperties.Set("recipe.2.test", "test2")
32-
recipes := findRecipes(buildProperties, "recipe", ".test")
32+
recipes := findRecipes(buildProperties, "recipe.test")
3333
require.Equal(t, []string{"recipe.2.test", "recipe.12.test"}, recipes)
3434
})
3535
t.Run("UnnumberedRecipies", func(t *testing.T) {
3636
buildProperties := properties.NewMap()
3737
buildProperties.Set("recipe.test", "test")
3838
buildProperties.Set("recipe.a.test", "test3")
3939
buildProperties.Set("recipe.b.test", "test2")
40-
recipes := findRecipes(buildProperties, "recipe", ".test")
40+
recipes := findRecipes(buildProperties, "recipe.test")
4141
require.Equal(t, []string{"recipe.test"}, recipes)
4242
})
43+
t.Run("MixedRecipies", func(t *testing.T) {
44+
buildProperties := properties.NewMap()
45+
buildProperties.Set("recipe.test", "test")
46+
buildProperties.Set("recipe.2.test", "test3")
47+
buildProperties.Set("recipe.a.test", "test3")
48+
buildProperties.Set("recipe.1.test", "test3")
49+
buildProperties.Set("recipe.b.test", "test2")
50+
recipes := findRecipes(buildProperties, "recipe.test")
51+
require.Equal(t, []string{"recipe.1.test", "recipe.2.test"}, recipes)
52+
})
4353
}

0 commit comments

Comments
 (0)