Skip to content

Commit

Permalink
Convert all scripts to Leiningen. Refs #34.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brenton Ashworth committed Jan 23, 2012
1 parent e01b960 commit 578895b
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 131 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ docs
/.lein-deps-sum
*~
/.lein-failures
/.lein-git-deps/
2 changes: 1 addition & 1 deletion Procfile
@@ -1 +1 @@
web: lein run
web: lein run -m script.serve
14 changes: 8 additions & 6 deletions project.clj
Expand Up @@ -10,13 +10,15 @@
:dev-dependencies [[jline "0.9.94"]
[marginalia "0.7.0-SNAPSHOT"]
[lein-marginalia "0.7.0-SNAPSHOT"]]
:repositories {"oss-sonatype-staging"
"https://oss.sonatype.org/content/groups/staging/"}
:main ^:skip-aot one.sample.launchpad
:git-dependencies [["https://github.com/clojure/clojurescript.git"
"329708bdd0f039241b187bc639836d9997d8fbd4"]
["https://github.com/levand/domina.git"
"c0eb06f677e0f9f72537682e3c702dd27b03e2e4"]]
:repl-init one.sample.repl
:source-path "src/app/clj"
:extra-classpath-dirs ["lib/clojurescript/src/clj"
"lib/clojurescript/src/cljs"
"lib/domina/src/cljs"
:extra-classpath-dirs [".lein-git-deps/clojurescript/src/clj"
".lein-git-deps/clojurescript/src/cljs"
".lein-git-deps/domina/src/cljs"
"src/app/cljs"
"src/app/cljs-macros"
"src/lib/clj"
Expand Down
23 changes: 1 addition & 22 deletions script/build
@@ -1,24 +1,3 @@
#!/bin/bash

set -e

cd `dirname $0`/..

mkdir -p out

echo "Creating out/public..."
cp -a public out/
rm out/public/index.html
rm out/public/design.html
rm -rf out/public/javascripts/*

source script/setup_classpath.sh

echo "Create advanced compiled JavaScript..."
java -server -cp $CLJSC_CP jline.ConsoleRunner clojure.main -e \
"(use 'one.tools)
(use 'one.sample.config)
(build-project config)"

echo "[build complete]"

lein run -m script.build
1 change: 1 addition & 0 deletions script/build.cmd
@@ -0,0 +1 @@
lein run -m script.build
13 changes: 0 additions & 13 deletions script/cljs-repl

This file was deleted.

22 changes: 0 additions & 22 deletions script/deps

This file was deleted.

6 changes: 6 additions & 0 deletions script/repl
@@ -0,0 +1,6 @@
#!/bin/bash

# This is here for convention. Many Emacs users have their
# inferior-lisp-program set to script/repl.

lein repl
10 changes: 0 additions & 10 deletions script/run

This file was deleted.

6 changes: 6 additions & 0 deletions script/serve
@@ -0,0 +1,6 @@
#!/bin/bash

# Start the production server which will serve the contents of
# out/public and the API for the sample application.

lein run -m script.serve
8 changes: 0 additions & 8 deletions script/setup_classpath.sh

This file was deleted.

10 changes: 0 additions & 10 deletions script/test

This file was deleted.

39 changes: 0 additions & 39 deletions src/app/clj/one/sample/launchpad.clj

This file was deleted.

30 changes: 30 additions & 0 deletions src/app/clj/one/sample/repl.clj
@@ -0,0 +1,30 @@
(ns one.sample.repl
"The starting namespace for the project. This is the namespace that
users will land in when they start a Clojure REPL. It exists both to
process commands passed on the command line (such as 'build') and to
provide convenience functions like 'go'."
(:use [clojure.repl])
(:require [one.tools :as tools]
[one.sample.dev-server :as dev]
[clojure.java.browse :as browse]))

(defn go
"Start a browser-connected REPL and launch a browser to talk to it."
[]
(dev/run-server)
(future (Thread/sleep 3000)
(browse/browse-url "http://localhost:8080/development"))
(tools/cljs-repl))

(defn dev-server
"Start the development server and open the host application in the
default browser."
[]
(dev/run-server)
(future (Thread/sleep 3000)
(browse/browse-url "http://localhost:8080")))

(println)
(println "Type (go) to launch the development server and setup a browser-connected REPL.")
(println "Type (dev-server) to launch only the development server.")
(println)
15 changes: 15 additions & 0 deletions src/app/clj/script/build.clj
@@ -0,0 +1,15 @@
(ns script.build
(:require [clojure.java.io :as io]
[one.tools :as tools]
[one.sample.config :as config]))

(defn -main []
(println "Creating out/public...")
(.mkdir (io/file "out"))
(tools/copy-recursive-into "public" "out")
(tools/delete "out/public/index.html"
"out/public/design.html"
"out/public/javascripts")
(.mkdir (io/file "out/public/javascripts"))
(println "Create advanced compiled JavaScript...")
(tools/build-project config/config))
5 changes: 5 additions & 0 deletions src/app/clj/script/serve.clj
@@ -0,0 +1,5 @@
(ns script.serve
(:require [one.sample.prod-server :as prod]))

(defn -main []
(prod/run-server))
9 changes: 9 additions & 0 deletions src/leiningen/bootstrap.clj
@@ -0,0 +1,9 @@
(ns leiningen.bootstrap
(:require leiningen.deps
leiningen.git-deps))

(defn bootstrap
"Bootstrap the project by running lein deps and lein git-deps."
[project]
(leiningen.deps/deps project)
(leiningen.git-deps/git-deps project))
108 changes: 108 additions & 0 deletions src/leiningen/git_deps.clj
@@ -0,0 +1,108 @@
(ns leiningen.git-deps
"How this works: It clones projects into .lein-git-deps/<whatever>.
If the directory already exists, it does a git pull and git checkout."
(:require [clojure.java.shell :as sh]
[clojure.java.io :as io]
[clojure.string :as string]))

(def ^:private git-deps-dir ".lein-git-deps")

(defn- directory-exists?
"Return true if the specified directory exists."
[dir]
(.isDirectory (io/file dir)))

(defn- penultimate
"Return the second-to-last element of a collection."
[coll]
(last (butlast coll)))

(defn- default-clone-dir
"Given a git URL, return the directory it would clone into by default."
[uri]
(string/join "." (-> uri
(string/split #"/")
(last)
(string/split #"\.")
butlast)))

(defn- exec
"Run a command, throwing an exception if it fails, returning the
result as with clojure.java.shell/sh."
[& args]
(let [{:keys [exit out err] :as result} (apply sh/sh args)]
(if (zero? exit)
result
(throw
(Exception.
(format "Command %s failed with exit code %s\n%s\n%s"
(apply str (interpose " " args))
exit
out
err))))))

(defn- git-clone
"Clone the git repository at url into dir-name while working in
directory working-dir."
[url dir-name working-dir]
(apply exec (remove nil? ["git" "clone" url (str dir-name) :dir working-dir])))

(defn- git-checkout
"Check out the specified commit in dir"
[commit dir]
(println "Running git checkout " commit " in " (str dir))
(exec "git" "checkout" commit :dir dir))

(defn- detached-head?
"Return true if the git repository in dir has HEAD detached."
[dir]
(let [{out :out} (exec "git" "branch" "--color=never" :dir dir)
lines (string/split-lines out)
current-branch (first (filter #(.startsWith % "*") lines))]
(when-not current-branch
(throw (Exception. "Unable to determine current branch")))
(= current-branch "* (no branch)")))

(defn- git-pull
"Run 'git-pull' in directory dir, but only if we're on a branch. If
HEAD is detached, we only do a fetch, not a full pull."
[dir]
(println "Running git pull on " (str dir))
(if (detached-head? dir)
(do
(println "Not on a branch, so fetching instead of pulling.")
(exec "git" "fetch" :dir dir))
(exec "git" "pull" :dir dir)))

(defn git-deps
"A leiningen task that will pull dependencies in via git.
Dependencies should be listed in project.clj under the
:git-dependencies key in one of these three forms:
:git-dependencies [;; First form: just a URL.
[\"https://github.com/foo/bar.git\"]
;; Second form: A URL and a ref, which can be anything
;; you can specify for 'git checkout', like a commit ide
;; or a branch name.
[\"https://github.com/foo/baz.git\"
\"329708b\"]
;; Third form: A URL, a commit, and a map
[\"https://github.com/foo/quux.git\"
\"some-branch\"
{:dir \"alternate-directory\"}]]"
[project]
(when-not (directory-exists? git-deps-dir)
(.mkdir (io/file git-deps-dir)))
(doseq [dep (:git-dependencies project)]
(println "Setting up dependency for " dep)
(let [[dep-url commit {clone-dir-name :dir}] dep
commit (or commit "master")
clone-dir-name (or clone-dir-name (default-clone-dir dep-url))
clone-dir (io/file git-deps-dir clone-dir-name)]
(if (directory-exists? clone-dir)
(git-pull clone-dir)
(git-clone dep-url clone-dir-name git-deps-dir))
(git-checkout commit clone-dir))))

0 comments on commit 578895b

Please sign in to comment.