Unverified Commit ec376ded authored by Anton Smirnov's avatar Anton Smirnov Committed by GitHub

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: default avatarSilvano Cerza <silvanocerza@gmail.com>
parent 1bd9945c
......@@ -16,13 +16,16 @@
package sketch
import (
"io/ioutil"
"context"
"os"
"path/filepath"
"strings"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
sk "github.com/arduino/arduino-cli/commands/sketch"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"
)
......@@ -38,32 +41,24 @@ func initNewCommand() *cobra.Command {
return newCommand
}
var emptySketch = []byte(`
void setup() {
}
void loop() {
}
`)
func runNewCommand(cmd *cobra.Command, args []string) {
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
trimmedSketchName := strings.TrimSuffix(args[0], ".ino")
sketchDir, err := filepath.Abs(trimmedSketchName)
sketchName := args[0]
trimmedSketchName := strings.TrimSuffix(sketchName, globals.MainFileValidExtension)
sketchDirPath, err := paths.New(trimmedSketchName).Abs()
if err != nil {
feedback.Errorf(tr("Error creating sketch: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
feedback.Errorf(tr("Could not create sketch directory: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
sketchName := filepath.Base(sketchDir)
sketchFile := filepath.Join(sketchDir, sketchName+".ino")
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
_, err = sk.NewSketch(context.Background(), &rpc.NewSketchRequest{
Instance: nil,
SketchName: sketchDirPath.Base(),
SketchDir: sketchDirPath.Parent().String(),
})
if err != nil {
feedback.Errorf(tr("Error creating sketch: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
feedback.Print(tr("Sketch created in: %s", sketchDir))
feedback.Print(tr("Sketch created in: %s", sketchDirPath))
}
......@@ -243,6 +243,12 @@ 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) {
resp, err := sketch.NewSketch(ctx, req)
return resp, convertErrorToRPCStatus(err)
}
// LoadSketch FIXMEDOC
func (s *ArduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
resp, err := commands.LoadSketch(ctx, req)
......
......@@ -368,6 +368,19 @@ func (e *MissingSketchPathError) ToRPCStatus() *status.Status {
return status.New(codes.InvalidArgument, e.Error())
}
// CantCreateSketchError is returned when the sketch cannot be created
type CantCreateSketchError struct {
Cause error
}
func (e *CantCreateSketchError) Error() string {
return composeErrorMsg(tr("Can't create sketch"), e.Cause)
}
func (e *CantCreateSketchError) Unwrap() error {
return e.Cause
}
// CantOpenSketchError is returned when the sketch is not found or cannot be opened
type CantOpenSketchError struct {
Cause error
......
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package sketch
import (
"context"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
)
var emptySketch = []byte(`
void setup() {
}
void loop() {
}
`)
// NewSketch creates a new sketch via gRPC
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
var sketchesDir string
if len(req.SketchDir) > 0 {
sketchesDir = req.SketchDir
} else {
sketchesDir = configuration.Settings.GetString("directories.User")
}
sketchDirPath := paths.New(sketchesDir).Join(req.SketchName)
if err := sketchDirPath.MkdirAll(); err != nil {
return nil, &commands.CantCreateSketchError{Cause: err}
}
sketchName := sketchDirPath.Base()
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
return nil, &commands.CantCreateSketchError{Cause: err}
}
return &rpc.NewSketchResponse{MainFile: sketchMainFilePath.String()}, nil
}
......@@ -70,7 +70,7 @@ msgstr "%s pattern is missing"
msgid "%s uninstalled"
msgstr "%s uninstalled"
#: commands/errors.go:658
#: commands/errors.go:671
msgid "'%s' has an invalid signature"
msgstr "'%s' has an invalid signature"
......@@ -247,6 +247,10 @@ msgstr "Builds of 'core.a' are saved into this path to be cached and reused."
msgid "Can't create data directory %s"
msgstr "Can't create data directory %s"
#: commands/errors.go:377
msgid "Can't create sketch"
msgstr "Can't create sketch"
#: commands/lib/download.go:60
#: commands/lib/download.go:63
#: commands/lib/download.go:65
......@@ -260,7 +264,7 @@ msgstr "Can't download library"
msgid "Can't find dependencies for platform %s"
msgstr "Can't find dependencies for platform %s"
#: commands/errors.go:377
#: commands/errors.go:390
msgid "Can't open sketch"
msgstr "Can't open sketch"
......@@ -294,11 +298,11 @@ msgstr "Cannot create config file directory: %v"
msgid "Cannot create config file: %v"
msgstr "Cannot create config file: %v"
#: commands/errors.go:621
#: commands/errors.go:634
msgid "Cannot create temp dir"
msgstr "Cannot create temp dir"
#: commands/errors.go:639
#: commands/errors.go:652
msgid "Cannot create temp file"
msgstr "Cannot create temp file"
......@@ -448,10 +452,6 @@ msgstr "Could not connect via HTTP"
msgid "Could not create index directory"
msgstr "Could not create index directory"
#: cli/sketch/new.go:58
msgid "Could not create sketch directory: %v"
msgstr "Could not create sketch directory: %v"
#: legacy/builder/phases/core_builder.go:48
msgid "Couldn't deeply cache core build: {0}"
msgstr "Couldn't deeply cache core build: {0}"
......@@ -465,8 +465,8 @@ msgstr "Couldn't determine program size"
msgid "Couldn't get current working directory: %v"
msgstr "Couldn't get current working directory: %v"
#: cli/sketch/new.go:32
#: cli/sketch/new.go:33
#: cli/sketch/new.go:35
#: cli/sketch/new.go:36
msgid "Create a new Sketch"
msgstr "Create a new Sketch"
......@@ -655,8 +655,8 @@ msgstr "Error creating output dir"
msgid "Error creating sketch archive"
msgstr "Error creating sketch archive"
#: cli/sketch/new.go:54
#: cli/sketch/new.go:64
#: cli/sketch/new.go:50
#: cli/sketch/new.go:59
msgid "Error creating sketch: %v"
msgstr "Error creating sketch: %v"
......@@ -1344,7 +1344,7 @@ msgstr "Library '%s' not found"
msgid "Library can't use both '%[1]s' and '%[2]s' folders. Double check {0}"
msgstr "Library can't use both '%[1]s' and '%[2]s' folders. Double check {0}"
#: commands/errors.go:414
#: commands/errors.go:427
msgid "Library install failed"
msgstr "Library install failed"
......@@ -1741,7 +1741,7 @@ msgstr "Port"
msgid "Port closed:"
msgstr "Port closed:"
#: commands/errors.go:508
#: commands/errors.go:521
msgid "Port monitor error"
msgstr "Port monitor error"
......@@ -1978,7 +1978,7 @@ msgstr "Size (bytes):"
msgid "Sketch cannot be located in build path. Please specify a different build path"
msgstr "Sketch cannot be located in build path. Please specify a different build path"
#: cli/sketch/new.go:68
#: cli/sketch/new.go:63
msgid "Sketch created in: %s"
msgstr "Sketch created in: %s"
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -64,6 +64,9 @@ service ArduinoCoreService {
// Get the version of Arduino CLI in use.
rpc Version(VersionRequest) returns (VersionResponse) {}
// Create a new Sketch
rpc NewSketch(NewSketchRequest) returns (NewSketchResponse) {}
// Returns all files composing a Sketch
rpc LoadSketch(LoadSketchRequest) returns (LoadSketchResponse) {}
......@@ -280,6 +283,23 @@ message VersionResponse {
string version = 1;
}
message NewSketchRequest {
// Arduino Core Service instance from the Init response.
Instance instance = 1;
// New sketch name
string sketch_name = 2;
// Optional: create a Sketch in this directory
// (used as "Sketchbook" directory).
// Default Sketchbook directory "directories.User" is used if sketch_dir is
// empty.
string sketch_dir = 3;
}
message NewSketchResponse {
// Absolute path to a main sketch file
string main_file = 1;
}
message LoadSketchRequest {
// Arduino Core Service instance from the Init response.
Instance instance = 1;
......
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