Skip to content

Instantly share code, notes, and snippets.

@robjens
Created December 20, 2015 20:28
Show Gist options
  • Select an option

  • Save robjens/5d72bc0f2ab2ea82a7b0 to your computer and use it in GitHub Desktop.

Select an option

Save robjens/5d72bc0f2ab2ea82a7b0 to your computer and use it in GitHub Desktop.
Small helper to quickly create a working cljs project from scratch
#!/usr/bin/env bash %
#
# bootstrap.sh
#
# Copyright (C) 2015 Rob Jentzema <[email protected]>
#
# Distributed under terms of the MIT license.
#
# Reference(s):
# 1. https://github.com/clojure/clojurescript/wiki/Quick-Start
#
# 1a. get absolute directory path resolved
target=$(dirname $(readlink -f ${0}))
# 1b. set a default project name in case none is provided
project=cljs-quickstart
# 1c. cljs on github root uri
release=https://github.com/clojure/clojurescript/releases
# 2. helpers
f=$(tput sgr0)
function go() { printf "$(tput setaf 5)؟$f ${1}"; }
function pr() { printf "\r$(tput setaf 3)→$f ${1}\n"; }
function ok() { printf "\r$(tput setaf 2)✓$f ${1}\n"; }
function err() { printf "\r$(tput setaf 1)✗$f ${1}\n"; }
# 3. user input option parsing, disable verbose error handling by prefix
# colon : suffix colons means the option before (both d and n at this time)
# expect a value
go "check user input"
while getopts ":d:n:" opt; do
case $opt in
d)
# reusing target directory variable, in case provided
target=$(readlink -f ${OPTARG})
pr "setting target directory to ${target} (cli argument)"
;;
n)
# same goes for project name
project=${OPTARG}
pr "setting project name to ${project} (cli argument)"
;;
\?)
err "invalid option -$OPTARG" >&2
;;
esac
done
ok "check user input"
go "set project safe name"
safename=${target}/src/$(echo ${project} | tr '-' '_')
ok "set safe name replaced hyphens for underscores"
go "check for cljs.jar file in project"
test -f ${target}/cljs.jar || {
err "cljs.jar not found"
go "resolve url for download cljs.jar"
location=$(curl -ILs -o /dev/null -w %{url_effective} "${release}/latest")
download="${release}/download/${location##*/}/cljs.jar"
ok "resolved url proceed to download"
go "download cljs.jar file"
wget -P ${target} ${download}
test -f ${target}/cljs.jar && ok "file cljs.jar succesfully downloaded" ||\
err "failed to download cljs.jar, check script input"
}
ok "check for cljs.jar complete"
# 6. see if we might already have a src/ folder
go "checking for a source folder"
test -d ${safename} || {
err "source folder not found"
go "create source folder"
mkdir -p ${safename}
test -d ${safename} && ok "source folder now found" || err "source folder still not found"
}
ok "source folder check done now"
go "see if we have a core cljs file"
test -f ${safename}/core.cljs || {
cat <<-eos > ${safename}/core.cljs
(ns ${project}.core)
(enable-console-print!)
(println "Hello world!")
eos
}
test -f ${safename}/core.cljs &&\
ok "core cljs file found in the target ${safename} location" ||\
err "core cljs file not found in target ${safename} location"
go "check build script"
test -f ${target}/build.clj || {
cat <<-eos > ${target}/build.clj
(require 'cljs.build.api)
(cljs.build.api/build "src"
{:main '${project}.core
:output-to "out/main.js"})
eos
}
ok "build script found ok"
go "check for watch script"
test -f ${target}/watch.clj || {
cat <<-eos > ${target}/watch.clj
(require 'cljs.build.api)
(cljs.build.api/watch "src"
{:main '${project}.core
:output-to "out/main.js"})
eos
}
ok "watch script found or created"
go "check for index.html file"
test -f ${target}/index.html || {
cat <<-eos > ${target}/index.html
<html>
<body>
<script type="text/javascript" src="out/main.js"></script>
</body>
</html>
eos
}
ok "index.html file found in root"
go "compile cljs"
java -cp cljs.jar:src clojure.main build.clj
ok "compilation of cljs file done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment