Skip to content

Commit

Permalink
Make leiningen.core.project/read init the project.
Browse files Browse the repository at this point in the history
Add a raw-read alternative that reads without initializing.

This solves a certain chicken/egg problem with wanting to read profiles
from plugins before the plugins had been loaded.
  • Loading branch information
technomancy committed Sep 6, 2014
1 parent 0a8c0aa commit 6a7a35d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion leiningen-core/src/leiningen/core/classpath.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(let [project (.getAbsolutePath project-file)]
;; TODO 3.0: core.project and core.classpath currently rely upon each other *uk*
(require 'leiningen.core.project)
(try ((resolve 'leiningen.core.project/read) project [:default])
(try ((resolve 'leiningen.core.project/read) project)
(catch Exception e
(throw (Exception. (format "Problem loading %s" project) e)))))
(warn "WARN ignoring checkouts directory" dep
Expand Down
16 changes: 7 additions & 9 deletions leiningen-core/src/leiningen/core/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,13 @@ Get the latest version of Leiningen at http://leiningen.org or by executing
(try
(user/init)
(let [project (binding [project/*suppress-profile-warnings* true]
(project/init-project
(if (.exists (io/file *cwd* "project.clj"))
(project/read (str (io/file *cwd* "project.clj")))
(-> (project/make {:eval-in :leiningen :prep-tasks []
:source-paths ^:replace []
:resource-paths ^:replace []
:test-paths ^:replace []})
project/project-with-profiles
(project/init-profiles [:default])))))
(if (.exists (io/file *cwd* "project.clj"))
(project/read (str (io/file *cwd* "project.clj")))
(-> (project/make {:eval-in :leiningen :prep-tasks []
:source-paths ^:replace []
:resource-paths ^:replace []
:test-paths ^:replace []})
(project/init-project))))
project (project/set-profiles project [:default])]
(when (:min-lein-version project) (verify-min-version project))
(configure-http)
Expand Down
48 changes: 28 additions & 20 deletions leiningen-core/src/leiningen/core/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -816,13 +816,16 @@
(pomegranate/add-classpath path))))

(defn init-project
"Initializes a project. This is called at startup with the default profiles."
[project]
(-> (doto project
(load-certificates)
(init-lein-classpath)
(load-plugins))
(activate-middleware)))
"Initializes a project by loading certificates, plugins, middleware, etc.
Also merges default profiles."
([project default-profiles]
(-> (project-with-profiles (doto project
(load-certificates)
(init-lein-classpath)
(load-plugins)))
(init-profiles default-profiles)
(activate-middleware)))
([project] (init-project project [:default])))

(defn add-profiles
"Add the profiles in the given profiles map to the project map, taking care
Expand All @@ -837,19 +840,24 @@
merge profiles-map)})
(vary-meta update-in [:profiles] merge profiles-map)))

(defn read-raw
"Read project file without loading certificates, plugins, middleware, etc."
[file]
(locking read-raw
(binding [*ns* (find-ns 'leiningen.core.project)]
(try (load-file file)
(catch Exception e
(throw (Exception. (format "Error loading %s" file) e)))))
(let [project (resolve 'leiningen.core.project/project)]
(when-not project
(throw (Exception. (format "%s must define project map" file))))
;; return it to original state
(ns-unmap 'leiningen.core.project 'project)
@project)))

(defn read
"Read project map out of file, which defaults to project.clj."
([file profiles]
(locking read
(binding [*ns* (find-ns 'leiningen.core.project)]
(try (load-file file)
(catch Exception e
(throw (Exception. (format "Error loading %s" file) e)))))
(let [project (resolve 'leiningen.core.project/project)]
(when-not project
(throw (Exception. (format "%s must define project map" file))))
;; return it to original state
(ns-unmap 'leiningen.core.project 'project)
(init-profiles (project-with-profiles @project) profiles))))
"Read project map out of file, which defaults to project.clj.
Also initializes the project; see read-raw for a version that skips init."
([file profiles] (init-project (read-raw file) profiles))
([file] (read file [:default]))
([] (read "project.clj")))

0 comments on commit 6a7a35d

Please sign in to comment.