-
Notifications
You must be signed in to change notification settings - Fork 10
/
nimcr.nim
66 lines (56 loc) · 2 KB
/
nimcr.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
import os, osproc
import hashes, strutils
const nimArgsPrefix = "#nimcr-args "
# Inspect command line parameters
let args = commandLineParams()
if args.len == 0:
stderr.write "Usage on the command line: nimcr filename [arguments to program]\n"
stderr.write "Usage in a script:\n"
stderr.write "\t1- add `#!/usr/bin/env nimcr` to your script as first line\n"
stderr.write "\t2- (optional) add `#nimcr-args [arguments for nim compiler]` to your script as second line\n"
quit -1
var filenamePos = args.low
let filename = args[filenamePos].expandFilename
# Split the file path and make a new one which is a hidden file on Linux, Windows file hiding comes later
let
splitName = filename.splitfile
ext =
when defined(windows):
".exe"
else:
""
exeName = splitName.dir/("." & splitName.name & ext)
# Compilation of script if target doesn't exist
var
buildStatus = 0
output = ""
command = ""
if not exeName.fileExists or filename.fileNewer exeName:
var nimArgs: string = "c -d:release"
# Get extra arguments for nim compiler from the second line (it must start with #nimcr-args [args] )
block:
for line in filename.lines:
if line.len == 0 or line[0] != '#':
break
if line.startsWith(nimArgsPrefix):
nimArgs = line[nimArgsPrefix.len .. ^1]
break
exeName.removeFile
command = "nim " & nimArgs & " --colors:on --nimcache:" &
getTempDir()/("nimcache-" & filename.hash.toHex) &
" --out:\"" & exeName & "\" " & filename
(output, buildStatus) = execCmdEx(command)
# Windows file hiding (hopefully, not tested)
when defined(windows):
discard execShellCmd("attrib +H " & exeName)
# Run the target, or show an error
if buildStatus == 0:
let p = startProcess(exeName, args=args[args.low+1 .. ^1],
options={poStdErrToStdOut, poParentStreams, poUsePath})
let res = p.waitForExit()
p.close()
quit res
else:
stderr.write "(nimcr) Error on build running command: " & command
stderr.write output
quit buildStatus