Skip to content

Commit eb0cbab

Browse files
move ExecCommand in builder struct
1 parent 22a0249 commit eb0cbab

File tree

8 files changed

+55
-101
lines changed

8 files changed

+55
-101
lines changed

Diff for: arduino/builder/archive_compiled_files.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package builder
22

33
import (
4-
"github.com/arduino/arduino-cli/arduino/builder/internal/utils"
54
"github.com/arduino/go-paths-helper"
65
"github.com/pkg/errors"
76
)
@@ -52,11 +51,7 @@ func (b *Builder) archiveCompiledFiles(buildPath *paths.Path, archiveFile *paths
5251
return nil, errors.WithStack(err)
5352
}
5453

55-
verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
56-
if b.logger.Verbose() {
57-
b.logger.Info(string(verboseInfo))
58-
}
59-
if err != nil {
54+
if err := b.execCommand(command); err != nil {
6055
return nil, errors.WithStack(err)
6156
}
6257
}

Diff for: arduino/builder/builder.go

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/arduino/arduino-cli/arduino/builder/internal/detector"
2828
"github.com/arduino/arduino-cli/arduino/builder/internal/logger"
2929
"github.com/arduino/arduino-cli/arduino/builder/internal/progress"
30+
"github.com/arduino/arduino-cli/arduino/builder/internal/utils"
3031
"github.com/arduino/arduino-cli/arduino/cores"
3132
"github.com/arduino/arduino-cli/arduino/libraries"
3233
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -528,3 +529,17 @@ func (b *Builder) prepareCommandForRecipe(buildProperties *properties.Map, recip
528529

529530
return command, nil
530531
}
532+
533+
func (b *Builder) execCommand(command *executils.Process) error {
534+
if b.logger.Verbose() {
535+
b.logger.Info(utils.PrintableCommand(command.GetArgs()))
536+
command.RedirectStdoutTo(b.logger.Stdout())
537+
}
538+
command.RedirectStderrTo(b.logger.Stderr())
539+
540+
if err := command.Start(); err != nil {
541+
return err
542+
}
543+
544+
return command.Wait()
545+
}

Diff for: arduino/builder/compilation.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package builder
22

33
import (
4+
"bytes"
45
"fmt"
56
"runtime"
67
"strings"
@@ -133,21 +134,21 @@ func (b *Builder) compileFileWithRecipe(
133134
b.compilationDatabase.Add(source, command)
134135
}
135136
if !objIsUpToDate && !b.onlyUpdateCompilationDatabase {
137+
commandStdout, commandStderr := &bytes.Buffer{}, &bytes.Buffer{}
138+
command.RedirectStdoutTo(commandStdout)
139+
command.RedirectStderrTo(commandStderr)
140+
136141
// Since this compile could be multithreaded, we first capture the command output
137-
info, stdout, stderr, err := utils.ExecCommand(
138-
b.logger.Verbose(),
139-
b.logger.Stdout(),
140-
b.logger.Stderr(),
141-
command,
142-
utils.Capture,
143-
utils.Capture,
144-
)
142+
if err := command.Start(); err != nil {
143+
return nil, err
144+
}
145+
err := command.Wait()
145146
// and transfer all at once at the end...
146147
if b.logger.Verbose() {
147-
b.logger.Info(string(info))
148-
b.logger.WriteStdout(stdout)
148+
b.logger.Info(utils.PrintableCommand(command.GetArgs()))
149+
b.logger.WriteStdout(commandStdout.Bytes())
149150
}
150-
b.logger.WriteStderr(stderr)
151+
b.logger.WriteStderr(commandStderr.Bytes())
151152

152153
// ...and then return the error
153154
if err != nil {

Diff for: arduino/builder/internal/utils/utils.go

+2-55
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
package utils
1717

1818
import (
19-
"bytes"
20-
"io"
2119
"os"
2220
"strings"
2321
"unicode"
2422

25-
"github.com/arduino/arduino-cli/executils"
2623
"github.com/arduino/arduino-cli/i18n"
2724
f "github.com/arduino/arduino-cli/internal/algorithms"
2825
"github.com/arduino/go-paths-helper"
@@ -201,14 +198,6 @@ func FindFilesInFolder(dir *paths.Path, recurse bool, extensions ...string) (pat
201198
return dir.ReadDir(fileFilter)
202199
}
203200

204-
// nolint
205-
const (
206-
Ignore = 0 // Redirect to null
207-
Show = 1 // Show on stdout/stderr as normal
208-
ShowIfVerbose = 2 // Show if verbose is set, Ignore otherwise
209-
Capture = 3 // Capture into buffer
210-
)
211-
212201
func printableArgument(arg string) string {
213202
if strings.ContainsAny(arg, "\"\\ \t") {
214203
arg = strings.ReplaceAll(arg, "\\", "\\\\")
@@ -218,56 +207,14 @@ func printableArgument(arg string) string {
218207
return arg
219208
}
220209

221-
// Convert a command and argument slice back to a printable string.
210+
// PrintableCommand Convert a command and argument slice back to a printable string.
222211
// This adds basic escaping which is sufficient for debug output, but
223212
// probably not for shell interpretation. This essentially reverses
224213
// ParseCommandLine.
225-
func printableCommand(parts []string) string {
214+
func PrintableCommand(parts []string) string {
226215
return strings.Join(f.Map(parts, printableArgument), " ")
227216
}
228217

229-
// ExecCommand fixdoc
230-
func ExecCommand(
231-
verbose bool,
232-
stdoutWriter, stderrWriter io.Writer,
233-
command *executils.Process, stdout int, stderr int,
234-
) ([]byte, []byte, []byte, error) {
235-
verboseInfoBuf := &bytes.Buffer{}
236-
if verbose {
237-
verboseInfoBuf.WriteString(printableCommand(command.GetArgs()))
238-
}
239-
240-
stdoutBuffer := &bytes.Buffer{}
241-
if stdout == Capture {
242-
command.RedirectStdoutTo(stdoutBuffer)
243-
} else if stdout == Show || (stdout == ShowIfVerbose && verbose) {
244-
if stdoutWriter != nil {
245-
command.RedirectStdoutTo(stdoutWriter)
246-
} else {
247-
command.RedirectStdoutTo(os.Stdout)
248-
}
249-
}
250-
251-
stderrBuffer := &bytes.Buffer{}
252-
if stderr == Capture {
253-
command.RedirectStderrTo(stderrBuffer)
254-
} else if stderr == Show || (stderr == ShowIfVerbose && verbose) {
255-
if stderrWriter != nil {
256-
command.RedirectStderrTo(stderrWriter)
257-
} else {
258-
command.RedirectStderrTo(os.Stderr)
259-
}
260-
}
261-
262-
err := command.Start()
263-
if err != nil {
264-
return verboseInfoBuf.Bytes(), nil, nil, errors.WithStack(err)
265-
}
266-
267-
err = command.Wait()
268-
return verboseInfoBuf.Bytes(), stdoutBuffer.Bytes(), stderrBuffer.Bytes(), errors.WithStack(err)
269-
}
270-
271218
// DirContentIsOlderThan DirContentIsOlderThan returns true if the content of the given directory is
272219
// older than target file. If extensions are given, only the files with these
273220
// extensions are tested.

Diff for: arduino/builder/internal/utils/utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestPrintableCommand(t *testing.T) {
3939
" \"specialchar-`~!@#$%^&*()-_=+[{]}\\\\|;:'\\\",<.>/?-argument\"" +
4040
" \"arg with spaces\" \"arg\twith\t\ttabs\"" +
4141
" lastarg"
42-
result := printableCommand(parts)
42+
result := PrintableCommand(parts)
4343
require.Equal(t, correct, result)
4444
}
4545

Diff for: arduino/builder/linker.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package builder
1818
import (
1919
"strings"
2020

21-
"github.com/arduino/arduino-cli/arduino/builder/internal/utils"
2221
f "github.com/arduino/arduino-cli/internal/algorithms"
2322
"github.com/arduino/go-paths-helper"
2423
"github.com/pkg/errors"
@@ -77,10 +76,7 @@ func (b *Builder) link() error {
7776
return errors.WithStack(err)
7877
}
7978

80-
if verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
81-
if b.logger.Verbose() {
82-
b.logger.Info(string(verboseInfo))
83-
}
79+
if err := b.execCommand(command); err != nil {
8480
return errors.WithStack(err)
8581
}
8682
}
@@ -101,12 +97,5 @@ func (b *Builder) link() error {
10197
return err
10298
}
10399

104-
verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
105-
if b.logger.Verbose() {
106-
b.logger.Info(string(verboseInfo))
107-
}
108-
if err != nil {
109-
return err
110-
}
111-
return nil
100+
return b.execCommand(command)
112101
}

Diff for: arduino/builder/recipe.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"sort"
2121
"strings"
2222

23-
"github.com/arduino/arduino-cli/arduino/builder/internal/utils"
2423
properties "github.com/arduino/go-properties-orderedmap"
2524
"github.com/pkg/errors"
2625
"github.com/sirupsen/logrus"
@@ -51,11 +50,7 @@ func (b *Builder) RunRecipe(prefix, suffix string, skipIfOnlyUpdatingCompilation
5150
return nil
5251
}
5352

54-
verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
55-
if b.logger.Verbose() {
56-
b.logger.Info(string(verboseInfo))
57-
}
58-
if err != nil {
53+
if err := b.execCommand(command); err != nil {
5954
return errors.WithStack(err)
6055
}
6156
}

Diff for: arduino/builder/sizer.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package builder
1717

1818
import (
19+
"bytes"
1920
"encoding/json"
2021
"fmt"
2122
"regexp"
@@ -76,12 +77,16 @@ func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) {
7677
if err != nil {
7778
return nil, errors.New(tr("Error while determining sketch size: %s", err))
7879
}
79-
80-
verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */)
8180
if b.logger.Verbose() {
82-
b.logger.Info(string(verboseInfo))
81+
b.logger.Info(utils.PrintableCommand(command.GetArgs()))
8382
}
84-
if err != nil {
83+
out := &bytes.Buffer{}
84+
command.RedirectStdoutTo(out)
85+
command.RedirectStderrTo(b.logger.Stderr())
86+
if err := command.Start(); err != nil {
87+
return nil, errors.New(tr("Error while determining sketch size: %s", err))
88+
}
89+
if err := command.Wait(); err != nil {
8590
return nil, errors.New(tr("Error while determining sketch size: %s", err))
8691
}
8792

@@ -100,7 +105,7 @@ func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) {
100105
}
101106

102107
var resp AdvancedSizerResponse
103-
if err := json.Unmarshal(out, &resp); err != nil {
108+
if err := json.Unmarshal(out.Bytes(), &resp); err != nil {
104109
return nil, errors.New(tr("Error while determining sketch size: %s", err))
105110
}
106111

@@ -209,15 +214,22 @@ func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, data
209214
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
210215
return
211216
}
212-
213-
verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */)
214217
if b.logger.Verbose() {
215-
b.logger.Info(string(verboseInfo))
218+
b.logger.Info(utils.PrintableCommand(command.GetArgs()))
216219
}
217-
if err != nil {
220+
commandStdout := &bytes.Buffer{}
221+
command.RedirectStdoutTo(commandStdout)
222+
command.RedirectStderrTo(b.logger.Stderr())
223+
if err := command.Start(); err != nil {
218224
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
219225
return
220226
}
227+
if err := command.Wait(); err != nil {
228+
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
229+
return
230+
}
231+
232+
out := commandStdout.Bytes()
221233

222234
// force multiline match prepending "(?m)" to the actual regexp
223235
// return an error if RECIPE_SIZE_REGEXP doesn't exist

0 commit comments

Comments
 (0)