├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── project.clj └── src ├── leiningen └── viz_deps.clj └── shared_deps └── plugin.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hg/ 11 | .idea 12 | *.iml 13 | 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.2.8 - 4 Jan 2017 2 | 3 | Dependencies defined inside dependency sets are now automatically non-transitive. 4 | Instead, dependency sets should extend other dependency sets to provide transitive 5 | dependencies. 6 | 7 | ## 0.2.7 - 1 Apr 2016 8 | 9 | Recompute dependencies from the base version of the project (before profiles are applied). 10 | This may cause incompatibilities with other plugins that modify the dependencies, but 11 | does a better job in other cases. 12 | 13 | [Closed Issues](https://github.com/walmartlabs/shared-deps/issues?q=milestone%3A0.2.7+is%3Aclosed) 14 | 15 | ## 0.2.6 - 22 Jan 2016 16 | 17 | The list of known dependency ids, output when an unknown dependency id is encountered, 18 | is now output in multiple columns. 19 | 20 | The plugin now includes a `viz-deps` task; this will generate a visualization 21 | of the project's dependency graph, as a PDF file. 22 | 23 | ## 0.2.5 - 16 Dec 2015 24 | 25 | Adding caching for speed improvements under very large projects. 26 | 27 | ## 0.2.4 - 30 Nov 2015 28 | 29 | Ensure each profile is processed just once. 30 | 31 | Project names are reported fully qualified with namespace, when one is provided. 32 | 33 | Fix a number of issues with dependencies vs. pom.xml generation. 34 | There are still outstanding issues with pom.xml generation. 35 | 36 | ## 0.2.3 - 27 Nov 2015 37 | 38 | Properly handle dependencies across profiles so that a correct pom.xml 39 | may be generated. Previously, dependency sets from all modules were merged 40 | into the base profile, which meant that testing dependencies appeared 41 | in the pom.xml (they should be excluded). 42 | 43 | ## 0.2.2 - 25 Nov 2015 44 | 45 | Automatically locate sibling projects and establish them as sharable 46 | dependencies. 47 | 48 | Improved reporting of unknown dependency ids. 49 | 50 | ## 0.2.1 - 20 Nov 2015 51 | 52 | First public release. 53 | Improvements to documentation. 54 | 55 | ## 0.2.0 - 9 Nov 2015 56 | 57 | Rename "categories" to "sets". 58 | Fix a bug where a dependencies.edn file more than one directory above 59 | the project root directory caused an exception. 60 | 61 | ## 0.1.0 - 6 Nov 2015 62 | 63 | Initial release. 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # shared-deps 2 | 3 | [![Clojars Project](http://clojars.org/walmartlabs/shared-deps/latest-version.svg)](http://clojars.org/walmartlabs/shared-deps) 4 | 5 | If you've worked with projects with *large* numbers of subprojects 6 | (using the [lein-sub](https://github.com/kumarshantanu/lein-sub) plugin), 7 | you may have noticed that coordinating your dependencies can get a bit 8 | cumbersome. On one of our main projects, we have 70 sub-projects. 9 | Upgrading to the latest release of, say, core.async, involves too 10 | much global search and replace ... it is not *Don't Repeat Yourself*. 11 | 12 | With this plugin, you can define *sets* of dependencies, 13 | and store them across all-sub modules in a single `dependencies.edn` file 14 | at the root of your project. 15 | 16 | Each sub-module must include the shared-deps plugin, and specify 17 | a :dependency-sets key, a list of dependency set ids. 18 | 19 | `dependencies.edn` contains a single map: from dependency set id 20 | to a vector of dependencies for that dependency set. 21 | 22 | More than just coordinating artifact versions 23 | is the challenge of maintaining the nest of exclusion rules 24 | that can occur when trying to mix and match various third party 25 | dependencies and their individual transitive dependencies. 26 | Having a DRY solution here keeps the `project.clj` files very concise 27 | and readable. 28 | 29 | **NOTE:** We've recently discovered that the plugin is 30 | [not compatible with Leiningen checkouts](https://github.com/walmartlabs/shared-deps/issues/3). 31 | This is an outstanding issue. 32 | 33 | ## Example 34 | 35 | Your `dependencies.edn` file contains the following: 36 | 37 | ```clojure 38 | {:clojure [[org.clojure/clojure "1.7.0"]] 39 | :core.async [[org.clojure/core.async "0.2.371"]] 40 | :speclj [[speclj "3.3.1"]] 41 | :cljs [[org.clojure/clojurescript "1.7.170"]]} 42 | ``` 43 | 44 | Each dependency set can define any number of dependencies. 45 | These dependencies are simply 46 | appended to the standard list of dependencies provided 47 | in `project.clj`. 48 | 49 | Each dependency in a dependency set is *non-transitive*. 50 | 51 | A sub-project may define dependencies on some or all of these: 52 | 53 | ```clojure 54 | (defproject my-app "0.1.0-SNAPSHOT" 55 | :dependencies [[org.clojure/core.match "0.2.2"]] 56 | :dependency-sets [:clojure 57 | :core.async] 58 | :profiles {:dev {:dependency-sets [:speclj]}}) 59 | ``` 60 | 61 | The extra dependencies are available to the REPL, tests, or other plugins, exactly 62 | as if specified directly in `project.clj` traditionally. 63 | 64 | ## Extending Sets 65 | 66 | Say you notice that *everywhere* that you use ClojureScript (the :cljs dependency set) 67 | you are also using the :core.async dependency set. That can be expressed 68 | by changing `dependencies.edn`: 69 | 70 | ```clojure 71 | {:clojure [[org.clojure/clojure "1.7.0"]] 72 | :core.async [[org.clojure/core.async "0.2.371"]] 73 | :speclj [[speclj "3.3.1"]] 74 | :cljs {:extends [:core.async] 75 | :dependencies [[org.clojure/clojurescript "1.7.170"]]}} 76 | ``` 77 | 78 | At this point, if you specify the :cljs dependency set in a sub-project, 79 | you get not just the :cljs artifact dependencies, but the :core.async 80 | artifact dependencies 'for free'. 81 | 82 | The shared-deps plugin 83 | treats the :cljs dependency set as a dependency of the :core.async dependency set; this means that 84 | the :core.async artifact dependencies will be added first, and 85 | the :cljs artifact dependencies added later. 86 | 87 | ## Leveraging Sets 88 | 89 | You shouldn't think of a set as a way of just specifying a single dependency; 90 | you will likely find cases, as above with :cljs and :core.async, where you 91 | consistently use several artifacts together. 92 | 93 | We regularly have complex dependency sets with ids like :logging, :testing, 94 | or :database. 95 | 96 | ## Sibling Dependencies 97 | 98 | Large projects quite often contain modules that are themselves dependencies 99 | of other modules within the same umbrella project. In practice, this means that 100 | it is necessary to keep track of all the version numbers of the modules, 101 | or keep them in lock sync. 102 | 103 | The shared-deps plugin addresses this with automatic sibling dependencies. 104 | It builds a map of all sibling projects, keyed on the project name symbol. 105 | 106 | This is helpful as it is always up to date with the version number, also defined 107 | in each sibling module's `project.clj`. 108 | 109 | This works by reading the umbrella project's `project.clj`, and reading the 110 | :subs key (which is primarily used by the lein-sub plugin), then reading 111 | each sibling module's `project.clj`. 112 | 113 | Be aware that your `dependencies.edn` file may also have symbol keys (not 114 | just keywords), and 115 | those will _silently_ override any automatically generated sibling dependencies. 116 | 117 | ## Visualization 118 | 119 | The plugin adds a Leiningen task, **viz-deps**. 120 | The task will assemble the complete classpath for the project, including 121 | dependency sets, and generate a diagram file 122 | showing those dependencies. 123 | 124 | You must have [graphviz](http://www.graphviz.org) installed. 125 | 126 | ## Usage 127 | 128 | Put `[walmartlabs/shared-deps "0.2.8"]` into the `:plugins` vector of your `project.clj`. 129 | 130 | This must be done in *each* sub-module, and must *not* be in the top-level module 131 | (the one that has the `lein-sub` plugin). 132 | 133 | You can verify the behavior with `lein pprint :dependencies`; the output from 134 | this command will be the full list of dependencies, after the shared-deps plugin 135 | has processed all dependency sets in the active profiles. You will need 136 | to enable the lein-pprint plugin. 137 | 138 | ## License 139 | 140 | Copyright © 2015-2017 Walmart Labs 141 | 142 | Distributed under the Apache Software License 2.0. 143 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject walmartlabs/shared-deps "0.2.8" 2 | :description "Centralized declaration of dependencies for multi-projects" 3 | :url "https://github.com/walmartlabs/shared-deps" 4 | :license {:name "Apache Software License 2.0" 5 | :url "http://www.apache.org/licenses/LICENSE-2.0"} 6 | :eval-in-leiningen true 7 | :dependencies [[io.aviso/toolchest "0.1.3"] 8 | [dorothy "0.0.6"] 9 | ;; This version of dependency is compatible with 10 | ;; Clojure < 1.7. 11 | [com.stuartsierra/dependency "0.1.1"] 12 | [medley "0.6.0"]]) 13 | -------------------------------------------------------------------------------- /src/leiningen/viz_deps.clj: -------------------------------------------------------------------------------- 1 | (ns leiningen.viz-deps 2 | "Visualize your dependencies using Graphviz." 3 | (:use clojure.repl 4 | clojure.pprint) 5 | (:require [leiningen.core.main :as main] 6 | [shared-deps.plugin :as plugin] 7 | [io.aviso.toolchest.macros :refer [cond-let]] 8 | [clojure.java.browse :refer [browse-url]] 9 | [leiningen.core.classpath :as classpath] 10 | [dorothy.core :as d])) 11 | 12 | (defn- merge-project-data 13 | [profiles project key] 14 | (into (get project key) 15 | (mapcat #(get-in project [:profiles % key]) 16 | profiles))) 17 | 18 | (defn- dependency->label 19 | [dependency] 20 | (let [[artifact-name version] dependency] 21 | (format "%s%n%s" artifact-name version))) 22 | 23 | (defn gen-graph-id 24 | [k] 25 | (str (gensym (str (name k) "-")))) 26 | 27 | (defn- minimal-dependency 28 | "Reduces a dependency to just its first two values; symbol (for group and artifact) and 29 | version." 30 | [dependency] 31 | (if (= 2 (count dependency)) 32 | dependency 33 | (->> dependency (take 2) vec))) 34 | 35 | 36 | (defn- find-dependency-graph-id 37 | [graph dependency] 38 | (get-in graph [:deps (first dependency)])) 39 | 40 | (defn- add-edge 41 | [graph from-graph-id to-graph-id] 42 | (update-in graph [:edges] conj [from-graph-id to-graph-id])) 43 | 44 | (defn- add-node 45 | [graph node-id attributes] 46 | (assoc-in graph [:nodes node-id] (if (string? attributes) 47 | {:label attributes} 48 | attributes))) 49 | 50 | (defn- add-dependencies 51 | [graph source-graph-id target-dependencies] 52 | (->> target-dependencies 53 | (map minimal-dependency) 54 | distinct 55 | (reduce (fn [graph' dependency] 56 | (if-let [dep-graph-id (find-dependency-graph-id graph' dependency)] 57 | (add-edge graph' source-graph-id dep-graph-id) 58 | (let [dependency-key (first dependency) 59 | new-dependency-graph-id (gen-graph-id dependency-key)] 60 | (-> graph' 61 | (assoc-in [:deps dependency-key] new-dependency-graph-id) 62 | (add-node new-dependency-graph-id (dependency->label dependency)) 63 | (add-edge source-graph-id new-dependency-graph-id))))) 64 | graph))) 65 | 66 | (defn- add-root-dependencies 67 | [graph profiles project] 68 | (add-dependencies graph :root (merge-project-data profiles project :dependencies))) 69 | 70 | (defn- add-set 71 | [graph from-graph-id set-id shared-dependencies] 72 | (if (get-in graph [:sets set-id]) 73 | graph ; already present 74 | (let [set-graph-id (gen-graph-id set-id) 75 | set-dependencies (get-in shared-dependencies [set-id :dependencies]) 76 | set-extends (get-in shared-dependencies [set-id :extends]) 77 | graph' (-> graph 78 | (assoc-in [:sets set-id] set-graph-id) 79 | (add-node set-graph-id {:label (str set-id) 80 | :shape :trapezium}) 81 | (add-edge from-graph-id set-graph-id) 82 | (add-dependencies set-graph-id set-dependencies))] 83 | (reduce (fn [graph-a extended-set-id] 84 | (add-set graph-a set-graph-id extended-set-id shared-dependencies)) 85 | graph' 86 | set-extends)))) 87 | 88 | (defn- add-root-sets 89 | [graph profiles project shared-dependencies] 90 | (->> (merge-project-data profiles project :dependency-sets) 91 | distinct 92 | (reduce (fn [graph' set-id] 93 | (add-set graph' :root set-id shared-dependencies)) 94 | graph))) 95 | 96 | (defn- add-transitive-dependencies [graph project] 97 | (let [hierarchy (classpath/dependency-hierarchy :dependencies project)] 98 | (loop [graph graph 99 | queue (into [] hierarchy) 100 | processed #{}] 101 | (cond-let 102 | (empty? queue) 103 | graph 104 | 105 | [[[dependency transitives] & more-queue] queue 106 | dependency' (minimal-dependency dependency)] 107 | 108 | ;; May need to get the minimal of this 109 | (contains? processed dependency') 110 | (recur graph more-queue processed) 111 | 112 | :else 113 | (let [source-graph-id (find-dependency-graph-id graph dependency')] 114 | (recur (add-dependencies graph source-graph-id (keys transitives)) 115 | (concat more-queue transitives) 116 | (conj processed dependency))))))) 117 | 118 | (defn- dependency-graph 119 | "Builds out a structured dependency graph, from which a Dorothy node graph can be constructed." 120 | [{:keys [profiles project shared-dependencies]} merged-project] 121 | (let [root-dependency [(symbol (-> project :group str) (-> project :name str)) (:version project)]] 122 | ;; :nodes - map from node graph id to node attributes 123 | ;; :edges - list of edge tuples [from graph id, to graph id] 124 | ;; :sets - map from set id (a keyword or a symbol, typically) to a generated node graph id (a symbol) 125 | ;; :deps - map from artifact symbol (e.g., com.walmartlab/shared-deps) to a generated node graph id 126 | (-> {:nodes {:root {:label (dependency->label root-dependency) 127 | :shape :doubleoctagon}} 128 | :edges [] 129 | :sets {} 130 | :deps {}} 131 | (add-root-dependencies profiles project) 132 | (add-root-sets profiles project shared-dependencies) 133 | (add-transitive-dependencies merged-project)))) 134 | 135 | (defn- node-graph 136 | [dependency-graph] 137 | (reduce into 138 | [(d/graph-attrs {:rankdir :LR})] 139 | [(for [[k v] (:nodes dependency-graph)] 140 | [k v]) 141 | (:edges dependency-graph)])) 142 | 143 | (defn- build-dot 144 | [project] 145 | (-> (dependency-graph (plugin/extract-data project) project) 146 | node-graph 147 | d/digraph 148 | d/dot)) 149 | 150 | (def ^:private output-file "target/dependencies.pdf") 151 | 152 | (defn viz-deps 153 | "Generate a visualization of this project's dependencies. 154 | 155 | Normally, the output file is opened automatically; this can be 156 | prevented with the :no-view argument. " 157 | ([project] 158 | (viz-deps project nil)) 159 | ([project view] 160 | (when-not (#{nil ":no-view"} view) 161 | (main/warn "view should be omitted, or :no-view to avoid opening the generated dependency graph.")) 162 | 163 | (-> project 164 | build-dot 165 | (d/save! output-file {:format :pdf})) 166 | 167 | (main/info (format "Dependency graph saved to `%s'." output-file)) 168 | (when-not (= ":no-view" view) 169 | (browse-url output-file)))) 170 | -------------------------------------------------------------------------------- /src/shared_deps/plugin.clj: -------------------------------------------------------------------------------- 1 | (ns shared-deps.plugin 2 | "Modifies the project by reading the :dependency-sets key 3 | and merging in the appropriate dependencies specified in the 4 | dependencies.edn file." 5 | (:require [leiningen.core.main :as main] 6 | [leiningen.core.project :as project] 7 | [clojure.java.io :as io] 8 | [clojure.string :as str] 9 | [io.aviso.toolchest.macros :refer [cond-let]] 10 | [io.aviso.toolchest.metadata :refer [assoc-meta]] 11 | [clojure.edn :as edn] 12 | [medley.core :as medley] 13 | [com.stuartsierra.dependency :as d] 14 | [robert.hooke :as hooke] 15 | [leiningen.pom :as pom]) 16 | (:import (java.io PushbackReader File))) 17 | 18 | (defn ^:private project-name 19 | [{project-group :group 20 | project-name :name}] 21 | (if project-group 22 | (str project-group "/" project-name) 23 | project-name)) 24 | 25 | (defn ^:private find-dependencies-file 26 | [{root-path :root 27 | :as project}] 28 | (main/debug (format "Seaching for shared dependencies of project %s, starting in `%s'." (project-name project) root-path)) 29 | (loop [dir (io/file root-path)] 30 | (cond-let 31 | (nil? dir) 32 | (main/warn "Unable to find dependencies.edn file in directory `%s', or any parent directory." root-path) 33 | 34 | [f (io/file dir "dependencies.edn")] 35 | 36 | (.exists f) 37 | f 38 | 39 | :else 40 | (recur (.getParentFile dir))))) 41 | 42 | (defn ^:private add-exclusion 43 | [dependency] 44 | (let [[artifact-id artifact-version & options-pairs] dependency] 45 | (into [artifact-id artifact-version :exclusions '[*/*]] options-pairs))) 46 | 47 | (defn ^:private add-dependency-exclusions 48 | [dependencies] 49 | (mapv add-exclusion dependencies)) 50 | 51 | (def ^:private read-dependencies-file 52 | (memoize 53 | (fn [file] 54 | (main/debug (format "Reading shared dependencies from `%s'." file)) 55 | (with-open [s (io/reader file)] 56 | ;; Allow the values for each dependency set to be a vector, which is expanded 57 | ;; to a map with key :dependencies 58 | (let [dependency-defs (-> s 59 | PushbackReader. 60 | edn/read)] 61 | (->> dependency-defs 62 | (medley/map-vals 63 | #(if (vector? %) 64 | {:dependencies %} 65 | %)) 66 | (medley/map-vals 67 | #(update-in % [:dependencies] add-dependency-exclusions)))))))) 68 | 69 | (defn ^:private read-raw 70 | [^File f] 71 | (main/debug (format "Reading project file `%s'." f)) 72 | (try 73 | ;; Apparently, the path for read-raw has to be a string, not a File. 74 | (project/read-raw (.getAbsolutePath f)) 75 | (catch Throwable t 76 | (main/warn (.getMessage t) t)))) 77 | 78 | (def ^:private build-sibling-dependencies-map 79 | (memoize 80 | (fn [root-dir root-project-file] 81 | (let [root-project (read-raw root-project-file)] 82 | (->> root-project 83 | :sub 84 | (reduce (fn [deps module-path] 85 | (if-let [project (read-raw (io/file root-dir module-path "project.clj"))] 86 | (let [{project-name :name 87 | project-group :group 88 | version :version} project 89 | name-symbol (symbol project-group project-name)] 90 | (assoc-in deps [name-symbol :dependencies] 91 | ;; The value is just like an entry in the 92 | ;; dependencies.edn file: a vector of dependency 93 | ;; specs (each a vector). 94 | [[name-symbol version]])) 95 | deps)) 96 | {})))))) 97 | 98 | (defn ^:private build-sibling-project-dependencies 99 | "Starting from a sub-project, work upwards to the parent project.clj; 100 | from this, extract's the :sub key (normally used by the lein-sub plugin), 101 | then use this to read all the sibling project's project.clj. From this, a map 102 | from project name (symbol) to dependency vector is created, using each 103 | project's name and version." 104 | [{root-path :root}] 105 | (loop [dir (-> root-path io/file .getParentFile)] 106 | (cond-let 107 | (nil? dir) 108 | (main/warn "Unable to find containing project's project.clj from directory `%s'." root-path) 109 | 110 | [f (io/file dir "project.clj")] 111 | 112 | (.exists f) 113 | (build-sibling-dependencies-map dir f) 114 | 115 | :else 116 | (recur (.getParentFile dir))))) 117 | 118 | (defn ^:private read-shared-dependencies 119 | [project] 120 | (let [sibling-dependencies (build-sibling-project-dependencies project) 121 | shared-dependencies (some-> (find-dependencies-file project) 122 | read-dependencies-file)] 123 | (merge sibling-dependencies shared-dependencies))) 124 | 125 | (defn ^:private ->id-list 126 | [ids] 127 | (->> ids 128 | (map str) 129 | sort 130 | (interleave (repeat "\n - ")) 131 | (apply str))) 132 | 133 | (defn ^:private pad-to [s width] 134 | (let [l (.length s)] 135 | (if (= l width) 136 | s 137 | (apply str s 138 | (repeat (- width l) " "))))) 139 | 140 | (def ^:private desired-width 141 | "This is somewhat arbitrary." 142 | 120) 143 | 144 | (defn ^:private ->long-id-list 145 | "Somewhat like [[->id-list]], but formats the results into three columns of equal widths." 146 | [ids] 147 | (let [sorted (->> ids 148 | (map str) 149 | sort) 150 | longest (->> sorted 151 | (map #(.length %)) 152 | (reduce max)) 153 | id-count (count sorted) 154 | columns (-> (/ desired-width (+ 2 longest)) 155 | Math/floor 156 | long) 157 | rows (-> id-count 158 | (/ columns) 159 | Math/ceil 160 | long)] 161 | (with-out-str 162 | (doseq [row (range 0 rows) 163 | col (range 0 columns) 164 | :let [i (+ (* col rows) row)] 165 | :when (< i (dec id-count)) 166 | :let [id (nth sorted i)]] 167 | (when (and (pos? row) 168 | (zero? col)) 169 | (println)) 170 | (print " " 171 | (if (= col (dec columns)) 172 | id 173 | (pad-to id longest))))))) 174 | 175 | (defn ^:private report-unknown-dependencies 176 | [project unknown-ids shared-dependencies] 177 | (let [n (count unknown-ids)] 178 | (when (pos? n) 179 | (main/warn 180 | (apply str 181 | (format "Dependency error in project `%s':\n" 182 | (project-name project)) 183 | (if (= (count unknown-ids) 1) 184 | (format "The dependency id `%s' is not defined." (first unknown-ids)) 185 | (str "The following dependency ids are not defined:" 186 | (->id-list unknown-ids))) 187 | "\nAvailable dependency ids:\n" 188 | (-> shared-dependencies keys ->long-id-list)))))) 189 | 190 | (defn ^:private order-sets-by-dependency 191 | "Builds a graph of the dependencies of the sets, used to order 192 | them when creating artifact dependencies in the project map. 193 | 194 | Each set may have an :extends key in the shared dependencies map. 195 | This is is used to set dependency set ordering. 196 | 197 | Returns an ordered seq of set names reflecting dependencies and 198 | additional dependency sets added due to :extends." 199 | [project shared-dependencies set-ids] 200 | (loop [set-queue set-ids 201 | unknown-ids #{} 202 | visited? #{} 203 | graph (d/graph)] 204 | (cond-let 205 | 206 | (empty? set-queue) 207 | (do 208 | (report-unknown-dependencies project unknown-ids shared-dependencies) 209 | (->> graph 210 | d/topo-sort 211 | (remove nil?))) 212 | 213 | [[set-id & more-set-ids] set-queue] 214 | 215 | (visited? set-id) 216 | (recur more-set-ids unknown-ids visited? graph) 217 | 218 | ;; this set name has been visited; this is true even 219 | ;; if the set name is invalid (not present in the shared dependencies). 220 | [visited?' (conj visited? set-id) 221 | dependency-set (get shared-dependencies set-id)] 222 | 223 | (nil? dependency-set) 224 | (recur more-set-ids 225 | (conj unknown-ids set-id) 226 | visited?' 227 | graph) 228 | 229 | [extends (:extends dependency-set)] 230 | 231 | (nil? extends) 232 | (recur more-set-ids 233 | unknown-ids 234 | visited?' 235 | (d/depend graph set-id nil)) 236 | 237 | :else 238 | (recur (into more-set-ids extends) 239 | unknown-ids 240 | visited?' 241 | (reduce #(d/depend %1 set-id %2) graph extends))))) 242 | 243 | (defn ^:private apply-set 244 | [project set-id shared-dependencies dependencies-ks] 245 | (update-in project dependencies-ks 246 | into (get-in shared-dependencies [set-id :dependencies]))) 247 | 248 | (def ^:private vec-distinct (comp vec distinct)) 249 | 250 | (require '[clojure.pprint :refer [pprint]]) 251 | (defn ^:private apply-sets 252 | [project profile shared-dependencies] 253 | (let [base-ks (if profile 254 | [:profiles profile] 255 | []) 256 | dependencies-ks (conj base-ks :dependencies) 257 | sets-ks (conj base-ks :dependency-sets) 258 | ;; By the time the middleware is invoked, the :dependency-sets key has 259 | ;; been merged. We want to see the raw version, from before profiles 260 | ;; are merged. However, from the parent project's sub task, :without-profiles 261 | ;; metadata can be missing, so just use the raw project in that case. 262 | without-profiles (-> project meta (get :without-profiles project)) 263 | sets (get-in without-profiles sets-ks)] 264 | (if (seq sets) 265 | (do 266 | (main/debug (format "Applying dependency sets %s (from %s profile) to project %s." 267 | (str/join ", " sets) 268 | (or profile "default") 269 | (project-name project))) 270 | (let [project' (-> (reduce #(apply-set %1 %2 shared-dependencies dependencies-ks) 271 | ;; Since order counts, and what we get is usually some flavor of list (or lazy 272 | ;; seq), convert dependencies into a vector first. 273 | (update-in project dependencies-ks vec) 274 | (order-sets-by-dependency project shared-dependencies sets)) 275 | ;; There's any number of ways that we can end up with duplicated lines, 276 | ;; so we clean the dependencies. 277 | (update-in dependencies-ks vec-distinct)) 278 | dependencies+ (get-in project' dependencies-ks) 279 | without-profiles+ (update without-profiles dependencies-ks into dependencies+)] 280 | ;; Rewrite the :without-profiles version of the project as if the dependency set 281 | ;; dependencies were there originally; this is necessary to make things work correctly 282 | ;; when using the pom task. 283 | (vary-meta project' assoc :without-profiles without-profiles+))) 284 | project))) 285 | 286 | (defn middleware 287 | "The middleware invoked by Leiningen, to extend and modify 288 | the project description. This middleware is triggered by the :dependency-sets 289 | key, and modifies the :dependencies key." 290 | [project] 291 | (let [profiles (->> project meta :included-profiles (filter keyword?) distinct)] 292 | (if-let [shared-dependencies (read-shared-dependencies project)] 293 | (do 294 | (main/debug (format 295 | "Adding shared dependencies for project %s with profiles %s." 296 | (project-name project) 297 | (str/join ", " profiles))) 298 | (let [project' (reduce (fn [project' profile] 299 | (apply-sets project' profile shared-dependencies)) 300 | (-> project meta :without-profiles) 301 | (into [nil] profiles)) 302 | ;; Ok, now we need to "fold" each profile's dependencies into 303 | ;; the main :dependencies; this replicates what is normally done 304 | ;; when normalizing a profile before the middleware is invoked. 305 | combined-dependencies (reduce into 306 | (:dependencies project') 307 | (map #(get-in project' [:profiles % :dependencies]) 308 | profiles))] 309 | (-> project 310 | ;; The above may (possibly) introduce some duplication: 311 | (assoc :dependencies (-> combined-dependencies distinct vec)) 312 | (assoc-meta ::data 313 | {:profiles profiles 314 | :project project 315 | :shared-dependencies shared-dependencies})))) 316 | ;; Case where dependencies file not found: 317 | project))) 318 | 319 | (defn extract-data 320 | "Extracts data from the project (placed there by the middleware) that defines the profiles, 321 | raw project, and shared dependencies." 322 | [project] 323 | (-> project meta ::data)) 324 | 325 | ;; This is necessary to work around a problem with the pom task; it captures 326 | ;; the original project, but in our case, that's not quite what we want. 327 | ;; Without this hook, we end up with all profile dependencies (including 328 | ;; :dev and :test) as full dependencies in the pom.xml. 329 | ;; However, this is not perfect as it leaves some built-in dependencies on 330 | ;; org.clojure/tools.nrepl and clojure-complete in the output pom.xml. 331 | 332 | (hooke/add-hook #'pom/make-pom 333 | (fn [f project & rest] 334 | (apply f (-> project meta :without-profiles) rest))) 335 | --------------------------------------------------------------------------------