Skip to content

Instantly share code, notes, and snippets.

@flaviut
Last active August 29, 2015 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flaviut/27c2dc9cd2d8f5ced4e4 to your computer and use it in GitHub Desktop.
Save flaviut/27c2dc9cd2d8f5ced4e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# A wrapper around the Nim compiler to allow for easy scripting of Nim. Puts
# all temporary files in a temporary directory and cleans up after itself.
#
# Usage:
# - add `#!/usr/bin/env nimrun` at the beginning of your script
# - execute the nim file with it, for example, `nimrun file.nim args`
#
# Possible future extentions:
# - cache compilation results for a while
# - configurable temporary directory
# - allow for custom `nim c` parameters
#
# Author: Flaviu Tamas <tamas.flaviu@gmail.com>
# Copyright (c) 2014 Flaviu Tamas
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
output=$(mktemp -d)
trap "rm -rf $output" EXIT
nim c --verbosity:0 \
--hints:off \
--out:"$output/executable" \
--nimcache:"$output/" \
"$1"
# first argument is filename, not needed any more
shift
compiler_exit=$?
if $compiler_exit; then # compile success
$output/executable $*
exit $?
else # compile fail
exit $compiler_exit
fi
@bluenote10
Copy link

Nice script! However, I always get a "0: not found" in line 53. See my fork for a fix that works for my.

@bluenote10
Copy link

Note that there is another issue, i.e., line 49 and 51 should be exchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment