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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
import tables, os, osproc, strutils, sequtils import ../core/parser, ../core/value, ../core/interpreter, ../core/utils, ../core/fileutils when not defined(lite): import ../packages/nim-miniz/miniz proc unix(s: string): string = return s.replace("\\", "/") proc sys_module*(i: In)= let def = i.define() def.symbol(".") do (i: In): i.push newVal(getCurrentDir().unix) def.symbol("..") do (i: In): i.push newVal(getCurrentDir().parentDir.unix) def.symbol("cd") do (i: In): var f: MinValue i.reqStringLike f f.getString.setCurrentDir def.symbol("ls") do (i: In): var a: MinValue i.reqStringLike a var list = newSeq[MinValue](0) for i in walkDir(a.getString): list.add newVal(i.path.unix) i.push list.newVal(i.scope) def.symbol("ls-r") do (i: In): var a: MinValue i.reqStringLike a var list = newSeq[MinValue](0) for i in walkDirRec(a.getString): list.add newVal(i.unix) i.push list.newVal(i.scope) def.symbol("system") do (i: In): var a: MinValue i.reqStringLike a i.push execShellCmd(a.getString).newVal def.symbol("run") do (i: In): var cmd: MinValue i.reqStringLike cmd let res = execCmdEx(cmd.getString) i.push @[@["output".newSym, res.output.newVal].newVal(i.scope), @["code".newSym, res.exitCode.newVal].newVal(i.scope)].newVal(i.scope) def.symbol("get-env") do (i: In): var a: MinValue i.reqStringLike a i.push a.getString.getEnv.newVal def.symbol("put-env") do (i: In): var key, value: MinValue i.reqTwoStringLike key, value key.getString.putEnv value.getString def.symbol("env?") do (i: In): var s: MinValue i.reqStringLike s i.push s.getString.existsEnv.newVal def.symbol("which") do (i: In): var s: MinValue i.reqStringLike s 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("exists?") do (i: In): var f: MinValue i.reqStringLike f i.push newVal(f.getString.fileExists or f.getString.dirExists) def.symbol("file?") do (i: In): var f: MinValue i.reqStringLike f i.push f.getString.fileExists.newVal def.symbol("dir?") do (i: In): var f: MinValue i.reqStringLike f i.push f.getString.dirExists.newVal def.symbol("rm") do (i: In): var v: MinValue i.reqStringLike v let f = v.getString if f.existsFile: f.removeFile elif f.existsDir: f.removeDir else: raiseInvalid("File '$1' does not exist." % f) def.symbol("cp") do (i: In): var a, b: MinValue i.reqTwoStringLike a, b 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): var a, b: MinValue i.reqTwoStringLike a, b let src = b.getString var dest = a.getString if dest.dirExists: dest = dest / src.extractFilename moveFile src, dest def.symbol("rmdir") do (i: In): var f: MinValue i.reqStringLike f f.getString.removeDir def.symbol("mkdir") do (i: In): var f: MinValue i.reqStringLike f f.getString.createDir def.symbol("sleep") do (i: In): var ms: MinValue i.reqInt ms sleep ms.intVal.int def.symbol("chmod") do (i: In): var s, perms: MinValue i.reqIntAndString perms, s s.getString.setFilePermissions(perms.intVal.toFilePermissions) def.symbol("symlink?") do (i: In): var s: MinValue i.reqStringLike s i.push s.getString.symlinkExists.newVal def.symbol("symlink") do (i: In): var src, dest: MinValue i.reqTwoStringLike dest, src src.getString.createSymlink dest.getString def.symbol("hardlink") do (i: In): var src, dest: MinValue i.reqTwoStringLike dest, src src.getString.createHardlink dest.getString def.symbol("filename") do (i: In): var f: MinValue i.reqStringLike f i.push f.getString.extractFilename.unix.newVal def.symbol("dirname") do (i: In): var f: MinValue i.reqStringLike f i.push f.getString.parentDir.unix.newVal def.symbol("$") do (i: In): i.push("get-env".newSym) def.symbol("!") do (i: In): i.push("system".newSym) def.symbol("&") do (i: In): i.push("run".newSym) def.sigil("$") do (i: In): i.push("get-env".newSym) def.sigil("!") do (i: In): i.push("system".newSym) def.sigil("&") do (i: In): i.push("run".newSym) when not defined(lite): def.symbol("unzip") do (i: In): var f, dir: MinValue i.reqTwoStringLike dir, f miniz.unzip(f.getString, dir.getString) def.symbol("zip") do (i: In): var files, file: MinValue i.reqStringLikeAndQuotation file, files miniz.zip(files.qVal.mapIt(it.getString), file.getString) def.finalize("sys") |