Skip to content

Commit

Permalink
refactor CreateBuildOptionsMap 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 057c40e commit de0e25e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 98 deletions.
11 changes: 10 additions & 1 deletion legacy/builder/container_build_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ import (
type ContainerBuildOptions struct{}

func (s *ContainerBuildOptions) Run(ctx *types.Context) error {
buildPropertiesJSON, err := CreateBuildOptionsMap(
ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs,
ctx.BuiltInLibrariesDirs, ctx.Sketch, ctx.CustomBuildProperties,
ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"),
)
if err != nil {
return errors.WithStack(err)
}
ctx.BuildOptionsJson = buildPropertiesJSON

commands := []types.Command{
&CreateBuildOptionsMap{},
&LoadPreviousBuildOptionsMap{},
&WipeoutBuildPathIfBuildOptionsChanged{},
&StoreBuildOptionsMap{},
Expand Down
43 changes: 35 additions & 8 deletions legacy/builder/create_build_options_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,47 @@ package builder

import (
"encoding/json"
"strings"

"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)

type CreateBuildOptionsMap struct{}
func CreateBuildOptionsMap(
hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList,
builtInLibrariesDirs *paths.Path,
sketch *sketch.Sketch,
customBuildProperties []string,
fqbn, compilerOptimizationFlags string,
) (string, error) {
opts := properties.NewMap()
opts.Set("hardwareFolders", strings.Join(hardwareDirs.AsStrings(), ","))
opts.Set("builtInToolsFolders", strings.Join(builtInToolsDirs.AsStrings(), ","))
if builtInLibrariesDirs != nil {
opts.Set("builtInLibrariesFolders", builtInLibrariesDirs.String())
}
opts.Set("otherLibrariesFolders", strings.Join(otherLibrariesDirs.AsStrings(), ","))
opts.SetPath("sketchLocation", sketch.FullPath)
var additionalFilesRelative []string
absPath := sketch.FullPath.Parent()
for _, f := range sketch.AdditionalFiles {
relPath, err := f.RelTo(absPath)
if err != nil {
continue // ignore
}
additionalFilesRelative = append(additionalFilesRelative, relPath.String())
}
opts.Set("fqbn", fqbn)
opts.Set("customBuildProperties", strings.Join(customBuildProperties, ","))
opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ","))
opts.Set("compiler.optimization_flags", compilerOptimizationFlags)

func (s *CreateBuildOptionsMap) Run(ctx *types.Context) error {
buildOptions := ctx.ExtractBuildOptions()
bytes, err := json.MarshalIndent(buildOptions, "", " ")
buildOptionsJSON, err := json.MarshalIndent(opts, "", " ")
if err != nil {
return errors.WithStack(err)
return "", errors.WithStack(err)
}
ctx.BuildOptionsJson = string(bytes)

return nil
return string(buildOptionsJSON), nil
}
9 changes: 6 additions & 3 deletions legacy/builder/test/create_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ func TestCreateBuildOptionsMap(t *testing.T) {
BuildProperties: properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}),
}

create := builder.CreateBuildOptionsMap{}
err := create.Run(ctx)
buildPropertiesJSON, err := builder.CreateBuildOptionsMap(
ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs,
ctx.BuiltInLibrariesDirs, ctx.Sketch, ctx.CustomBuildProperties,
ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"),
)
require.NoError(t, err)

require.Equal(t, `{
Expand All @@ -51,5 +54,5 @@ func TestCreateBuildOptionsMap(t *testing.T) {
"hardwareFolders": "hardware,hardware2",
"otherLibrariesFolders": "libraries",
"sketchLocation": "sketchLocation"
}`, ctx.BuildOptionsJson)
}`, buildPropertiesJSON)
}
12 changes: 8 additions & 4 deletions legacy/builder/test/store_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ func TestStoreBuildOptionsMap(t *testing.T) {
buildPath := SetupBuildPath(t, ctx)
defer buildPath.RemoveAll()

commands := []types.Command{
&builder.CreateBuildOptionsMap{},
&builder.StoreBuildOptionsMap{},
}
buildPropertiesJSON, err := builder.CreateBuildOptionsMap(
ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs,
ctx.BuiltInLibrariesDirs, ctx.Sketch, ctx.CustomBuildProperties,
ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"),
)
require.NoError(t, err)
ctx.BuildOptionsJson = buildPropertiesJSON

commands := []types.Command{&builder.StoreBuildOptionsMap{}}
for _, command := range commands {
err := command.Run(ctx)
require.NoError(t, err)
Expand Down
26 changes: 0 additions & 26 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"

"github.com/arduino/arduino-cli/arduino/builder"
Expand Down Expand Up @@ -106,31 +105,6 @@ type Context struct {
SourceOverride map[string]string
}

func (ctx *Context) ExtractBuildOptions() *properties.Map {
opts := properties.NewMap()
opts.Set("hardwareFolders", strings.Join(ctx.HardwareDirs.AsStrings(), ","))
opts.Set("builtInToolsFolders", strings.Join(ctx.BuiltInToolsDirs.AsStrings(), ","))
if ctx.BuiltInLibrariesDirs != nil {
opts.Set("builtInLibrariesFolders", ctx.BuiltInLibrariesDirs.String())
}
opts.Set("otherLibrariesFolders", strings.Join(ctx.OtherLibrariesDirs.AsStrings(), ","))
opts.SetPath("sketchLocation", ctx.Sketch.FullPath)
var additionalFilesRelative []string
absPath := ctx.Sketch.FullPath.Parent()
for _, f := range ctx.Sketch.AdditionalFiles {
relPath, err := f.RelTo(absPath)
if err != nil {
continue // ignore
}
additionalFilesRelative = append(additionalFilesRelative, relPath.String())
}
opts.Set("fqbn", ctx.FQBN.String())
opts.Set("customBuildProperties", strings.Join(ctx.CustomBuildProperties, ","))
opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ","))
opts.Set("compiler.optimization_flags", ctx.BuildProperties.Get("compiler.optimization_flags"))
return opts
}

func (ctx *Context) PushProgress() {
if ctx.ProgressCB != nil {
ctx.ProgressCB(&rpc.TaskProgress{
Expand Down
56 changes: 0 additions & 56 deletions legacy/builder/types/context_test.go

This file was deleted.

0 comments on commit de0e25e

Please sign in to comment.