Skip to content

Changed AdditionalSketchFilesCopier legacy command into new builder API function #293

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 3 commits into from
Jul 30, 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
74 changes: 66 additions & 8 deletions arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package builder

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -39,16 +40,16 @@ func QuoteCppString(str string) string {
return "\"" + str + "\""
}

// SaveSketchItemCpp saves a preprocessed .cpp sketch file on disk
func SaveSketchItemCpp(item *sketch.Item, buildPath string) error {
// SketchSaveItemCpp saves a preprocessed .cpp sketch file on disk
func SketchSaveItemCpp(item *sketch.Item, destPath string) error {

sketchName := filepath.Base(item.Path)

if err := os.MkdirAll(buildPath, os.FileMode(0755)); err != nil {
if err := os.MkdirAll(destPath, os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create a folder to save the sketch")
}

destFile := filepath.Join(buildPath, sketchName+".cpp")
destFile := filepath.Join(destPath, sketchName+".cpp")

if err := ioutil.WriteFile(destFile, item.Source, os.FileMode(0644)); err != nil {
return errors.Wrap(err, "unable to save the sketch on disk")
Expand All @@ -57,10 +58,10 @@ func SaveSketchItemCpp(item *sketch.Item, buildPath string) error {
return nil
}

// LoadSketch collects all the files composing a sketch.
// SketchLoad 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) {
func SketchLoad(sketchPath, buildPath string) (*sketch.Sketch, error) {
stat, err := os.Stat(sketchPath)
if err != nil {
return nil, errors.Wrap(err, "unable to stat Sketch location")
Expand Down Expand Up @@ -133,8 +134,8 @@ func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) {
return sketch.New(sketchFolder, mainSketchFile, buildPath, files)
}

// MergeSketchSources merges all the source files included in a sketch
func MergeSketchSources(sketch *sketch.Sketch) (int, string) {
// SketchMergeSources merges all the source files included in a sketch
func SketchMergeSources(sketch *sketch.Sketch) (int, string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if these functions (SketchMergeSources, SketchCopyAdditionalFiles, etc.) may become methods of sketch.Sketch?

lineOffset := 0
mergedSource := ""

Expand All @@ -155,3 +156,60 @@ func MergeSketchSources(sketch *sketch.Sketch) (int, string) {

return lineOffset, mergedSource
}

// SketchCopyAdditionalFiles copies the additional files for a sketch to the
// specified destination directory.
func SketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath string) error {
if err := os.MkdirAll(destPath, os.FileMode(0755)); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at some point we may think to replace those path strings with paths.Path objects that have a much cleaner API to handle file operations.

return errors.Wrap(err, "unable to create a folder to save the sketch files")
}

for _, item := range sketch.AdditionalFiles {
relpath, err := filepath.Rel(sketch.LocationPath, item.Path)
if err != nil {
return errors.Wrap(err, "unable to compute relative path to the sketch for the item")
}

targetPath := filepath.Join(destPath, relpath)
// create the directory containing the target
if err = os.MkdirAll(filepath.Dir(targetPath), os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create the folder containing the item")
}

err = writeIfDifferent(item.Path, targetPath)
if err != nil {
return errors.Wrap(err, "unable to write to destination file")
}
}

return nil
}

func writeIfDifferent(sourcePath, destPath string) error {
// read the source file
newbytes, err := ioutil.ReadFile(sourcePath)
if err != nil {
return errors.Wrap(err, "unable to read contents of the source item")
}

// check whether the destination file exists
_, err = os.Stat(destPath)
if os.IsNotExist(err) {
// write directly
return ioutil.WriteFile(destPath, newbytes, os.FileMode(0644))
}

// read the destination file if it ex
existingBytes, err := ioutil.ReadFile(destPath)
if err != nil {
return errors.Wrap(err, "unable to read contents of the destination item")
}

// overwrite if contents are different
if bytes.Compare(existingBytes, newbytes) != 0 {
return ioutil.WriteFile(destPath, newbytes, os.FileMode(0644))
}

// source and destination are the same, don't write anything
return nil
}
121 changes: 78 additions & 43 deletions arduino/builder/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package builder_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -24,7 +25,7 @@ import (

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

func TestSaveSketch(t *testing.T) {
Expand All @@ -38,64 +39,64 @@ func TestSaveSketch(t *testing.T) {
t.Fatalf("unable to read golden file %s: %v", sketchFile, err)
}

builder.SaveSketchItemCpp(&sketch.Item{Path: sketchName, Source: source}, tmp)
builder.SketchSaveItemCpp(&sketch.Item{Path: sketchName, Source: 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)
require.Equal(t, source, out)
}

func TestLoadSketchFolder(t *testing.T) {
// pass the path to the sketch folder
sketchPath := filepath.Join("testdata", t.Name())
mainFilePath := filepath.Join(sketchPath, t.Name()+".ino")
s, err := builder.LoadSketch(sketchPath, "")
assert.Nil(t, err)
assert.NotNil(t, s)
assert.Equal(t, mainFilePath, s.MainFile.Path)
assert.Equal(t, sketchPath, s.LocationPath)
assert.Len(t, s.OtherSketchFiles, 2)
assert.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
assert.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
assert.Len(t, s.AdditionalFiles, 3)
assert.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
assert.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
assert.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
s, err := builder.SketchLoad(sketchPath, "")
require.Nil(t, err)
require.NotNil(t, s)
require.Equal(t, mainFilePath, s.MainFile.Path)
require.Equal(t, sketchPath, s.LocationPath)
require.Len(t, s.OtherSketchFiles, 2)
require.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
require.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
require.Len(t, s.AdditionalFiles, 3)
require.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
require.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))

// pass the path to the main file
sketchPath = mainFilePath
s, err = builder.LoadSketch(sketchPath, "")
assert.Nil(t, err)
assert.NotNil(t, s)
assert.Equal(t, mainFilePath, s.MainFile.Path)
assert.Len(t, s.OtherSketchFiles, 2)
assert.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
assert.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
assert.Len(t, s.AdditionalFiles, 3)
assert.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
assert.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
assert.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
s, err = builder.SketchLoad(sketchPath, "")
require.Nil(t, err)
require.NotNil(t, s)
require.Equal(t, mainFilePath, s.MainFile.Path)
require.Len(t, s.OtherSketchFiles, 2)
require.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
require.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
require.Len(t, s.AdditionalFiles, 3)
require.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
require.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
}

func TestLoadSketchFolderWrongMain(t *testing.T) {
sketchPath := filepath.Join("testdata", t.Name())
_, err := builder.LoadSketch(sketchPath, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "unable to find the main sketch file")
_, err := builder.SketchLoad(sketchPath, "")
require.Error(t, err)
require.Contains(t, err.Error(), "unable to find the main sketch file")

_, err = builder.LoadSketch("does/not/exist", "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "no such file or directory")
_, err = builder.SketchLoad("does/not/exist", "")
require.Error(t, err)
require.Contains(t, err.Error(), "no such file or directory")
}

func TestMergeSketchSources(t *testing.T) {
// borrow the sketch from TestLoadSketchFolder to avoid boilerplate
s, err := builder.LoadSketch(filepath.Join("testdata", "TestLoadSketchFolder"), "")
assert.Nil(t, err)
assert.NotNil(t, s)
s, err := builder.SketchLoad(filepath.Join("testdata", "TestLoadSketchFolder"), "")
require.Nil(t, err)
require.NotNil(t, s)

// load expected result
mergedPath := filepath.Join("testdata", t.Name()+".txt")
Expand All @@ -104,17 +105,51 @@ func TestMergeSketchSources(t *testing.T) {
t.Fatalf("unable to read golden file %s: %v", mergedPath, err)
}

offset, source := builder.MergeSketchSources(s)
assert.Equal(t, 2, offset)
assert.Equal(t, string(mergedBytes), source)
offset, source := builder.SketchMergeSources(s)
require.Equal(t, 2, offset)
require.Equal(t, string(mergedBytes), source)
}

func TestMergeSketchSourcesArduinoIncluded(t *testing.T) {
s, err := builder.LoadSketch(filepath.Join("testdata", t.Name()), "")
assert.Nil(t, err)
assert.NotNil(t, s)
s, err := builder.SketchLoad(filepath.Join("testdata", t.Name()), "")
require.Nil(t, err)
require.NotNil(t, s)

// ensure not to include Arduino.h when it's already there
_, source := builder.MergeSketchSources(s)
assert.Equal(t, 1, strings.Count(source, "<Arduino.h>"))
_, source := builder.SketchMergeSources(s)
require.Equal(t, 1, strings.Count(source, "<Arduino.h>"))
}

func TestCopyAdditionalFiles(t *testing.T) {
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)

// load the golden sketch
s1, err := builder.SketchLoad(filepath.Join("testdata", t.Name()), "")
require.Nil(t, err)
require.Len(t, s1.AdditionalFiles, 1)

// copy the sketch over, create a fake main file we don't care about it
// but we need it for `SketchLoad` to suceed later
err = builder.SketchCopyAdditionalFiles(s1, tmp)
require.Nil(t, err)
fakeIno := filepath.Join(tmp, fmt.Sprintf("%s.ino", filepath.Base(tmp)))
require.Nil(t, ioutil.WriteFile(fakeIno, []byte{}, os.FileMode(0644)))

// compare
s2, err := builder.SketchLoad(tmp, "")
require.Nil(t, err)
require.Len(t, s2.AdditionalFiles, 1)

// save file info
info1, err := os.Stat(s2.AdditionalFiles[0].Path)
require.Nil(t, err)

// copy again
err = builder.SketchCopyAdditionalFiles(s1, tmp)
require.Nil(t, err)

// verify file hasn't changed
info2, err := os.Stat(s2.AdditionalFiles[0].Path)
require.Equal(t, info1.ModTime(), info2.ModTime())
}
Empty file.
85 changes: 0 additions & 85 deletions legacy/builder/additional_sketch_files_copier.go

This file was deleted.

2 changes: 1 addition & 1 deletion legacy/builder/container_add_prototypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
}
}

if err := bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String()); err != nil {
if err := bldr.SketchSaveItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String()); err != nil {
return i18n.WrapError(err)
}

Expand Down
Loading