Skip to content

Commit

Permalink
Inlining methods in ArduinoCoreServiceImpl (part 13: NewSketch, LoadS…
Browse files Browse the repository at this point in the history
…ketch, SetSketchDefaults, ArchiveSketch)
  • Loading branch information
cmaglie committed Mar 18, 2024
1 parent ecf113e commit b76847f
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 73 deletions.
21 changes: 0 additions & 21 deletions commands/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ func (s *arduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq
return &rpc.VersionResponse{Version: s.versionString}, nil
}

// NewSketch FIXMEDOC
func (s *arduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
return NewSketch(ctx, req)
}

// LoadSketch FIXMEDOC
func (s *arduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
resp, err := LoadSketch(ctx, req)
return &rpc.LoadSketchResponse{Sketch: resp}, err
}

// SetSketchDefaults FIXMEDOC
func (s *arduinoCoreServerImpl) SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (*rpc.SetSketchDefaultsResponse, error) {
return SetSketchDefaults(ctx, req)
}

// Upload FIXMEDOC
func (s *arduinoCoreServerImpl) Upload(req *rpc.UploadRequest, stream rpc.ArduinoCoreService_UploadServer) error {
syncSend := NewSynchronizedSend(stream.Send)
Expand Down Expand Up @@ -144,11 +128,6 @@ func (s *arduinoCoreServerImpl) ListProgrammersAvailableForUpload(ctx context.Co
return ListProgrammersAvailableForUpload(ctx, req)
}

// ArchiveSketch FIXMEDOC
func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
return ArchiveSketch(ctx, req)
}

// EnumerateMonitorPortSettings FIXMEDOC
func (s *arduinoCoreServerImpl) EnumerateMonitorPortSettings(ctx context.Context, req *rpc.EnumerateMonitorPortSettingsRequest) (*rpc.EnumerateMonitorPortSettingsResponse, error) {
return EnumerateMonitorPortSettings(ctx, req)
Expand Down
2 changes: 1 addition & 1 deletion commands/service_set_sketch_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// SetSketchDefaults updates the sketch project file (sketch.yaml) with the given defaults
// for the values `default_fqbn`, `default_port`, and `default_protocol`.
func SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (*rpc.SetSketchDefaultsResponse, error) {
func (s *arduinoCoreServerImpl) SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (*rpc.SetSketchDefaultsResponse, error) {
sk, err := sketch.New(paths.New(req.GetSketchPath()))
if err != nil {
return nil, &cmderrors.CantOpenSketchError{Cause: err}
Expand Down
8 changes: 4 additions & 4 deletions commands/service_sketch_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// ArchiveSketch FIXMEDOC
func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
// sketchName is the name of the sketch without extension, for example "MySketch"
var sketchName string

Expand All @@ -38,13 +38,13 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
sketchPath = paths.New(".")
}

s, err := sketch.New(sketchPath)
sk, err := sketch.New(sketchPath)
if err != nil {
return nil, &cmderrors.CantOpenSketchError{Cause: err}
}

sketchPath = s.FullPath
sketchName = s.Name
sketchPath = sk.FullPath
sketchName = sk.Name

archivePath := paths.New(req.GetArchivePath())
if archivePath == nil {
Expand Down
4 changes: 2 additions & 2 deletions commands/service_sketch_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
)

// LoadSketch collects and returns all information about a sketch
func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.Sketch, error) {
func (s *arduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
sk, err := sketch.New(paths.New(req.GetSketchPath()))
if err != nil {
return nil, &cmderrors.CantOpenSketchError{Cause: err}
}
return sk.ToRpc(), nil
return &rpc.LoadSketchResponse{Sketch: sk.ToRpc()}, nil
}
7 changes: 4 additions & 3 deletions commands/service_sketch_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
)

func TestLoadSketchProfiles(t *testing.T) {
loadResp, err := LoadSketch(context.Background(), &commands.LoadSketchRequest{
srv := NewArduinoCoreServer("")
loadResp, err := srv.LoadSketch(context.Background(), &commands.LoadSketchRequest{
SketchPath: "./testdata/sketch_with_profile",
})
require.NoError(t, err)
require.Len(t, loadResp.GetProfiles(), 2)
require.Equal(t, loadResp.GetDefaultProfile().GetName(), "nanorp")
require.Len(t, loadResp.GetSketch().GetProfiles(), 2)
require.Equal(t, loadResp.GetSketch().GetDefaultProfile().GetName(), "nanorp")
}
2 changes: 1 addition & 1 deletion commands/service_sketch_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var invalidNames = []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2",
"COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}

// NewSketch creates a new sketch via gRPC
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
func (s *arduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
var sketchesDir string
if len(req.GetSketchDir()) > 0 {
sketchesDir = req.GetSketchDir()
Expand Down
21 changes: 13 additions & 8 deletions commands/service_sketch_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"testing"

"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/stretchr/testify/require"
)

Expand All @@ -34,8 +34,10 @@ func Test_SketchNameWrongPattern(t *testing.T) {
"||||||||||||||",
",`hack[}attempt{];",
}

srv := NewArduinoCoreServer("")
for _, name := range invalidNames {
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
_, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{
SketchName: name,
SketchDir: t.TempDir(),
})
Expand All @@ -46,9 +48,9 @@ func Test_SketchNameWrongPattern(t *testing.T) {
}

func Test_SketchNameEmpty(t *testing.T) {
emptyName := ""
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
SketchName: emptyName,
srv := NewArduinoCoreServer("")
_, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{
SketchName: "",
SketchDir: t.TempDir(),
})

Expand All @@ -60,7 +62,8 @@ func Test_SketchNameTooLong(t *testing.T) {
for i := range tooLongName {
tooLongName[i] = 'a'
}
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
srv := NewArduinoCoreServer("")
_, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{
SketchName: string(tooLongName),
SketchDir: t.TempDir(),
})
Expand All @@ -83,8 +86,9 @@ func Test_SketchNameOk(t *testing.T) {
"_hello_world",
string(lengthLimitName),
}
srv := NewArduinoCoreServer("")
for _, name := range validNames {
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
_, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{
SketchName: name,
SketchDir: t.TempDir(),
})
Expand All @@ -95,8 +99,9 @@ func Test_SketchNameOk(t *testing.T) {
func Test_SketchNameReserved(t *testing.T) {
invalidNames := []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5",
"COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}
srv := NewArduinoCoreServer("")
for _, name := range invalidNames {
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
_, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{
SketchName: name,
SketchDir: t.TempDir(),
})
Expand Down
11 changes: 8 additions & 3 deletions internal/cli/arguments/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

package arguments

import "github.com/spf13/cobra"
import (
"context"

rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/spf13/cobra"
)

// Profile contains the profile flag data.
// This is useful so all flags used by commands that need
Expand All @@ -25,14 +30,14 @@ type Profile struct {
}

// AddToCommand adds the flags used to set fqbn to the specified Command
func (f *Profile) AddToCommand(cmd *cobra.Command) {
func (f *Profile) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
cmd.Flags().StringVarP(&f.profile, "profile", "m", "", tr("Sketch profile to use"))
cmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var sketchProfile string
if len(args) > 0 {
sketchProfile = args[0]
}
return GetSketchProfiles(sketchProfile), cobra.ShellCompDirectiveDefault
return GetSketchProfiles(context.Background(), srv, sketchProfile), cobra.ShellCompDirectiveDefault
})
}

Expand Down
7 changes: 3 additions & 4 deletions internal/cli/arguments/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package arguments
import (
"context"

"github.com/arduino/arduino-cli/commands"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand All @@ -44,18 +43,18 @@ func InitSketchPath(path string) (sketchPath *paths.Path) {

// GetSketchProfiles is an helper function useful to autocomplete.
// It returns the profile names set in the sketch.yaml
func GetSketchProfiles(sketchPath string) []string {
func GetSketchProfiles(ctx context.Context, srv rpc.ArduinoCoreServiceServer, sketchPath string) []string {
if sketchPath == "" {
if wd, _ := paths.Getwd(); wd != nil && wd.String() != "" {
sketchPath = wd.String()
} else {
return nil
}
}
sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath})
resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath})
if err != nil {
return nil
}
profiles := sk.GetProfiles()
profiles := resp.GetSketch().GetProfiles()
return f.Map(profiles, (*rpc.SketchProfile).GetName)
}
4 changes: 2 additions & 2 deletions internal/cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"

"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -56,10 +55,11 @@ func initAttachCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
}

func runAttachCommand(srv rpc.ArduinoCoreServiceServer, path string, port *arguments.Port, fqbn string, programmer *arguments.Programmer) {
ctx := context.Background()
sketchPath := arguments.InitSketchPath(path)

portAddress, portProtocol, _ := port.GetPortAddressAndProtocol(nil, srv, "", "")
newDefaults, err := commands.SetSketchDefaults(context.Background(), &rpc.SetSketchDefaultsRequest{
newDefaults, err := srv.SetSketchDefaults(ctx, &rpc.SetSketchDefaultsRequest{
SketchPath: sketchPath.String(),
DefaultFqbn: fqbn,
DefaultProgrammer: programmer.GetProgrammer(),
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
cmd.AddCommand(lib.NewCommand(srv))
cmd.AddCommand(monitor.NewCommand(srv))
cmd.AddCommand(outdated.NewCommand(srv))
cmd.AddCommand(sketch.NewCommand())
cmd.AddCommand(sketch.NewCommand(srv))
cmd.AddCommand(update.NewCommand(srv))
cmd.AddCommand(upgrade.NewCommand(srv))
cmd.AddCommand(upload.NewCommand(srv))
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
}

fqbnArg.AddToCommand(compileCommand, srv)
profileArg.AddToCommand(compileCommand)
profileArg.AddToCommand(compileCommand, srv)
compileCommand.Flags().BoolVar(&dumpProfile, "dump-profile", false, tr("Create and print a profile configuration from the build."))
showPropertiesArg.AddToCommand(compileCommand)
compileCommand.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
Expand Down Expand Up @@ -161,10 +161,11 @@ func runCompileCommand(cmd *cobra.Command, args []string, srv rpc.ArduinoCoreSer
}

sketchPath := arguments.InitSketchPath(path)
sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
sk := resp.GetSketch()
feedback.WarnAboutDeprecatedFiles(sk)

var inst *rpc.Instance
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
fqbnArg.AddToCommand(debugCommand, srv)
portArgs.AddToCommand(debugCommand, srv)
programmer.AddToCommand(debugCommand, srv)
profileArg.AddToCommand(debugCommand)
profileArg.AddToCommand(debugCommand, srv)
debugCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3"))
debugCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries for debug."))
debugCommand.Flags().BoolVarP(&printInfo, "info", "I", false, tr("Show metadata about the debug session instead of starting the debugger."))
Expand All @@ -83,10 +83,11 @@ func runDebugCommand(srv rpc.ArduinoCoreServiceServer, args []string, portArgs *
}

sketchPath := arguments.InitSketchPath(path)
sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
sk := resp.GetSketch()
feedback.WarnAboutDeprecatedFiles(sk)

var inst *rpc.Instance
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
},
}
portArgs.AddToCommand(monitorCommand, srv)
profileArg.AddToCommand(monitorCommand)
profileArg.AddToCommand(monitorCommand, srv)
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
Expand Down Expand Up @@ -105,13 +105,14 @@ func runMonitorCmd(
// If only --port is set we read the fqbn in the following order: default_fqbn -> discovery
// If only --fqbn is set we read the port in the following order: default_port
sketchPath := arguments.InitSketchPath(sketchPathArg)
sketch, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil && !portArgs.IsPortFlagSet() {
feedback.Fatal(
tr("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s", err),
feedback.ErrGeneric,
)
}
sketch := resp.GetSketch()
if sketch != nil {
defaultPort, defaultProtocol = sketch.GetDefaultPort(), sketch.GetDefaultProtocol()
}
Expand Down
16 changes: 9 additions & 7 deletions internal/cli/sketch/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"

"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand All @@ -29,7 +28,7 @@ import (
)

// initArchiveCommand creates a new `archive` command
func initArchiveCommand() *cobra.Command {
func initArchiveCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
var includeBuildDir, overwrite bool

archiveCommand := &cobra.Command{
Expand All @@ -43,7 +42,9 @@ func initArchiveCommand() *cobra.Command {
" " + os.Args[0] + " archive /home/user/Arduino/MySketch\n" +
" " + os.Args[0] + " archive /home/user/Arduino/MySketch /home/user/MySketchArchive.zip",
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) { runArchiveCommand(args, includeBuildDir, overwrite) },
Run: func(cmd *cobra.Command, args []string) {
runArchiveCommand(srv, args, includeBuildDir, overwrite)
},
}

archiveCommand.Flags().BoolVar(&includeBuildDir, "include-build-dir", false, tr("Includes %s directory in the archive.", "build"))
Expand All @@ -52,9 +53,9 @@ func initArchiveCommand() *cobra.Command {
return archiveCommand
}

func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) {
func runArchiveCommand(srv rpc.ArduinoCoreServiceServer, args []string, includeBuildDir bool, overwrite bool) {
logrus.Info("Executing `arduino-cli sketch archive`")

ctx := context.Background()
sketchPathArg := ""
if len(args) > 0 {
sketchPathArg = args[0]
Expand All @@ -66,13 +67,14 @@ func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) {
}

sketchPath := arguments.InitSketchPath(sketchPathArg)
sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
sk := resp.GetSketch()
feedback.WarnAboutDeprecatedFiles(sk)

if _, err := commands.ArchiveSketch(context.Background(),
if _, err := srv.ArchiveSketch(ctx,
&rpc.ArchiveSketchRequest{
SketchPath: sketchPath.String(),
ArchivePath: archivePathArg,
Expand Down
Loading

0 comments on commit b76847f

Please sign in to comment.