8000 Enable creating new sketches from a template directory by gsingh93 · Pull Request #2359 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content
8000

Enable creating new sketches from a template directory #2359

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
Prev Previous commit
Next Next commit
Create sketch from template if template directory is set
  • Loading branch information
gsingh93 committed Oct 19, 2023
commit 78a742a0e5fd2f1600880558ffbbbe01a63dc746
35 changes: 24 additions & 11 deletions commands/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,32 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
return nil, err
}

templateDir := configuration.Settings.GetString("directories.template")
sketchDirPath := paths.New(sketchesDir).Join(req.SketchName)
if err := sketchDirPath.MkdirAll(); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
sketchName := sketchDirPath.Base()
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
if !req.Overwrite {
if sketchMainFilePath.Exist() {
return nil, &arduino.CantCreateSketchError{Cause: errors.New(tr(".ino file already exists"))}

var sketchMainFilePath *paths.Path
if templateDir != "" {
templateDirPath := paths.New(templateDir)
if err := templateDirPath.CopyDirTo(sketchDirPath); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
// TODO: Make this customizable?
sketchMainFilePath = sketchDirPath.Join("main.ino")
} else {
if err := sketchDirPath.MkdirAll(); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
sketchName := sketchDirPath.Base()
sketchMainFilePath = sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
if !req.Overwrite {
if sketchMainFilePath.Exist() {
return nil, &arduino.CantCreateSketchError{Cause: errors.New(tr(".ino file already exists"))}
}
}

if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
}
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}

return &rpc.NewSketchResponse{MainFile: sketchMainFilePath.String()}, nil
Expand Down
0