Skip to content

Commit 7f775a3

Browse files
committed
Moved Result structure into his own package
This change is preparatory for next refactorings
1 parent ca07725 commit 7f775a3

File tree

6 files changed

+45
-52
lines changed

6 files changed

+45
-52
lines changed

internal/arduino/builder/internal/detector/detector.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnostics"
3131
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor"
32+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/runner"
3233
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
3334
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
3435
"github.com/arduino/arduino-cli/internal/arduino/cores"
@@ -339,7 +340,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
339340
}
340341

341342
var preprocErr error
342-
var preprocFirstResult preprocessor.Result
343+
var preprocFirstResult *runner.Result
343344

344345
var missingIncludeH string
345346
if unchanged && cache.valid {
@@ -350,18 +351,18 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
350351
} else {
351352
preprocFirstResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
352353
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
353-
l.logger.WriteStdout(preprocFirstResult.Stdout())
354+
l.logger.WriteStdout(preprocFirstResult.Stdout)
354355
}
355356
// Unwrap error and see if it is an ExitError.
356357
var exitErr *exec.ExitError
357358
if preprocErr == nil {
358359
// Preprocessor successful, done
359360
missingIncludeH = ""
360-
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || preprocFirstResult.Stderr() == nil {
361+
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || len(preprocFirstResult.Stderr) == 0 {
361362
// Ignore ExitErrors (e.g. gcc returning non-zero status), but bail out on other errors
362363
return preprocErr
363364
} else {
364-
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr()))
365+
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr))
365366
if missingIncludeH == "" && l.logger.VerbosityLevel() == logger.VerbosityVerbose {
366367
l.logger.Info(i18n.Tr("Error while detecting libraries included by %[1]s", sourcePath))
367368
}
@@ -377,11 +378,11 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
377378
library := l.resolveLibrary(missingIncludeH, platformArch)
378379
if library == nil {
379380
// Library could not be resolved, show error
380-
if preprocErr == nil || preprocFirstResult.Stderr() == nil {
381+
if preprocErr == nil || len(preprocFirstResult.Stderr) == 0 {
381382
// Filename came from cache, so run preprocessor to obtain error to show
382383
result, err := preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
383384
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
384-
l.logger.WriteStdout(result.Stdout())
385+
l.logger.WriteStdout(result.Stdout)
385386
}
386387
if err == nil {
387388
// If there is a missing #include in the cache, but running
@@ -390,12 +391,12 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
390391
// deleted, so hopefully the next compilation will succeed.
391392
return errors.New(i18n.Tr("Internal error in cache"))
392393
}
393-
l.diagnosticStore.Parse(result.Args(), result.Stderr())
394-
l.logger.WriteStderr(result.Stderr())
394+
l.diagnosticStore.Parse(result.Args, result.Stderr)
395+
l.logger.WriteStderr(result.Stderr)
395396
return err
396397
}
397-
l.diagnosticStore.Parse(preprocFirstResult.Args(), preprocFirstResult.Stderr())
398-
l.logger.WriteStderr(preprocFirstResult.Stderr())
398+
l.diagnosticStore.Parse(preprocFirstResult.Args, preprocFirstResult.Stderr)
399+
l.logger.WriteStderr(preprocFirstResult.Stderr)
399400
return preprocErr
400401
}
401402

internal/arduino/builder/internal/preprocessor/arduino_preprocessor.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"runtime"
2424

25+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/runner"
2526
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
2627
"github.com/arduino/arduino-cli/internal/arduino/sketch"
2728
"github.com/arduino/arduino-cli/internal/i18n"
@@ -35,7 +36,7 @@ func PreprocessSketchWithArduinoPreprocessor(
3536
ctx context.Context,
3637
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
3738
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
38-
) (*Result, error) {
39+
) (*runner.Result, error) {
3940
verboseOut := &bytes.Buffer{}
4041
normalOut := &bytes.Buffer{}
4142
if err := buildPath.Join("preproc").MkdirAll(); err != nil {
@@ -45,8 +46,8 @@ func PreprocessSketchWithArduinoPreprocessor(
4546
sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
4647
targetFile := buildPath.Join("preproc", "sketch_merged.cpp")
4748
gccResult, err := GCC(ctx, sourceFile, targetFile, includeFolders, buildProperties)
48-
verboseOut.Write(gccResult.Stdout())
49-
verboseOut.Write(gccResult.Stderr())
49+
verboseOut.Write(gccResult.Stdout)
50+
verboseOut.Write(gccResult.Stderr)
5051
if err != nil {
5152
return nil, err
5253
}
@@ -83,13 +84,13 @@ func PreprocessSketchWithArduinoPreprocessor(
8384
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(ctx)
8485
verboseOut.Write(commandStdErr)
8586
if err != nil {
86-
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
87+
return &runner.Result{Args: gccResult.Args, Stdout: verboseOut.Bytes(), Stderr: normalOut.Bytes()}, err
8788
}
8889
result := utils.NormalizeUTF8(commandStdOut)
8990

9091
destFile := buildPath.Join(sk.MainFile.Base() + ".cpp")
9192
if err := destFile.WriteFile(result); err != nil {
92-
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
93+
return &runner.Result{Args: gccResult.Args, Stdout: verboseOut.Bytes(), Stderr: normalOut.Bytes()}, err
9394
}
94-
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
95+
return &runner.Result{Args: gccResult.Args, Stdout: verboseOut.Bytes(), Stderr: normalOut.Bytes()}, err
9596
}

internal/arduino/builder/internal/preprocessor/ctags.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
2929
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor/internal/ctags"
30+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/runner"
3031
"github.com/arduino/arduino-cli/internal/arduino/sketch"
3132
"github.com/arduino/arduino-cli/internal/i18n"
3233
"github.com/arduino/go-paths-helper"
@@ -43,7 +44,7 @@ func PreprocessSketchWithCtags(
4344
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
4445
lineOffset int, buildProperties *properties.Map,
4546
onlyUpdateCompilationDatabase, verbose bool,
46-
) (*Result, error) {
47+
) (*runner.Result, error) {
4748
// Create a temporary working directory
4849
tmpDir, err := paths.MkTempDir("", "")
4950
if err != nil {
@@ -57,29 +58,29 @@ func PreprocessSketchWithCtags(
5758
// Run GCC preprocessor
5859
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
5960
result, err := GCC(ctx, sourceFile, ctagsTarget, includes, buildProperties)
60-
stdout.Write(result.Stdout())
61-
stderr.Write(result.Stderr())
61+
stdout.Write(result.Stdout)
62+
stderr.Write(result.Stderr)
6263
if err != nil {
6364
if !onlyUpdateCompilationDatabase {
64-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
65+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
6566
}
6667

6768
// Do not bail out if we are generating the compile commands database
6869
fmt.Fprintf(stderr, "%s: %s",
6970
i18n.Tr("An error occurred adding prototypes"),
7071
i18n.Tr("the compilation database may be incomplete or inaccurate"))
7172
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
72-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
73+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
7374
}
7475
}
7576

7677
if src, err := ctagsTarget.ReadFile(); err == nil {
7778
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false)
7879
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
79-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
80+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
8081
}
8182
} else {
82-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
83+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
8384
}
8485

8586
// Run CTags on gcc-preprocessed source
@@ -89,7 +90,7 @@ func PreprocessSketchWithCtags(
8990
stderr.Write(ctagsStdErr)
9091
}
9192
if err != nil {
92-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
93+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
9394
}
9495

9596
// Parse CTags output
@@ -104,13 +105,13 @@ func PreprocessSketchWithCtags(
104105
if sourceData, err := sourceFile.ReadFile(); err == nil {
105106
source = string(sourceData)
106107
} else {
107-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
108+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
108109
}
109110
source = strings.ReplaceAll(source, "\r\n", "\n")
110111
source = strings.ReplaceAll(source, "\r", "\n")
111112
sourceRows := strings.Split(source, "\n")
112113
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
113-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil
114+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, nil
114115
}
115116

116117
insertionLine := firstFunctionLine + lineOffset - 1
@@ -136,7 +137,7 @@ func PreprocessSketchWithCtags(
136137

137138
// Write back arduino-preprocess output to the sourceFile
138139
err = sourceFile.WriteFile([]byte(preprocessedSource))
139-
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
140+
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
140141
}
141142

142143
func composePrototypeSection(line int, prototypes []*ctags.Prototype) string {

internal/arduino/builder/internal/preprocessor/gcc.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
26+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/runner"
2627
"github.com/arduino/arduino-cli/internal/i18n"
2728
"github.com/arduino/go-paths-helper"
2829
"github.com/arduino/go-properties-orderedmap"
@@ -35,7 +36,7 @@ func GCC(
3536
ctx context.Context,
3637
sourceFilePath, targetFilePath *paths.Path,
3738
includes paths.PathList, buildProperties *properties.Map,
38-
) (Result, error) {
39+
) (*runner.Result, error) {
3940
gccBuildProperties := properties.NewMap()
4041
gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")
4142
gccBuildProperties.Merge(buildProperties)
@@ -60,14 +61,14 @@ func GCC(
6061

6162
pattern := gccBuildProperties.Get(gccPreprocRecipeProperty)
6263
if pattern == "" {
63-
return Result{}, errors.New(i18n.Tr("%s pattern is missing", gccPreprocRecipeProperty))
64+
return nil, errors.New(i18n.Tr("%s pattern is missing", gccPreprocRecipeProperty))
6465
}
6566

6667
commandLine := gccBuildProperties.ExpandPropsInString(pattern)
6768
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
6869
args, err := properties.SplitQuotedString(commandLine, `"'`, false)
6970
if err != nil {
70-
return Result{}, err
71+
return nil, err
7172
}
7273

7374
// Remove -MMD argument if present. Leaving it will make gcc try
@@ -76,7 +77,7 @@ func GCC(
7677

7778
proc, err := paths.NewProcess(nil, args...)
7879
if err != nil {
79-
return Result{}, err
80+
return nil, err
8081
}
8182

8283
stdout := bytes.NewBuffer(nil)
@@ -103,13 +104,13 @@ func GCC(
103104
fmt.Fprintln(stdout, strings.Join(args, " "))
104105

105106
if err := proc.Start(); err != nil {
106-
return Result{}, err
107+
return &runner.Result{}, err
107108
}
108109

109110
// Wait for the process to finish
110111
err = proc.WaitWithinContext(ctx)
111112

112-
return Result{args: proc.GetArgs(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
113+
return &runner.Result{Args: proc.GetArgs(), Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
113114
}
114115

115116
type writerFunc func(p []byte) (n int, err error)

internal/arduino/builder/internal/preprocessor/result.go renamed to internal/arduino/builder/internal/runner/task.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,11 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
1515

16-
package preprocessor
16+
package runner
1717

18+
// Result contains the output of a command execution
1819
type Result struct {
19-
args []string
20-
stdout []byte
21-
stderr []byte
22-
}
23-
24-
func (r Result) Args() []string {
25-
return r.args
26-
}
27-
28-
func (r Result) Stdout() []byte {
29-
return r.stdout
30-
}
31-
32-
func (r Result) Stderr() []byte {
33-
return r.stderr
20+
Args []string
21+
Stdout []byte
22+
Stderr []byte
3423
}

internal/arduino/builder/preprocess_sketch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
3232
)
3333
if result != nil {
3434
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
35-
b.logger.WriteStdout(result.Stdout())
35+
b.logger.WriteStdout(result.Stdout)
3636
}
37-
b.logger.WriteStderr(result.Stderr())
38-
b.diagnosticStore.Parse(result.Args(), result.Stderr())
37+
b.logger.WriteStderr(result.Stderr)
38+
b.diagnosticStore.Parse(result.Args, result.Stderr)
3939
}
4040

4141
return err

0 commit comments

Comments
 (0)