Skip to content

Commit 4a8523b

Browse files
authored
remove commands in favor of a new builder API (#264)
1 parent 519d87a commit 4a8523b

File tree

6 files changed

+119
-215
lines changed

6 files changed

+119
-215
lines changed

Diff for: arduino/builder/builder.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to [email protected].
15+
16+
package builder
17+
18+
import (
19+
"crypto/md5"
20+
"encoding/hex"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
25+
"github.com/pkg/errors"
26+
)
27+
28+
// GenBuildPath generates a suitable name for the build folder
29+
func GenBuildPath(sketchPath string) string {
30+
md5SumBytes := md5.Sum([]byte(sketchPath))
31+
md5Sum := strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))
32+
33+
return filepath.Join(os.TempDir(), "arduino-sketch-"+md5Sum)
34+
}
35+
36+
// EnsureBuildPathExists creates the build path if doesn't already exists.
37+
func EnsureBuildPathExists(path string) error {
38+
if err := os.MkdirAll(path, os.FileMode(0755)); err != nil {
39+
return errors.Wrap(err, "unable to create build path")
40+
}
41+
return nil
42+
}

Diff for: arduino/builder/builder_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to [email protected].
15+
16+
package builder_test
17+
18+
import (
19+
"fmt"
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/arduino/arduino-cli/arduino/builder"
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func tmpDirOrDie() string {
30+
dir, err := ioutil.TempDir(os.TempDir(), "builder_test")
31+
if err != nil {
32+
panic(fmt.Sprintf("error creating tmp dir: %v", err))
33+
}
34+
return dir
35+
}
36+
37+
func TestGenBuildPath(t *testing.T) {
38+
want := filepath.Join(os.TempDir(), "arduino-sketch-ACBD18DB4CC2F85CEDEF654FCCC4A4D8")
39+
assert.Equal(t, want, builder.GenBuildPath("foo"))
40+
}
41+
42+
func TestEnsureBuildPathExists(t *testing.T) {
43+
tmp := tmpDirOrDie()
44+
defer os.RemoveAll(tmp)
45+
bp := filepath.Join(tmp, "build_path")
46+
47+
assert.Nil(t, builder.EnsureBuildPathExists(bp))
48+
_, err := os.Stat(bp)
49+
assert.Nil(t, err)
50+
51+
// run again over an existing folder
52+
assert.Nil(t, builder.EnsureBuildPathExists(bp))
53+
_, err = os.Stat(bp)
54+
assert.Nil(t, err)
55+
}

Diff for: legacy/builder/builder.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ import (
3535
"strconv"
3636
"time"
3737

38+
bldr "github.com/arduino/arduino-cli/arduino/builder"
3839
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
3940
"github.com/arduino/arduino-cli/legacy/builder/constants"
4041
"github.com/arduino/arduino-cli/legacy/builder/i18n"
4142
"github.com/arduino/arduino-cli/legacy/builder/phases"
4243
"github.com/arduino/arduino-cli/legacy/builder/types"
4344
"github.com/arduino/arduino-cli/legacy/builder/utils"
45+
"github.com/arduino/go-paths-helper"
4446
)
4547

4648
var MAIN_FILE_VALID_EXTENSIONS = map[string]bool{".ino": true, ".pde": true}
@@ -54,10 +56,15 @@ const DEFAULT_SOFTWARE = "ARDUINO"
5456
type Builder struct{}
5557

5658
func (s *Builder) Run(ctx *types.Context) error {
57-
commands := []types.Command{
58-
&GenerateBuildPathIfMissing{},
59-
&EnsureBuildPathExists{},
59+
if ctx.BuildPath == nil {
60+
ctx.BuildPath = paths.New(bldr.GenBuildPath(ctx.SketchLocation.String()))
61+
}
62+
63+
if err := bldr.EnsureBuildPathExists(ctx.BuildPath.String()); err != nil {
64+
return err
65+
}
6066

67+
commands := []types.Command{
6168
&ContainerSetupHardwareToolsLibsSketchAndProps{},
6269

6370
&ContainerBuildOptions{},
@@ -141,10 +148,15 @@ func (s *PreprocessSketch) Run(ctx *types.Context) error {
141148
type Preprocess struct{}
142149

143150
func (s *Preprocess) Run(ctx *types.Context) error {
144-
commands := []types.Command{
145-
&GenerateBuildPathIfMissing{},
146-
&EnsureBuildPathExists{},
151+
if ctx.BuildPath == nil {
152+
ctx.BuildPath = paths.New(bldr.GenBuildPath(ctx.SketchLocation.String()))
153+
}
147154

155+
if err := bldr.EnsureBuildPathExists(ctx.BuildPath.String()); err != nil {
156+
return err
157+
}
158+
159+
commands := []types.Command{
148160
&ContainerSetupHardwareToolsLibsSketchAndProps{},
149161

150162
&ContainerBuildOptions{},
@@ -168,9 +180,11 @@ func (s *Preprocess) Run(ctx *types.Context) error {
168180
type ParseHardwareAndDumpBuildProperties struct{}
169181

170182
func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error {
171-
commands := []types.Command{
172-
&GenerateBuildPathIfMissing{},
183+
if ctx.BuildPath == nil {
184+
ctx.BuildPath = paths.New(bldr.GenBuildPath(ctx.SketchLocation.String()))
185+
}
173186

187+
commands := []types.Command{
174188
&ContainerSetupHardwareToolsLibsSketchAndProps{},
175189

176190
&DumpBuildProperties{},

Diff for: legacy/builder/ensure_buildpath_exists.go

-44
This file was deleted.

Diff for: legacy/builder/generate_buildpath_if_missing.go

-61
This file was deleted.

Diff for: legacy/builder/test/generate_buildpath_if_missing_test.go

-102
This file was deleted.

0 commit comments

Comments
 (0)