added variables and producer check

This commit is contained in:
Martino Ferrari
2026-01-28 17:50:49 +01:00
parent 8811ac9273
commit 03fe7d33b0
11 changed files with 413 additions and 48 deletions

View File

@@ -3,6 +3,8 @@ package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"github.com/marte-community/marte-dev-tools/internal/builder"
"github.com/marte-community/marte-dev-tools/internal/formatter"
@@ -17,10 +19,6 @@ func main() {
if len(os.Args) < 2 {
logger.Println("Usage: mdt <command> [arguments]")
logger.Println("Commands: lsp, build, check, fmt, init")
logger.Println(" build [-o output_file] <input_files...>")
logger.Println(" check <input_files...>")
logger.Println(" fmt <input_files...>")
logger.Println(" init <project_name>")
os.Exit(1)
}
@@ -47,46 +45,47 @@ func runLSP() {
}
func runBuild(args []string) {
if len(args) < 1 {
logger.Println("Usage: mdt build [-o output_file] <input_files...>")
os.Exit(1)
}
var outputFilePath string
var inputFiles []string
files := []string{}
overrides := make(map[string]string)
outputFile := ""
for i := 0; i < len(args); i++ {
if args[i] == "-o" {
arg := args[i]
if strings.HasPrefix(arg, "-v") {
pair := arg[2:]
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
overrides[parts[0]] = parts[1]
}
} else if arg == "-o" {
if i+1 < len(args) {
outputFilePath = args[i+1]
outputFile = args[i+1]
i++
} else {
logger.Println("Error: -o requires a file path")
os.Exit(1)
}
} else {
inputFiles = append(inputFiles, args[i])
files = append(files, arg)
}
}
if len(inputFiles) < 1 {
logger.Println("Usage: mdt build [-o output_file] <input_files...>")
if len(files) < 1 {
logger.Println("Usage: mdt build [-o output] [-vVAR=VAL] <input_files...>")
os.Exit(1)
}
output := os.Stdout
if outputFilePath != "" {
f, err := os.Create(outputFilePath)
b := builder.NewBuilder(files, overrides)
var out *os.File = os.Stdout
if outputFile != "" {
f, err := os.Create(outputFile)
if err != nil {
logger.Printf("Error creating output file %s: %v\n", outputFilePath, err)
logger.Printf("Error creating output file: %v\n", err)
os.Exit(1)
}
defer f.Close()
output = f
out = f
}
b := builder.NewBuilder(inputFiles)
err := b.Build(output)
err := b.Build(out)
if err != nil {
logger.Printf("Build failed: %v\n", err)
os.Exit(1)
@@ -175,24 +174,62 @@ func runInit(args []string) {
}
projectName := args[0]
if err := os.MkdirAll("src", 0755); err != nil {
if err := os.MkdirAll(filepath.Join(projectName, "src"), 0755); err != nil {
logger.Fatalf("Error creating project directories: %v", err)
}
files := map[string]string{
"Makefile": "MDT=mdt\n\nall: check build\n\ncheck:\n\t$(MDT) check src/*.marte\n\nbuild:\n\t$(MDT) build -o app.marte src/*.marte\n\nfmt:\n\t$(MDT) fmt src/*.marte\n",
".marte_schema.cue": "package schema\n\n#Classes: {\n // Add your project-specific classes here\n}\n",
"src/app.marte": "#package " + projectName + "\n\n+App = {\n Class = RealTimeApplication\n +Data = {\n Class = ReferenceContainer\n }\n +Functions = {\n Class = ReferenceContainer\n }\n +States = {\n Class = ReferenceContainer\n }\n +Scheduler = {\n Class = GAMScheduler\n TimingDataSource = TimingDataSource\n }\n}\n",
"src/data.marte": "#package " + projectName + ".App.Data\n\n// Define your DataSources here\nDefaultDataSource = DDB\n//# Default DB\n+DDB = {\n Class=GAMDataSource\n}\n//# Timing Data Source to track threads timings\n+TimingDataSource = {\n Class = TimingDataSource\n}",
"src/functions.marte": "#package " + projectName + ".App.Functions\n\n// Define your GAMs here\n",
"Makefile": `MDT=mdt
all: check build
check:
$(MDT) check src/*.marte
build:
$(MDT) build -o app.marte src/*.marte
fmt:
$(MDT) fmt src/*.marte
`,
".marte_schema.cue": `package schema
#Classes: {
// Add your project-specific classes here
}
`,
"src/app.marte": `#package App
+Main = {
Class = RealTimeApplication
+States = {
Class = ReferenceContainer
+Run = {
Class = RealTimeState
+MainThread = {
Class = RealTimeThread
Functions = {}
}
}
}
+Data = {
Class = ReferenceContainer
}
}
`,
"src/components.marte": `#package App.Data
// Define your DataSources here
`,
}
for path, content := range files {
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
logger.Fatalf("Error creating file %s: %v", path, err)
fullPath := filepath.Join(projectName, path)
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
logger.Fatalf("Error creating file %s: %v", fullPath, err)
}
logger.Printf("Created %s\n", path)
logger.Printf("Created %s\n", fullPath)
}
logger.Printf("Project '%s' initialized successfully.\n", projectName)
}
}