Skip to content

Commit ec376de

Browse files
Add gRPC function to create new Sketch (#1498)
* #1456 - Add "new sketch" in gRPC .proto, regenerate .pg.go. Use "user" dir for sketches created via gRPC * #1456 - Avoid using hardcoded extension (address comment). * #1456 - Add optional dir field in the request, use it. * #1456 - Use go-paths-helper * Use gRPC function from CLI to create new sketch Co-authored-by: Silvano Cerza <[email protected]>
1 parent 1bd9945 commit ec376de

File tree

9 files changed

+865
-570
lines changed

9 files changed

+865
-570
lines changed

Diff for: cli/sketch/new.go

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

1818
import (
19-
"io/ioutil"
19+
"context"
2020
"os"
21-
"path/filepath"
2221
"strings"
2322

23+
"github.com/arduino/arduino-cli/arduino/globals"
2424
"github.com/arduino/arduino-cli/cli/errorcodes"
2525
"github.com/arduino/arduino-cli/cli/feedback"
26+
sk "github.com/arduino/arduino-cli/commands/sketch"
27+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
28+
paths "github.com/arduino/go-paths-helper"
2629
"github.com/spf13/cobra"
2730
)
2831

@@ -38,32 +41,24 @@ func initNewCommand() *cobra.Command {
3841
return newCommand
3942
}
4043

41-
var emptySketch = []byte(`
42-
void setup() {
43-
}
44-
45-
void loop() {
46-
}
47-
`)
48-
4944
func runNewCommand(cmd *cobra.Command, args []string) {
5045
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
51-
trimmedSketchName := strings.TrimSuffix(args[0], ".ino")
52-
sketchDir, err := filepath.Abs(trimmedSketchName)
46+
sketchName := args[0]
47+
trimmedSketchName := strings.TrimSuffix(sketchName, globals.MainFileValidExtension)
48+
sketchDirPath, err := paths.New(trimmedSketchName).Abs()
5349
if err != nil {
5450
feedback.Errorf(tr("Error creating sketch: %v"), err)
5551
os.Exit(errorcodes.ErrGeneric)
5652
}
57-
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
58-
feedback.Errorf(tr("Could not create sketch directory: %v"), err)
59-
os.Exit(errorcodes.ErrGeneric)
60-
}
61-
sketchName := filepath.Base(sketchDir)
62-
sketchFile := filepath.Join(sketchDir, sketchName+".ino")
63-
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
53+
_, err = sk.NewSketch(context.Background(), &rpc.NewSketchRequest{
54+
Instance: nil,
55+
SketchName: sketchDirPath.Base(),
56+
SketchDir: sketchDirPath.Parent().String(),
57+
})
58+
if err != nil {
6459
feedback.Errorf(tr("Error creating sketch: %v"), err)
6560
os.Exit(errorcodes.ErrGeneric)
6661
}
6762

68-
feedback.Print(tr("Sketch created in: %s", sketchDir))
63+
feedback.Print(tr("Sketch created in: %s", sketchDirPath))
6964
}

Diff for: commands/daemon/daemon.go

+6
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ func (s *ArduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq
243243
return &rpc.VersionResponse{Version: s.VersionString}, nil
244244
}
245245

246+
// NewSketch FIXMEDOC
247+
func (s *ArduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
248+
resp, err := sketch.NewSketch(ctx, req)
249+
return resp, convertErrorToRPCStatus(err)
250+
}
251+
246252
// LoadSketch FIXMEDOC
247253
func (s *ArduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
248254
resp, err := commands.LoadSketch(ctx, req)

Diff for: commands/errors.go

+13
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,19 @@ func (e *MissingSketchPathError) ToRPCStatus() *status.Status {
368368
return status.New(codes.InvalidArgument, e.Error())
369369
}
370370

371+
// CantCreateSketchError is returned when the sketch cannot be created
372+
type CantCreateSketchError struct {
373+
Cause error
374+
}
375+
376+
func (e *CantCreateSketchError) Error() string {
377+
return composeErrorMsg(tr("Can't create sketch"), e.Cause)
378+
}
379+
380+
func (e *CantCreateSketchError) Unwrap() error {
381+
return e.Cause
382+
}
383+
371384
// CantOpenSketchError is returned when the sketch is not found or cannot be opened
372385
type CantOpenSketchError struct {
373386
Cause error

Diff for: commands/sketch/new.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package sketch
17+
18+
import (
19+
"context"
20+
21+
"github.com/arduino/arduino-cli/arduino/globals"
22+
"github.com/arduino/arduino-cli/commands"
23+
"github.com/arduino/arduino-cli/configuration"
24+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
25+
paths "github.com/arduino/go-paths-helper"
26+
)
27+
28+
var emptySketch = []byte(`
29+
void setup() {
30+
}
31+
32+
void loop() {
33+
}
34+
`)
35+
36+
// NewSketch creates a new sketch via gRPC
37+
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
38+
var sketchesDir string
39+
if len(req.SketchDir) > 0 {
40+
sketchesDir = req.SketchDir
41+
} else {
42+
sketchesDir = configuration.Settings.GetString("directories.User")
43+
}
44+
sketchDirPath := paths.New(sketchesDir).Join(req.SketchName)
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 {
51+
return nil, &commands.CantCreateSketchError{Cause: err}
52+
}
53+
54+
return &rpc.NewSketchResponse{MainFile: sketchMainFilePath.String()}, nil
55+
}

Diff for: i18n/data/en.po

+15-15
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ msgstr "%s pattern is missing"
7070
msgid "%s uninstalled"
7171
msgstr "%s uninstalled"
7272

73-
#: commands/errors.go:658
73+
#: commands/errors.go:671
7474
msgid "'%s' has an invalid signature"
7575
msgstr "'%s' has an invalid signature"
7676

@@ -247,6 +247,10 @@ msgstr "Builds of 'core.a' are saved into this path to be cached and reused."
247247
msgid "Can't create data directory %s"
248248
msgstr "Can't create data directory %s"
249249

250+
#: commands/errors.go:377
251+
msgid "Can't create sketch"
252+
msgstr "Can't create sketch"
253+
250254
#: commands/lib/download.go:60
251255
#: commands/lib/download.go:63
252256
#: commands/lib/download.go:65
@@ -260,7 +264,7 @@ msgstr "Can't download library"
260264
msgid "Can't find dependencies for platform %s"
261265
msgstr "Can't find dependencies for platform %s"
262266

263-
#: commands/errors.go:377
267+
#: commands/errors.go:390
264268
msgid "Can't open sketch"
265269
msgstr "Can't open sketch"
266270

@@ -294,11 +298,11 @@ msgstr "Cannot create config file directory: %v"
294298
msgid "Cannot create config file: %v"
295299
msgstr "Cannot create config file: %v"
296300

297-
#: commands/errors.go:621
301+
#: commands/errors.go:634
298302
msgid "Cannot create temp dir"
299303
msgstr "Cannot create temp dir"
300304

301-
#: commands/errors.go:639
305+
#: commands/errors.go:652
302306
msgid "Cannot create temp file"
303307
msgstr "Cannot create temp file"
304308

@@ -448,10 +452,6 @@ msgstr "Could not connect via HTTP"
448452
msgid "Could not create index directory"
449453
msgstr "Could not create index directory"
450454

451-
#: cli/sketch/new.go:58
452-
msgid "Could not create sketch directory: %v"
453-
msgstr "Could not create sketch directory: %v"
454-
455455
#: legacy/builder/phases/core_builder.go:48
456456
msgid "Couldn't deeply cache core build: {0}"
457457
msgstr "Couldn't deeply cache core build: {0}"
@@ -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:32
469-
#: cli/sketch/new.go:33
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:54
659-
#: cli/sketch/new.go:64
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

@@ -1344,7 +1344,7 @@ msgstr "Library '%s' not found"
13441344
msgid "Library can't use both '%[1]s' and '%[2]s' folders. Double check {0}"
13451345
msgstr "Library can't use both '%[1]s' and '%[2]s' folders. Double check {0}"
13461346

1347-
#: commands/errors.go:414
1347+
#: commands/errors.go:427
13481348
msgid "Library install failed"
13491349
msgstr "Library install failed"
13501350

@@ -1741,7 +1741,7 @@ msgstr "Port"
17411741
msgid "Port closed:"
17421742
msgstr "Port closed:"
17431743

1744-
#: commands/errors.go:508
1744+
#: commands/errors.go:521
17451745
msgid "Port monitor error"
17461746
msgstr "Port monitor error"
17471747

@@ -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:68
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.

0 commit comments

Comments
 (0)