Skip to content
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

Enable creating new sketches from a template directory #2359

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 23 additions & 9 deletions commands/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,33 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
return nil, err
}

templateDir := configuration.Settings.GetString("sketch.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"))}

if templateDir != "" {
templateDirPath := paths.New(templateDir)
if err := templateDirPath.CopyDirTo(sketchDirPath); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}

oldMainFilePath := sketchDirPath.Join(templateDirPath.Base() + globals.MainFileValidExtension)
if err := oldMainFilePath.Rename(sketchMainFilePath); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
} else {
if err := sketchDirPath.MkdirAll(); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
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
2 changes: 2 additions & 0 deletions configuration/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ func SetDefaults(settings *viper.Viper) {
settings.BindEnv("directories.Downloads", "ARDUINO_DOWNLOADS_DIR")
settings.BindEnv("directories.Data", "ARDUINO_DATA_DIR")
settings.BindEnv("sketch.always_export_binaries", "ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES")

settings.SetDefault("sketch.template", "")
cmaglie marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions internal/cli/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var validMap = map[string]reflect.Kind{
"logging.format": reflect.String,
"logging.level": reflect.String,
"sketch.always_export_binaries": reflect.Bool,
"sketch.template": reflect.String,
"metrics.addr": reflect.String,
"metrics.enabled": reflect.Bool,
"network.proxy": reflect.String,
Expand Down