Skip to content

Commit

Permalink
Add RootFolderFiles to gRPC LoadSketchResp (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Feb 11, 2021
1 parent cc15dde commit 834c108
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 233 deletions.
25 changes: 25 additions & 0 deletions arduino/builder/sketch_test.go
Expand Up @@ -65,6 +65,11 @@ func TestLoadSketchFolder(t *testing.T) {
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))
require.Len(t, s.RootFolderFiles, 4)
require.Equal(t, "header.h", filepath.Base(s.RootFolderFiles[0].Path))
require.Equal(t, "old.pde", filepath.Base(s.RootFolderFiles[1].Path))
require.Equal(t, "other.ino", filepath.Base(s.RootFolderFiles[2].Path))
require.Equal(t, "s_file.S", filepath.Base(s.RootFolderFiles[3].Path))

// pass the path to the main file
sketchPath = mainFilePath
Expand All @@ -79,6 +84,11 @@ func TestLoadSketchFolder(t *testing.T) {
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))
require.Len(t, s.RootFolderFiles, 4)
require.Equal(t, "header.h", filepath.Base(s.RootFolderFiles[0].Path))
require.Equal(t, "old.pde", filepath.Base(s.RootFolderFiles[1].Path))
require.Equal(t, "other.ino", filepath.Base(s.RootFolderFiles[2].Path))
require.Equal(t, "s_file.S", filepath.Base(s.RootFolderFiles[3].Path))
}

func TestLoadSketchFolderPde(t *testing.T) {
Expand All @@ -97,6 +107,11 @@ func TestLoadSketchFolderPde(t *testing.T) {
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))
require.Len(t, s.RootFolderFiles, 4)
require.Equal(t, "header.h", filepath.Base(s.RootFolderFiles[0].Path))
require.Equal(t, "old.pde", filepath.Base(s.RootFolderFiles[1].Path))
require.Equal(t, "other.ino", filepath.Base(s.RootFolderFiles[2].Path))
require.Equal(t, "s_file.S", filepath.Base(s.RootFolderFiles[3].Path))
}

func TestLoadSketchFolderBothInoAndPde(t *testing.T) {
Expand Down Expand Up @@ -128,6 +143,11 @@ func TestLoadSketchFolderSymlink(t *testing.T) {
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))
require.Len(t, s.RootFolderFiles, 4)
require.Equal(t, "header.h", filepath.Base(s.RootFolderFiles[0].Path))
require.Equal(t, "old.pde", filepath.Base(s.RootFolderFiles[1].Path))
require.Equal(t, "other.ino", filepath.Base(s.RootFolderFiles[2].Path))
require.Equal(t, "s_file.S", filepath.Base(s.RootFolderFiles[3].Path))

// pass the path to the main file
symlinkSketchPath = mainFilePath
Expand All @@ -142,6 +162,11 @@ func TestLoadSketchFolderSymlink(t *testing.T) {
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))
require.Len(t, s.RootFolderFiles, 4)
require.Equal(t, "header.h", filepath.Base(s.RootFolderFiles[0].Path))
require.Equal(t, "old.pde", filepath.Base(s.RootFolderFiles[1].Path))
require.Equal(t, "other.ino", filepath.Base(s.RootFolderFiles[2].Path))
require.Equal(t, "s_file.S", filepath.Base(s.RootFolderFiles[3].Path))
}

func TestLoadSketchFolderIno(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions arduino/sketch/sketch.go
Expand Up @@ -70,6 +70,7 @@ type Sketch struct {
LocationPath string
OtherSketchFiles []*Item
AdditionalFiles []*Item
RootFolderFiles []*Item
}

// New creates an Sketch instance by reading all the files composing a sketch and grouping them
Expand All @@ -95,19 +96,24 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin
// organize the Items
additionalFiles := []*Item{}
otherSketchFiles := []*Item{}
rootFolderFiles := []*Item{}
for p, item := range pathToItem {
ext := strings.ToLower(filepath.Ext(p))
if _, found := globals.MainFileValidExtensions[ext]; found {
// item is a valid main file, see if it's stored at the
// sketch root and ignore if it's not.
if filepath.Dir(p) == sketchFolderPath {
otherSketchFiles = append(otherSketchFiles, item)
rootFolderFiles = append(rootFolderFiles, item)
}
} else if _, found := globals.AdditionalFileValidExtensions[ext]; found {
// item is a valid sketch file, grab it only if the buildPath is empty
// or the file is within the buildPath
if buildPath == "" || !strings.Contains(filepath.Dir(p), buildPath) {
additionalFiles = append(additionalFiles, item)
if filepath.Dir(p) == sketchFolderPath {
rootFolderFiles = append(rootFolderFiles, item)
}
}
} else {
return nil, errors.Errorf("unknown sketch file extension '%s'", ext)
Expand All @@ -116,6 +122,7 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin

sort.Sort(ItemByPath(additionalFiles))
sort.Sort(ItemByPath(otherSketchFiles))
sort.Sort(ItemByPath(rootFolderFiles))

if err := CheckSketchCasing(sketchFolderPath); err != nil {
return nil, err
Expand All @@ -126,6 +133,7 @@ func New(sketchFolderPath, mainFilePath, buildPath string, allFilesPaths []strin
LocationPath: sketchFolderPath,
OtherSketchFiles: otherSketchFiles,
AdditionalFiles: additionalFiles,
RootFolderFiles: rootFolderFiles,
}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions arduino/sketch/sketch_test.go
Expand Up @@ -73,6 +73,9 @@ func TestNew(t *testing.T) {
assert.Equal(t, sketchFolderPath, sketch.LocationPath)
assert.Len(t, sketch.OtherSketchFiles, 0)
assert.Len(t, sketch.AdditionalFiles, 1)
assert.Equal(t, sketch.AdditionalFiles[0].Path, paths.New(sketchFolderPath).Join("other.cpp").String())
assert.Len(t, sketch.RootFolderFiles, 1)
assert.Equal(t, sketch.RootFolderFiles[0].Path, paths.New(sketchFolderPath).Join("other.cpp").String())
}

func TestNewSketchCasingWrong(t *testing.T) {
Expand All @@ -94,6 +97,7 @@ func TestNewSketchCasingCorrect(t *testing.T) {
assert.Equal(t, mainFilePath, sketch.MainFile.Path)
assert.Len(t, sketch.OtherSketchFiles, 0)
assert.Len(t, sketch.AdditionalFiles, 0)
assert.Len(t, sketch.RootFolderFiles, 0)
}

func TestCheckSketchCasingWrong(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions commands/instances.go
Expand Up @@ -744,10 +744,16 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchReq) (*rpc.LoadSketchRes
additionalFiles[i] = file.Path
}

rootFolderFiles := make([]string, len(sketch.RootFolderFiles))
for i, file := range sketch.RootFolderFiles {
rootFolderFiles[i] = file.Path
}

return &rpc.LoadSketchResp{
MainFile: sketch.MainFile.Path,
LocationPath: sketch.LocationPath,
OtherSketchFiles: otherSketchFiles,
AdditionalFiles: additionalFiles,
RootFolderFiles: rootFolderFiles,
}, nil
}

0 comments on commit 834c108

Please sign in to comment.