|
| 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 license@arduino.cc. |
| 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 | +} |
0 commit comments