Skip to content

Commit

Permalink
Sets source-paths for a build relative to the working directory (#465)
Browse files Browse the repository at this point in the history
The working directory is usually the project's home directory, except
when using lein-monolith. If lein-monolith is used we need to adjust the
source-paths to be relative to the current working directory, so that
the compiler can find the source files.
  • Loading branch information
mneise committed Jul 13, 2017
1 parent 018c5f9 commit c18cf85
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example-projects/advanced/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:exclusions [org.apache.ant/ant]]
[compojure "1.1.6"]
[hiccup "1.0.4"]]
:plugins [[lein-cljsbuild "1.1.6"]
:plugins [[lein-cljsbuild "1.1.7-SNAPSHOT"]
[lein-ring "0.8.7"]]
; Enable the lein hooks for: compile, test, and jar.
:hooks [leiningen.cljsbuild]
Expand Down
2 changes: 1 addition & 1 deletion example-projects/none/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.521"]]

:plugins [[lein-cljsbuild "1.1.6"]]
:plugins [[lein-cljsbuild "1.1.7-SNAPSHOT"]]

:source-paths ["src"]

Expand Down
2 changes: 1 addition & 1 deletion example-projects/simple/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:exclusions [org.apache.ant/ant]]
[compojure "1.1.6"]
[hiccup "1.0.4"]]
:plugins [[lein-cljsbuild "1.1.6"]
:plugins [[lein-cljsbuild "1.1.7-SNAPSHOT"]
[lein-ring "0.8.7"]]
:cljsbuild {
:builds [{:source-paths ["src-cljs"]
Expand Down
3 changes: 2 additions & 1 deletion plugin/src/leiningen/cljsbuild.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[leiningen.cljsbuild.config :as config]
[leiningen.cljsbuild.jar :as jar]
[leiningen.cljsbuild.subproject :as subproject]
[leiningen.cljsbuild.util :as util]
[leiningen.compile :as lcompile]
[leiningen.core.eval :as leval]
[leiningen.core.project :as lproject]
Expand Down Expand Up @@ -73,7 +74,7 @@
project
(fn [checkout]
(let [{:keys [builds]} (config/extract-options checkout)
root (:root checkout)]
root (util/working-directory)]
(for [build builds
path (:source-paths build)]
(fs/absolute-path (io/file root path)))))))
Expand Down
33 changes: 28 additions & 5 deletions plugin/src/leiningen/cljsbuild/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Utilities for parsing the cljsbuild config."
(:require
[clojure.pprint :as pprint]
[clojure.java.io :as io]
[leiningen.cljsbuild.util :as util]))

(defn in-target-path [target-path subpath]
Expand Down Expand Up @@ -115,8 +116,7 @@
(defn- set-default-output-dirs [target-path options]
(let [output-dir-key [:compiler :output-dir]
relative-target-path (when target-path
(util/relative-path (str (System/getProperty "user.dir")
(System/getProperty "file.separator")) target-path))
(util/relative-path (util/working-directory) target-path))
builds
(for [[build counter] (map vector (:builds options) (range))]
(if (get-in build output-dir-key)
Expand All @@ -143,16 +143,39 @@
(assoc options :builds
(map set-build-global-dirs (:builds options))))

(defn set-build-source-paths
"Sets the source paths for a build relative to the current working directory."
[root build]
(assoc build :source-paths
(map
(fn [source-path]
(let [absolute-path (try
(str (io/file root source-path))
(catch Exception e
(throw (Exception. (format "Check format for source-path: %s" source-path) e))))
relative-path (util/relative-path (util/working-directory) absolute-path)]
(if (= absolute-path relative-path)
source-path
relative-path)))
(:source-paths build))))

(defn set-compiler-source-paths
"Sets the source paths for each build."
[root options]
(assoc options :builds
(map #(set-build-source-paths root %) (:builds options))))

(defn- normalize-options
"Sets default options and accounts for backwards compatibility."
[target-path options]
[target-path root options]
(let [options (convert-builds-map options)
compat (backwards-compat options)]
(when (not= options compat)
(warn-deprecated compat))
(->> compat
(set-default-options target-path)
set-compiler-global-dirs)))
set-compiler-global-dirs
(set-compiler-source-paths root))))

(defn parse-shell-command [raw]
(let [[shell tail] (split-with (comp not keyword?) raw)
Expand Down Expand Up @@ -180,4 +203,4 @@
(when (nil? (:cljsbuild project))
(println "WARNING: no :cljsbuild entry found in project definition."))
(let [raw-options (:cljsbuild project)]
(normalize-options (:target-path project) raw-options)))
(normalize-options (:target-path project) (:root project) raw-options)))
11 changes: 9 additions & 2 deletions plugin/src/leiningen/cljsbuild/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
[clojure.string :as s]))

(defn relative-path
"Given two normalized path strings, returns a path string of the second relative to the first.
Otherwise returns the second path."
"Given two normalized path strings, returns a path string of the second
relative to the first. Otherwise returns the second path."
[parent child]
(let [relative (s/replace child parent "")]
(if (= child relative)
child
(s/replace relative #"^[\\/]" ""))))

(defn working-directory
"Returns the full path to the working directory, which is the directory
the build was started in."
[]
(str (System/getProperty "user.dir")
(System/getProperty "file.separator")))
2 changes: 2 additions & 0 deletions plugin/test/leiningen/test/cljsbuild.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[leiningen.cljsbuild.config :as config]
[leiningen.cljsbuild.jar :as jar]
[leiningen.cljsbuild.subproject :as subproject]
[leiningen.cljsbuild.util :as util]
[leiningen.core.main :as lmain]
[leiningen.core.eval :as leval]
[leiningen.core.project :as lproject]
Expand Down Expand Up @@ -165,6 +166,7 @@
(fs/list-dir checkout-b-checkouts) => nil, :times 1
(fs/exists? checkout-a-project) => true, :times 1
(fs/exists? checkout-b-project) => true, :times 1
(util/working-directory) => "/project"

(lproject/read "/project/checkouts/lib-a/project.clj") =>
(assoc project :root (fs/absolute-path checkout-a-dir)), :times 1
Expand Down
39 changes: 38 additions & 1 deletion plugin/test/leiningen/test/cljsbuild/config.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns leiningen.test.cljsbuild.config
(:use
leiningen.cljsbuild.config
midje.sweet))
midje.sweet)
(:require
[leiningen.cljsbuild.util :as util]))

(fact
(let [config-0-0-x-early {:source-path "a"
Expand Down Expand Up @@ -74,3 +76,38 @@

(fact
(extract-options {:cljsbuild config-in}) => config-out)

(fact "source path is changed when project root is not working directory"
(set-build-source-paths
"/home/project/subproject"
{:source-paths ["src"]}) => {:source-paths ["subproject/src"]}
(provided
(util/working-directory) => "/home/project"))

(fact "source path is not changed when project is working directory"
(set-build-source-paths
"/home/project/subproject"
{:source-paths ["src"]}) => {:source-paths ["src"]}
(provided
(util/working-directory) => "/home/project/subproject"))

(fact "incorrect source path formatting throws exception"
(set-build-source-paths
"/home/project/subproject"
{:source-paths ["/src"]}) => (throws Exception))

(fact "source paths are changed when project root is not working directory"
(set-compiler-source-paths"/home/project/subproject"
{:builds [{:source-paths ["src/cljs" "src/cljc"]}
{:source-paths ["tests"]}]}) => {:builds [{:source-paths ["subproject/src/cljs" "subproject/src/cljc"]}
{:source-paths ["subproject/tests"]}]}
(provided
(util/working-directory) => "/home/project"))

(fact "source paths are not changed when project root is working directory"
(set-compiler-source-paths"/home/project/subproject"
{:builds [{:source-paths ["src/cljs" "src/cljc"]}
{:source-paths ["tests"]}]}) => {:builds [{:source-paths ["src/cljs" "src/cljc"]}
{:source-paths ["tests"]}]}
(provided
(util/working-directory) => "/home/project/subproject"))
2 changes: 1 addition & 1 deletion plugin/test/leiningen/test/cljsbuild/jar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(defn make-jar-directories [names]
(let [directories (for [n (map name names)]
(make-jar-directory
(join-paths "/root" n)
(join-paths "root" n)
(str "file-" n)
(str "contents-" n)))]
(into {} (map vector names directories))))
Expand Down

0 comments on commit c18cf85

Please sign in to comment.