Skip to content

Update gRPC API to create a new sketch #1498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions cli/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package sketch

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
sk "github.com/arduino/arduino-cli/commands/sketch"
"github.com/spf13/cobra"
)

Expand All @@ -38,29 +38,17 @@ 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")
sketchName := args[0]
trimmedSketchName := strings.TrimSuffix(sketchName, ".ino")
sketchDir, err := filepath.Abs(trimmedSketchName)
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.CreateSketch(sketchDir, sketchName)
if err != nil {
feedback.Errorf(tr("Error creating sketch: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
Expand Down
6 changes: 6 additions & 0 deletions commands/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions commands/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions commands/sketch/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 [email protected].

package sketch

import (
"context"
"io/ioutil"
"os"
"path/filepath"

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

var emptySketch = []byte(`
void setup() {
}

void loop() {
}
`)

// CreateSketch creates a new sketch
func CreateSketch(sketchDir string, sketchName string) (string, error) {
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
return "", err
}
baseSketchName := filepath.Base(sketchDir)
sketchFile := filepath.Join(sketchDir, baseSketchName+".ino")
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
return "", err
}
return sketchFile, nil
}

// NewSketch FIXMEDOC
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
sketchesDir := configuration.Settings.GetString("directories.User")
sketchDir := filepath.Join(sketchesDir, req.SketchName)
sketchFile, err := CreateSketch(sketchDir, req.SketchName)
if err != nil {
return nil, &commands.CantCreateSketchError{Cause: err}
}

return &rpc.NewSketchResponse{MainFile: sketchFile}, nil
}
26 changes: 13 additions & 13 deletions i18n/data/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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:47
#: cli/sketch/new.go:52
msgid "Error creating sketch: %v"
msgstr "Error creating sketch: %v"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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:56
msgid "Sketch created in: %s"
msgstr "Sketch created in: %s"

Expand Down
Loading