nix-configs/build-utils/templ-edit.nu

47 lines
924 B
Text
Executable file

#!/usr/bin/env nu
def main [] {}
def "main find" [
file: string
] {
open $file --raw
| lines
| enumerate
| update item {|it| $it.item | parse '{current}#:{name}:-{template}-:#' }
| filter {|it| $it.item | is-not-empty}
| flatten -a
| rename -c { index: line }
}
# TODO: support mroe then single template
def "main edit" [
file: path
name: string
to: string
--preview
] {
let template = main find $file
| where name == $name
| first;
let new = $template.template
| str replace $"%($name)%" $to
| append $" #:($template.name):-($template.template)-:#"
| str join;
let rest = open $file --raw
| lines
| enumerate
| where index != $template.line
| append { index: $template.line, item: $new}
| sort-by index
| reject index
| get item
| append ""
| str join "\n";
if $preview {
print $rest
} else {
$rest | save -f $file
}
}