all repos — min @ 9e1e4ed24522a4c406d8da61b4e1b5e62b2ad6c6

A small but practical concatenative programming language.

Improved sys module.
h3rald h3rald@h3rald.com
Sat, 17 Sep 2016 16:40:53 +0200
commit

9e1e4ed24522a4c406d8da61b4e1b5e62b2ad6c6

parent

9a0150f451aa6839fe04745d3af4992c95c6e570

3 files changed, 85 insertions(+), 32 deletions(-)

jump to
M lib/min_sys.nimlib/min_sys.nim

@@ -19,34 +19,34 @@ i.push newVal(getCurrentDir().parentDir)

.symbol("cd") do (i: In): var f: MinValue - i.reqString f - f.strVal.setCurrentDir + i.reqStringLike f + f.getString.setCurrentDir .symbol("ls") do (i: In): var a: MinValue - i.reqString a + i.reqStringLike a var list = newSeq[MinValue](0) - for i in walkDir(a.strVal): + for i in walkDir(a.getString): list.add newVal(i.path) i.push list.newVal .symbol("ls-r") do (i: In): var a: MinValue - i.reqString a + i.reqStringLike a var list = newSeq[MinValue](0) - for i in walkDirRec(a.strVal): + for i in walkDirRec(a.getString): list.add newVal(i) i.push list.newVal .symbol("system") do (i: In): var a: MinValue - i.reqString a - i.push execShellCmd(a.strVal).newVal + i.reqStringLike a + i.push execShellCmd(a.getString).newVal .symbol("run") do (i: In): var a: MinValue - i.reqString a - let words = a.strVal.split(" ") + i.reqStringLike a + let words = a.getString.split(" ") let cmd = words[0] var args = newSeq[string](0) if words.len > 1:

@@ -55,13 +55,13 @@ i.push execProcess(cmd, args, nil, {poUsePath}).newVal

.symbol("getenv") do (i: In): var a: MinValue - i.reqString a - i.push a.strVal.getEnv.newVal + i.reqStringLike a + i.push a.getString.getEnv.newVal .symbol("putenv") do (i: In): var key, value: MinValue - i.reqTwoStrings key, value - key.strVal.putEnv value.strVal + i.reqTwoStringLike key, value + key.getString.putEnv value.getString .symbol("env?") do (i: In): var s: MinValue

@@ -81,38 +81,59 @@ i.push hostCPU.newVal

.symbol("file?") do (i: In): var f: MinValue - i.reqString f - i.push f.strVal.fileExists.newVal + i.reqStringLike f + i.push f.getString.fileExists.newVal .symbol("dir?") do (i: In): var f: MinValue - i.reqString f - i.push f.strVal.dirExists.newVal + i.reqStringLike f + i.push f.getString.dirExists.newVal .symbol("rm") do (i: In): - var f: MinValue - i.reqString f - f.strVal.removeFile + 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) .symbol("cp") do (i: In): var a, b: MinValue - i.reqTwoStrings a, b - copyFile b.strVal, a.strVal + 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 + .symbol("mv") do (i: In): var a, b: MinValue - i.reqTwoStrings a, b - moveFile b.strVal, a.strVal + i.reqTwoStringLike a, b + let src = b.getString + var dest = a.getString + if dest.dirExists: + dest = dest / src.extractFilename + moveFile src, dest .symbol("rmdir") do (i: In): var f: MinValue - i.reqString f - f.strVal.removeDir + i.reqStringLike f + f.getString.removeDir .symbol("mkdir") do (i: In): var f: MinValue - i.reqString f - f.strVal.createDir + i.reqStringLike f + f.getString.createDir .symbol("sleep") do (i: In): var ms: MinValue

@@ -131,12 +152,23 @@ i.push s.getString.symlinkExists.newVal

.symbol("symlink") do (i: In): var src, dest: MinValue - i.reqTwoStrings dest, src + i.reqTwoStringLike dest, src src.getString.createSymlink dest.getString .symbol("hardlink") do (i: In): var src, dest: MinValue - i.reqTwoStrings dest, src + i.reqTwoStringLike dest, src src.getString.createHardlink dest.getString + + .symbol("filename") do (i: In): + var f: MinValue + i.reqStringLike f + i.push f.getString.extractFilename.newVal + + .symbol("dirname") do (i: In): + var f: MinValue + i.reqStringLike f + i.push f.getString.parentDir.newVal + .finalize()
M tests/crypto.mintests/crypto.min

@@ -21,4 +21,3 @@ ("test" "test" aes "test" aes strip "test" ==) assert

report clear-stack - "test.txt" rm
M tests/sys.mintests/sys.min

@@ -3,6 +3,28 @@ 'test import

"sys" describe + ("dir1" mkdir "dir1" dir?) assert + + ("dir1" "dir2" mv "dir2" dir?) assert + + ("dir1" dir? false ==) assert + + ("dir2" "dir1" cp "dir1" dir?) assert + + ("..." "dir1/test.txt" fwrite "dir1/test.txt" file?) assert + + ("dir1/test.txt" "dir2" mv "dir2/test.txt" file?) assert + + ("dir1/test.txt" file? false ==) assert + + ("dir2/test.txt" "dir1" cp "dir1/test.txt" file?) assert + + ('dir1 ls 'filename map ("test.txt") ==) assert + + ('dir2 ls 'dirname map ("dir2") ==) assert + + ('dir1 rmdir 'dir2 rmdir 'dir1 dir? 'dir2 dir? or false ==) assert + ("systest" mkdir . ls . "/systest" concat contains) assert ("systest" cd . "systest" match) assert