Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract out the lein command finding fns.
This makes them available for plugins to use for testing.
- Loading branch information
Showing
4 changed files
with
103 additions
and
67 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
(ns leinjacker.lein-runner | ||
"Functions to locate and run the 'lein' command for either lein generation." | ||
(:require [clojure.java.io :as io] | ||
[clojure.java.shell :as shell])) | ||
|
||
(defn- clean-lein-env | ||
"Gets a copy of the system environment tha removes Leiningen environment variables." | ||
[] | ||
(dissoc (into {} (System/getenv)) | ||
"CLASSPATH" "LEIN_JVM_OPTS")) | ||
|
||
(defn- memoize-to-file | ||
"Memoizes the result if not-found-fn under key in filename." | ||
[filename key not-found-fn] | ||
(let [cache-file (io/file filename) | ||
cache (if (.exists cache-file) (read-string (slurp cache-file)) {})] | ||
(or (cache key) | ||
(let [cmd (not-found-fn key)] | ||
(spit cache-file (prn-str (assoc cache key cmd))) | ||
cmd)))) | ||
|
||
(defn- find-lein-cmd* | ||
"Does the work of looking up the lein command without any memoization options." | ||
[generation] | ||
(if-let [cmd (some | ||
(fn [cmd] | ||
(try | ||
(if (.contains (:out (shell/sh cmd "version" :env (clean-lein-env))) | ||
(str "Leiningen " generation ".")) | ||
cmd) | ||
(catch java.io.IOException _))) | ||
["lein" (str "lein" generation)])] | ||
cmd | ||
(throw (IllegalStateException. | ||
(str "Unable to find Leiningen " generation | ||
" in the path as lein or lein" generation | ||
". Please make sure it is installed and in your path under one of those names."))))) | ||
|
||
(defn find-lein-cmd | ||
"Attempts to locate the lein command for the given command on the path. | ||
Looks for: lein, lein<generation>. If memoize? isn't provided or is true, | ||
the results are cached in ./.lein-commands." | ||
([generation] | ||
(find-lein-cmd generation true)) | ||
([generation memoize?] | ||
(if memoize? | ||
(memoize-to-file ".lein-commands" generation find-lein-cmd*) | ||
(find-lein-cmd* generation)))) | ||
|
||
(defn run-lein | ||
"Runs lein for the given generation with args. | ||
:dir and :env options may be specified with kwargs. Returns the result from sh." | ||
[generation & args] | ||
(let [[real-args {:as opts}] (split-with string? args)] | ||
(apply shell/sh (find-lein-cmd generation) | ||
(conj (vec real-args) | ||
:dir (:dir opts) | ||
:env (assoc (merge (clean-lein-env) (:env opts)) | ||
"LEIN_GENERATION" generation))))) | ||
|
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns leinjacker.test-multiple-lein-versions | ||
"Runs the tests in the test-projects projects under lein1 and lein2." | ||
{:author "Toby Crawley"} | ||
(:use midje.sweet) | ||
(:require [leinjacker.utils :as utils] | ||
[leinjacker.lein-runner :as runner] | ||
[leiningen.install :as install])) | ||
|
||
(defn- run-lein-on-sub-project | ||
"Runs the midje command for the given generation of lein in test-project/" | ||
[generation] | ||
(println "\n==> Running 'lein midje' (generation" (str generation \)) "in test-project/") | ||
(let [result (runner/run-lein generation "midje" :dir "test-project")] | ||
(print (:out result)) | ||
(print (:err result)) | ||
(println "==> Done\n") | ||
(:exit result))) | ||
|
||
;; install leinjacker for the test project to use | ||
(install/install (utils/read-lein-project)) | ||
|
||
(fact "run lein1 tests." | ||
;; lein1 needs deps resolved to see the installed leinjacker | ||
(runner/run-lein "1" "deps" :dir "test-project") | ||
(run-lein-on-sub-project "1") => 0) | ||
|
||
(fact "run lein2 tests." | ||
(run-lein-on-sub-project "2") => 0) | ||
|