Skip to content

Commit ffe44ea

Browse files
authored
Add back support for sketches with .pde extension and deprecate it (arduino#1157)
* Compile command now works with sketches containing .pde files * Upload command now works with sketches and builds from .pde files * Archive command now works with sketches containing .pde files * [skip changelog] Add test to verify debug command works with pde sketches * Fix lib examples not showing sketches with .pde files * [skip changelog] Remove duplicated code and enhance tests
1 parent d1163cb commit ffe44ea

File tree

21 files changed

+469
-45
lines changed

21 files changed

+469
-45
lines changed

arduino/globals/globals.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ package globals
1818
var (
1919
empty struct{}
2020

21+
// MainFileValidExtension is the extension that must be used for files in new sketches
22+
MainFileValidExtension string = ".ino"
23+
2124
// MainFileValidExtensions lists valid extensions for a sketch file
2225
MainFileValidExtensions = map[string]struct{}{
23-
".ino": empty,
26+
MainFileValidExtension: empty,
27+
// .pde extension is deprecated and must not be used for new sketches
2428
".pde": empty,
2529
}
2630

arduino/libraries/loader.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"strings"
2121

22+
"github.com/arduino/arduino-cli/arduino/sketches"
2223
"github.com/arduino/go-paths-helper"
2324
properties "github.com/arduino/go-properties-orderedmap"
2425
"github.com/pkg/errors"
@@ -172,7 +173,8 @@ func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error
172173
return err
173174
}
174175
for _, file := range files {
175-
if isExample(file) {
176+
_, err := sketches.NewSketchFromPath(file)
177+
if err == nil {
176178
list.Add(file)
177179
} else if file.IsDir() {
178180
if err := addExamplesToPathList(file, list); err != nil {
@@ -182,9 +184,3 @@ func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error
182184
}
183185
return nil
184186
}
185-
186-
// isExample returns true if examplePath contains an example
187-
func isExample(examplePath *paths.Path) bool {
188-
mainIno := examplePath.Join(examplePath.Base() + ".ino")
189-
return mainIno.Exist() && mainIno.IsNotDir()
190-
}

arduino/sketches/sketches.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ import (
2020
"fmt"
2121

2222
"github.com/arduino/arduino-cli/arduino/builder"
23+
"github.com/arduino/arduino-cli/arduino/globals"
2324
"github.com/arduino/go-paths-helper"
2425
"github.com/pkg/errors"
2526
)
2627

2728
// Sketch is a sketch for Arduino
2829
type Sketch struct {
29-
Name string
30-
FullPath *paths.Path
31-
Metadata *Metadata
30+
Name string
31+
MainFileExtension string
32+
FullPath *paths.Path
33+
Metadata *Metadata
3234
}
3335

3436
// Metadata is the kind of data associated to a project such as the connected board
@@ -52,14 +54,32 @@ func NewSketchFromPath(path *paths.Path) (*Sketch, error) {
5254
if !path.IsDir() {
5355
path = path.Parent()
5456
}
55-
sketchFile := path.Join(path.Base() + ".ino")
56-
if !sketchFile.Exist() {
57-
return nil, errors.Errorf("no valid sketch found in %s: missing %s", path, sketchFile.Base())
57+
58+
var mainSketchFile *paths.Path
59+
for ext := range globals.MainFileValidExtensions {
60+
candidateSketchMainFile := path.Join(path.Base() + ext)
61+
if candidateSketchMainFile.Exist() {
62+
if mainSketchFile == nil {
63+
mainSketchFile = candidateSketchMainFile
64+
} else {
65+
return nil, errors.Errorf("multiple main sketch files found (%v, %v)",
66+
mainSketchFile,
67+
candidateSketchMainFile,
68+
)
69+
}
70+
}
71+
}
72+
73+
if mainSketchFile == nil {
74+
sketchFile := path.Join(path.Base() + globals.MainFileValidExtension)
75+
return nil, errors.Errorf("no valid sketch found in %s: missing %s", path, sketchFile)
5876
}
77+
5978
sketch := &Sketch{
60-
FullPath: path,
61-
Name: path.Base(),
62-
Metadata: &Metadata{},
79+
FullPath: path,
80+
MainFileExtension: mainSketchFile.Ext(),
81+
Name: path.Base(),
82+
Metadata: &Metadata{},
6383
}
6484
sketch.ImportMetadata()
6585
return sketch, nil
@@ -108,3 +128,20 @@ func (s *Sketch) BuildPath() (*paths.Path, error) {
108128
}
109129
return builder.GenBuildPath(s.FullPath), nil
110130
}
131+
132+
// CheckForPdeFiles returns all files ending with .pde extension
133+
// in dir, this is mainly used to warn the user that these files
134+
// must be changed to .ino extension.
135+
// When .pde files won't be supported anymore this function must be removed.
136+
func CheckForPdeFiles(sketch *paths.Path) []*paths.Path {
137+
if sketch.IsNotDir() {
138+
sketch = sketch.Parent()
139+
}
140+
141+
files, err := sketch.ReadDirRecursive()
142+
if err != nil {
143+
return []*paths.Path{}
144+
}
145+
files.FilterSuffix(".pde")
146+
return files
147+
}

arduino/sketches/sketches_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,59 @@ func TestSketchBuildPath(t *testing.T) {
5353
require.NoError(t, err)
5454
require.Contains(t, buildPath.String(), "arduino-sketch-")
5555

56+
// Verifies sketch path is returned if sketch has .pde extension
57+
sketchPath = paths.New("testdata", "SketchPde")
58+
sketch, err = NewSketchFromPath(sketchPath)
59+
require.NoError(t, err)
60+
require.NotNil(t, sketch)
61+
buildPath, err = sketch.BuildPath()
62+
require.NoError(t, err)
63+
require.Contains(t, buildPath.String(), "arduino-sketch-")
64+
65+
// Verifies error is returned if there are multiple main files
66+
sketchPath = paths.New("testdata", "SketchMultipleMainFiles")
67+
sketch, err = NewSketchFromPath(sketchPath)
68+
require.Nil(t, sketch)
69+
require.Error(t, err, "multiple main sketch files found")
70+
5671
// Verifies error is returned if sketch path is not set
5772
sketch = &Sketch{}
5873
buildPath, err = sketch.BuildPath()
5974
require.Nil(t, buildPath)
6075
require.Error(t, err, "sketch path is empty")
6176
}
77+
78+
func TestCheckForPdeFiles(t *testing.T) {
79+
sketchPath := paths.New("testdata", "Sketch1")
80+
files := CheckForPdeFiles(sketchPath)
81+
require.Empty(t, files)
82+
83+
sketchPath = paths.New("testdata", "SketchPde")
84+
files = CheckForPdeFiles(sketchPath)
85+
require.Len(t, files, 1)
86+
require.Equal(t, sketchPath.Join("SketchPde.pde"), files[0])
87+
88+
sketchPath = paths.New("testdata", "SketchMultipleMainFiles")
89+
files = CheckForPdeFiles(sketchPath)
90+
require.Len(t, files, 1)
91+
require.Equal(t, sketchPath.Join("SketchMultipleMainFiles.pde"), files[0])
92+
93+
sketchPath = paths.New("testdata", "Sketch1", "Sketch1.ino")
94+
files = CheckForPdeFiles(sketchPath)
95+
require.Empty(t, files)
96+
97+
sketchPath = paths.New("testdata", "SketchPde", "SketchPde.pde")
98+
files = CheckForPdeFiles(sketchPath)
99+
require.Len(t, files, 1)
100+
require.Equal(t, sketchPath.Parent().Join("SketchPde.pde"), files[0])
101+
102+
sketchPath = paths.New("testdata", "SketchMultipleMainFiles", "SketchMultipleMainFiles.ino")
103+
files = CheckForPdeFiles(sketchPath)
104+
require.Len(t, files, 1)
105+
require.Equal(t, sketchPath.Parent().Join("SketchMultipleMainFiles.pde"), files[0])
106+
107+
sketchPath = paths.New("testdata", "SketchMultipleMainFiles", "SketchMultipleMainFiles.pde")
108+
files = CheckForPdeFiles(sketchPath)
109+
require.Len(t, files, 1)
110+
require.Equal(t, sketchPath.Parent().Join("SketchMultipleMainFiles.pde"), files[0])
111+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void setup() {}
3+
void loop() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void setup() {}
3+
void loop() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void setup() {}
3+
void loop() {}

cli/compile/compile.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"os"
2323

24+
"github.com/arduino/arduino-cli/arduino/sketches"
2425
"github.com/arduino/arduino-cli/cli/feedback"
2526
"github.com/arduino/arduino-cli/cli/output"
2627
"github.com/arduino/arduino-cli/configuration"
@@ -127,6 +128,15 @@ func run(cmd *cobra.Command, args []string) {
127128
}
128129

129130
sketchPath := initSketchPath(path)
131+
132+
// .pde files are still supported but deprecated, this warning urges the user to rename them
133+
if files := sketches.CheckForPdeFiles(sketchPath); len(files) > 0 {
134+
feedback.Error("Sketches with .pde extension are deprecated, please rename the following files to .ino:")
135+
for _, f := range files {
136+
feedback.Error(f)
137+
}
138+
}
139+
130140
// We must read this from settings since the value is set when the binding is accessed from viper,
131141
// accessing it from cobra would only read it if the flag is explicitly set by the user and ignore
132142
// the config file and the env vars.

cli/sketch/archive.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/arduino/sketches"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/commands/sketch"
2526
rpc "github.com/arduino/arduino-cli/rpc/commands"
27+
"github.com/arduino/go-paths-helper"
2628
"github.com/sirupsen/logrus"
2729
"github.com/spf13/cobra"
2830
)
@@ -53,11 +55,19 @@ func initArchiveCommand() *cobra.Command {
5355
func runArchiveCommand(cmd *cobra.Command, args []string) {
5456
logrus.Info("Executing `arduino sketch archive`")
5557

56-
sketchPath := ""
58+
sketchPath := "."
5759
if len(args) >= 1 {
5860
sketchPath = args[0]
5961
}
6062

63+
// .pde files are still supported but deprecated, this warning urges the user to rename them
64+
if files := sketches.CheckForPdeFiles(paths.New(sketchPath)); len(files) > 0 {
65+
feedback.Error("Sketches with .pde extension are deprecated, please rename the following files to .ino:")
66+
for _, f := range files {
67+
feedback.Error(f)
68+
}
69+
}
70+
6171
archivePath := ""
6272
if len(args) == 2 {
6373
archivePath = args[1]

cli/upload/upload.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/arduino/sketches"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/cli/instance"
@@ -83,6 +84,14 @@ func run(command *cobra.Command, args []string) {
8384
}
8485
sketchPath := initSketchPath(path)
8586

87+
// .pde files are still supported but deprecated, this warning urges the user to rename them
88+
if files := sketches.CheckForPdeFiles(sketchPath); len(files) > 0 {
89+
feedback.Error("Sketches with .pde extension are deprecated, please rename the following files to .ino:")
90+
for _, f := range files {
91+
feedback.Error(f)
92+
}
93+
}
94+
8695
if _, err := upload.Upload(context.Background(), &rpc.UploadReq{
8796
Instance: instance,
8897
Fqbn: fqbn,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy