Skip to content

Commit 8af70b5

Browse files
committed
changed SketchSaver command into builder function
1 parent 4a8523b commit 8af70b5

8 files changed

+116
-69
lines changed

Diff for: arduino/builder/sketch.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
"io/ioutil"
20+
"os"
21+
"path/filepath"
22+
23+
"github.com/pkg/errors"
24+
)
25+
26+
// SaveSketch saves a preprocessed .cpp sketch file on disk
27+
func SaveSketch(sketchName string, source string, buildPath string) error {
28+
29+
if err := os.MkdirAll(buildPath, os.FileMode(0755)); err != nil {
30+
return errors.Wrap(err, "unable to create a folder to save the sketch")
31+
}
32+
33+
destFile := filepath.Join(buildPath, sketchName+".cpp")
34+
35+
if err := ioutil.WriteFile(destFile, []byte(source), os.FileMode(0644)); err != nil {
36+
return errors.Wrap(err, "unable to save the sketch on disk")
37+
}
38+
39+
return nil
40+
}

Diff for: arduino/builder/sketch_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
"io/ioutil"
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/arduino/arduino-cli/arduino/builder"
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestSaveSketch(t *testing.T) {
29+
sketchName := t.Name() + ".ino"
30+
outName := sketchName + ".cpp"
31+
sketchFile := filepath.Join("testdata", sketchName)
32+
tmp := tmpDirOrDie()
33+
defer os.RemoveAll(tmp)
34+
source, err := ioutil.ReadFile(sketchFile)
35+
if err != nil {
36+
t.Fatalf("unable to read golden file %s: %v", sketchFile, err)
37+
}
38+
39+
builder.SaveSketch(sketchName, string(source), tmp)
40+
41+
out, err := ioutil.ReadFile(filepath.Join(tmp, outName))
42+
if err != nil {
43+
t.Fatalf("unable to read output file %s: %v", outName, err)
44+
}
45+
46+
assert.Equal(t, source, out)
47+
}

Diff for: arduino/builder/testdata/TestSaveSketch.ino

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <Bridge.h>
2+
#include <IRremote.h>
3+
#include <IRremoteInt.h>
4+
5+
void setup() {}
6+
void loop() {}

Diff for: legacy/builder/container_add_prototypes.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package builder
3131

3232
import (
33+
bldr "github.com/arduino/arduino-cli/arduino/builder"
3334
"github.com/arduino/arduino-cli/legacy/builder/constants"
3435
"github.com/arduino/arduino-cli/legacy/builder/i18n"
3536
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -56,7 +57,6 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
5657
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E},
5758
&CTagsRunner{},
5859
&PrototypesAdder{},
59-
&SketchSaver{},
6060
}
6161

6262
for _, command := range commands {
@@ -67,5 +67,9 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
6767
}
6868
}
6969

70+
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
71+
return i18n.WrapError(err)
72+
}
73+
7074
return nil
7175
}

Diff for: legacy/builder/container_merge_copy_sketch_files.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,25 @@
3030
package builder
3131

3232
import (
33+
bldr "github.com/arduino/arduino-cli/arduino/builder"
3334
"github.com/arduino/arduino-cli/legacy/builder/i18n"
3435
"github.com/arduino/arduino-cli/legacy/builder/types"
3536
)
3637

3738
type ContainerMergeCopySketchFiles struct{}
3839

3940
func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error {
40-
commands := []types.Command{
41-
&SketchSourceMerger{},
42-
&SketchSaver{},
43-
&AdditionalSketchFilesCopier{},
41+
if err := new(SketchSourceMerger).Run(ctx); err != nil {
42+
return i18n.WrapError(err)
4443
}
4544

46-
for _, command := range commands {
47-
PrintRingNameIfDebug(ctx, command)
48-
err := command.Run(ctx)
49-
if err != nil {
50-
return i18n.WrapError(err)
51-
}
45+
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
46+
return i18n.WrapError(err)
5247
}
5348

54-
return nil
49+
if err := new(AdditionalSketchFilesCopier).Run(ctx); err != nil {
50+
return i18n.WrapError(err)
51+
}
5552

53+
return nil
5654
}

Diff for: legacy/builder/create_cmake_rule.go

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
130130
//&ContainerMergeCopySketchFiles{},
131131
&ContainerAddPrototypes{},
132132
//&FilterSketchSource{Source: &ctx.Source, RemoveLineMarkers: true},
133-
//&SketchSaver{},
134133
}
135134

136135
for _, command := range commands {

Diff for: legacy/builder/preprocess_sketch.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"runtime"
3838
"strings"
3939

40+
bldr "github.com/arduino/arduino-cli/arduino/builder"
4041
"github.com/arduino/arduino-cli/legacy/builder/constants"
4142
"github.com/arduino/arduino-cli/legacy/builder/i18n"
4243
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -66,12 +67,6 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
6667
return i18n.WrapError(err)
6768
}
6869

69-
if ctx.CodeCompleteAt != "" {
70-
commands = append(commands, &OutputCodeCompletions{})
71-
} else {
72-
commands = append(commands, &SketchSaver{})
73-
}
74-
7570
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E), ctx.IncludeFolders)
7671

7772
for _, command := range commands {
@@ -82,7 +77,14 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
8277
}
8378
}
8479

85-
return nil
80+
var err error
81+
if ctx.CodeCompleteAt != "" {
82+
err = new(OutputCodeCompletions).Run(ctx)
83+
} else {
84+
err = bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String())
85+
}
86+
87+
return err
8688
}
8789

8890
type ArduinoPreprocessorRunner struct{}

Diff for: legacy/builder/sketch_saver.go

-49
This file was deleted.

0 commit comments

Comments
 (0)