mirror of
https://github.com/RGBCube/ncc
synced 2025-07-29 11:07:44 +00:00
nushell: clean up dump module, add dump list
This commit is contained in:
parent
f8c824dd0a
commit
85019a5bd7
1 changed files with 107 additions and 50 deletions
|
@ -28,13 +28,68 @@ def --env mcg [path: path]: nothing -> nothing {
|
||||||
jj git init --colocate
|
jj git init --colocate
|
||||||
}
|
}
|
||||||
|
|
||||||
let site_path = glob "~" | path join "projects" "site"
|
module dump {
|
||||||
|
def site-path []: nothing -> path {
|
||||||
|
$env.HOME | path join "Projects" "site"
|
||||||
|
}
|
||||||
|
|
||||||
|
def dump-path []: nothing -> path {
|
||||||
|
site-path | path join "site" "dump"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Convert a thought dump namespace to the filesystem path.
|
||||||
|
export def to-path []: string -> path {
|
||||||
|
let namespace = $in
|
||||||
|
|
||||||
|
dump-path
|
||||||
|
| path join ...($namespace | split row ".")
|
||||||
|
| $in + ".md"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Convert a filesystem path to a thought dump namespace.
|
||||||
|
export def to-dump []: path -> string {
|
||||||
|
let path = $in
|
||||||
|
|
||||||
|
print $path (dump-path)
|
||||||
|
|
||||||
|
$path
|
||||||
|
| path relative-to (dump-path)
|
||||||
|
| path split
|
||||||
|
| str join "."
|
||||||
|
| str substring 0..<-3
|
||||||
|
}
|
||||||
|
|
||||||
|
# List all thought dumps that start with the given namespace.
|
||||||
|
export def list [
|
||||||
|
namespace: string = ""
|
||||||
|
]: nothing -> table<namespace: string, path: path> {
|
||||||
|
let dump_prefix = dump-path | path join ...($namespace | split row ".")
|
||||||
|
|
||||||
|
let dump_parent_contents = glob ($dump_prefix | path parse | get parent | path join "**" "*.md")
|
||||||
|
let dump_matches = $dump_parent_contents | filter { str starts-with $dump_prefix }
|
||||||
|
|
||||||
|
ls ...$dump_matches | each {
|
||||||
|
merge { path: $in.name }
|
||||||
|
| select path size modified
|
||||||
|
| merge { namespace: ($in.path | to-dump) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Deploy the thought dumps and thus the website.
|
||||||
|
export def deploy []: nothing -> nothing {
|
||||||
|
print $"(ansi green)deploying...(ansi reset)"
|
||||||
|
|
||||||
|
cd (site-path)
|
||||||
|
./apply.nu
|
||||||
|
}
|
||||||
|
|
||||||
# Edit a thought dump.
|
# Edit a thought dump.
|
||||||
def "dump ed" [
|
export def edit [
|
||||||
namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension.
|
namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension.
|
||||||
]: nothing -> nothing {
|
]: nothing -> nothing {
|
||||||
let dump_path = $site_path | path join "site" "dump" ...($namespace | split row ".") | $in + ".md"
|
let dump_path = $namespace | to-path
|
||||||
|
|
||||||
|
let old_dump_size = try { ls $dump_path }
|
||||||
|
|
||||||
mkdir ($dump_path | path parse | get parent)
|
mkdir ($dump_path | path parse | get parent)
|
||||||
touch $dump_path
|
touch $dump_path
|
||||||
|
@ -46,38 +101,37 @@ def "dump ed" [
|
||||||
let dump_size = ls $dump_path | get 0.size
|
let dump_size = ls $dump_path | get 0.size
|
||||||
if $dump_size == 0b {
|
if $dump_size == 0b {
|
||||||
print $"(ansi red)thought dump was emptied(ansi reset)"
|
print $"(ansi red)thought dump was emptied(ansi reset)"
|
||||||
dump rm $namespace
|
delete $namespace --existed-before ($old_dump_size != null)
|
||||||
} else if $old_dump_hash == (open $dump_path | hash sha256) {
|
} else if $old_dump_hash == (open $dump_path | hash sha256) {
|
||||||
print $"(ansi yellow)thought dump was not changed(ansi reset)"
|
print $"(ansi yellow)thought dump was not modifier, doing nothing(ansi reset)"
|
||||||
} else {
|
} else {
|
||||||
print $"(ansi magenta)thought dump was edited(ansi reset)"
|
print $"(ansi magenta)thought dump was edited(ansi reset)"
|
||||||
print $"(ansi green)deploying...(ansi reset)"
|
|
||||||
|
|
||||||
cd $site_path
|
let jj_arguments = [ "--repository", (site-path) ]
|
||||||
|
|
||||||
jj commit --message $"dump\(($namespace)\): update"
|
jj ...$jj_arguments commit --message $"dump\(($namespace)\): update"
|
||||||
jj bookmark set master --revision @-
|
jj ...$jj_arguments bookmark set master --revision @-
|
||||||
|
|
||||||
[
|
[
|
||||||
{ jj git push --remote origin }
|
{ jj ...$jj_arguments git push --remote origin }
|
||||||
{ jj git push --remote rad }
|
{ jj ...$jj_arguments git push --remote rad }
|
||||||
{ ./apply.nu }
|
{ deploy }
|
||||||
] | par-each { do $in }
|
] | par-each { do $in }
|
||||||
|
|
||||||
cd -
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete a thought dump.
|
# Delete a thought dump.
|
||||||
def "dump rm" [
|
export def delete [
|
||||||
namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension.
|
namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension.
|
||||||
|
--existed-before = true
|
||||||
]: nothing -> nothing {
|
]: nothing -> nothing {
|
||||||
print $namespace
|
let dump_path = $namespace | to-path
|
||||||
let dump_path = $site_path | path join "site" "dump" ...($namespace | split row ".") | $in + ".md"
|
|
||||||
let parent_path = $dump_path | path parse | get parent
|
let parent_path = $dump_path | path parse | get parent
|
||||||
|
|
||||||
print $"(ansi red)deleting thought dump...(ansi reset)"
|
print $"(ansi red)deleting thought dump...(ansi reset)"
|
||||||
rm $dump_path
|
print --no-newline (ansi red)
|
||||||
|
rm --verbose $dump_path
|
||||||
|
print --no-newline (ansi reset)
|
||||||
|
|
||||||
if (ls $parent_path | length) == 0 {
|
if (ls $parent_path | length) == 0 {
|
||||||
print $"(ansi red)parent folder is empty, deleting that too...(ansi reset)"
|
print $"(ansi red)parent folder is empty, deleting that too...(ansi reset)"
|
||||||
|
@ -85,9 +139,12 @@ def "dump rm" [
|
||||||
rm $parent_path
|
rm $parent_path
|
||||||
}
|
}
|
||||||
|
|
||||||
print $"(ansi green)deploying...(ansi reset)"
|
if $existed_before {
|
||||||
|
deploy
|
||||||
cd $site_path
|
} else {
|
||||||
./apply.nu
|
print $"(ansi green)the thought dump didn't exist before, so skipping deployment(ansi reset)"
|
||||||
cd -
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use dump
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue