Commit 2a6a03e4 authored by Massimiliano Pippi's avatar Massimiliano Pippi

use sketch.Item

parent 6fb3d481
......@@ -19,12 +19,18 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/pkg/errors"
)
// SaveSketch saves a preprocessed .cpp sketch file on disk
func SaveSketch(sketchName string, source string, buildPath string) error {
// SaveSketchItemCpp saves a preprocessed .cpp sketch file on disk
func SaveSketchItemCpp(item *sketch.Item, buildPath string) error {
sketchName := filepath.Base(item.Path)
if err := os.MkdirAll(buildPath, os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create a folder to save the sketch")
......@@ -32,9 +38,85 @@ func SaveSketch(sketchName string, source string, buildPath string) error {
destFile := filepath.Join(buildPath, sketchName+".cpp")
if err := ioutil.WriteFile(destFile, []byte(source), os.FileMode(0644)); err != nil {
if err := ioutil.WriteFile(destFile, item.Source, os.FileMode(0644)); err != nil {
return errors.Wrap(err, "unable to save the sketch on disk")
}
return nil
}
// LoadSketch collects all the files composing a sketch.
// The parameter `sketchPath` holds a path pointing to a single sketch file or a sketch folder,
// the path must be absolute.
func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) {
stat, err := os.Stat(sketchPath)
if err != nil {
return nil, errors.Wrap(err, "unable to stat Sketch location")
}
var sketchFolder, mainSketchFile string
// if a sketch folder was passed, save the parent and point sketchPath to the main .ino file
if stat.IsDir() {
sketchFolder = sketchPath
mainSketchFile = filepath.Join(sketchPath, stat.Name()+".ino")
// in the case a dir was passed, ensure the main file exists and is readable
f, err := os.Open(mainSketchFile)
if err != nil {
return nil, errors.Wrap(err, "unable to find the main sketch file")
}
f.Close()
} else {
sketchFolder = filepath.Dir(sketchPath)
mainSketchFile = sketchPath
}
// collect all the sketch files
var files []string
err = filepath.Walk(sketchFolder, func(path string, info os.FileInfo, err error) error {
// ignore hidden files and skip hidden directories
if strings.HasPrefix(info.Name(), ".") {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
// skip legacy SCM directories
if info.IsDir() && strings.HasPrefix(info.Name(), "CVS") || strings.HasPrefix(info.Name(), "RCS") {
return filepath.SkipDir
}
// ignore directory entries
if info.IsDir() {
return nil
}
// ignore if file extension doesn't match
ext := strings.ToLower(filepath.Ext(path))
_, isMain := globals.MainFileValidExtensions[ext]
_, isAdditional := globals.AdditionalFileValidExtensions[ext]
if !(isMain || isAdditional) {
return nil
}
// check if file is readable
f, err := os.Open(path)
if err != nil {
return nil
}
f.Close()
// collect the file
files = append(files, path)
// done
return nil
})
if err != nil {
return nil, errors.Wrap(err, "there was an error while collecting the sketch files")
}
return sketch.New(sketchFolder, mainSketchFile, buildPath, files)
}
......@@ -22,6 +22,7 @@ import (
"testing"
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/stretchr/testify/assert"
)
......@@ -36,7 +37,7 @@ func TestSaveSketch(t *testing.T) {
t.Fatalf("unable to read golden file %s: %v", sketchFile, err)
}
builder.SaveSketch(sketchName, string(source), tmp)
builder.SaveSketchItemCpp(&sketch.Item{Path: sketchName, Source: source}, tmp)
out, err := ioutil.ReadFile(filepath.Join(tmp, outName))
if err != nil {
......
......@@ -31,6 +31,7 @@ package builder
import (
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
......@@ -67,7 +68,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
}
}
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
if err := bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String()); err != nil {
return i18n.WrapError(err)
}
......
......@@ -31,6 +31,7 @@ package builder
import (
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
)
......@@ -42,7 +43,7 @@ func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error {
return i18n.WrapError(err)
}
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
if err := bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String()); err != nil {
return i18n.WrapError(err)
}
......
......@@ -38,6 +38,7 @@ import (
"strings"
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
......@@ -81,7 +82,7 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
if ctx.CodeCompleteAt != "" {
err = new(OutputCodeCompletions).Run(ctx)
} else {
err = bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String())
err = bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String())
}
return 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