dotfiles/home/private_dot_config/nim/config.nims

122 lines
3.2 KiB
Text
Raw Normal View History

2024-11-14 16:03:43 -06:00
# when defined(nimsuggest):
# import system/nimscript
2024-11-14 14:37:18 -06:00
2024-10-20 12:44:36 -05:00
import std/[
os, strutils, strformat
]
2023-02-13 12:42:14 -06:00
switch("hint","[Conf]:off")
2024-11-18 11:02:01 -06:00
proc forwardArgs(task_name: string): seq[string] =
2023-11-28 11:41:12 -06:00
let args = command_line_params()
let arg_start = args.find(task_name) + 1
return args[arg_start..^1]
2023-09-08 10:34:25 -05:00
proc gorgeExCd(command: string, dir: string = getCurrentDir()): tuple[output: string, exitCode: int] =
gorgeEx("cd $1 && $2" % [dir, command])
2023-02-13 12:42:14 -06:00
proc getGitRootMaybe(): string =
## Try to get the path to the current git root directory.
## Return ``projectDir()`` if a ``.git`` directory is not found.
const
maxAttempts = 10 # arbitrarily picked
var
path = projectDir() # projectDir() needs nim 0.20.0 (or nim devel as of Tue Oct 16 08:41:09 EDT 2018)
attempt = 0
while (attempt < maxAttempts) and (not dirExists(path / ".git")):
path = path / "../"
attempt += 1
if dirExists(path / ".git"):
result = path
else:
result = projectDir()
2024-10-20 12:44:36 -05:00
const
2024-09-16 11:15:24 -05:00
formatter = "nph"
2023-02-13 12:42:14 -06:00
2024-09-16 11:15:24 -05:00
proc formatNimCode(pattern = r"^[src|tests].*\.nim(s)?$") =
2023-11-28 11:41:12 -06:00
let srcFiles = gorgeExCd(fmt"nimgrep --filenames -r '{pattern}' --noColor").output.split("\n")[0..^2]
2023-11-25 01:20:45 -06:00
for file in srcFiles:
2024-01-16 13:56:36 -06:00
# let cmd = "nph $1" % [file]
2024-09-16 11:15:24 -05:00
let cmd = "$1 $2" % [formatter, file]
2023-11-25 01:20:45 -06:00
echo "Running $1 .." % [cmd]
exec(cmd)
2024-10-16 18:47:53 -05:00
task fmt, fmt"Run {formatter} on all git-managed .nim files in the current repo":
2023-11-28 11:41:12 -06:00
## 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)
2023-02-13 12:42:14 -06:00
setCommand("nop")
2023-09-08 10:34:25 -05:00
task i, "install package":
exec "nimble install"
2023-11-25 01:20:45 -06:00
setCommand("nop")
2023-02-13 12:42:14 -06:00
2023-09-08 10:34:25 -05:00
2024-10-20 12:44:36 -05:00
task lexidInc, "bump lexicographic id":
2023-09-08 10:34:25 -05:00
let (vsn, code) = gorgeExCd("git describe --tags --always --dirty=-dev")
2024-01-16 13:56:36 -06:00
if code != 0:
2023-09-08 10:34:25 -05:00
echo "is this a git repo?"
echo &"output: {vsn}"
quit 1
2023-02-13 12:42:14 -06:00
let
2023-09-08 10:34:25 -05:00
parts = vsn.split(".")
year = parts[0].replace("v","")
build = parts[1]
2023-02-13 12:42:14 -06:00
2023-09-08 10:34:25 -05:00
if "-dev" in build:
echo "warning! uncommitted work, stash or commit"
quit 1
2023-02-13 12:42:14 -06:00
2023-09-08 10:34:25 -05:00
var next = $(parseInt(build) + 1)
if build[0] < next[0]:
next = $(parseInt($next[0])*11) & next[1..^1]
2023-02-13 12:42:14 -06:00
2023-09-08 10:34:25 -05:00
let newVersion = &"{year}.{next}"
when defined(commit):
exec &"sed -i 's/version = .*/version = \"{newVersion}\"/' {pkgName}.nimble"
exec &"git add {pkgName}.nimble"
exec &"git commit -m 'chore: bump {year}.{build} -> {newVersion}'"
exec &"git tag v{newVersion}"
else:
echo "next version is: ", newVersion,"\n"
2023-02-13 12:42:14 -06:00
2024-11-11 17:17:43 -06:00
task h, "":
exec "nim help"
const name = projectDir().lastPathPart
task b, fmt"build binary, default: {name}":
switch("outdir", "bin")
if projectName() == "":
let name = projectDir().lastPathPart
setCommand "c", "src/" & name & ".nim"
else:
setCommand "c",""
2024-11-14 14:37:18 -06:00
task updateLock, "workaround for nimble lock probs":
2024-11-18 11:02:01 -06:00
let params = forwardArgs("updateLock")
let nimbleFile =
if params.len == 1: params[0]
else: projectDir().lastPathPart & ".nimble"
2024-11-14 14:37:18 -06:00
if not fileExists nimbleFile:
quit "expected to find: " & nimbleFile
2024-11-18 16:26:23 -06:00
rmDir projectDir() / "nimbledeps"
rmFile projectDir() / "nimble.lock"
rmFile projectDir() / "nimble.paths"
2024-11-14 14:37:18 -06:00
exec "nimble lock -l"
exec "nimble setup -l"
2024-11-11 17:17:43 -06:00
2024-11-14 14:37:18 -06:00
# line delemiter for `nim help`
2024-09-16 11:15:24 -05:00
task _,"_______________":
discard
2024-10-20 12:44:36 -05:00