Skip to content

Commit

Permalink
Update with-profile to accept +/- prefixes
Browse files Browse the repository at this point in the history
Allow the profiles to be used to be specified as a delta from the default
profile list.
  • Loading branch information
hugoduncan committed Nov 26, 2012
1 parent 37aee43 commit 3f08092
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions doc/PROFILES.md
@@ -1,3 +1,4 @@
<!-- TODO update for with-profile +/- syntax -->
# Profiles # Profiles


In Leiningen 2.x you can change the configuration of your project by In Leiningen 2.x you can change the configuration of your project by
Expand Down
67 changes: 51 additions & 16 deletions src/leiningen/with_profile.clj
@@ -1,39 +1,74 @@
(ns leiningen.with-profile (ns leiningen.with-profile
(:require [leiningen.core.main :as main] (:require [clojure.string :as string]
[leiningen.core.main :as main]
[leiningen.core.project :as project] [leiningen.core.project :as project]
[robert.hooke :as hooke])) [robert.hooke :as hooke]))


(defn- with-profile* (defn ^:internal with-profiles*
"Apply the given task with a comma-separated profile list." "Apply the given task with a comma-separated profile list."
[project profiles task-name & args] [project profiles task-name args]
(hooke/with-scope (hooke/with-scope
(let [profiles (map keyword (.split profiles ",")) (let [project (and project (project/set-profiles project profiles))
project (and project (project/set-profiles project profiles))
task-name (main/lookup-alias task-name project)] task-name (main/lookup-alias task-name project)]
(main/apply-task task-name project args)))) (main/apply-task task-name project args))))


(defn profiles-in-group
[project profile-group]
(let [profiles (.split profile-group ",")
prefixes (map first profiles)]
(cond
(every? #{\+ \-} prefixes)
(reduce
(fn [result profile]
(if (= \+ (first profile))
(concat result [(keyword (subs profile 1))])
(remove #(= (keyword (subs profile 1)) %) result)))
(:active-profiles (meta project))
profiles)

(not-any? #{\+ \-} prefixes)
(map keyword profiles)

:else
(throw
(ex-info
"Profiles in with-profile must either all be qualified, or none qualified"
{:exit-code 1})))))

(defn ^:no-project-needed ^:higher-order with-profile (defn ^:no-project-needed ^:higher-order with-profile
"Apply the given task with the profile(s) specified. "Apply the given task with the profile(s) specified.
Comma-separated profiles may be given to merge profiles and perform the task. Comma-separated profiles may be given to merge profiles and perform the task.
Colon-separated profiles may be given for sequential profile task application. Colon-separated profiles may be given for sequential profile task application.
A profile list may either be a list of profiles to use, or may specify the
profiles to add or remove from the active profile list using + or - prefixes.
For example:
lein with-profile user,dev test
lein with-profile -dev test
lein with-profile +1.4:+1.4,-dev:base,user test
To list all profiles or show a single one, see the show-profiles task. To list all profiles or show a single one, see the show-profiles task.
For a detailed description of profiles, see `lein help profiles`." For a detailed description of profiles, see `lein help profiles`."
[project profiles task-name & args] [project profiles task-name & args]
(let [profile-groups (seq (.split profiles ":")) (let [profile-groups (seq (.split profiles ":"))
failures (atom 0)] failures (atom 0)]
(doseq [profile-group profile-groups] (doseq [profiles (map (partial profiles-in-group project) profile-groups)]
(main/info (format "Performing task '%s' with profile(s): '%s'"
task-name (string/join "," (map name profiles))))
(binding [main/*exit-process?* false] (binding [main/*exit-process?* false]
(main/info (format "Performing task '%s' with profile(s): '%s'" (try
task-name profile-group)) (with-profiles* project profiles task-name args)
(try (apply with-profile* project profile-group task-name args) (catch Exception e
(catch Exception e (main/info
(main/info (format "Error encountered performing task '%s' with profile(s): '%s'" (format
task-name profile-group)) "Error encountered performing task '%s' with profile(s): '%s'"
(if (and (:exit-code (ex-data e)) (not main/*debug*)) task-name (string/join "," (map name profiles))))
(main/info (.getMessage e)) (if (and (:exit-code (ex-data e)) (not main/*debug*))
(.printStackTrace e)) (main/info (.getMessage e))
(swap! failures inc))))) (.printStackTrace e))
(swap! failures inc)))))
(when (pos? @failures) (when (pos? @failures)
(main/abort)))) (main/abort))))

0 comments on commit 3f08092

Please sign in to comment.