feat(newproject): add archduino new project scaffolder

This commit is contained in:
MDietermann
2026-04-14 14:16:07 +02:00
parent bbaa21a6bf
commit 2464e70cd2
9 changed files with 248 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
Serial.println("Hello from Arduino!");
}
void loop() {
// Your code here
}

View File

@@ -0,0 +1,22 @@
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
delay(1000);
}

View File

@@ -0,0 +1,9 @@
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
Serial.println("Hello from Arduino!");
}
void loop() {
// Your code here
}

View File

@@ -0,0 +1,22 @@
#include <ESP8266WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
delay(1000);
}

View File

@@ -0,0 +1,9 @@
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
Serial.println("Hello from Arduino!");
}
void loop() {
// Your code here
}

View File

@@ -0,0 +1,9 @@
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
Serial.println("Hello from Arduino!");
}
void loop() {
// Your code here
}

View File

@@ -0,0 +1,9 @@
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
Serial.println("Hello from Arduino!");
}
void loop() {
// Your code here
}

View File

@@ -0,0 +1,75 @@
// Package newproject scaffolds a new Arduino sketch directory.
package newproject
import (
"embed"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/MDietermann/ArduinoArchSetup/internal/config"
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
)
//go:embed assets
var assetsFS embed.FS
const clangdContent = `CompileFlags:
Add:
- -xc++
- -std=c++17
`
// Run creates a new Arduino project directory. It errors if the project
// directory already exists, if the board is unknown, or if the project name
// contains a path separator.
func Run(cfg config.Config, project, board string) error {
if project == "" {
return fmt.Errorf("project name required")
}
if strings.ContainsAny(project, "/\\") || strings.HasPrefix(project, ".") {
return fmt.Errorf("invalid project name %q", project)
}
var b *config.Board
var valid []string
for i := range cfg.Scaffolding.Boards {
bp := &cfg.Scaffolding.Boards[i]
valid = append(valid, bp.Name)
if bp.Name == board {
b = bp
}
}
if b == nil {
return fmt.Errorf("unknown board %q; valid: %s", board, strings.Join(valid, ", "))
}
if _, err := os.Stat(project); err == nil {
return fmt.Errorf("project directory %q already exists", project)
}
if err := os.MkdirAll(project, 0o755); err != nil {
return err
}
// Pick .ino template: try <template>.ino, else default.ino.
inoData, err := assetsFS.ReadFile(filepath.Join("assets", b.Template+".ino"))
if err != nil {
inoData, err = assetsFS.ReadFile("assets/default.ino")
if err != nil {
return err
}
}
if err := platform.WriteUserFile(filepath.Join(project, project+".ino"), inoData, 0o644); err != nil {
return err
}
sketch := fmt.Sprintf("default_fqbn: %s\ndefault_port: auto\n", b.FQBN)
if err := platform.WriteUserFile(filepath.Join(project, "sketch.yaml"), []byte(sketch), 0o644); err != nil {
return err
}
if err := platform.WriteUserFile(filepath.Join(project, ".clangd"), []byte(clangdContent), 0o644); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,84 @@
package newproject
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/MDietermann/ArduinoArchSetup/internal/config"
)
func sampleCfg() config.Config {
return config.Config{
Scaffolding: config.Scaffolding{Boards: []config.Board{
{Name: "uno", FQBN: "arduino:avr:uno", Template: "uno"},
{Name: "xyz", FQBN: "fake:fqbn:xyz", Template: "does-not-exist"},
}},
}
}
func TestRunCreatesProject(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
if err := Run(sampleCfg(), "blinky", "uno"); err != nil {
t.Fatal(err)
}
ino, err := os.ReadFile(filepath.Join("blinky", "blinky.ino"))
if err != nil {
t.Fatalf("blinky.ino missing: %v", err)
}
if len(ino) == 0 {
t.Error("blinky.ino empty")
}
sketch, err := os.ReadFile(filepath.Join("blinky", "sketch.yaml"))
if err != nil {
t.Fatalf("sketch.yaml missing: %v", err)
}
if !strings.Contains(string(sketch), "arduino:avr:uno") {
t.Errorf("sketch.yaml missing fqbn: %s", sketch)
}
if _, err := os.Stat(filepath.Join("blinky", ".clangd")); err != nil {
t.Errorf(".clangd missing: %v", err)
}
}
func TestRunRefusesExistingDir(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
os.Mkdir("blinky", 0o755)
if err := Run(sampleCfg(), "blinky", "uno"); err == nil {
t.Fatal("want error on existing dir")
}
}
func TestRunFallsBackToDefaultTemplate(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
if err := Run(sampleCfg(), "thing", "xyz"); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join("thing", "thing.ino")); err != nil {
t.Fatalf("thing.ino missing: %v", err)
}
}
func TestRunUnknownBoardErrors(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
err := Run(sampleCfg(), "thing", "bogus")
if err == nil {
t.Fatal("want error")
}
if !strings.Contains(err.Error(), "uno") {
t.Errorf("error should list valid boards: %v", err)
}
}
func TestRunRejectsSlashInProjectName(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
if err := Run(sampleCfg(), "bad/name", "uno"); err == nil {
t.Fatal("want error on slashes")
}
}