Skip to content

Commit 6a6f50a

Browse files
committed
Use gRPC function from CLI to create new sketch
1 parent cd49a5f commit 6a6f50a

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

Diff for: cli/sketch/new.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
package sketch
1717

1818
import (
19+
"context"
1920
"os"
2021
"strings"
2122

2223
"github.com/arduino/arduino-cli/arduino/globals"
2324
"github.com/arduino/arduino-cli/cli/errorcodes"
2425
"github.com/arduino/arduino-cli/cli/feedback"
2526
sk "github.com/arduino/arduino-cli/commands/sketch"
27+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2628
paths "github.com/arduino/go-paths-helper"
2729
"github.com/spf13/cobra"
2830
)
@@ -48,11 +50,15 @@ func runNewCommand(cmd *cobra.Command, args []string) {
4850
feedback.Errorf(tr("Error creating sketch: %v"), err)
4951
os.Exit(errorcodes.ErrGeneric)
5052
}
51-
_, err = sk.CreateSketch(sketchDirPath)
53+
_, err = sk.NewSketch(context.Background(), &rpc.NewSketchRequest{
54+
Instance: nil,
55+
SketchName: sketchDirPath.Base(),
56+
SketchDir: sketchDirPath.Parent().String(),
57+
})
5258
if err != nil {
5359
feedback.Errorf(tr("Error creating sketch: %v"), err)
5460
os.Exit(errorcodes.ErrGeneric)
5561
}
5662

57-
feedback.Print(tr("Sketch created in: %s", sketchDirPath.String()))
63+
feedback.Print(tr("Sketch created in: %s", sketchDirPath))
5864
}

Diff for: commands/sketch/new.go

+7-16
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ void loop() {
3333
}
3434
`)
3535

36-
// CreateSketch creates a new sketch
37-
func CreateSketch(sketchDirPath *paths.Path) (*paths.Path, error) {
38-
if err := sketchDirPath.MkdirAll(); err != nil {
39-
return nil, err
40-
}
41-
baseSketchName := sketchDirPath.Base()
42-
sketchFilePath := sketchDirPath.Join(baseSketchName + globals.MainFileValidExtension)
43-
if err := sketchFilePath.WriteFile(emptySketch); err != nil {
44-
return nil, err
45-
}
46-
return sketchFilePath, nil
47-
}
48-
4936
// NewSketch creates a new sketch via gRPC
5037
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
5138
var sketchesDir string
@@ -55,10 +42,14 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
5542
sketchesDir = configuration.Settings.GetString("directories.User")
5643
}
5744
sketchDirPath := paths.New(sketchesDir).Join(req.SketchName)
58-
sketchFilePath, err := CreateSketch(sketchDirPath)
59-
if err != nil {
45+
if err := sketchDirPath.MkdirAll(); err != nil {
46+
return nil, &commands.CantCreateSketchError{Cause: err}
47+
}
48+
sketchName := sketchDirPath.Base()
49+
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
50+
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
6051
return nil, &commands.CantCreateSketchError{Cause: err}
6152
}
6253

63-
return &rpc.NewSketchResponse{MainFile: sketchFilePath.String()}, nil
54+
return &rpc.NewSketchResponse{MainFile: sketchMainFilePath.String()}, nil
6455
}

Diff for: i18n/data/en.po

+5-5
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ msgstr "Couldn't determine program size"
465465
msgid "Couldn't get current working directory: %v"
466466
msgstr "Couldn't get current working directory: %v"
467467

468-
#: cli/sketch/new.go:33
469-
#: cli/sketch/new.go:34
468+
#: cli/sketch/new.go:35
469+
#: cli/sketch/new.go:36
470470
msgid "Create a new Sketch"
471471
msgstr "Create a new Sketch"
472472

@@ -655,8 +655,8 @@ msgstr "Error creating output dir"
655655
msgid "Error creating sketch archive"
656656
msgstr "Error creating sketch archive"
657657

658-
#: cli/sketch/new.go:48
659-
#: cli/sketch/new.go:53
658+
#: cli/sketch/new.go:50
659+
#: cli/sketch/new.go:59
660660
msgid "Error creating sketch: %v"
661661
msgstr "Error creating sketch: %v"
662662

@@ -1978,7 +1978,7 @@ msgstr "Size (bytes):"
19781978
msgid "Sketch cannot be located in build path. Please specify a different build path"
19791979
msgstr "Sketch cannot be located in build path. Please specify a different build path"
19801980

1981-
#: cli/sketch/new.go:57
1981+
#: cli/sketch/new.go:63
19821982
msgid "Sketch created in: %s"
19831983
msgstr "Sketch created in: %s"
19841984

Diff for: i18n/rice-box.go

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/cc/arduino/cli/commands/v1/commands.proto

+4-3
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ message NewSketchRequest {
288288
Instance instance = 1;
289289
// New sketch name
290290
string sketch_name = 2;
291-
// Optional: create a sketch directory in this directory
292-
// (used as "sketches" directory), the directory must exist.
293-
// "directories.User" directory will be used otherwise.
291+
// Optional: create a Sketch in this directory
292+
// (used as "Sketchbook" directory).
293+
// Default Sketchbook directory "directories.User" is used if sketch_dir is
294+
// empty.
294295
string sketch_dir = 3;
295296
}
296297

0 commit comments

Comments
 (0)