Skip to content

Commit 53f3bf4

Browse files
committed
arduino#1456 - Add "new sketch" in gRPC .proto, regenerate .pg.go. Use "user" dir for sketches created via gRPC
1 parent 7226e52 commit 53f3bf4

File tree

8 files changed

+838
-563
lines changed

8 files changed

+838
-563
lines changed

Diff for: cli/sketch/new.go

+5-17
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
package sketch
1717

1818
import (
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221
"strings"
2322

2423
"github.com/arduino/arduino-cli/cli/errorcodes"
2524
"github.com/arduino/arduino-cli/cli/feedback"
25+
sk "github.com/arduino/arduino-cli/commands/sketch"
2626
"github.com/spf13/cobra"
2727
)
2828

@@ -38,29 +38,17 @@ func initNewCommand() *cobra.Command {
3838
return newCommand
3939
}
4040

41-
var emptySketch = []byte(`
42-
void setup() {
43-
}
44-
45-
void loop() {
46-
}
47-
`)
48-
4941
func runNewCommand(cmd *cobra.Command, args []string) {
5042
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
51-
trimmedSketchName := strings.TrimSuffix(args[0], ".ino")
43+
sketchName := args[0]
44+
trimmedSketchName := strings.TrimSuffix(sketchName, ".ino")
5245
sketchDir, err := filepath.Abs(trimmedSketchName)
5346
if err != nil {
5447
feedback.Errorf(tr("Error creating sketch: %v"), err)
5548
os.Exit(errorcodes.ErrGeneric)
5649
}
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 {
50+
_, err = sk.CreateSketch(sketchDir, sketchName)
51+
if err != nil {
6452
feedback.Errorf(tr("Error creating sketch: %v"), err)
6553
os.Exit(errorcodes.ErrGeneric)
6654
}

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

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
24+
"github.com/arduino/arduino-cli/commands"
25+
"github.com/arduino/arduino-cli/configuration"
26+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27+
)
28+
29+
var emptySketch = []byte(`
30+
void setup() {
31+
}
32+
33+
void loop() {
34+
}
35+
`)
36+
37+
// CreateSketch creates a new sketch
38+
func CreateSketch(sketchDir string, sketchName string) (string, error) {
39+
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
40+
return "", err
41+
}
42+
baseSketchName := filepath.Base(sketchDir)
43+
sketchFile := filepath.Join(sketchDir, baseSketchName+".ino")
44+
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
45+
return "", err
46+
}
47+
return sketchFile, nil
48+
}
49+
50+
// NewSketch FIXMEDOC
51+
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
52+
sketchesDir := configuration.Settings.GetString("directories.User")
53+
sketchDir := filepath.Join(sketchesDir, req.SketchName)
54+
sketchFile, err := CreateSketch(sketchDir, req.SketchName)
55+
if err != nil {
56+
return nil, &commands.CantCreateSketchError{Cause: err}
57+
}
58+
59+
return &rpc.NewSketchResponse{MainFile: sketchFile}, nil
60+
}

Diff for: i18n/data/en.po

+13-13
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}"
@@ -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:47
659+
#: cli/sketch/new.go:52
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:56
19821982
msgid "Sketch created in: %s"
19831983
msgstr "Sketch created in: %s"
19841984

0 commit comments

Comments
 (0)