all repos — min @ d0d644faeeecdd1812927dbd61b3061ab3e81187

A small but practical concatenative programming language.

minpkg/lib/min_sys.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
import
  std/[os,
  osproc,
  strutils,
  logging,
  tables]
import
  ../core/parser,
  ../core/baseutils,
  ../core/value,
  ../core/interpreter,
  ../core/utils,
  ../core/fileutils

import zippy/ziparchives

proc sys_module*(i: In) =
  let def = i.define()

  def.symbol("pwd") do (i: In):
    i.push newVal(getCurrentDir().unix)

  def.symbol("parent-dir") do (i: In):
    i.push newVal(getCurrentDir().parentDir.unix)

  def.symbol("cd") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0].getString
    i.pwd = joinPath(getCurrentDir(), f)
    info("Current directory changed to: ", i.pwd)
    f.setCurrentDir

  def.symbol("ls") do (i: In):
    let vals = i.expect("'sym")
    let a = vals[0]
    var list = newSeq[MinValue](0)
    for i in walkDir(a.getString):
      list.add newVal(i.path.unix)
    i.push list.newVal

  def.symbol("ls-r") do (i: In):
    let vals = i.expect("'sym")
    let a = vals[0]
    var list = newSeq[MinValue](0)
    for i in walkDirRec(a.getString):
      list.add newVal(i.unix)
    i.push list.newVal

  def.symbol("system") do (i: In):
    let vals = i.expect("'sym")
    let a = vals[0]
    i.push execShellCmd(a.getString).newVal

  def.symbol("run") do (i: In):
    let vals = i.expect("'sym")
    let cmd = vals[0]
    let res = execCmdEx(cmd.getString)
    var d = newDict(i.scope)
    i.dset(d, "output", res.output.strip.newVal)
    i.dset(d, "code", res.exitCode.newVal)
    i.push(d)

  def.symbol("env?") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.existsEnv.newVal

  def.symbol("which") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.findExe.newVal

  def.symbol("os") do (i: In):
    i.push hostOS.newVal

  def.symbol("cpu") do (i: In):
    i.push hostCPU.newVal

  def.symbol("rm") do (i: In):
    let vals = i.expect("'sym")
    let v = vals[0]
    let f = v.getString
    if f.fileExists:
      f.removeFile
    else:
      raiseInvalid("File '$1' does not exist." % f)

  def.symbol("cp") do (i: In):
    let vals = i.expect("'sym", "'sym")
    let a = vals[0]
    let b = vals[1]
    let src = b.getString
    var dest = a.getString
    if src.dirExists:
      copyDirWithPermissions src, dest
    elif dest.dirExists:
      if src.dirExists:
        copyDirWithPermissions src, dest
      else:
        copyFileWithPermissions src, dest / src.extractFilename
    else:
      copyFileWithPermissions src, dest

  def.symbol("mv") do (i: In):
    let vals = i.expect("'sym", "'sym")
    let a = vals[0]
    let b = vals[1]
    let src = b.getString
    var dest = a.getString
    if dest.dirExists:
      dest = dest / src.extractFilename
    moveFile src, dest

  def.symbol("rmdir") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0]
    f.getString.removeDir

  def.symbol("mkdir") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0]
    f.getString.createDir

  def.symbol("sleep") do (i: In):
    let vals = i.expect("int")
    let ms = vals[0]
    sleep ms.intVal.int

  def.symbol("chmod") do (i: In):
    let vals = i.expect("int", "str")
    let perms = vals[0]
    let s = vals[1]
    s.getString.setFilePermissions(perms.intVal.toFilePermissions)

  def.symbol("symlink") do (i: In):
    let vals = i.expect("'sym", "'sym")
    let dest = vals[0]
    let src = vals[1]
    src.getString.createSymlink dest.getString

  def.symbol("hardlink") do (i: In):
    let vals = i.expect("'sym", "'sym")
    let dest = vals[0]
    let src = vals[1]
    src.getString.createHardlink dest.getString

  def.symbol("unzip") do (i: In):
    let vals = i.expect("'sym", "'sym")
    var dir = vals[0].getString
    let f = vals[1].getString
    dir = dir.unix
    if not dir.contains("/"):
      dir = "./" & dir
    extractAll(f, dir)

  def.symbol("zip") do (i: In):
    let vals = i.expect("'sym", "quot")
    let file = vals[0]
    let files = vals[1]
    let archive = ZipArchive()
    for rawEntry in files.qVal:
      let entry = rawEntry.getString.relativePath(getCurrentDir())
      if entry.dirExists:
        archive.addDir(entry)
      else:
        archive.contents[entry] = ArchiveEntry(contents: readFile(entry))
    archive.writeZipArchive(file.getString)

  def.symbol("admin?") do (i: In):
    i.push isAdmin().newVal

  def.finalize("sys")