Skip to content

Commit

Permalink
refactor SketchBuilder in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 8, 2023
1 parent 447419d commit a688f45
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
23 changes: 22 additions & 1 deletion legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,28 @@ func (s *Builder) Run(ctx *types.Context) error {

logIfVerbose(false, tr("Compiling sketch...")),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.prebuild", Suffix: ".pattern"},
&phases.SketchBuilder{},
types.BareCommand(func(ctx *types.Context) error {
sketchObjectFiles, err := phases.SketchBuilder(
ctx.SketchBuildPath,
ctx.BuildProperties,
ctx.SketchLibrariesDetector.IncludeFolders(),
ctx.OnlyUpdateCompilationDatabase,
ctx.Verbose,
ctx.CompilationDatabase,
ctx.Jobs,
ctx.WarningsLevel,
ctx.Stdout, ctx.Stderr,
func(msg string) { ctx.Info(msg) },
func(data []byte) { ctx.WriteStdout(data) },
func(data []byte) { ctx.WriteStderr(data) },
&ctx.Progress, ctx.ProgressCB,
)
if err != nil {
return err
}
ctx.SketchObjectFiles = sketchObjectFiles
return nil
}),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},

logIfVerbose(false, tr("Compiling libraries...")),
Expand Down
81 changes: 46 additions & 35 deletions legacy/builder/phases/sketch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,76 @@
package phases

import (
"io"

"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/builder/progress"
"github.com/arduino/arduino-cli/arduino/builder/utils"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/legacy/builder/types"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)

type SketchBuilder struct{}

func (s *SketchBuilder) Run(ctx *types.Context) error {
sketchBuildPath := ctx.SketchBuildPath
buildProperties := ctx.BuildProperties
includesFolders := ctx.SketchLibrariesDetector.IncludeFolders()
func SketchBuilder(
sketchBuildPath *paths.Path,
buildProperties *properties.Map,
includesFolders paths.PathList,
onlyUpdateCompilationDatabase, verbose bool,
compilationDatabase *builder.CompilationDatabase,
jobs int,
warningsLevel string,
stdoutWriter, stderrWriter io.Writer,
verboseInfoFn func(msg string),
verboseStdoutFn, verboseStderrFn func(data []byte),
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)

if err := sketchBuildPath.MkdirAll(); err != nil {
return errors.WithStack(err)
return nil, errors.WithStack(err)
}

objectFiles, err := utils.CompileFiles(
sketchObjectFiles, err := utils.CompileFiles(
sketchBuildPath, sketchBuildPath, buildProperties, includes,
ctx.OnlyUpdateCompilationDatabase,
ctx.CompilationDatabase,
ctx.Jobs,
ctx.Verbose,
ctx.WarningsLevel,
ctx.Stdout, ctx.Stderr,
func(msg string) { ctx.Info(msg) },
func(data []byte) { ctx.WriteStdout(data) },
func(data []byte) { ctx.WriteStderr(data) },
&ctx.Progress, ctx.ProgressCB,
onlyUpdateCompilationDatabase,
compilationDatabase,
jobs,
verbose,
warningsLevel,
stdoutWriter, stderrWriter,
verboseInfoFn,
verboseStdoutFn,
verboseStderrFn,
progress, progressCB,
)
if err != nil {
return errors.WithStack(err)
return nil, errors.WithStack(err)
}

// The "src/" subdirectory of a sketch is compiled recursively
sketchSrcPath := sketchBuildPath.Join("src")
if sketchSrcPath.IsDir() {
srcObjectFiles, err := utils.CompileFilesRecursive(
sketchSrcPath, sketchSrcPath, buildProperties, includes,
ctx.OnlyUpdateCompilationDatabase,
ctx.CompilationDatabase,
ctx.Jobs,
ctx.Verbose,
ctx.WarningsLevel,
ctx.Stdout, ctx.Stderr,
func(msg string) { ctx.Info(msg) },
func(data []byte) { ctx.WriteStdout(data) },
func(data []byte) { ctx.WriteStderr(data) },
&ctx.Progress, ctx.ProgressCB,
onlyUpdateCompilationDatabase,
compilationDatabase,
jobs,
verbose,
warningsLevel,
stdoutWriter, stderrWriter,
verboseInfoFn,
verboseStdoutFn,
verboseStderrFn,
progress, progressCB,
)
if err != nil {
return errors.WithStack(err)
return nil, errors.WithStack(err)
}
objectFiles.AddAll(srcObjectFiles)
sketchObjectFiles.AddAll(srcObjectFiles)
}

ctx.SketchObjectFiles = objectFiles

return nil
return sketchObjectFiles, nil
}

0 comments on commit a688f45

Please sign in to comment.