changed: added optional parameter to `onyx pkg new`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 14 May 2023 05:01:58 +0000 (00:01 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 14 May 2023 05:01:58 +0000 (00:01 -0500)
scripts/default.json
scripts/onyx-pkg.onyx

index 85e0c551f891fb4e8a9c0cefe824f478537aa56a..e03520319f989ce832dbf57b1c21c755531fb47d 100644 (file)
         }
     },
     "files": {
-        ".gitignore": "*.wasm",
+        ".gitignore": "*.wasm\nlib\nbin",
         "onyx-lsp.ini": "[lsp]\nmode=project\nonyxFiles=src/main.onyx\nworkingDir=.",
         "onyx-pkg.ini": "[metadata]\nname={{name}}\ndescription={{description}}\nurl={{url}}\nauthor={{author}}\nversion=0.0.1\n",
         "src": {
             "main.onyx": "use core {*}\n\nmain :: () {\n    println(\"Hello Onyx!\");\n}\n"
         }
-    }
+    },
+    "commands": [
+        "git init"
+    ]
 }
index 6c33d27de2cbc147b5fae5f6cc381de3a2a1a673..ddedbc00cec4f6a1d4a2d853f34b9fa36ed2e4ef 100644 (file)
@@ -67,6 +67,7 @@ main :: (args: [] cstr) {
     if command_tag.tag.require_config_file {
         if !load_config_file() {
             error_print("Failed to open {}.\n", global_arguments.config_file);
+            info_print("", "Is this the root directory of an Onyx project?\n");
             os.exit(1);
         }
 
@@ -483,9 +484,11 @@ run_test_command :: (args: [] cstr) {
 }
 
 #tag Command.{
-    "new", "Create a new project from a template in the current directory.", "[template_name] | --list",
+    "new", "Create a new project from a template in the current directory.", "(template_name | --list) [directory]",
 """
-template_name    Template name to create. Set to 'default' if unspecified.
+template_name    Template name to create.
+
+directory        Directory in which to place the new package. Defaults to '.'.
 
 """,
     require_config_file = false
@@ -503,17 +506,22 @@ run_new_command :: (args: [] cstr) {
         }
     }
 
-    if os.list_directory(".")->count(x => true) > 0 {
-        error_print("Refusing to initialize project in non-empty directory.\n");
-        return;
-    }
-
     template_name := "default";
+    directory     := ".";
 
     if args.count >= 1 {
         template_name = string.as_str(args[0]);
     }
 
+    if args.count >= 2 {
+        directory = string.as_str(args[1]);
+    }
+
+    if os.list_directory(directory)->count(x => true) > 0 {
+        error_print("Refusing to initialize project in non-empty directory.\n");
+        return;
+    }
+
     template_dir := Template_Directory();
     template_file := tprintf("{}{}.json", template_dir, template_name);
     if !os.file_exists(template_file) {
@@ -543,7 +551,27 @@ run_new_command :: (args: [] cstr) {
         vars->put(it.key, line);
     }
 
-    populate_directory(".", template.root["files"], &vars);
+    if !os.dir_exists(directory) {
+        os.dir_create(directory);
+    }
+
+    populate_directory(directory, template.root["files"], &vars);
+
+    for template.root["commands"]->as_array_iter() {
+        command := it->as_str();
+        info_print("Running", "{}\n", command);
+
+        args := string.split(command, ' ', context.temp_allocator);
+        cmd  := args[0];
+        args  = args[1 .. args.count];
+
+        run_proc   := os.process_spawn(cmd, args, starting_directory=directory);
+        run_result := os.process_wait(&run_proc);
+
+        if run_result != .Success {
+            error_print("Failed to run '{}'\n", command);
+        }
+    }
 
     populate_directory :: (dir: str, files: json.Value, vars: &Map(str, str)) {
         for files->as_map_iter() {
@@ -1168,7 +1196,7 @@ info_print :: (verb: str, text: str, va: ..any) {
     buf: [1024] u8;
 
     // HACK
-    for 12 - verb.length do print(" ");
+    for 12 - cast(i32) verb.length do print(" ");
 
     color_print(
         .{ .Green, tprintf("{}  ", verb) },