Commit 8af70b5b authored by Massimiliano Pippi's avatar Massimiliano Pippi

changed SketchSaver command into builder function

parent 4a8523b9
// 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 license@arduino.cc.
package builder
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// SaveSketch saves a preprocessed .cpp sketch file on disk
func SaveSketch(sketchName string, source string, buildPath string) error {
if err := os.MkdirAll(buildPath, os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create a folder to save the sketch")
}
destFile := filepath.Join(buildPath, sketchName+".cpp")
if err := ioutil.WriteFile(destFile, []byte(source), os.FileMode(0644)); err != nil {
return errors.Wrap(err, "unable to save the sketch on disk")
}
return nil
}
// 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 license@arduino.cc.
package builder_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/stretchr/testify/assert"
)
func TestSaveSketch(t *testing.T) {
sketchName := t.Name() + ".ino"
outName := sketchName + ".cpp"
sketchFile := filepath.Join("testdata", sketchName)
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)
source, err := ioutil.ReadFile(sketchFile)
if err != nil {
t.Fatalf("unable to read golden file %s: %v", sketchFile, err)
}
builder.SaveSketch(sketchName, string(source), tmp)
out, err := ioutil.ReadFile(filepath.Join(tmp, outName))
if err != nil {
t.Fatalf("unable to read output file %s: %v", outName, err)
}
assert.Equal(t, source, out)
}
#include <Bridge.h>
#include <IRremote.h>
#include <IRremoteInt.h>
void setup() {}
void loop() {}
\ No newline at end of file
......@@ -30,6 +30,7 @@
package builder
import (
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
......@@ -56,7 +57,6 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E},
&CTagsRunner{},
&PrototypesAdder{},
&SketchSaver{},
}
for _, command := range commands {
......@@ -67,5 +67,9 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
}
}
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
return i18n.WrapError(err)
}
return nil
}
......@@ -30,6 +30,7 @@
package builder
import (
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
)
......@@ -37,20 +38,17 @@ import (
type ContainerMergeCopySketchFiles struct{}
func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error {
commands := []types.Command{
&SketchSourceMerger{},
&SketchSaver{},
&AdditionalSketchFilesCopier{},
if err := new(SketchSourceMerger).Run(ctx); err != nil {
return i18n.WrapError(err)
}
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
return i18n.WrapError(err)
}
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
return i18n.WrapError(err)
}
return nil
if err := new(AdditionalSketchFilesCopier).Run(ctx); err != nil {
return i18n.WrapError(err)
}
return nil
}
......@@ -130,7 +130,6 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
//&ContainerMergeCopySketchFiles{},
&ContainerAddPrototypes{},
//&FilterSketchSource{Source: &ctx.Source, RemoveLineMarkers: true},
//&SketchSaver{},
}
for _, command := range commands {
......
......@@ -37,6 +37,7 @@ import (
"runtime"
"strings"
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
......@@ -66,12 +67,6 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
return i18n.WrapError(err)
}
if ctx.CodeCompleteAt != "" {
commands = append(commands, &OutputCodeCompletions{})
} else {
commands = append(commands, &SketchSaver{})
}
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E), ctx.IncludeFolders)
for _, command := range commands {
......@@ -82,7 +77,14 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
}
}
return nil
var err error
if ctx.CodeCompleteAt != "" {
err = new(OutputCodeCompletions).Run(ctx)
} else {
err = bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String())
}
return err
}
type ArduinoPreprocessorRunner struct{}
......
/*
* This file is part of Arduino Builder.
*
* Arduino Builder is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*/
package builder
import (
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
)
type SketchSaver struct{}
func (s *SketchSaver) Run(ctx *types.Context) error {
sketch := ctx.Sketch
sketchBuildPath := ctx.SketchBuildPath
if err := sketchBuildPath.MkdirAll(); err != nil {
return i18n.WrapError(err)
}
err := sketchBuildPath.Join(sketch.MainFile.Name.Base() + ".cpp").WriteFile([]byte(ctx.Source))
return i18n.WrapError(err)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment