• Operand
  • hole: deep pi.

gram:build

> ./config/nushell/gram.nu

Lenses
(coming soon!)


def "grammar norm" [base: path] { {
  base: ($base | path expand),
  name: ($base | path basename),
  machine: $m.base,
} }

def "gram clone" [ name: string ] {
  wget -O - $"https://share.operand.online/gram/($name)/?download=tar_gz" | tar -xzv;
  cd $name }

def "grammar gleam" [base: path] { {
  base: ($base | path expand),
  name: ($base | path basename),
  machine: null, #$m.base,
  shares: [ doc README.md ],
  command: { preshare: "nix develop --command gleam docs build" },
} }

def "grammar mix" [base: path] { {
  base: ($base | path expand),
  name: ($base | path basename),
  machine: $m.base,
  shares: [ doc share ],
  command: {
    # basis: "nix develop --command mix deps.get",
    preshare: "nix develop --command mix docs",
  }
} }

let grams = {
  build: { base: ~/.build, name: build, machine: null, shares: [ share ] }
  share: { base: null, name: share, domain: "share.operand.online", machine: $m.base }
  page:  { base: ~/base/code/page, name: page, machine: null }
  scope: { base: ~/base/code/scope, name: scope, machine: null }
  gloss: { base: ~/base/code/gloss, name: gloss, machine: null }
  nuer: { base: ~/base/code/nuer, name: nuer, machine: null }
  min: { base: ~/base/code/min, name: qu, machine: null }
  qu: { base: ~/base/code/qu, name: qu, machine: null }

  blade:   ( grammar mix ~/base/code/blade | upsert machine null),
  cc:      ( grammar mix ~/base/code/cc),
  op:      ( grammar mix ~/base/code/operand | upsert name "op"),
  session: ( grammar mix ~/base/mirror/asciinema-server | upsert name "session"),
  pain:    ( grammar mix ~/base/code/pain),
  gram:    ( grammar gleam ~/base/code/gram),
  lens:    ( grammar gleam ~/base/code/lens),
  panel:   ( grammar gleam ~/base/code/panel),
}; let g = $grams

def "gram rebase" [gram: record] {
  shell $machines.base $"close-($gram.name); do-($gram.name) daemon;"
}

def "gram help" [gram: record] {
  let source = [ $grams.share.domain gram ($gram.base | path basename) ] | path join
  print ([
    "# Reload your copy quickly using:"
    "rm -r ./($gram.name);"
    "wget --accept-regex '($source | str replace '/' '\/')' -r https://($grams.share.domain)/($source)"
    "git -C ./($grams.share.domain)/($source) checkout main"
    "cd ./($grams.share.domain)/($source)"
  ] | str join "\n") }

def "gram share" [...gs: record] {
  $gs | each {|gram|
    let base = $gram.base | path expand
    let share = [ /root share gram $gram.name ] | path join

    let prepare = (['('
      $"mkdir '($share)';"
      $"cd '($share)';"
      $"nix-shell -p git --command 'git -C ($share) init';"
      $"nix-shell -p git --command 'git config --global --add safe.directory ($share)';"
      $"nix-shell -p git --command 'git -C ($share) branch -m main';"
      $"nix-shell -p git --command 'git -C ($share) config --add receive.denyCurrentBranch warn';"
    ')'] | str join "\n")
    print "Preparing codebase"; print $prepare
    shell $grams.share.machine $prepare
    # ssh (machine address $grams.share.machine) $prepare
    # ssh (machine address $grams.share.machine) $"ls ($share)" | print

    let aim = [ (machine address $grams.share.machine) $share ] | str join ':'
    # print $aim; cd $base
    if (code -C $base remote | lines | filter {|a| $a == 'share'} | is-empty) {
      code -C $base remote add share $aim } else {
      code -C $base remote set-url share $aim }
    print (code -C $base remote -v)
    code -C $base push share
    print "pushed."

    shell $grams.share.machine $"nix-shell -p git --command 'git -C ($share) reset --hard main';"
    shell $grams.share.machine $"nix-shell -p git --command 'git -C ($share) checkout main';"
    gram help $gram

    try { cd $base; nu -c $gram.command.basis } catch { echo "no command.basis; passing." }
    try { cd $base; nu -c $gram.command.preshare } catch { echo "no command.preshare; passing." }

    if not ($gram | get -i shares | is-empty) {
      shell $grams.share.machine $"mkdir '(dirname $share)'"
      print $"time rsync -av --delete ...($gram.shares | | each {|s| ([$base $s] | path join | path expand) }) ($aim)"
      time rsync -av --delete ...($gram.shares | | each {|s| ([$base $s] | path join | path expand) }) $aim
    }
} }

def "gram ply" [...gs: record] {
# deploy gram images on machine, compiled and managed by colmena
  $gs | group-by machine.name | transpose | get column1 | each {|machines|
    $machines | each {|mach| cd ~/.build/nixos
      nsh colmena colmena apply --on $mach.machine.name --build-on-target --impure switch } }

  $gs | each { |gram|
    gram share $gram
    let base = try { $gram.base | path basename } catch {  }
    let logs = "~/log" | path expand; mkdir $logs;
    let log = [ $logs ([ (clock | str trim) $base deploy log ] | str join '.') ] | path join
    try { shell $gram.machine $"run-($gram.name)" | tee { save $log } | print }
  }
}

def "gram cycle" [...gs: record] {
  $gs | par-each { |gram|
    let base = try { $gram.base | path basename } catch {  }
    let logs = "~/qu/log" | path expand; mkdir $logs;
    let log = [ $logs ([ (clock | str trim) $base cycle log ] | str join '.') ] | path join
    try { shell $gram.machine $"cycle-($gram.name)" | tee { save $log } | print }
    gram share $gram
  }
}

def "gram clean" [...gs: record] {
  $gs | par-each {|gram|
    shell $gram.machine ("time rm -r ...(
      ls ($gram.name)/ | get name | path basename | filter {
       $in != run and $in != (
        '~/($gram.name)/run' | path expand | path dirname | path basename
      ) } | each { ([ ($gram.name) $in ] | path join | path expand) } | tee { print }
    )" | str replace -a '($gram.name)' $gram.name)
} }

def g [...name: any] { $g | get ($name | into cell-path) }