From e53be01d137c66adf7edd4f259c5815060d3eeb8 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 28 Nov 2023 11:41:12 -0600 Subject: [PATCH] add arg helper --- home/private_dot_config/nim/config.nims | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/home/private_dot_config/nim/config.nims b/home/private_dot_config/nim/config.nims index 57fa949..48e438d 100644 --- a/home/private_dot_config/nim/config.nims +++ b/home/private_dot_config/nim/config.nims @@ -2,6 +2,11 @@ import std/[os, strutils, strformat] switch("hint","[Conf]:off") +proc forward_args(task_name: string): seq[string] = + let args = command_line_params() + let arg_start = args.find(task_name) + 1 + return args[arg_start..^1] + proc gorgeExCd(command: string, dir: string = getCurrentDir()): tuple[output: string, exitCode: int] = gorgeEx("cd $1 && $2" % [dir, command]) @@ -26,20 +31,22 @@ let (_, pkgName) = root.splitPath() srcFile = root / "src" / (pkgName & ".nim") -proc projectGorgeEx(cmd: string): string = - ## cd into the project before running any commands - let (output, code) = gorgeEx(fmt"cd {getCurrentDir()} && {cmd}") - if code != 0: echo "ERROR executing: " & cmd; quit 1 - return output - -task fmt, "Run nimpretty on all git-managed .nim files in the current repo": - ## Usage: nim fmt - let srcFiles = projectGorgeEx(r"nimgrep --filenames -r '^src/.*\.nim$' --noColor").split("\n")[0..^2] +proc formatNimCode(pattern = r"^src/.*\.nim$") = + let srcFiles = gorgeExCd(fmt"nimgrep --filenames -r '{pattern}' --noColor").output.split("\n")[0..^2] for file in srcFiles: let cmd = "nimpretty $1" % [file] echo "Running $1 .." % [cmd] exec(cmd) +task fmt, "Run nimpretty on all git-managed .nim files in the current repo": + ## Usage: nim fmt | nim fmt . + let dirs = forward_args("fmt") + if dirs.len == 0: + formatNimCode() + else: + for dir in dirs: + let pattern = fmt"^{dir}/.*\.nim$" + formatNimCode(pattern) setCommand("nop") task i, "install package":