Skip to content
This repository
Browse code

Convert all scripts to Leiningen. Refs #34.

  • Loading branch information...
commit 578895bde94967eb5298cc59bde246017ea05dfb 1 parent e01b960
Brenton Ashworth authored January 23, 2012
1  .gitignore
@@ -7,3 +7,4 @@ docs
7 7
 /.lein-deps-sum
8 8
 *~
9 9
 /.lein-failures
  10
+/.lein-git-deps/
2  Procfile
... ...
@@ -1 +1 @@
1  
-web: lein run
  1
+web: lein run -m script.serve
14  project.clj
@@ -10,13 +10,15 @@
10 10
   :dev-dependencies [[jline "0.9.94"]
11 11
                      [marginalia "0.7.0-SNAPSHOT"]
12 12
                      [lein-marginalia "0.7.0-SNAPSHOT"]]
13  
-  :repositories {"oss-sonatype-staging"
14  
-                 "https://oss.sonatype.org/content/groups/staging/"}
15  
-  :main ^:skip-aot one.sample.launchpad
  13
+  :git-dependencies [["https://github.com/clojure/clojurescript.git"
  14
+                      "329708bdd0f039241b187bc639836d9997d8fbd4"]
  15
+                     ["https://github.com/levand/domina.git"
  16
+                      "c0eb06f677e0f9f72537682e3c702dd27b03e2e4"]]
  17
+  :repl-init one.sample.repl
16 18
   :source-path "src/app/clj"
17  
-  :extra-classpath-dirs ["lib/clojurescript/src/clj"
18  
-                         "lib/clojurescript/src/cljs"
19  
-                         "lib/domina/src/cljs"
  19
+  :extra-classpath-dirs [".lein-git-deps/clojurescript/src/clj"
  20
+                         ".lein-git-deps/clojurescript/src/cljs"
  21
+                         ".lein-git-deps/domina/src/cljs"
20 22
                          "src/app/cljs"
21 23
                          "src/app/cljs-macros"
22 24
                          "src/lib/clj"
23  script/build
... ...
@@ -1,24 +1,3 @@
1 1
 #!/bin/bash
2 2
 
3  
-set -e
4  
-
5  
-cd `dirname $0`/..
6  
-
7  
-mkdir -p out
8  
-
9  
-echo "Creating out/public..."
10  
-cp -a public out/
11  
-rm out/public/index.html
12  
-rm out/public/design.html
13  
-rm -rf out/public/javascripts/*
14  
-
15  
-source script/setup_classpath.sh
16  
-
17  
-echo "Create advanced compiled JavaScript..."
18  
-java -server -cp $CLJSC_CP jline.ConsoleRunner clojure.main -e \
19  
-"(use 'one.tools)
20  
- (use 'one.sample.config)
21  
- (build-project config)"
22  
-
23  
-echo "[build complete]"
24  
-
  3
+lein run -m script.build
1  script/build.cmd
... ...
@@ -0,0 +1 @@
  1
+lein run -m script.build
13  script/cljs-repl
... ...
@@ -1,13 +0,0 @@
1  
-#!/bin/bash
2  
-
3  
-cd `dirname $0`/..
4  
-
5  
-source script/setup_classpath.sh
6  
-
7  
-java -server -cp $CLJSC_CP jline.ConsoleRunner clojure.main -e \
8  
-"(use '[one.sample.dev-server :only (run-server cljs-repl)])
9  
- (use '[clojure.java.browse :only (browse-url)])
10  
- (run-server)
11  
- (future (Thread/sleep 3000) (browse-url \"http://localhost:8080/development\"))
12  
- (cljs-repl)" --repl
13  
-
22  script/deps
... ...
@@ -1,22 +0,0 @@
1  
-#!/bin/bash
2  
-
3  
-set -e
4  
-
5  
-cd `dirname $0`/..
6  
-
7  
-echo "Getting lein deps..."
8  
-lein deps
9  
-
10  
-cd lib
11  
-
12  
-git clone https://github.com/levand/domina.git
13  
-
14  
-git clone https://github.com/clojure/clojurescript.git
15  
-
16  
-cd domina
17  
-git checkout -b clojurescript-one-lib c0eb06f677e0f9f72537682e3c702dd27b03e2e4
18  
-
19  
-cd ../clojurescript
20  
-git checkout -b clojurescript-one-lib 329708bdd0f039241b187bc639836d9997d8fbd4
21  
-
22  
-echo "[Deps Completed]"
6  script/repl
... ...
@@ -0,0 +1,6 @@
  1
+#!/bin/bash
  2
+
  3
+# This is here for convention. Many Emacs users have their
  4
+# inferior-lisp-program set to script/repl.
  5
+
  6
+lein repl
10  script/run
... ...
@@ -1,10 +0,0 @@
1  
-#!/bin/bash
2  
-
3  
-cd `dirname $0`/..
4  
-
5  
-source script/setup_classpath.sh
6  
-
7  
-java -server -cp $CLJSC_CP jline.ConsoleRunner clojure.main -e \
8  
-"(use 'one.sample.dev-server)
9  
- (run-server)
10  
- (println \"The application is being served from localhost:8080\")" --repl
6  script/serve
... ...
@@ -0,0 +1,6 @@
  1
+#!/bin/bash
  2
+
  3
+# Start the production server which will serve the contents of
  4
+# out/public and the API for the sample application.
  5
+
  6
+lein run -m script.serve
8  script/setup_classpath.sh
... ...
@@ -1,8 +0,0 @@
1  
-CLOJURESCRIPT_HOME=lib/clojurescript
2  
-
3  
-CLJSC_CP='lib/*:lib/dev/*'
4  
-for next in 'src/clj' 'src/cljs' 'test/cljs'; do
5  
-  CLJSC_CP=${CLJSC_CP}:$CLOJURESCRIPT_HOME'/'$next
6  
-done
7  
-
8  
-CLJSC_CP=$CLJSC_CP':lib/domina/src/cljs:src/app/clj:src/app/cljs:src/app/cljs-macros:src/lib/clj:src/lib/cljs:test:templates'
10  script/test
... ...
@@ -1,10 +0,0 @@
1  
-#!/bin/bash
2  
-
3  
-cd `dirname $0`/..
4  
-
5  
-source script/setup_classpath.sh
6  
-
7  
-java -server -cp $CLJSC_CP jline.ConsoleRunner clojure.main -e \
8  
-"(use 'one.sample.test.suite)
9  
- (run-all)
10  
- (shutdown-agents)"
39  src/app/clj/one/sample/launchpad.clj
... ...
@@ -1,39 +0,0 @@
1  
-(ns one.sample.launchpad
2  
-  "The starting namespace for the project. This is the namespace that
3  
-  users will land in when they start a Clojure REPL. It exists both to
4  
-  process commands passed on the command line (such as 'build') and to
5  
-  provide convenience functions like 'go'."
6  
-  (:use [clojure.repl])
7  
-  (:require [one.sample.config :as config]
8  
-            [one.tools :as tools]
9  
-            [one.sample.dev-server :as dev]
10  
-            [one.sample.prod-server :as prod]
11  
-            [clojure.java.io :as io]
12  
-            [clojure.java.browse :as browse]))
13  
-
14  
-(defn go
15  
-  "Start a browser-connected REPL and launch a browser to talk to it."
16  
-  []
17  
-  (dev/run-server)
18  
-  (future (Thread/sleep 3000)
19  
-          (browse/browse-url "http://localhost:8080/development"))
20  
-  (tools/cljs-repl))
21  
-
22  
-(defmulti command (fn [args] (first args)))
23  
-
24  
-(defmethod command "build" [_]
25  
-  (println "Creating out/public...")
26  
-  (.mkdir (io/file "out"))
27  
-  (tools/copy-recursive-into "public" "out")
28  
-  (tools/delete "out/public/index.html"
29  
-                "out/public/design.html"
30  
-                "out/public/javascripts")
31  
-  (.mkdir (io/file "out/public/javascripts"))
32  
-  (println "Create advanced compiled JavaScript...")
33  
-  (tools/build-project config/config))
34  
-
35  
-(defmethod command nil [_]
36  
-  (prod/run-server))
37  
-
38  
-(defn -main [& args]
39  
-  (command args))
30  src/app/clj/one/sample/repl.clj
... ...
@@ -0,0 +1,30 @@
  1
+(ns one.sample.repl
  2
+  "The starting namespace for the project. This is the namespace that
  3
+  users will land in when they start a Clojure REPL. It exists both to
  4
+  process commands passed on the command line (such as 'build') and to
  5
+  provide convenience functions like 'go'."
  6
+  (:use [clojure.repl])
  7
+  (:require [one.tools :as tools]
  8
+            [one.sample.dev-server :as dev]
  9
+            [clojure.java.browse :as browse]))
  10
+
  11
+(defn go
  12
+  "Start a browser-connected REPL and launch a browser to talk to it."
  13
+  []
  14
+  (dev/run-server)
  15
+  (future (Thread/sleep 3000)
  16
+          (browse/browse-url "http://localhost:8080/development"))
  17
+  (tools/cljs-repl))
  18
+
  19
+(defn dev-server
  20
+  "Start the development server and open the host application in the
  21
+  default browser."
  22
+  []
  23
+  (dev/run-server)
  24
+  (future (Thread/sleep 3000)
  25
+          (browse/browse-url "http://localhost:8080")))
  26
+
  27
+(println)
  28
+(println "Type (go) to launch the development server and setup a browser-connected REPL.")
  29
+(println "Type (dev-server) to launch only the development server.")
  30
+(println)
15  src/app/clj/script/build.clj
... ...
@@ -0,0 +1,15 @@
  1
+(ns script.build
  2
+  (:require [clojure.java.io :as io]
  3
+            [one.tools :as tools]
  4
+            [one.sample.config :as config]))
  5
+
  6
+(defn -main []
  7
+  (println "Creating out/public...")
  8
+  (.mkdir (io/file "out"))
  9
+  (tools/copy-recursive-into "public" "out")
  10
+  (tools/delete "out/public/index.html"
  11
+                "out/public/design.html"
  12
+                "out/public/javascripts")
  13
+  (.mkdir (io/file "out/public/javascripts"))
  14
+  (println "Create advanced compiled JavaScript...")
  15
+  (tools/build-project config/config))
5  src/app/clj/script/serve.clj
... ...
@@ -0,0 +1,5 @@
  1
+(ns script.serve
  2
+  (:require [one.sample.prod-server :as prod]))
  3
+
  4
+(defn -main []
  5
+  (prod/run-server))
9  src/leiningen/bootstrap.clj
... ...
@@ -0,0 +1,9 @@
  1
+(ns leiningen.bootstrap
  2
+  (:require leiningen.deps
  3
+            leiningen.git-deps))
  4
+
  5
+(defn bootstrap
  6
+  "Bootstrap the project by running lein deps and lein git-deps."
  7
+  [project]
  8
+  (leiningen.deps/deps project)
  9
+  (leiningen.git-deps/git-deps project))
108  src/leiningen/git_deps.clj
... ...
@@ -0,0 +1,108 @@
  1
+(ns leiningen.git-deps
  2
+  "How this works: It clones projects into .lein-git-deps/<whatever>.
  3
+  If the directory already exists, it does a git pull and git checkout."
  4
+  (:require [clojure.java.shell :as sh]
  5
+            [clojure.java.io :as io]
  6
+            [clojure.string :as string]))
  7
+
  8
+(def ^:private git-deps-dir ".lein-git-deps")
  9
+
  10
+(defn- directory-exists?
  11
+  "Return true if the specified directory exists."
  12
+  [dir]
  13
+  (.isDirectory (io/file dir)))
  14
+
  15
+(defn- penultimate
  16
+  "Return the second-to-last element of a collection."
  17
+  [coll]
  18
+  (last (butlast coll)))
  19
+
  20
+(defn- default-clone-dir
  21
+  "Given a git URL, return the directory it would clone into by default."
  22
+  [uri]
  23
+  (string/join "." (-> uri
  24
+                       (string/split #"/")
  25
+                       (last)
  26
+                       (string/split #"\.")
  27
+                       butlast)))
  28
+
  29
+(defn- exec
  30
+  "Run a command, throwing an exception if it fails, returning the
  31
+result as with clojure.java.shell/sh."
  32
+  [& args]
  33
+  (let [{:keys [exit out err] :as result} (apply sh/sh args)]
  34
+    (if (zero? exit)
  35
+      result
  36
+      (throw
  37
+       (Exception.
  38
+        (format "Command %s failed with exit code %s\n%s\n%s"
  39
+                (apply str (interpose " " args))
  40
+                exit
  41
+                out
  42
+                err))))))
  43
+
  44
+(defn- git-clone
  45
+  "Clone the git repository at url into dir-name while working in
  46
+directory working-dir."
  47
+  [url dir-name working-dir]
  48
+  (apply exec (remove nil? ["git" "clone" url (str dir-name) :dir working-dir])))
  49
+
  50
+(defn- git-checkout
  51
+  "Check out the specified commit in dir"
  52
+  [commit dir]
  53
+  (println "Running git checkout " commit " in " (str dir))
  54
+  (exec "git" "checkout" commit :dir dir))
  55
+
  56
+(defn- detached-head?
  57
+  "Return true if the git repository in dir has HEAD detached."
  58
+  [dir]
  59
+  (let [{out :out} (exec "git" "branch" "--color=never" :dir dir)
  60
+        lines (string/split-lines out)
  61
+        current-branch (first (filter #(.startsWith % "*") lines))]
  62
+    (when-not current-branch
  63
+      (throw (Exception. "Unable to determine current branch")))
  64
+    (= current-branch "* (no branch)")))
  65
+
  66
+(defn- git-pull
  67
+  "Run 'git-pull' in directory dir, but only if we're on a branch. If
  68
+HEAD is detached, we only do a fetch, not a full pull."
  69
+  [dir]
  70
+  (println "Running git pull on " (str dir))
  71
+  (if (detached-head? dir)
  72
+    (do
  73
+      (println "Not on a branch, so fetching instead of pulling.")
  74
+      (exec "git" "fetch" :dir dir))
  75
+    (exec "git" "pull" :dir dir)))
  76
+
  77
+(defn git-deps
  78
+  "A leiningen task that will pull dependencies in via git.
  79
+
  80
+Dependencies should be listed in project.clj under the
  81
+:git-dependencies key in one of these three forms:
  82
+
  83
+:git-dependencies [;; First form: just a URL.
  84
+[\"https://github.com/foo/bar.git\"]
  85
+
  86
+;; Second form: A URL and a ref, which can be anything
  87
+;; you can specify for 'git checkout', like a commit ide
  88
+;; or a branch name.
  89
+[\"https://github.com/foo/baz.git\"
  90
+ \"329708b\"]
  91
+
  92
+;; Third form: A URL, a commit, and a map
  93
+[\"https://github.com/foo/quux.git\"
  94
+ \"some-branch\"
  95
+ {:dir \"alternate-directory\"}]]"
  96
+[project]
  97
+(when-not (directory-exists? git-deps-dir)
  98
+  (.mkdir (io/file git-deps-dir)))
  99
+(doseq [dep (:git-dependencies project)]
  100
+  (println "Setting up dependency for " dep)
  101
+  (let [[dep-url commit {clone-dir-name :dir}] dep
  102
+        commit (or commit "master")
  103
+        clone-dir-name (or clone-dir-name (default-clone-dir dep-url))
  104
+        clone-dir (io/file git-deps-dir clone-dir-name)]
  105
+    (if (directory-exists? clone-dir)
  106
+      (git-pull clone-dir)
  107
+      (git-clone dep-url clone-dir-name git-deps-dir))
  108
+    (git-checkout commit clone-dir))))

0 notes on commit 578895b

Please sign in to comment.
Something went wrong with that request. Please try again.