Skip to content

Commit

Permalink
refactor RecipeRunner 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 b8024c3 commit 057c40e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 82 deletions.
18 changes: 14 additions & 4 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
exportBinaries = false
}
if exportBinaries {
presaveHex := builder.RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.savehex.presavehex", Suffix: ".pattern"}
if err := presaveHex.Run(builderCtx); err != nil {
err := builder.RecipeByPrefixSuffixRunner(
"recipe.hooks.savehex.presavehex", ".pattern", false,
builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose,
builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr,
func(msg string) { builderCtx.Info(msg) },
)
if err != nil {
return r, err
}

Expand Down Expand Up @@ -405,8 +410,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
}
}

postsaveHex := builder.RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.savehex.postsavehex", Suffix: ".pattern"}
if err := postsaveHex.Run(builderCtx); err != nil {
err = builder.RecipeByPrefixSuffixRunner(
"recipe.hooks.savehex.postsavehex", ".pattern", false,
builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose,
builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr,
func(msg string) { builderCtx.Info(msg) },
)
if err != nil {
return r, err
}
}
Expand Down
68 changes: 54 additions & 14 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (s *Builder) Run(ctx *types.Context) error {
commands := []types.Command{
&ContainerBuildOptions{},

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.prebuild", ".pattern", false)
}),

types.BareCommand(func(ctx *types.Context) error {
ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride, ctx.SketchBuildPath)
Expand All @@ -59,7 +61,11 @@ func (s *Builder) Run(ctx *types.Context) error {
types.BareCommand(PreprocessSketch),

logIfVerbose(false, tr("Compiling sketch...")),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.prebuild", Suffix: ".pattern"},

types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.sketch.prebuild", ".pattern", false)
}),

types.BareCommand(func(ctx *types.Context) error {
sketchObjectFiles, err := phases.SketchBuilder(
ctx.SketchBuildPath,
Expand All @@ -82,10 +88,15 @@ func (s *Builder) Run(ctx *types.Context) error {
ctx.SketchObjectFiles = sketchObjectFiles
return nil
}),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},

types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.sketch.postbuild", ".pattern", true)
}),

logIfVerbose(false, tr("Compiling libraries...")),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.libraries.prebuild", Suffix: ".pattern"},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.libraries.prebuild", ".pattern", false)
}),
&UnusedCompiledLibrariesRemover{},
types.BareCommand(func(ctx *types.Context) error {
librariesObjectFiles, err := phases.LibrariesBuilder(
Expand All @@ -112,10 +123,14 @@ func (s *Builder) Run(ctx *types.Context) error {

return nil
}),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.libraries.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.libraries.postbuild", ".pattern", true)
}),

logIfVerbose(false, tr("Compiling core...")),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.prebuild", Suffix: ".pattern"},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.core.prebuild", ".pattern", false)
}),

types.BareCommand(func(ctx *types.Context) error {
objectFiles, archiveFile, err := phases.CoreBuilder(
Expand All @@ -139,10 +154,14 @@ func (s *Builder) Run(ctx *types.Context) error {
return err
}),

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.core.postbuild", ".pattern", true)
}),

logIfVerbose(false, tr("Linking everything together...")),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.linking.prelink", Suffix: ".pattern"},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.linking.prelink", ".pattern", false)
}),

types.BareCommand(func(ctx *types.Context) error {
verboseInfoOut, err := phases.Linker(
Expand All @@ -164,15 +183,25 @@ func (s *Builder) Run(ctx *types.Context) error {
return err
}),

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.linking.postlink", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.linking.postlink", ".pattern", true)
}),

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.objcopy.preobjcopy", Suffix: ".pattern"},
&RecipeByPrefixSuffixRunner{Prefix: "recipe.objcopy.", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.objcopy.postobjcopy", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.objcopy.preobjcopy", ".pattern", false)
}),
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.objcopy.", ".pattern", true)
}),
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.objcopy.postobjcopy", ".pattern", true)
}),

&MergeSketchWithBootloader{},

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.postbuild", ".pattern", true)
}),
}

ctx.Progress.AddSubSteps(len(commands) + 5)
Expand Down Expand Up @@ -259,7 +288,9 @@ func (s *Preprocess) Run(ctx *types.Context) error {
commands := []types.Command{
&ContainerBuildOptions{},

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"},
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.prebuild", ".pattern", false)
}),

types.BareCommand(func(ctx *types.Context) error {
ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride, ctx.SketchBuildPath)
Expand Down Expand Up @@ -343,3 +374,12 @@ func logIfVerbose(warn bool, msg string) types.BareCommand {
return nil
})
}

func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase bool) error {
return RecipeByPrefixSuffixRunner(
prefix, suffix, skipIfOnlyUpdatingCompilationDatabase,
ctx.OnlyUpdateCompilationDatabase, ctx.Verbose,
ctx.BuildProperties, ctx.Stdout, ctx.Stderr,
func(msg string) { ctx.Info(msg) },
)
}
37 changes: 19 additions & 18 deletions legacy/builder/recipe_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@ package builder

import (
"fmt"
"io"
"sort"
"strings"

"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/arduino/builder/utils"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type RecipeByPrefixSuffixRunner struct {
Prefix string
Suffix string
SkipIfOnlyUpdatingCompilationDatabase bool
}

func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
logrus.Debugf(fmt.Sprintf("Looking for recipes like %s", s.Prefix+"*"+s.Suffix))
func RecipeByPrefixSuffixRunner(
prefix, suffix string,
skipIfOnlyUpdatingCompilationDatabase, onlyUpdateCompilationDatabase, verbose bool,
buildProps *properties.Map,
stdoutWriter, stderrWriter io.Writer,
printInfoFn func(string),
) error {
logrus.Debugf(fmt.Sprintf("Looking for recipes like %s", prefix+"*"+suffix))

buildProperties := ctx.BuildProperties.Clone()
recipes := findRecipes(buildProperties, s.Prefix, s.Suffix)
// TODO is it necessary to use Clone?
buildProperties := buildProps.Clone()
recipes := findRecipes(buildProperties, prefix, suffix)

// TODO is it necessary to use Clone?
properties := buildProperties.Clone()
for _, recipe := range recipes {
logrus.Debugf(fmt.Sprintf("Running recipe: %s", recipe))
Expand All @@ -48,24 +50,23 @@ func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
return errors.WithStack(err)
}

if ctx.OnlyUpdateCompilationDatabase && s.SkipIfOnlyUpdatingCompilationDatabase {
if ctx.Verbose {
ctx.Info(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " ")))
if onlyUpdateCompilationDatabase && skipIfOnlyUpdatingCompilationDatabase {
if verbose {
printInfoFn(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " ")))
}
return nil
}

verboseInfo, _, _, err := utils.ExecCommand(ctx.Verbose, ctx.Stdout, ctx.Stderr, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if ctx.Verbose {
ctx.Info(string(verboseInfo))
verboseInfo, _, _, err := utils.ExecCommand(verbose, stdoutWriter, stderrWriter, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if verbose {
printInfoFn(string(verboseInfo))
}
if err != nil {
return errors.WithStack(err)
}
}

return nil

}

func findRecipes(buildProperties *properties.Map, patternPrefix string, patternSuffix string) []string {
Expand Down
46 changes: 0 additions & 46 deletions legacy/builder/test/recipe_runner_test.go

This file was deleted.

0 comments on commit 057c40e

Please sign in to comment.