Skip to content

remove commands in favor of a new builder API #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of arduino-cli.
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].

package builder

import (
"crypto/md5"
"encoding/hex"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
)

// GenBuildPath generates a suitable name for the build folder
func GenBuildPath(sketchPath string) string {
md5SumBytes := md5.Sum([]byte(sketchPath))
md5Sum := strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))

return filepath.Join(os.TempDir(), "arduino-sketch-"+md5Sum)
}

// EnsureBuildPathExists creates the build path if doesn't already exists.
func EnsureBuildPathExists(path string) error {
if err := os.MkdirAll(path, os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create build path")
}
return nil
}
55 changes: 55 additions & 0 deletions arduino/builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of arduino-cli.
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].

package builder_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/arduino/arduino-cli/arduino/builder"
"github.com/stretchr/testify/assert"
)

func tmpDirOrDie() string {
dir, err := ioutil.TempDir(os.TempDir(), "builder_test")
if err != nil {
panic(fmt.Sprintf("error creating tmp dir: %v", err))
}
return dir
}

func TestGenBuildPath(t *testing.T) {
want := filepath.Join(os.TempDir(), "arduino-sketch-ACBD18DB4CC2F85CEDEF654FCCC4A4D8")
assert.Equal(t, want, builder.GenBuildPath("foo"))
}

func TestEnsureBuildPathExists(t *testing.T) {
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)
bp := filepath.Join(tmp, "build_path")

assert.Nil(t, builder.EnsureBuildPathExists(bp))
_, err := os.Stat(bp)
assert.Nil(t, err)

// run again over an existing folder
assert.Nil(t, builder.EnsureBuildPathExists(bp))
_, err = os.Stat(bp)
assert.Nil(t, err)
}
30 changes: 22 additions & 8 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ import (
"strconv"
"time"

bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/phases"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/legacy/builder/utils"
"github.com/arduino/go-paths-helper"
)

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

func (s *Builder) Run(ctx *types.Context) error {
commands := []types.Command{
&GenerateBuildPathIfMissing{},
&EnsureBuildPathExists{},
if ctx.BuildPath == nil {
ctx.BuildPath = paths.New(bldr.GenBuildPath(ctx.SketchLocation.String()))
}

if err := bldr.EnsureBuildPathExists(ctx.BuildPath.String()); err != nil {
return err
}

commands := []types.Command{
&ContainerSetupHardwareToolsLibsSketchAndProps{},

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

func (s *Preprocess) Run(ctx *types.Context) error {
commands := []types.Command{
&GenerateBuildPathIfMissing{},
&EnsureBuildPathExists{},
if ctx.BuildPath == nil {
ctx.BuildPath = paths.New(bldr.GenBuildPath(ctx.SketchLocation.String()))
}

if err := bldr.EnsureBuildPathExists(ctx.BuildPath.String()); err != nil {
return err
}

commands := []types.Command{
&ContainerSetupHardwareToolsLibsSketchAndProps{},

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

func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error {
commands := []types.Command{
&GenerateBuildPathIfMissing{},
if ctx.BuildPath == nil {
ctx.BuildPath = paths.New(bldr.GenBuildPath(ctx.SketchLocation.String()))
}

commands := []types.Command{
&ContainerSetupHardwareToolsLibsSketchAndProps{},

&DumpBuildProperties{},
Expand Down
44 changes: 0 additions & 44 deletions legacy/builder/ensure_buildpath_exists.go

This file was deleted.

61 changes: 0 additions & 61 deletions legacy/builder/generate_buildpath_if_missing.go

This file was deleted.

102 changes: 0 additions & 102 deletions legacy/builder/test/generate_buildpath_if_missing_test.go

This file was deleted.