Browse Source

feat: Add ECAl file formatter

Matthias Ladkau 3 years ago
parent
commit
e3db154542
3 changed files with 85 additions and 2 deletions
  1. 4 1
      cli/ecal.go
  2. 78 0
      cli/tool/format.go
  3. 3 1
      cli/tool/pack.go

+ 4 - 1
cli/ecal.go

@@ -46,9 +46,10 @@ func main() {
 		fmt.Println("Available commands:")
 		fmt.Println()
 		fmt.Println("    console   Interactive console (default)")
-		fmt.Println("    run       Execute ECAL code")
 		fmt.Println("    debug     Run in debug mode")
+		fmt.Println("    format    Format all ECAL files in a directory structure")
 		fmt.Println("    pack      Create a single executable from ECAL code")
+		fmt.Println("    run       Execute ECAL code")
 		fmt.Println()
 		fmt.Println(fmt.Sprintf("Use %s <command> -help for more information about a given command.", os.Args[0]))
 		fmt.Println()
@@ -73,6 +74,8 @@ func main() {
 			} else if arg == "pack" {
 				packer := tool.NewCLIPacker()
 				err = packer.Pack()
+			} else if arg == "format" {
+				err = tool.Format()
 			} else {
 				flag.Usage()
 			}

+ 78 - 0
cli/tool/format.go

@@ -0,0 +1,78 @@
+/*
+ * ECAL
+ *
+ * Copyright 2020 Matthias Ladkau. All rights reserved.
+ *
+ * This Source Code Form is subject to the terms of the MIT
+ * License, If a copy of the MIT License was not distributed with this
+ * file, You can obtain one at https://opensource.org/licenses/MIT.
+ */
+
+package tool
+
+import (
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+
+	"devt.de/krotik/ecal/parser"
+)
+
+func Format() error {
+	var err error
+
+	wd, _ := os.Getwd()
+
+	dir := flag.String("dir", wd, "Root directory for ECAL files")
+	ext := flag.String("ext", ".ecal", "Extension for ECAL files")
+	showHelp := flag.Bool("help", false, "Show this help message")
+
+	flag.Usage = func() {
+		fmt.Println()
+		fmt.Println(fmt.Sprintf("Usage of %s format [options]", os.Args[0]))
+		fmt.Println()
+		flag.PrintDefaults()
+		fmt.Println()
+		fmt.Println("This tool will format all ECAL files in a directory structure.")
+		fmt.Println()
+	}
+
+	if len(os.Args) >= 2 {
+		flag.CommandLine.Parse(os.Args[2:])
+
+		if *showHelp {
+			flag.Usage()
+			return nil
+		}
+	}
+
+	fmt.Println(fmt.Sprintf("Formatting all %v files in %v", *ext, *dir))
+
+	err = filepath.Walk(".",
+		func(path string, i os.FileInfo, err error) error {
+			if err == nil && !i.IsDir() {
+				var data []byte
+				var ast *parser.ASTNode
+				var srcFormatted string
+
+				if data, err = ioutil.ReadFile(path); err == nil {
+					var ferr error
+
+					if ast, ferr = parser.Parse(path, string(data)); ferr == nil {
+						if srcFormatted, ferr = parser.PrettyPrint(ast); ferr == nil {
+							ioutil.WriteFile(path, []byte(srcFormatted), i.Mode())
+						}
+					}
+
+					if ferr != nil {
+						fmt.Fprintln(os.Stderr, fmt.Sprintf("Could not format %v: %v", path, ferr))
+					}
+				}
+			}
+			return err
+		})
+
+	return err
+}

+ 3 - 1
cli/tool/pack.go

@@ -75,7 +75,7 @@ func (p *CLIPacker) ParseArgs() bool {
 
 	flag.Usage = func() {
 		fmt.Println()
-		fmt.Println(fmt.Sprintf("Usage of %s run [options] [entry file]", os.Args[0]))
+		fmt.Println(fmt.Sprintf("Usage of %s pack [options] [entry file]", os.Args[0]))
 		fmt.Println()
 		flag.PrintDefaults()
 		fmt.Println()
@@ -89,6 +89,8 @@ func (p *CLIPacker) ParseArgs() bool {
 
 		if cargs := flag.Args(); len(cargs) > 0 {
 			p.EntryFile = flag.Arg(0)
+		} else {
+			*showHelp = true
 		}
 
 		if *showHelp {