Last active
February 10, 2017 10:05
-
-
Save pesterhazy/bac40121b179aa461d89740b799c0555 to your computer and use it in GitHub Desktop.
Run lumo with ClojureScript (maven) or JavaScript (npm) dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# lumo-deps: lumo with JavaScript or ClojureScript dependencies | |
# | |
# Synopsis: | |
# | |
# lumo-deps [-c clojure-dep] [-j javascript-dep] [-- lumo-opt1 lumo-opt2 ...] | |
# | |
# Starts a lumo session with ClojureScript or JavaScript dependencies loaded. | |
# | |
# -c clojure-dep: Add ClojureScript (maven) dependency. `clojure-dep' is a | |
# maven artifact with optional version number | |
# | |
# E.g.: -c com.andrewmcveigh/cljs-time:0.4.0 | |
# | |
# If the version number is omitted, the latest version is used. | |
# | |
# -j js-dep: Add JavaScript (npm) dependency. `js-dep' is the name of an | |
# npm package with optional version number | |
# | |
# E.g.: -j node-fetch@1.6.3 | |
# | |
# If the version number is omitted, the latest version is used. | |
# | |
# Both `-c` and `-j` can be specified multiple times to load multiple dependencies. | |
# | |
# Examples: | |
# | |
# lumo-deps -j node-fetch | |
# | |
# lumo-deps -j node-fetch -- script.cljs | |
# | |
# Requires yarn (for npm dependencies) and boot 2.7.1 or above (for maven | |
# dependencies) to be installed. | |
# | |
# Attention: deletes an existing package.json in the current directory | |
# | |
import sys, subprocess, tempfile | |
def split_argv(argv): | |
if '--' in argv: | |
idx = argv.index('--') | |
return [argv[:idx],argv[idx+1:]] | |
else: | |
return [argv, []] | |
def chunks(l, n): | |
n = max(1, n) | |
return (l[i:i+n] for i in xrange(0, len(l), n)) | |
def parse_argv(xs): | |
cdeps = [] | |
jdeps = [] | |
if (len(xs) % 2) != 0: | |
print "Needs even number of arguments" | |
sys.exit(1) | |
for tag, val in chunks(xs,2): | |
if tag == "-c": | |
cdeps.append(val) | |
elif tag == "-j": | |
jdeps.append(val) | |
else: | |
print "Unknown option" | |
sys.exit(1) | |
return cdeps, jdeps | |
def transform_cdeps(cdeps): | |
if cdeps: | |
tf = tempfile.NamedTemporaryFile() | |
deps = sum([["-d", dep] for dep in cdeps], []) | |
subprocess.check_call(["boot"]+deps+["with-cp","--write","--file",tf.name]) | |
cp=tf.file.read().strip() | |
return ["-c", cp] | |
else: | |
return [] | |
def transform_jdeps(jdeps): | |
if jdeps: | |
subprocess.check_call(["rm", "-f", "package.json"]) | |
subprocess.check_call(["yarn", "add"]+jdeps) | |
return [] | |
def main(): | |
a, b = split_argv(sys.argv[1:]) | |
cdeps,jdeps = parse_argv(a) | |
subprocess.check_call(["lumo"]+transform_cdeps(cdeps)+transform_jdeps(jdeps)+b) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment