Skip to content

Commit ebb47c7

Browse files
committed
Support .pde extension by compile when only folder specified (arduino#386)
When only specifying folder, the logic will try both <DirName>.ino and <DirName>.pde as the main sketch file. If both are found, and error is also thrown (it would break on compile later on). Added couple of unit tests also. Signed-off-by: Lluis Campos <[email protected]>
1 parent c30151b commit ebb47c7

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

Diff for: arduino/builder/sketch.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,31 @@ func SketchLoad(sketchPath, buildPath string) (*sketch.Sketch, error) {
108108

109109
var sketchFolder, mainSketchFile string
110110

111-
// if a sketch folder was passed, save the parent and point sketchPath to the main .ino file
111+
// if a sketch folder was passed, save the parent and point sketchPath to the main sketch file
112112
if stat.IsDir() {
113113
sketchFolder = sketchPath
114-
mainSketchFile = filepath.Join(sketchPath, stat.Name()+".ino")
114+
// allowed extensions are .ino and .pde (but not both)
115+
allowedSketchExtensions := [...]string{".ino", ".pde"}
116+
for _, extension := range allowedSketchExtensions {
117+
candidateSketchFile := filepath.Join(sketchPath, stat.Name()+extension)
118+
if _, err := os.Stat(candidateSketchFile); !os.IsNotExist(err) {
119+
if mainSketchFile == "" {
120+
mainSketchFile = candidateSketchFile
121+
} else {
122+
return nil, errors.Errorf("more than one main sketch file found (%v,%v)",
123+
filepath.Base(mainSketchFile),
124+
filepath.Base(candidateSketchFile))
125+
}
126+
}
127+
}
128+
// check that .pde or .ino was found
129+
if mainSketchFile == "" {
130+
return nil, errors.Errorf("unable to find an sketch file in directory %v", sketchFolder)
131+
}
115132
// in the case a dir was passed, ensure the main file exists and is readable
116133
f, err := os.Open(mainSketchFile)
117134
if err != nil {
118-
return nil, errors.Wrap(err, "unable to find the main sketch file")
135+
return nil, errors.Wrap(err, "unable to open the main sketch file")
119136
}
120137
f.Close()
121138
// ensure it is not a directory

Diff for: arduino/builder/sketch_test.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ func TestLoadSketchFolder(t *testing.T) {
8282
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
8383
}
8484

85+
func TestLoadSketchFolderPde(t *testing.T) {
86+
// pass the path to the sketch folder
87+
sketchPath := filepath.Join("testdata", t.Name())
88+
mainFilePath := filepath.Join(sketchPath, t.Name()+".pde")
89+
s, err := builder.SketchLoad(sketchPath, "")
90+
require.Nil(t, err)
91+
require.NotNil(t, s)
92+
require.Equal(t, mainFilePath, s.MainFile.Path)
93+
require.Equal(t, sketchPath, s.LocationPath)
94+
require.Len(t, s.OtherSketchFiles, 2)
95+
require.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
96+
require.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
97+
require.Len(t, s.AdditionalFiles, 3)
98+
require.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
99+
require.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
100+
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
101+
}
102+
103+
func TestLoadSketchFolderBothInoAndPde(t *testing.T) {
104+
// pass the path to the sketch folder containing two main sketches, .ino and .pde
105+
sketchPath := filepath.Join("testdata", t.Name())
106+
_, err := builder.SketchLoad(sketchPath, "")
107+
require.Error(t, err)
108+
require.Contains(t, err.Error(), "more than one main sketch file found")
109+
require.Contains(t, err.Error(), t.Name()+".ino")
110+
require.Contains(t, err.Error(), t.Name()+".pde")
111+
}
112+
85113
func TestLoadSketchFolderSymlink(t *testing.T) {
86114
// pass the path to the sketch folder
87115
symlinkSketchPath := filepath.Join("testdata", t.Name())
@@ -128,7 +156,7 @@ func TestLoadSketchFolderWrongMain(t *testing.T) {
128156
sketchPath := filepath.Join("testdata", t.Name())
129157
_, err := builder.SketchLoad(sketchPath, "")
130158
require.Error(t, err)
131-
require.Contains(t, err.Error(), "unable to find the main sketch file")
159+
require.Contains(t, err.Error(), "unable to find an sketch file in directory testdata")
132160

133161
_, err = builder.SketchLoad("does/not/exist", "")
134162
require.Error(t, err)

0 commit comments

Comments
 (0)