From a688f452ab59515c2e34df1620ce8b2ac025df3c Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 7 Sep 2023 10:12:53 +0200 Subject: [PATCH] refactor SketchBuilder in a function --- legacy/builder/builder.go | 23 ++++++- legacy/builder/phases/sketch_builder.go | 81 ++++++++++++++----------- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 9b4a8b0a01c..05fb52fa059 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -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...")), diff --git a/legacy/builder/phases/sketch_builder.go b/legacy/builder/phases/sketch_builder.go index bd71c06cd63..190604ddc0a 100644 --- a/legacy/builder/phases/sketch_builder.go +++ b/legacy/builder/phases/sketch_builder.go @@ -16,40 +16,53 @@ 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 @@ -57,24 +70,22 @@ func (s *SketchBuilder) Run(ctx *types.Context) error { 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 }