├── .gitignore ├── README.md ├── nimr.nimble └── src └── nimr.nim /.gitignore: -------------------------------------------------------------------------------- 1 | nimcache/* 2 | /src/nimcache/* 3 | /nimr 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nimr: Run nim programs like scripts 2 | 3 | This is a pretty much direct port of the excellent `nimrun` which was 4 | written by Flaviu Tamas. Unlike `nimrun`, `nimr` is implemented in 5 | nim and can be installed via nimble (for a nice smooth install). 6 | 7 | ## Requirements 8 | 9 | Nim compiler >= 0.16.0 10 | 11 | ## Installation 12 | 13 | Installation via Nimble is recommended: `nimble install nimr` 14 | 15 | ## Use 16 | 17 | After installation, simply add `#!/usr/bin/env nimr` as the first line 18 | of your script (just like you would with python/ruby/etc). 19 | 20 | ## Features 21 | 22 | * Caching of compilation results 23 | -------------------------------------------------------------------------------- /nimr.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.0" 4 | author = "Jeff Ciesielski" 5 | description = "Helper to run nim code like a script" 6 | license = "MIT" 7 | 8 | # Dependencies 9 | 10 | requires "nim >= 0.16.0" 11 | 12 | # Executable 13 | 14 | bin = @["nimr"] 15 | 16 | # Settings 17 | 18 | srcDir = "src" 19 | 20 | -------------------------------------------------------------------------------- /src/nimr.nim: -------------------------------------------------------------------------------- 1 | import os 2 | import osproc 3 | import strutils 4 | 5 | 6 | proc findOrCreateTempDir(srcFile: string): string = 7 | ## Checks for existing temporary directories so that we might use 8 | ## the already cached compilation results. If no directory exists, 9 | ## we create one 10 | let tmpDir = getTempDir() 11 | let (_, srcName, _) = splitFile(srcFile) 12 | 13 | result = tmpDir / "nimr" / srcName 14 | 15 | createDir(result) 16 | 17 | 18 | proc selectCompiler(): string = 19 | ## Try to use tcc/clang first since they tend to be faster, default 20 | ## to GCC if we can't find either of the others in our path 21 | if findExe("tcc") != "": 22 | result = "tcc" 23 | elif findExe("clang") != "": 24 | result = "clang" 25 | else: 26 | result = "gcc" 27 | 28 | 29 | proc compile(compiler, tmpdir, srcFile: string): int = 30 | ## Executes the nim compiler, placing the nimcache and executables 31 | ## in /nimr/ 32 | let 33 | compCmd = "nim c --threads:on --verbosity:0 --hints:off --cc:$1 ".format(compiler) 34 | opts = if compiler == "tcc": 35 | "--tlsEmulation:on " 36 | else: 37 | "" 38 | outputs = "--out:$1_executable --nimcache:$1 ".format(tmpdir) 39 | 40 | result = execCmd(compCmd & opts & outputs & srcFile) 41 | 42 | 43 | proc main() = 44 | let compiler = selectCompiler() 45 | var args = commandLineParams() 46 | 47 | if args.len == 0: 48 | quit(0) 49 | 50 | let srcFile = args[0] 51 | args.delete(0) 52 | 53 | let tmpDir = findOrCreateTempDir(srcFile) 54 | defer: 55 | removeDir(tmpDir) 56 | 57 | let compilerResult = compile(compiler, tmpDir, srcFile) 58 | 59 | if compilerResult == 0: 60 | quit(execCmd("$1_executable ".format(tmpDir) & args.join(" "))) 61 | else: 62 | quit(compilerResult) 63 | 64 | when isMainModule: 65 | main() 66 | --------------------------------------------------------------------------------