Skip to content

Commit 7810f31

Browse files
committed
refactor sketch path calculation
1 parent 2ee59f9 commit 7810f31

File tree

5 files changed

+61
-64
lines changed

5 files changed

+61
-64
lines changed

cli/arguments/sketch.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package arguments
1818
import (
1919
"os"
2020

21+
"github.com/arduino/arduino-cli/arduino/discovery"
22+
"github.com/arduino/arduino-cli/arduino/sketch"
2123
"github.com/arduino/arduino-cli/cli/errorcodes"
2224
"github.com/arduino/arduino-cli/cli/feedback"
25+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2326
"github.com/arduino/go-paths-helper"
2427
"github.com/sirupsen/logrus"
2528
)
@@ -39,3 +42,54 @@ func InitSketchPath(sketchPath string) *paths.Path {
3942
logrus.Infof("Reading sketch from dir: %s", wd)
4043
return wd
4144
}
45+
46+
// SketchPathCalculation creates the path of a sketch, optionally it warns the user when using pde files.
47+
// it also gets the port of a board
48+
// it returns the created sketch and the discovery port
49+
func SketchPathCalculation(path string, port Port, instance *rpc.Instance, warn bool) (*paths.Path, *sketch.Sketch, *discovery.Port) {
50+
sketchPath := InitSketch(path, warn)
51+
sketch := CreateSketch(sketchPath)
52+
discoveryPort := GetDiscoveryPort(instance, port, sketch)
53+
return sketchPath, sketch, discoveryPort
54+
}
55+
56+
// InitSketch is a helper function useful to create the sketch path, optionally it warns the user when using pde files.
57+
func InitSketch(path string, warn bool) *paths.Path {
58+
sketchPath := InitSketchPath(path)
59+
60+
if warn {
61+
WarnDeprecatedPdeFiles(sketchPath)
62+
}
63+
return sketchPath
64+
}
65+
66+
// CreateSketch is a helper function usefull to create a sketch
67+
func CreateSketch(sketchPath *paths.Path) *sketch.Sketch {
68+
sketch, err := sketch.New(sketchPath)
69+
if err != nil {
70+
feedback.Errorf(tr("Error creating sketch: %v"), err)
71+
os.Exit(errorcodes.ErrGeneric)
72+
}
73+
return sketch
74+
}
75+
76+
// WarnDeprecatedPdeFiles warns the user that .pde files are deprecated
77+
func WarnDeprecatedPdeFiles(sketchPath *paths.Path) {
78+
// .pde files are still supported but deprecated, this warning urges the user to rename them
79+
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
80+
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
81+
for _, f := range files {
82+
feedback.Error(f)
83+
}
84+
}
85+
}
86+
87+
// GetDiscoveryPort is a helper function useful to get the port and handle possible errors
88+
func GetDiscoveryPort(instance *rpc.Instance, port Port, sk *sketch.Sketch) *discovery.Port {
89+
discoveryPort, err := port.GetPort(instance, sk)
90+
if err != nil {
91+
feedback.Errorf(tr("Error discovering port: %v"), err)
92+
os.Exit(errorcodes.ErrGeneric)
93+
}
94+
return discoveryPort
95+
}

cli/compile/compile.go

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

24-
"github.com/arduino/arduino-cli/arduino/discovery"
25-
"github.com/arduino/arduino-cli/arduino/sketch"
2624
"github.com/arduino/arduino-cli/cli/arguments"
2725
"github.com/arduino/arduino-cli/cli/feedback"
2826
"github.com/arduino/arduino-cli/cli/output"
@@ -129,15 +127,8 @@ func run(cmd *cobra.Command, args []string) {
129127
if len(args) > 0 {
130128
path = args[0]
131129
}
132-
sketchPath := arguments.InitSketchPath(path)
133130

134-
// .pde files are still supported but deprecated, this warning urges the user to rename them
135-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
136-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
137-
for _, f := range files {
138-
feedback.Error(f)
139-
}
140-
}
131+
sketchPath := arguments.InitSketch(path, true)
141132

142133
var overrides map[string]string
143134
if sourceOverrides != "" {
@@ -189,18 +180,8 @@ func run(cmd *cobra.Command, args []string) {
189180
}
190181

191182
if compileError == nil && uploadAfterCompile {
192-
var sk *sketch.Sketch
193-
sk, err := sketch.New(sketchPath)
194-
if err != nil {
195-
feedback.Errorf(tr("Error during Upload: %v"), err)
196-
os.Exit(errorcodes.ErrGeneric)
197-
}
198-
var discoveryPort *discovery.Port
199-
discoveryPort, err = port.GetPort(inst, sk)
200-
if err != nil {
201-
feedback.Errorf(tr("Error during Upload: %v"), err)
202-
os.Exit(errorcodes.ErrGeneric)
203-
}
183+
sk := arguments.CreateSketch(sketchPath)
184+
discoveryPort := arguments.GetDiscoveryPort(inst, port, sk)
204185

205186
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
206187
Instance: inst,

cli/debug/debug.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"os/signal"
2222
"sort"
2323

24-
"github.com/arduino/arduino-cli/arduino/sketch"
2524
"github.com/arduino/arduino-cli/cli/arguments"
2625
"github.com/arduino/arduino-cli/cli/errorcodes"
2726
"github.com/arduino/arduino-cli/cli/feedback"
@@ -81,17 +80,7 @@ func run(command *cobra.Command, args []string) {
8180
if len(args) > 0 {
8281
path = args[0]
8382
}
84-
sketchPath := arguments.InitSketchPath(path)
85-
sk, err := sketch.New(sketchPath)
86-
if err != nil {
87-
feedback.Errorf(tr("Error during Debug: %v"), err)
88-
os.Exit(errorcodes.ErrGeneric)
89-
}
90-
discoveryPort, err := port.GetPort(instance, sk)
91-
if err != nil {
92-
feedback.Errorf(tr("Error during Debug: %v"), err)
93-
os.Exit(errorcodes.ErrGeneric)
94-
}
83+
sketchPath, _, discoveryPort := arguments.SketchPathCalculation(path, port, instance, false)
9584
debugConfigRequested := &dbg.DebugConfigRequest{
9685
Instance: instance,
9786
Fqbn: fqbn,

cli/sketch/archive.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222

23-
"github.com/arduino/arduino-cli/arduino/sketch"
23+
"github.com/arduino/arduino-cli/cli/arguments"
2424
"github.com/arduino/arduino-cli/cli/errorcodes"
2525
"github.com/arduino/arduino-cli/cli/feedback"
2626
sk "github.com/arduino/arduino-cli/commands/sketch"
@@ -61,13 +61,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
6161
sketchPath = paths.New(args[0])
6262
}
6363

64-
// .pde files are still supported but deprecated, this warning urges the user to rename them
65-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
66-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
67-
for _, f := range files {
68-
feedback.Error(f)
69-
}
70-
}
64+
arguments.WarnDeprecatedPdeFiles(sketchPath)
7165

7266
archivePath := ""
7367
if len(args) == 2 {

cli/upload/upload.go

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

22-
"github.com/arduino/arduino-cli/arduino/sketch"
2322
"github.com/arduino/arduino-cli/cli/arguments"
2423
"github.com/arduino/arduino-cli/cli/errorcodes"
2524
"github.com/arduino/arduino-cli/cli/feedback"
@@ -86,27 +85,7 @@ func run(command *cobra.Command, args []string) {
8685
if len(args) > 0 {
8786
path = args[0]
8887
}
89-
sketchPath := arguments.InitSketchPath(path)
90-
91-
// .pde files are still supported but deprecated, this warning urges the user to rename them
92-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 && importDir == "" && importFile == "" {
93-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
94-
for _, f := range files {
95-
feedback.Error(f)
96-
}
97-
}
98-
99-
sk, err := sketch.New(sketchPath)
100-
if err != nil && importDir == "" && importFile == "" {
101-
feedback.Errorf(tr("Error during Upload: %v"), err)
102-
os.Exit(errorcodes.ErrGeneric)
103-
}
104-
105-
discoveryPort, err := port.GetPort(instance, sk)
106-
if err != nil {
107-
feedback.Errorf(tr("Error during Upload: %v"), err)
108-
os.Exit(errorcodes.ErrGeneric)
109-
}
88+
sketchPath, sk, discoveryPort := arguments.SketchPathCalculation(path, port, instance, true)
11089

11190
if fqbn == "" && sk != nil && sk.Metadata != nil {
11291
// If the user didn't specify an FQBN and a sketch.json file is present

0 commit comments

Comments
 (0)