Commit be5022e0 authored by Umberto Baldi's avatar Umberto Baldi

refactor sketch path calculation

parent 9771b998
......@@ -18,10 +18,12 @@ package arguments
import (
"fmt"
"net/url"
"os"
"time"
"github.com/arduino/arduino-cli/arduino/discovery"
"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/commands"
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
}
}
}
// 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
import (
"os"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/go-paths-helper"
......@@ -26,16 +27,40 @@ import (
// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
// If sketchPath is an empty string returns the current working directory.
func InitSketchPath(sketchPath string) *paths.Path {
if sketchPath != "" {
return paths.New(sketchPath)
// In both cases it warns the user if he's using deprecated files
func InitSketchPath(path string) (sketchPath *paths.Path) {
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 {
feedback.Errorf(tr("Couldn't get current working directory: %v"), err)
feedback.Errorf(tr("Error creating sketch: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Infof("Reading sketch from dir: %s", wd)
return wd
return sketch
}
// 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 (
"encoding/json"
"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/feedback"
"github.com/arduino/arduino-cli/cli/output"
......@@ -129,15 +127,8 @@ func run(cmd *cobra.Command, args []string) {
if len(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 {
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
for _, f := range files {
feedback.Error(f)
}
}
sketchPath := arguments.InitSketchPath(path)
var overrides map[string]string
if sourceOverrides != "" {
......@@ -189,18 +180,8 @@ func run(cmd *cobra.Command, args []string) {
}
if compileError == nil && uploadAfterCompile {
var sk *sketch.Sketch
sk, err := sketch.New(sketchPath)
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)
}
sk := arguments.NewSketch(sketchPath)
discoveryPort := port.GetDiscoveryPort(inst, sk)
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
Instance: inst,
......
......@@ -21,7 +21,6 @@ import (
"os/signal"
"sort"
"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/feedback"
......@@ -81,17 +80,11 @@ func run(command *cobra.Command, args []string) {
if len(args) > 0 {
path = args[0]
}
sketchPath := arguments.InitSketchPath(path)
sk, err := sketch.New(sketchPath)
if err != nil {
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)
}
sk := arguments.NewSketch(sketchPath)
discoveryPort := port.GetDiscoveryPort(instance, sk)
debugConfigRequested := &dbg.DebugConfigRequest{
Instance: instance,
Fqbn: fqbn,
......
......@@ -20,7 +20,7 @@ import (
"fmt"
"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/feedback"
sk "github.com/arduino/arduino-cli/commands/sketch"
......@@ -61,13 +61,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
sketchPath = paths.New(args[0])
}
// .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)
}
}
arguments.WarnDeprecatedFiles(sketchPath)
archivePath := ""
if len(args) == 2 {
......
......@@ -19,7 +19,6 @@ import (
"context"
"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/feedback"
......@@ -86,27 +85,10 @@ func run(command *cobra.Command, args []string) {
if len(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)
if err != nil {
feedback.Errorf(tr("Error during Upload: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
sketchPath := arguments.InitSketchPath(path)
sk := arguments.NewSketch(sketchPath)
discoveryPort := port.GetDiscoveryPort(instance, sk)
if fqbn == "" && sk != nil && sk.Metadata != nil {
// 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