• Operand
  • someone had 2.

gram: build

> ./config/nushell/gram.nu

Lenses
(coming soon!)


source ./grammar.nu
source ./nix.nu
source ./machine.nu

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

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

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

let grams = {
  build: { base: ~/.build, name: build, machine: $m.baseboard, shares: [ share ] }
  ledge: { base: ~/ledge, name: ledge, machine: $m.baseboard }
  course: { base: ~/course, name: course, machine: $m.baseboard }

  # publish!
  gear: { base: ~/gear, name: gear, machine: $m.baseboard }
  # group: { base: ~/group, name: group, machine: null }
  # page:  { base: ~/disc/nvme0n1p6/gram/page, name: page, machine: null }

  page:  { base: ~/page, name: page, machine: $m.baseboard, command: {
    # preshare: 'nd nu diagram.nu'
  }, shares: [share] }

  scope: { base: ~/disc/nvme0n1p6/gram/scope, name: scope, machine: $m.baseboard }
  gloss: { base: ~/disc/nvme0n1p6/gram/gloss, name: gloss, machine: $m.baseboard }
  nuer: { base: ~/disc/nvme0n1p6/gram/nuer, name: nuer, machine: $m.baseboard }
  min: { base: ~/disc/nvme0n1p6/gram/min, name: qu, machine: $m.baseboard }
  qu: { base: ~/disc/nvme0n1p6/gram/qu, name: qu, machine: $m.baseboard }
  aurora: { base: ~/disc/nvme0n1p6/gram/aurora, name: aurora, machine: $m.baseboard }

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

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

def "gram share" [...gs: record] {
  $gs | each {|gram|
    let base = $gram.base | path expand
    let share = [ /home/calliope 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 $gram.machine $prepare
    # ssh (machine address $gram.machine) $prepare
    # ssh (machine address $gram.machine) $"ls ($share)" | print

    let aim = [ (machine address $gram.machine) $share ] | str join ':'
    # print $aim; cd $base
    if not ('share' in (code -C $base remote | lines)) {
      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 $gram.machine $"nix-shell -p git --command 'git -C ($share) reset --hard main';"
    shell $gram.machine $"nix-shell -p git --command 'git -C ($share) checkout main';"

    if ($gram | get -i command.basis | is-not-empty) {
      cd $base; nu run -s [gram] $gram.command.basis
    } else { print "no command.basis; passing." }

    if ($gram | get -i command.preshare | is-not-empty) {
      cd $base; nu run -s [gram] $gram.command.preshare | print
    } else { print "no command.preshare; passing." }

    if not ($gram | get -i shares | is-empty) {
      shell $gram.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 | print
    }
} }

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 = "~/disc/nvme0n1p6/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
    echo "running cycle"
    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 "gram clean" [gram: record] {
# let running = shell $gram.machine $"(~/($gram.name)/run | path expand | path dirname | path basename)"
# time rm -r ...(ls $gram.name | get name | path basename | filter {
#   $in != run and $in != $running } | each { ([ $gram.name $in ] | path join | path expand) })
# }

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