Skip to content

Commit 5edef82

Browse files
authored
[skip-changelog] Some small refactoring on legacy package (part 2) (#1079)
* legacy: inlined utils.PrepareCommand * legacy: inlined ExecRecipe function
1 parent f954d7a commit 5edef82

File tree

7 files changed

+64
-47
lines changed

7 files changed

+64
-47
lines changed

legacy/builder/builder_utils/utils.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
248248
return nil, errors.WithStack(err)
249249
}
250250
if !objIsUpToDate {
251-
_, _, _, err := ExecRecipe(ctx, properties, recipe, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
251+
command, err := PrepareCommandForRecipe(properties, recipe, false)
252+
if err != nil {
253+
return nil, errors.WithStack(err)
254+
}
255+
256+
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
252257
if err != nil {
253258
return nil, errors.WithStack(err)
254259
}
@@ -479,23 +484,18 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
479484
properties.SetPath(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, archiveFilePath)
480485
properties.SetPath(constants.BUILD_PROPERTIES_OBJECT_FILE, objectFile)
481486

482-
if _, _, _, err := ExecRecipe(ctx, properties, constants.RECIPE_AR_PATTERN, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
487+
command, err := PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
488+
if err != nil {
483489
return nil, errors.WithStack(err)
484490
}
485-
}
486491

487-
return archiveFilePath, nil
488-
}
489-
490-
func ExecRecipe(ctx *types.Context, buildProperties *properties.Map, recipe string, stdout int, stderr int) (*exec.Cmd, []byte, []byte, error) {
491-
// See util.ExecCommand for stdout/stderr arguments
492-
command, err := PrepareCommandForRecipe(buildProperties, recipe, false)
493-
if err != nil {
494-
return nil, nil, nil, errors.WithStack(err)
492+
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
493+
if err != nil {
494+
return nil, errors.WithStack(err)
495+
}
495496
}
496497

497-
outbytes, errbytes, err := utils.ExecCommand(ctx, command, stdout, stderr)
498-
return command, outbytes, errbytes, err
498+
return archiveFilePath, nil
499499
}
500500

501501
const COMMANDLINE_LIMIT = 30000
@@ -511,7 +511,11 @@ func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, rem
511511
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
512512
}
513513

514-
command, err := utils.PrepareCommand(commandLine)
514+
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
515+
if err != nil {
516+
return nil, errors.WithStack(err)
517+
}
518+
command := exec.Command(parts[0], parts[1:]...)
515519

516520
// if the overall commandline is too long for the platform
517521
// try reducing the length by making the filenames relative
@@ -530,9 +534,5 @@ func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, rem
530534
command.Dir = relativePath
531535
}
532536

533-
if err != nil {
534-
return nil, errors.WithStack(err)
535-
}
536-
537537
return command, nil
538538
}

legacy/builder/ctags_runner.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package builder
1717

1818
import (
19+
"os/exec"
20+
1921
"github.com/arduino/arduino-cli/legacy/builder/constants"
2022
"github.com/arduino/arduino-cli/legacy/builder/ctags"
2123
"github.com/arduino/arduino-cli/legacy/builder/types"
2224
"github.com/arduino/arduino-cli/legacy/builder/utils"
25+
properties "github.com/arduino/go-properties-orderedmap"
2326
"github.com/pkg/errors"
2427
)
2528

@@ -29,21 +32,21 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
2932
buildProperties := ctx.BuildProperties
3033
ctagsTargetFilePath := ctx.CTagsTargetFile
3134

32-
properties := buildProperties.Clone()
33-
properties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
34-
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, ctagsTargetFilePath)
35+
ctagsProperties := buildProperties.Clone()
36+
ctagsProperties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
37+
ctagsProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, ctagsTargetFilePath)
3538

36-
pattern := properties.Get(constants.BUILD_PROPERTIES_PATTERN)
39+
pattern := ctagsProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
3740
if pattern == constants.EMPTY_STRING {
3841
return errors.Errorf("%s pattern is missing", constants.CTAGS)
3942
}
4043

41-
commandLine := properties.ExpandPropsInString(pattern)
42-
command, err := utils.PrepareCommand(commandLine)
44+
commandLine := ctagsProperties.ExpandPropsInString(pattern)
45+
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
4346
if err != nil {
4447
return errors.WithStack(err)
4548
}
46-
49+
command := exec.Command(parts[0], parts[1:]...)
4750
sourceBytes, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Ignore /* stderr */)
4851
if err != nil {
4952
return errors.WithStack(err)

legacy/builder/phases/linker.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
8080
properties.Set("archive_file", archive.Base())
8181
properties.SetPath("archive_file_path", archive)
8282
properties.SetPath("object_file", object)
83-
_, _, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_AR_PATTERN, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
83+
84+
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
8485
if err != nil {
85-
return err
86+
return errors.WithStack(err)
87+
}
88+
89+
if _, _, err := utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
90+
return errors.WithStack(err)
8691
}
8792
}
8893

@@ -97,7 +102,12 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
97102
properties.Set(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, coreArchiveFilePath.String())
98103
properties.Set("object_files", objectFileList)
99104

100-
_, _, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_C_COMBINE_PATTERN, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
105+
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false)
106+
if err != nil {
107+
return err
108+
}
109+
110+
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
101111
return err
102112
}
103113

legacy/builder/phases/sizer.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
127127
}
128128

129129
func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) {
130-
_, out, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_SIZE_PATTERN, utils.Capture /* stdout */, utils.Show /* stderr */)
130+
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_SIZE_PATTERN, false)
131+
if err != nil {
132+
resErr = errors.New("Error while determining sketch size: " + err.Error())
133+
return
134+
}
135+
136+
out, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Show /* stderr */)
131137
if err != nil {
132138
resErr = errors.New("Error while determining sketch size: " + err.Error())
133139
return

legacy/builder/preprocess_sketch.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
7878
buildProperties := ctx.BuildProperties
7979
targetFilePath := ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E)
8080

81-
properties := buildProperties.Clone()
81+
preprocProperties := buildProperties.Clone()
8282
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
83-
properties.Merge(toolProps)
84-
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, targetFilePath)
83+
preprocProperties.Merge(toolProps)
84+
preprocProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, targetFilePath)
8585
if ctx.CodeCompleteAt != "" {
8686
if runtime.GOOS == "windows" {
8787
//use relative filepath to avoid ":" escaping
@@ -93,21 +93,22 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
9393
ctx.CodeCompleteAt = strings.Join(splt[1:], ":")
9494
}
9595
}
96-
properties.Set("codecomplete", "-output-code-completions="+ctx.CodeCompleteAt)
96+
preprocProperties.Set("codecomplete", "-output-code-completions="+ctx.CodeCompleteAt)
9797
} else {
98-
properties.Set("codecomplete", "")
98+
preprocProperties.Set("codecomplete", "")
9999
}
100100

101-
pattern := properties.Get(constants.BUILD_PROPERTIES_PATTERN)
101+
pattern := preprocProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
102102
if pattern == constants.EMPTY_STRING {
103103
return errors.New("arduino-preprocessor pattern is missing")
104104
}
105105

106-
commandLine := properties.ExpandPropsInString(pattern)
107-
command, err := utils.PrepareCommand(commandLine)
106+
commandLine := preprocProperties.ExpandPropsInString(pattern)
107+
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
108108
if err != nil {
109109
return errors.WithStack(err)
110110
}
111+
command := exec.Command(parts[0], parts[1:]...)
111112

112113
if runtime.GOOS == "windows" {
113114
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)

legacy/builder/recipe_runner.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
4747
if ctx.DebugLevel >= 10 {
4848
logger.Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_RECIPE, recipe)
4949
}
50-
_, _, _, err := builder_utils.ExecRecipe(ctx, properties, recipe, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
50+
51+
command, err := builder_utils.PrepareCommandForRecipe(properties, recipe, false)
52+
if err != nil {
53+
return errors.WithStack(err)
54+
}
55+
56+
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
5157
if err != nil {
5258
return errors.WithStack(err)
5359
}

legacy/builder/utils/utils.go

-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/arduino/arduino-cli/legacy/builder/gohasissues"
3333
"github.com/arduino/arduino-cli/legacy/builder/types"
3434
paths "github.com/arduino/go-paths-helper"
35-
"github.com/arduino/go-properties-orderedmap"
3635
"github.com/pkg/errors"
3736
"golang.org/x/text/transform"
3837
"golang.org/x/text/unicode/norm"
@@ -147,14 +146,6 @@ func TrimSpace(value string) string {
147146
return strings.TrimSpace(value)
148147
}
149148

150-
func PrepareCommand(pattern string) (*exec.Cmd, error) {
151-
parts, err := properties.SplitQuotedString(pattern, `"'`, false)
152-
if err != nil {
153-
return nil, errors.WithStack(err)
154-
}
155-
return exec.Command(parts[0], parts[1:]...), nil
156-
}
157-
158149
func printableArgument(arg string) string {
159150
if strings.ContainsAny(arg, "\"\\ \t") {
160151
arg = strings.Replace(arg, "\\", "\\\\", -1)

0 commit comments

Comments
 (0)