Skip to content

Commit fc9d011

Browse files
refactor MergeSketchWithBootloader in a function
1 parent 7fc0034 commit fc9d011

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

legacy/builder/builder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@ func (s *Builder) Run(ctx *types.Context) error {
197197
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.objcopy.postobjcopy", ".pattern", true)
198198
}),
199199

200-
&MergeSketchWithBootloader{},
200+
types.BareCommand(func(ctx *types.Context) error {
201+
return MergeSketchWithBootloader(
202+
ctx.OnlyUpdateCompilationDatabase, ctx.Verbose,
203+
ctx.BuildPath, ctx.Sketch, ctx.BuildProperties,
204+
func(s string) { ctx.Info(s) },
205+
func(s string) { ctx.Warn(s) },
206+
)
207+
}),
201208

202209
types.BareCommand(func(ctx *types.Context) error {
203210
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.postbuild", ".pattern", true)

legacy/builder/merge_sketch_with_bootloader.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,30 @@ import (
2020
"strconv"
2121
"strings"
2222

23+
"github.com/arduino/arduino-cli/arduino/sketch"
2324
"github.com/arduino/arduino-cli/legacy/builder/constants"
24-
"github.com/arduino/arduino-cli/legacy/builder/types"
2525
"github.com/arduino/go-paths-helper"
26+
"github.com/arduino/go-properties-orderedmap"
2627
"github.com/marcinbor85/gohex"
2728
"github.com/pkg/errors"
2829
)
2930

30-
type MergeSketchWithBootloader struct{}
31-
32-
func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error {
33-
if ctx.OnlyUpdateCompilationDatabase {
31+
func MergeSketchWithBootloader(
32+
onlyUpdateCompilationDatabase, verbose bool,
33+
buildPath *paths.Path,
34+
sketch *sketch.Sketch,
35+
buildProperties *properties.Map,
36+
printInfoFn, printWarnFn func(string),
37+
) error {
38+
if onlyUpdateCompilationDatabase {
3439
return nil
3540
}
3641

37-
buildProperties := ctx.BuildProperties
3842
if !buildProperties.ContainsKey(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK) && !buildProperties.ContainsKey(constants.BUILD_PROPERTIES_BOOTLOADER_FILE) {
3943
return nil
4044
}
4145

42-
buildPath := ctx.BuildPath
43-
sketch := ctx.Sketch
4446
sketchFileName := sketch.MainFile.Base()
45-
4647
sketchInBuildPath := buildPath.Join(sketchFileName + ".hex")
4748
sketchInSubfolder := buildPath.Join(constants.FOLDER_SKETCH, sketchFileName+".hex")
4849

@@ -65,8 +66,8 @@ func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error {
6566

6667
bootloaderPath := buildProperties.GetPath("runtime.platform.path").Join(constants.FOLDER_BOOTLOADERS, bootloader)
6768
if bootloaderPath.NotExist() {
68-
if ctx.Verbose {
69-
ctx.Warn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath))
69+
if verbose {
70+
printWarnFn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath))
7071
}
7172
return nil
7273
}
@@ -75,13 +76,13 @@ func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error {
7576

7677
// Ignore merger errors for the first iteration
7778
maximumBinSize := 16000000
78-
if uploadMaxSize, ok := ctx.BuildProperties.GetOk("upload.maximum_size"); ok {
79+
if uploadMaxSize, ok := buildProperties.GetOk("upload.maximum_size"); ok {
7980
maximumBinSize, _ = strconv.Atoi(uploadMaxSize)
8081
maximumBinSize *= 2
8182
}
8283
err := merge(builtSketchPath, bootloaderPath, mergedSketchPath, maximumBinSize)
83-
if err != nil && ctx.Verbose {
84-
ctx.Info(err.Error())
84+
if err != nil && verbose {
85+
printInfoFn(err.Error())
8586
}
8687

8788
return nil

legacy/builder/test/merge_sketch_with_bootloader_test.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ func TestMergeSketchWithBootloader(t *testing.T) {
6969
err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex))
7070
require.NoError(t, err)
7171

72-
commands := []types.Command{
73-
&builder.MergeSketchWithBootloader{},
74-
}
75-
76-
for _, command := range commands {
77-
err := command.Run(ctx)
78-
require.NoError(t, err)
79-
}
72+
err = builder.MergeSketchWithBootloader(
73+
ctx.OnlyUpdateCompilationDatabase, ctx.Verbose,
74+
ctx.BuildPath, ctx.Sketch, ctx.BuildProperties,
75+
func(s string) { ctx.Info(s) },
76+
func(s string) { ctx.Warn(s) },
77+
)
78+
require.NoError(t, err)
8079

8180
bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile()
8281
require.NoError(t, err)
@@ -127,14 +126,13 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) {
127126
err = buildPath.Join("sketch1.ino.hex").WriteFile([]byte(fakeSketchHex))
128127
require.NoError(t, err)
129128

130-
commands := []types.Command{
131-
&builder.MergeSketchWithBootloader{},
132-
}
133-
134-
for _, command := range commands {
135-
err := command.Run(ctx)
136-
require.NoError(t, err)
137-
}
129+
err = builder.MergeSketchWithBootloader(
130+
ctx.OnlyUpdateCompilationDatabase, ctx.Verbose,
131+
ctx.BuildPath, ctx.Sketch, ctx.BuildProperties,
132+
func(s string) { ctx.Info(s) },
133+
func(s string) { ctx.Warn(s) },
134+
)
135+
require.NoError(t, err)
138136

139137
bytes, err := buildPath.Join("sketch1.ino.with_bootloader.hex").ReadFile()
140138
require.NoError(t, err)
@@ -154,8 +152,12 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) {
154152
buildProperties.Remove(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK)
155153
buildProperties.Remove(constants.BUILD_PROPERTIES_BOOTLOADER_FILE)
156154

157-
command := &builder.MergeSketchWithBootloader{}
158-
err := command.Run(ctx)
155+
err := builder.MergeSketchWithBootloader(
156+
ctx.OnlyUpdateCompilationDatabase, ctx.Verbose,
157+
ctx.BuildPath, ctx.Sketch, ctx.BuildProperties,
158+
func(s string) { ctx.Info(s) },
159+
func(s string) { ctx.Warn(s) },
160+
)
159161
require.NoError(t, err)
160162

161163
exist, err := buildPath.Join("sketch.ino.with_bootloader.hex").ExistCheck()
@@ -210,14 +212,13 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) {
210212
err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex))
211213
require.NoError(t, err)
212214

213-
commands := []types.Command{
214-
&builder.MergeSketchWithBootloader{},
215-
}
216-
217-
for _, command := range commands {
218-
err := command.Run(ctx)
219-
require.NoError(t, err)
220-
}
215+
err = builder.MergeSketchWithBootloader(
216+
ctx.OnlyUpdateCompilationDatabase, ctx.Verbose,
217+
ctx.BuildPath, ctx.Sketch, ctx.BuildProperties,
218+
func(s string) { ctx.Info(s) },
219+
func(s string) { ctx.Warn(s) },
220+
)
221+
require.NoError(t, err)
221222

222223
bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile()
223224
require.NoError(t, err)

0 commit comments

Comments
 (0)