From 4790b6d63f0442774037b00a98e19150248051e7 Mon Sep 17 00:00:00 2001 From: cooldome Date: Mon, 5 Mar 2018 18:06:47 +0000 Subject: Fixes 7283 (#7284) --- lib/system/nimscript.nim | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/system/nimscript.nim') diff --git a/lib/system/nimscript.nim b/lib/system/nimscript.nim index f91dae41e..cb9f0ab01 100644 --- a/lib/system/nimscript.nim +++ b/lib/system/nimscript.nim @@ -114,6 +114,10 @@ proc existsEnv*(key: string): bool {.tags: [ReadIOEffect].} = ## Checks for the existence of an environment variable named `key`. builtin +proc putEnv*(key, val: string) {.tags: [WriteIOEffect].} = + ## Sets the value of the environment variable named key to val. + builtin + proc fileExists*(filename: string): bool {.tags: [ReadIOEffect].} = ## Checks if the file exists. builtin -- cgit 1.4.1-2-gfad0 From f6c8f97fe8bd22e7a2c1b9463f52747e98693682 Mon Sep 17 00:00:00 2001 From: genotrance Date: Tue, 10 Apr 2018 11:50:23 -0500 Subject: Add a few useful os calls to nimscript (#7442) --- changelog.md | 2 ++ compiler/scriptconfig.nim | 6 ++++++ lib/system/nimscript.nim | 28 ++++++++++++++++++++++++++-- tests/newconfig/tfoo.nims | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) (limited to 'lib/system/nimscript.nim') diff --git a/changelog.md b/changelog.md index 91c9b3d68..3bf9b8cb8 100644 --- a/changelog.md +++ b/changelog.md @@ -19,6 +19,8 @@ with ``strutils.split``. - Added ``system.toOpenArray`` in order to support zero-copy slicing operations. This is currently not yet available for the JavaScript target. +- Added ``getCurrentDir``, ``findExe``, ``cpDir`` and ``mvDir`` procs to + ``nimscript``. ### Library changes diff --git a/compiler/scriptconfig.nim b/compiler/scriptconfig.nim index ffb191376..7cf148b76 100644 --- a/compiler/scriptconfig.nim +++ b/compiler/scriptconfig.nim @@ -70,11 +70,17 @@ proc setupVM*(module: PSym; cache: IdentCache; scriptName: string; setResult(a, os.getCurrentDir()) cbos moveFile: os.moveFile(getString(a, 0), getString(a, 1)) + cbos moveDir: + os.moveDir(getString(a, 0), getString(a, 1)) cbos copyFile: os.copyFile(getString(a, 0), getString(a, 1)) + cbos copyDir: + os.copyDir(getString(a, 0), getString(a, 1)) cbos getLastModificationTime: # depends on Time's implementation! setResult(a, int64(getLastModificationTime(getString(a, 0)))) + cbos findExe: + setResult(a, os.findExe(getString(a, 0))) cbos rawExec: setResult(a, osproc.execCmd getString(a, 0)) diff --git a/lib/system/nimscript.nim b/lib/system/nimscript.nim index cb9f0ab01..7671e5962 100644 --- a/lib/system/nimscript.nim +++ b/lib/system/nimscript.nim @@ -38,13 +38,19 @@ proc removeFile(dir: string) {. tags: [ReadIOEffect, WriteIOEffect], raises: [OSError].} = builtin proc moveFile(src, dest: string) {. tags: [ReadIOEffect, WriteIOEffect], raises: [OSError].} = builtin +proc moveDir(src, dest: string) {. + tags: [ReadIOEffect, WriteIOEffect], raises: [OSError].} = builtin proc copyFile(src, dest: string) {. tags: [ReadIOEffect, WriteIOEffect], raises: [OSError].} = builtin +proc copyDir(src, dest: string) {. + tags: [ReadIOEffect, WriteIOEffect], raises: [OSError].} = builtin proc createDir(dir: string) {.tags: [WriteIOEffect], raises: [OSError].} = builtin proc getOsError: string = builtin proc setCurrentDir(dir: string) = builtin -proc getCurrentDir(): string = builtin +proc getCurrentDir*(): string = + ## Retrieves the current working directory. + builtin proc rawExec(cmd: string): int {.tags: [ExecIOEffect], raises: [OSError].} = builtin @@ -117,7 +123,7 @@ proc existsEnv*(key: string): bool {.tags: [ReadIOEffect].} = proc putEnv*(key, val: string) {.tags: [WriteIOEffect].} = ## Sets the value of the environment variable named key to val. builtin - + proc fileExists*(filename: string): bool {.tags: [ReadIOEffect].} = ## Checks if the file exists. builtin @@ -206,12 +212,24 @@ proc mvFile*(`from`, to: string) {.raises: [OSError].} = moveFile `from`, to checkOsError() +proc mvDir*(`from`, to: string) {.raises: [OSError].} = + ## Moves the dir `from` to `to`. + log "mvDir: " & `from` & ", " & to: + moveDir `from`, to + checkOsError() + proc cpFile*(`from`, to: string) {.raises: [OSError].} = ## Copies the file `from` to `to`. log "cpFile: " & `from` & ", " & to: copyFile `from`, to checkOsError() +proc cpDir*(`from`, to: string) {.raises: [OSError].} = + ## Copies the dir `from` to `to`. + log "cpDir: " & `from` & ", " & to: + copyDir `from`, to + checkOsError() + proc exec*(command: string) = ## Executes an external process. log "exec: " & command: @@ -265,6 +283,12 @@ proc cd*(dir: string) {.raises: [OSError].} = setCurrentDir(dir) checkOsError() +proc findExe*(bin: string): string = + ## Searches for bin in the current working directory and then in directories + ## listed in the PATH environment variable. Returns "" if the exe cannot be + ## found. + builtin + template withDir*(dir: string; body: untyped): untyped = ## Changes the current directory temporarily. ## diff --git a/tests/newconfig/tfoo.nims b/tests/newconfig/tfoo.nims index b3448f0df..3be42c38a 100644 --- a/tests/newconfig/tfoo.nims +++ b/tests/newconfig/tfoo.nims @@ -30,4 +30,51 @@ putEnv("dummy", "myval") doAssert(existsEnv("dummy") == true) doAssert(getEnv("dummy") == "myval") +# issue #7393 +let wd = getCurrentDir() +cd("..") +assert wd != getCurrentDir() +cd(wd) +assert wd == getCurrentDir() +assert findExe("nim") != "" + +# general tests +mode = ScriptMode.Verbose + +assert getCommand() == "c" +setCommand("cpp") +assert getCommand() == "cpp" +setCommand("c") + +assert cmpic("HeLLO", "hello") == 0 + +assert fileExists("tests/newconfig/tfoo.nims") == true +assert dirExists("tests") == true + +assert existsFile("tests/newconfig/tfoo.nims") == true +assert existsDir("tests") == true + +discard selfExe() + +when defined(windows): + assert toExe("nim") == "nim.exe" + assert toDll("nim") == "nim.dll" +else: + assert toExe("nim") == "nim" + assert toDll("nim") == "libnim.so" + +rmDir("tempXYZ") +assert dirExists("tempXYZ") == false +mkDir("tempXYZ") +assert dirExists("tempXYZ") == true +assert fileExists("tempXYZ/koch.nim") == false +cpFile("koch.nim", "tempXYZ/koch.nim") +assert fileExists("tempXYZ/koch.nim") == true +cpDir("nimsuggest", "tempXYZ/.") +assert dirExists("tempXYZ/tests") == true +assert fileExists("tempXYZ/nimsuggest.nim") == true +rmFile("tempXYZ/koch.nim") +assert fileExists("tempXYZ/koch.nim") == false +rmDir("tempXYZ") +assert dirExists("tempXYZ") == false -- cgit 1.4.1-2-gfad0