Working
This commit is contained in:
113
cmd/mdt/main.go
Normal file
113
cmd/mdt/main.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/marte-dev/marte-dev-tools/internal/builder"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/lsp"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/validator"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Usage: mdt <command> [arguments]")
|
||||
fmt.Println("Commands: lsp, build, check, fmt")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
command := os.Args[1]
|
||||
switch command {
|
||||
case "lsp":
|
||||
runLSP()
|
||||
case "build":
|
||||
runBuild(os.Args[2:])
|
||||
case "check":
|
||||
runCheck(os.Args[2:])
|
||||
case "fmt":
|
||||
runFmt()
|
||||
default:
|
||||
fmt.Printf("Unknown command: %s\n", command)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func runLSP() {
|
||||
lsp.RunServer()
|
||||
}
|
||||
|
||||
func runBuild(args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("Usage: mdt build <input_files...>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
outputDir := "build"
|
||||
os.MkdirAll(outputDir, 0755)
|
||||
|
||||
b := builder.NewBuilder(args)
|
||||
err := b.Build(outputDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Build failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("Build successful. Output in", outputDir)
|
||||
}
|
||||
|
||||
func runCheck(args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("Usage: mdt check <input_files...>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
idx := index.NewIndex()
|
||||
configs := make(map[string]*parser.Configuration)
|
||||
|
||||
for _, file := range args {
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading %s: %v\n", file, err)
|
||||
continue
|
||||
}
|
||||
|
||||
p := parser.NewParser(string(content))
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
fmt.Printf("%s: Grammar error: %v\n", file, err)
|
||||
continue
|
||||
}
|
||||
|
||||
configs[file] = config
|
||||
idx.IndexConfig(file, config)
|
||||
}
|
||||
|
||||
idx.ResolveReferences()
|
||||
v := validator.NewValidator(idx)
|
||||
|
||||
for file, config := range configs {
|
||||
v.Validate(file, config)
|
||||
}
|
||||
v.CheckUnused()
|
||||
|
||||
for _, diag := range v.Diagnostics {
|
||||
level := "ERROR"
|
||||
if diag.Level == validator.LevelWarning {
|
||||
level = "WARNING"
|
||||
}
|
||||
fmt.Printf("%s:%d:%d: %s: %s\n", diag.File, diag.Position.Line, diag.Position.Column, level, diag.Message)
|
||||
}
|
||||
|
||||
if len(v.Diagnostics) > 0 {
|
||||
fmt.Printf("\nFound %d issues.\n", len(v.Diagnostics))
|
||||
} else {
|
||||
fmt.Println("No issues found.")
|
||||
}
|
||||
}
|
||||
|
||||
func runFmt() {
|
||||
fmt.Println("Formatting files...")
|
||||
// TODO: Implement fmt
|
||||
}
|
||||
Reference in New Issue
Block a user