Commit be5022e0 authored by Umberto Baldi's avatar Umberto Baldi

refactor sketch path calculation

parent 9771b998
...@@ -18,10 +18,12 @@ package arguments ...@@ -18,10 +18,12 @@ package arguments
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"os"
"time" "time"
"github.com/arduino/arduino-cli/arduino/discovery" "github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
...@@ -144,3 +146,13 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po ...@@ -144,3 +146,13 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
} }
} }
} }
// GetDiscoveryPort is a helper function useful to get the port and handle possible errors
func (p *Port) GetDiscoveryPort(instance *rpc.Instance, sk *sketch.Sketch) *discovery.Port {
discoveryPort, err := p.GetPort(instance, sk)
if err != nil {
feedback.Errorf(tr("Error discovering port: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
return discoveryPort
}
...@@ -18,6 +18,7 @@ package arguments ...@@ -18,6 +18,7 @@ package arguments
import ( import (
"os" "os"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
...@@ -26,16 +27,40 @@ import ( ...@@ -26,16 +27,40 @@ import (
// InitSketchPath returns an instance of paths.Path pointing to sketchPath. // InitSketchPath returns an instance of paths.Path pointing to sketchPath.
// If sketchPath is an empty string returns the current working directory. // If sketchPath is an empty string returns the current working directory.
func InitSketchPath(sketchPath string) *paths.Path { // In both cases it warns the user if he's using deprecated files
if sketchPath != "" { func InitSketchPath(path string) (sketchPath *paths.Path) {
return paths.New(sketchPath) if path != "" {
sketchPath = paths.New(path)
} else {
wd, err := paths.Getwd()
if err != nil {
feedback.Errorf(tr("Couldn't get current working directory: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Infof("Reading sketch from dir: %s", wd)
sketchPath = wd
} }
WarnDeprecatedFiles(sketchPath)
return sketchPath
}
wd, err := paths.Getwd() // NewSketch is a helper function useful to create a sketch instance
func NewSketch(sketchPath *paths.Path) *sketch.Sketch {
sketch, err := sketch.New(sketchPath)
if err != nil { if err != nil {
feedback.Errorf(tr("Couldn't get current working directory: %v"), err) feedback.Errorf(tr("Error creating sketch: %v"), err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
} }
logrus.Infof("Reading sketch from dir: %s", wd) return sketch
return wd }
// WarnDeprecatedFiles warns the user that a type of sketch files are deprecated
func WarnDeprecatedFiles(sketchPath *paths.Path) {
// .pde files are still supported but deprecated, this warning urges the user to rename them
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
for _, f := range files {
feedback.Error(f)
}
}
} }
...@@ -21,8 +21,6 @@ import ( ...@@ -21,8 +21,6 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/arguments" "github.com/arduino/arduino-cli/cli/arguments"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/output" "github.com/arduino/arduino-cli/cli/output"
...@@ -129,15 +127,8 @@ func run(cmd *cobra.Command, args []string) { ...@@ -129,15 +127,8 @@ func run(cmd *cobra.Command, args []string) {
if len(args) > 0 { if len(args) > 0 {
path = args[0] path = args[0]
} }
sketchPath := arguments.InitSketchPath(path)
// .pde files are still supported but deprecated, this warning urges the user to rename them sketchPath := arguments.InitSketchPath(path)
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
for _, f := range files {
feedback.Error(f)
}
}
var overrides map[string]string var overrides map[string]string
if sourceOverrides != "" { if sourceOverrides != "" {
...@@ -189,18 +180,8 @@ func run(cmd *cobra.Command, args []string) { ...@@ -189,18 +180,8 @@ func run(cmd *cobra.Command, args []string) {
} }
if compileError == nil && uploadAfterCompile { if compileError == nil && uploadAfterCompile {
var sk *sketch.Sketch sk := arguments.NewSketch(sketchPath)
sk, err := sketch.New(sketchPath) discoveryPort := port.GetDiscoveryPort(inst, sk)
if err != nil {
feedback.Errorf(tr("Error during Upload: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
var discoveryPort *discovery.Port
discoveryPort, err = port.GetPort(inst, sk)
if err != nil {
feedback.Errorf(tr("Error during Upload: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{ userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
Instance: inst, Instance: inst,
......
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"os/signal" "os/signal"
"sort" "sort"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/arguments" "github.com/arduino/arduino-cli/cli/arguments"
"github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
...@@ -81,17 +80,11 @@ func run(command *cobra.Command, args []string) { ...@@ -81,17 +80,11 @@ func run(command *cobra.Command, args []string) {
if len(args) > 0 { if len(args) > 0 {
path = args[0] path = args[0]
} }
sketchPath := arguments.InitSketchPath(path) sketchPath := arguments.InitSketchPath(path)
sk, err := sketch.New(sketchPath) sk := arguments.NewSketch(sketchPath)
if err != nil { discoveryPort := port.GetDiscoveryPort(instance, sk)
feedback.Errorf(tr("Error during Debug: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
discoveryPort, err := port.GetPort(instance, sk)
if err != nil {
feedback.Errorf(tr("Error during Debug: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
debugConfigRequested := &dbg.DebugConfigRequest{ debugConfigRequested := &dbg.DebugConfigRequest{
Instance: instance, Instance: instance,
Fqbn: fqbn, Fqbn: fqbn,
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/cli/arguments"
"github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
sk "github.com/arduino/arduino-cli/commands/sketch" sk "github.com/arduino/arduino-cli/commands/sketch"
...@@ -61,13 +61,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) { ...@@ -61,13 +61,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
sketchPath = paths.New(args[0]) sketchPath = paths.New(args[0])
} }
// .pde files are still supported but deprecated, this warning urges the user to rename them arguments.WarnDeprecatedFiles(sketchPath)
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
for _, f := range files {
feedback.Error(f)
}
}
archivePath := "" archivePath := ""
if len(args) == 2 { if len(args) == 2 {
......
...@@ -19,7 +19,6 @@ import ( ...@@ -19,7 +19,6 @@ import (
"context" "context"
"os" "os"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/arguments" "github.com/arduino/arduino-cli/cli/arguments"
"github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
...@@ -86,27 +85,10 @@ func run(command *cobra.Command, args []string) { ...@@ -86,27 +85,10 @@ func run(command *cobra.Command, args []string) {
if len(args) > 0 { if len(args) > 0 {
path = args[0] path = args[0]
} }
sketchPath := arguments.InitSketchPath(path)
// .pde files are still supported but deprecated, this warning urges the user to rename them
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 && importDir == "" && importFile == "" {
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
for _, f := range files {
feedback.Error(f)
}
}
sk, err := sketch.New(sketchPath)
if err != nil && importDir == "" && importFile == "" {
feedback.Errorf(tr("Error during Upload: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
discoveryPort, err := port.GetPort(instance, sk) sketchPath := arguments.InitSketchPath(path)
if err != nil { sk := arguments.NewSketch(sketchPath)
feedback.Errorf(tr("Error during Upload: %v"), err) discoveryPort := port.GetDiscoveryPort(instance, sk)
os.Exit(errorcodes.ErrGeneric)
}
if fqbn == "" && sk != nil && sk.Metadata != nil { if fqbn == "" && sk != nil && sk.Metadata != nil {
// If the user didn't specify an FQBN and a sketch.json file is present // If the user didn't specify an FQBN and a sketch.json file is present
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment