Browse files

Add support for ~/.lein/profiles.d. Closes #871.

Add support for profiles.d within the user's Leiningen home directory. All .clj
files within LEIN_HOME/profiles.d will be treated as maps merged into the
original profiles.clj (if existing).

Erroneous .clj file inside profiles.d emits an error message and will be treated
as nil.

A profile defined multiple times will error and "crash" the loading of profiles,
such that profiles return nil.

The errors will only be emitted once for user-friendliness.
  • Loading branch information...
hypirion committed Jan 23, 2013
1 parent 730141f commit 3045ed7111fda4e4b16fd7df73542ac1caea5255
Showing with 31 additions and 4 deletions.
  1. +31 −4 leiningen-core/src/leiningen/core/user.clj
@@ -23,6 +23,23 @@
(catch Exception e
(.printStackTrace e))))))))
+(def profiles-d-profiles
+ "Load all Clojure files from the profiles.d folder in your Leiningen home if
+ present. Returns a realized seq with the different profiles."
+ (memoize
+ (fn []
+ (let [profile-dir (io/file (leiningen-home) "profiles.d")]
+ (when (and (.exists profile-dir) (.isDirectory profile-dir))
+ (doall
+ (for [file (.listFiles profile-dir)
+ :when (.. file getName (endsWith ".clj"))]
+ (try (utils/read-file file)
+ (catch Exception e
+ (binding [*out* *err*]
+ (println "Error reading" (.getName file)
+ "from" (str (leiningen-home) "/profiles.d:"))
+ (println (.getMessage e))))))))))))
(def ^:private load-profiles
"Load profiles.clj from your Leiningen home if present."
@@ -33,10 +50,20 @@
(println "Error reading profiles.clj from" (leiningen-home))
(println (.getMessage e))))))))
-(defn profiles
- "Load profiles.clj from your Leiningen home if present."
- []
- (load-profiles))
+(def profiles
+ "Load profiles.clj from your Leiningen home and profiles.d if present."
+ (memoize
+ (fn []
+ (let [error-fn ;; TODO: More descriptive error messages.
+ (fn [a b]
+ (binding [*out* *err*]
+ (println "Error: A profile is defined multiple times!")
+ (println "Please check your profiles.clj and your profiles"
+ "in the profiles.d directory."))
+ (throw (Exception. "Multiple profiles defined in ~/.lein")))]
+ (try (apply merge-with error-fn
+ (load-profiles) (profiles-d-profiles))
+ (catch Exception e))))))
(defn gpg-program
"Lookup the gpg program to use, defaulting to 'gpg'"

0 comments on commit 3045ed7

Please sign in to comment.