From 12ea17e3919bed3ea6164bfb9cef0cd118893a58 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 14 May 2023 00:01:58 -0500 Subject: [PATCH] changed: added optional parameter to `onyx pkg new` --- scripts/default.json | 7 +++++-- scripts/onyx-pkg.onyx | 46 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/scripts/default.json b/scripts/default.json index 85e0c551..e0352031 100644 --- a/scripts/default.json +++ b/scripts/default.json @@ -18,11 +18,14 @@ } }, "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" + ] } diff --git a/scripts/onyx-pkg.onyx b/scripts/onyx-pkg.onyx index 6c33d27d..ddedbc00 100644 --- a/scripts/onyx-pkg.onyx +++ b/scripts/onyx-pkg.onyx @@ -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) }, -- 2.25.1