Read profiles from ~/.lein/profiles.clj and :profiles key in defproject.

This commit is contained in:
Phil Hagelberg 2011-12-07 16:14:53 -08:00
parent 7a59a30193
commit d490acd9d7
3 changed files with 25 additions and 10 deletions

View file

@ -64,9 +64,9 @@
(def profiles
"Profiles get merged into the project map. The :dev and :user
profiles are active by default."
(atom {:dev {:test-path ["test"]
:resources-path ["dev-resources"]}
:user (user/profile)}))
(atom (merge {:dev {:test-path ["test"]
:resources-path ["dev-resources"]}}
(user/profiles))))
;; Modified merge-with to provide f with the conflicting key.
(defn- merge-with-key [f & maps]
@ -105,11 +105,14 @@
(defn- merge-profile [project profile]
(merge-with-key profile-key-merge project profile))
(defn ^:internal merge-profiles [project profiles-to-apply]
(defn- profiles-for [project profiles-to-apply]
;; We reverse because we want profile values to override the
;; project, so we need "last wins" in the reduce, but we want the
;; first profile specified by the user to take precedence.
(reduce merge-profile project (map @profiles (reverse profiles-to-apply))))
(map (merge @profiles (:profiles project)) (reverse profiles-to-apply)))
(defn ^:internal merge-profiles [project profiles-to-apply]
(reduce merge-profile project (profiles-for project profiles-to-apply)))
(defn read
"Read project map out of file, which defaults to project.clj."

View file

@ -33,5 +33,7 @@
(for [plugin (.listFiles (io/file (leiningen-home) "plugins"))]
(.getAbsolutePath plugin)))
(defn profile []
{})
(defn profiles []
(let [profiles-file (io/file (leiningen-home) "profiles.clj")]
(if (.exists profiles-file)
(load-file (.getAbsolutePath profiles-file)))))

View file

@ -1,7 +1,14 @@
(ns leiningen.core.test.project
(:refer-clojure :exclude [read])
(:use [clojure.test]
[leiningen.core.project]))
[leiningen.core.project])
(:require [leiningen.core.user :as user]))
(use-fixtures :once
(fn [f]
;; Can't have user-level profiles interfering!
(with-redefs [user/plugins (constantly {})]
(f))))
(deftest test-read-project
(is (= {:name "leiningen", :group "leiningen", :version "2.0.0-SNAPSHOT",
@ -41,5 +48,8 @@
(deftest test-merge-profile-paths
(with-redefs [profiles test-profiles]
(is (= {:resources-path ["/etc/myapp" "test/hi" "resources"]}
(merge-profiles {:resources-path ["resources"]} [:qa :test])))))
(is (= ["/etc/myapp" "test/hi" "blue-resources" "resources"]
(-> {:resources-path ["resources"]
:profiles {:blue {:resources-path ["blue-resources"]}}}
(merge-profiles [:qa :test :blue])
:resources-path)))))