Skip to content

Commit ea8c281

Browse files
authored
[breaking] Improved .pde warnings detection subroutines / gRPC LoadSketch API improvement (#2490)
* sketch.GetProfile returns an error if profile is not found * Added Profile.ToRpc helper * Added Sketch.ToRpc helper * Factored Sketch message in gRPC API for 'LoadSketch' call * Inline function call * Improved implementation of sketch.GetSketchProfiles * Moved subroutine that warns about .pde files in feedback package * Updated docs * Improved 'sketch archive' parameters check * Fixed bug detected by integration test... oops
1 parent 95753fc commit ea8c281

File tree

21 files changed

+980
-930
lines changed

21 files changed

+980
-930
lines changed

Diff for: commands/daemon/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (s *ArduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketc
170170
// LoadSketch FIXMEDOC
171171
func (s *ArduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
172172
resp, err := sketch.LoadSketch(ctx, req)
173-
return resp, convertErrorToRPCStatus(err)
173+
return &rpc.LoadSketchResponse{Sketch: resp}, convertErrorToRPCStatus(err)
174174
}
175175

176176
// SetSketchDefaults FIXMEDOC

Diff for: commands/instances.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
142142
if err != nil {
143143
return &cmderrors.InvalidArgumentError{Cause: err}
144144
}
145-
profile = sk.GetProfile(req.GetProfile())
146-
if profile == nil {
147-
return &cmderrors.UnknownProfileError{Profile: req.GetProfile()}
145+
p, err := sk.GetProfile(req.GetProfile())
146+
if err != nil {
147+
return err
148148
}
149+
profile = p
149150
responseCallback(&rpc.InitResponse{
150151
Message: &rpc.InitResponse_Profile{
151152
Profile: &rpc.Profile{

Diff for: commands/sketch/load.go

+3-48
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,11 @@ import (
2424
paths "github.com/arduino/go-paths-helper"
2525
)
2626

27-
// LoadSketch collects and returns all files composing a sketch
28-
func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
29-
// TODO: This should be a ToRpc function for the Sketch struct
27+
// LoadSketch collects and returns all information about a sketch
28+
func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.Sketch, error) {
3029
sk, err := sketch.New(paths.New(req.GetSketchPath()))
3130
if err != nil {
3231
return nil, &cmderrors.CantOpenSketchError{Cause: err}
3332
}
34-
35-
otherSketchFiles := make([]string, sk.OtherSketchFiles.Len())
36-
for i, file := range sk.OtherSketchFiles {
37-
otherSketchFiles[i] = file.String()
38-
}
39-
40-
additionalFiles := make([]string, sk.AdditionalFiles.Len())
41-
for i, file := range sk.AdditionalFiles {
42-
additionalFiles[i] = file.String()
43-
}
44-
45-
rootFolderFiles := make([]string, sk.RootFolderFiles.Len())
46-
for i, file := range sk.RootFolderFiles {
47-
rootFolderFiles[i] = file.String()
48-
}
49-
50-
defaultPort, defaultProtocol := sk.GetDefaultPortAddressAndProtocol()
51-
52-
profiles := make([](*rpc.SketchProfile), len(sk.Project.Profiles))
53-
for i, profile := range sk.Project.Profiles {
54-
profiles[i] = &rpc.SketchProfile{
55-
Name: profile.Name,
56-
Fqbn: profile.FQBN,
57-
}
58-
}
59-
60-
defaultProfileResp := &rpc.SketchProfile{}
61-
defaultProfile := sk.GetProfile(sk.Project.DefaultProfile)
62-
if defaultProfile != nil {
63-
defaultProfileResp.Name = defaultProfile.Name
64-
defaultProfileResp.Fqbn = defaultProfile.FQBN
65-
}
66-
67-
return &rpc.LoadSketchResponse{
68-
MainFile: sk.MainFile.String(),
69-
LocationPath: sk.FullPath.String(),
70-
OtherSketchFiles: otherSketchFiles,
71-
AdditionalFiles: additionalFiles,
72-
RootFolderFiles: rootFolderFiles,
73-
DefaultFqbn: sk.GetDefaultFQBN(),
74-
DefaultPort: defaultPort,
75-
DefaultProtocol: defaultProtocol,
76-
Profiles: profiles,
77-
DefaultProfile: defaultProfileResp,
78-
}, nil
33+
return sk.ToRpc(), nil
7934
}

Diff for: docs/UPGRADING.md

+40
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,46 @@ Here you can find a list of migration guides to handle breaking changes between
44

55
## 0.36.0
66

7+
### The gRPC `cc.arduino.cli.commands.v1.LoadSketchResponse` message has been changed.
8+
9+
Previously the `LoadSketchResponse` containted all the information about the sketch:
10+
11+
```proto
12+
message LoadSketchResponse {
13+
string main_file = 1;
14+
string location_path = 2;
15+
repeated string other_sketch_files = 3;
16+
repeated string additional_files = 4;
17+
repeated string root_folder_files = 5;
18+
string default_fqbn = 6;
19+
string default_port = 7;
20+
string default_protocol = 8;
21+
repeated SketchProfile profiles = 9;
22+
SketchProfile default_profile = 10;
23+
}
24+
```
25+
26+
Now all the metadata have been moved into a specific `Sketch` message:
27+
28+
```proto
29+
message LoadSketchResponse {
30+
Sketch sketch = 1;
31+
}
32+
33+
message Sketch {
34+
string main_file = 1;
35+
string location_path = 2;
36+
repeated string other_sketch_files = 3;
37+
repeated string additional_files = 4;
38+
repeated string root_folder_files = 5;
39+
string default_fqbn = 6;
40+
string default_port = 7;
41+
string default_protocol = 8;
42+
repeated SketchProfile profiles = 9;
43+
SketchProfile default_profile = 10;
44+
}
45+
```
46+
747
### Drop support for `builtin.tools`
848

949
We're dropping the `builtin.tools` support. It was the equivalent of Arduino IDE 1.x bundled tools directory.

Diff for: internal/arduino/sketch/profiles.go

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/arduino/arduino-cli/internal/arduino/utils"
27+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2728
"github.com/arduino/go-paths-helper"
2829
semver "go.bug.st/relaxed-semver"
2930
"gopkg.in/yaml.v3"
@@ -99,6 +100,14 @@ type Profile struct {
99100
Libraries ProfileRequiredLibraries `yaml:"libraries"`
100101
}
101102

103+
// ToRpc converts this Profile to an rpc.SketchProfile
104+
func (p *Profile) ToRpc() *rpc.SketchProfile {
105+
return &rpc.SketchProfile{
106+
Name: p.Name,
107+
Fqbn: p.FQBN,
108+
}
109+
}
110+
102111
// AsYaml outputs the profile as Yaml
103112
func (p *Profile) AsYaml() string {
104113
res := ""

Diff for: internal/arduino/sketch/sketch.go

+27-22
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
"sort"
2424
"strings"
2525

26+
"github.com/arduino/arduino-cli/commands/cmderrors"
27+
f "github.com/arduino/arduino-cli/internal/algorithms"
2628
"github.com/arduino/arduino-cli/internal/arduino/globals"
2729
"github.com/arduino/arduino-cli/internal/i18n"
30+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2831
"github.com/arduino/go-paths-helper"
2932
)
3033

@@ -180,15 +183,14 @@ func (s *Sketch) supportedFiles() (*paths.PathList, error) {
180183
return &files, nil
181184
}
182185

183-
// GetProfile returns the requested profile or nil if the profile
184-
// is not found.
185-
func (s *Sketch) GetProfile(profileName string) *Profile {
186+
// GetProfile returns the requested profile or an error if not found
187+
func (s *Sketch) GetProfile(profileName string) (*Profile, error) {
186188
for _, p := range s.Project.Profiles {
187189
if p.Name == profileName {
188-
return p
190+
return p, nil
189191
}
190192
}
191-
return nil
193+
return nil, &cmderrors.UnknownProfileError{Profile: profileName}
192194
}
193195

194196
// checkSketchCasing returns an error if the casing of the sketch folder and the main file are different.
@@ -278,23 +280,6 @@ func (e *InvalidSketchFolderNameError) Error() string {
278280
return tr("no valid sketch found in %[1]s: missing %[2]s", e.SketchFolder, e.SketchFile)
279281
}
280282

281-
// CheckForPdeFiles returns all files ending with .pde extension
282-
// in sketch, this is mainly used to warn the user that these files
283-
// must be changed to .ino extension.
284-
// When .pde files won't be supported anymore this function must be removed.
285-
func CheckForPdeFiles(sketch *paths.Path) []*paths.Path {
286-
if sketch.IsNotDir() {
287-
sketch = sketch.Parent()
288-
}
289-
290-
files, err := sketch.ReadDirRecursive()
291-
if err != nil {
292-
return []*paths.Path{}
293-
}
294-
files.FilterSuffix(".pde")
295-
return files
296-
}
297-
298283
// DefaultBuildPath generates the default build directory for a given sketch.
299284
// The build path is in a temporary directory and is unique for each sketch.
300285
func (s *Sketch) DefaultBuildPath() *paths.Path {
@@ -307,3 +292,23 @@ func (s *Sketch) Hash() string {
307292
md5SumBytes := md5.Sum([]byte(path))
308293
return strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))
309294
}
295+
296+
// ToRpc converts this Sketch into a rpc.LoadSketchResponse
297+
func (s *Sketch) ToRpc() *rpc.Sketch {
298+
defaultPort, defaultProtocol := s.GetDefaultPortAddressAndProtocol()
299+
res := &rpc.Sketch{
300+
MainFile: s.MainFile.String(),
301+
LocationPath: s.FullPath.String(),
302+
OtherSketchFiles: s.OtherSketchFiles.AsStrings(),
303+
AdditionalFiles: s.AdditionalFiles.AsStrings(),
304+
RootFolderFiles: s.RootFolderFiles.AsStrings(),
305+
DefaultFqbn: s.GetDefaultFQBN(),
306+
DefaultPort: defaultPort,
307+
DefaultProtocol: defaultProtocol,
308+
Profiles: f.Map(s.Project.Profiles, (*Profile).ToRpc),
309+
}
310+
if defaultProfile, err := s.GetProfile(s.Project.DefaultProfile); err == nil {
311+
res.DefaultProfile = defaultProfile.ToRpc()
312+
}
313+
return res
314+
}

Diff for: internal/arduino/sketch/sketch_test.go

-35
Original file line numberDiff line numberDiff line change
@@ -291,41 +291,6 @@ func TestGenBuildPath(t *testing.T) {
291291
assert.Equal(t, "ACBD18DB4CC2F85CEDEF654FCCC4A4D8", (&Sketch{FullPath: paths.New("foo")}).Hash())
292292
}
293293

294-
func TestCheckForPdeFiles(t *testing.T) {
295-
sketchPath := paths.New("testdata", "SketchSimple")
296-
files := CheckForPdeFiles(sketchPath)
297-
require.Empty(t, files)
298-
299-
sketchPath = paths.New("testdata", "SketchPde")
300-
files = CheckForPdeFiles(sketchPath)
301-
require.Len(t, files, 1)
302-
require.Equal(t, sketchPath.Join("SketchPde.pde"), files[0])
303-
304-
sketchPath = paths.New("testdata", "SketchMultipleMainFiles")
305-
files = CheckForPdeFiles(sketchPath)
306-
require.Len(t, files, 1)
307-
require.Equal(t, sketchPath.Join("SketchMultipleMainFiles.pde"), files[0])
308-
309-
sketchPath = paths.New("testdata", "SketchSimple", "SketchSimple.ino")
310-
files = CheckForPdeFiles(sketchPath)
311-
require.Empty(t, files)
312-
313-
sketchPath = paths.New("testdata", "SketchPde", "SketchPde.pde")
314-
files = CheckForPdeFiles(sketchPath)
315-
require.Len(t, files, 1)
316-
require.Equal(t, sketchPath.Parent().Join("SketchPde.pde"), files[0])
317-
318-
sketchPath = paths.New("testdata", "SketchMultipleMainFiles", "SketchMultipleMainFiles.ino")
319-
files = CheckForPdeFiles(sketchPath)
320-
require.Len(t, files, 1)
321-
require.Equal(t, sketchPath.Parent().Join("SketchMultipleMainFiles.pde"), files[0])
322-
323-
sketchPath = paths.New("testdata", "SketchMultipleMainFiles", "SketchMultipleMainFiles.pde")
324-
files = CheckForPdeFiles(sketchPath)
325-
require.Len(t, files, 1)
326-
require.Equal(t, sketchPath.Parent().Join("SketchMultipleMainFiles.pde"), files[0])
327-
}
328-
329294
func TestNewSketchWithSymlink(t *testing.T) {
330295
sketchPath, _ := paths.New("testdata", "SketchWithSymlink").Abs()
331296
mainFilePath := sketchPath.Join("SketchWithSymlink.ino")

Diff for: internal/cli/arguments/sketch.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020

2121
"github.com/arduino/arduino-cli/commands/sketch"
22+
f "github.com/arduino/arduino-cli/internal/algorithms"
2223
"github.com/arduino/arduino-cli/internal/cli/feedback"
2324
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2425
"github.com/arduino/go-paths-helper"
@@ -27,8 +28,7 @@ import (
2728

2829
// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
2930
// If sketchPath is an empty string returns the current working directory.
30-
// In both cases it warns the user if he's using deprecated files
31-
func InitSketchPath(path string, printWarnings bool) (sketchPath *paths.Path) {
31+
func InitSketchPath(path string) (sketchPath *paths.Path) {
3232
if path != "" {
3333
sketchPath = paths.New(path)
3434
} else {
@@ -39,11 +39,6 @@ func InitSketchPath(path string, printWarnings bool) (sketchPath *paths.Path) {
3939
logrus.Infof("Reading sketch from dir: %s", wd)
4040
sketchPath = wd
4141
}
42-
if printWarnings {
43-
if msg := sketch.WarnDeprecatedFiles(sketchPath); msg != "" {
44-
feedback.Warning(msg)
45-
}
46-
}
4742
return sketchPath
4843
}
4944

@@ -57,13 +52,10 @@ func GetSketchProfiles(sketchPath string) []string {
5752
return nil
5853
}
5954
}
60-
list, _ := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{
61-
SketchPath: sketchPath,
62-
})
63-
profiles := list.GetProfiles()
64-
res := make([]string, len(profiles))
65-
for i, p := range list.GetProfiles() {
66-
res[i] = p.GetName()
55+
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath})
56+
if err != nil {
57+
return nil
6758
}
68-
return res
59+
profiles := sk.GetProfiles()
60+
return f.Map(profiles, (*rpc.SketchProfile).GetName)
6961
}

Diff for: internal/cli/board/attach.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func initAttachCommand() *cobra.Command {
5353
}
5454

5555
func runAttachCommand(path string, port *arguments.Port, fqbn string) {
56-
sketchPath := arguments.InitSketchPath(path, true)
56+
sketchPath := arguments.InitSketchPath(path)
5757

5858
portAddress, portProtocol, _ := port.GetPortAddressAndProtocol(nil, "", "")
5959
newDefaults, err := sketch.SetSketchDefaults(context.Background(), &rpc.SetSketchDefaultsRequest{

Diff for: internal/cli/compile/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
158158
path = args[0]
159159
}
160160

161-
sketchPath := arguments.InitSketchPath(path, true)
162-
161+
sketchPath := arguments.InitSketchPath(path)
163162
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
164163
if err != nil {
165164
feedback.FatalError(err, feedback.ErrGeneric)
166165
}
166+
feedback.WarnAboutDeprecatedFiles(sk)
167167

168168
var inst *rpc.Instance
169169
var profile *rpc.Profile

Diff for: internal/cli/debug/debug.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ func runDebugCommand(args []string, portArgs *arguments.Port, fqbnArg *arguments
8181
path = args[0]
8282
}
8383

84-
sketchPath := arguments.InitSketchPath(path, true)
84+
sketchPath := arguments.InitSketchPath(path)
8585
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
8686
if err != nil {
8787
feedback.FatalError(err, feedback.ErrGeneric)
8888
}
89+
feedback.WarnAboutDeprecatedFiles(sk)
90+
8991
fqbn, port := arguments.CalculateFQBNAndPort(portArgs, fqbnArg, instance, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
9092
debugConfigRequested := &rpc.GetDebugConfigRequest{
9193
Instance: instance,

0 commit comments

Comments
 (0)