├── .gitignore ├── LICENSE ├── README.md ├── deps.edn ├── pom.xml ├── resources ├── .keep └── usage.txt ├── snowball-print.png ├── snowball.png └── src └── com └── phronemophobic └── snowball.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | *.jar 5 | *.class 6 | /.calva/output-window/ 7 | /.cpcache 8 | /.lein-* 9 | /.lsp/sqlite*.db 10 | /.nrepl-history 11 | /.nrepl-port 12 | /.rebel_readline_history 13 | /.socket-repl-port 14 | .hgignore 15 | .hg/ 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snowball 2 | 3 | View the sizes of your dependencies. 4 | 5 | Built with [membrane](https://github.com/phronmophobic/membrane). 6 | 7 | ### Treemap 8 | 9 | ```sh 10 | clojure -X:snowball :lib thheller/shadow-cljs :view treemap-image 11 | ``` 12 | ![shadow-cljs](/snowball.png?raw=true) 13 | 14 | ### Table in terminal 15 | 16 | ```sh 17 | clojure -X:snowball :lib thheller/shadow-cljs :view print | head -12 18 | ``` 19 | ```sh 20 | namespace | name | transitive-size | self-size 21 | ------------------------------------------------------------------------------------------------- 22 | org.graalvm.js | js | 43,361,897 | 18,305,220 23 | com.ibm.icu | icu4j | 13,298,680 | 13,298,680 24 | com.google.javascript | closure-compiler-unshaded | 13,008,835 | 13,008,835 25 | org.graalvm.truffle | truffle-api | 8,362,026 | 8,362,026 26 | org.clojure | google-closure-library | 5,970,746 | 5,970,746 27 | org.clojure | clojurescript | 4,892,746 | 4,892,746 28 | org.clojure | clojure | 4,539,616 | 3,914,649 29 | thheller | shadow-cljs | 78,811,694 | 2,988,201 30 | org.graalvm.regex | regex | 2,816,757 | 2,816,757 31 | io.undertow | undertow-core | 3,530,146 | 2,343,146 32 | ``` 33 | 34 | ## Usage 35 | 36 | Create an alias for snowball 37 | 38 | ```clojure 39 | 40 | { 41 | :aliases { 42 | ``` 43 | ```clojure 44 | :snowball 45 | {:exec-fn com.phronemophobic.snowball/-main 46 | :replace-deps {com.phronemophobic/snowball {:mvn/version "1.3"}}} 47 | ``` 48 | ```clojure 49 | } 50 | } 51 | ``` 52 | 53 | ## Examples 54 | 55 | 56 | Show the interactive treemap for the local deps file "deps.edn" 57 | 58 | ```sh 59 | clojure -X:snowball :deps deps.edn 60 | ``` 61 | 62 | Print the sizes for the local deps file "deps.edn" 63 | 64 | ```sh 65 | clojure -X:snowball :deps deps.edn :view print 66 | ``` 67 | 68 | Save a treemap image to "sizes.png" for the local deps file "deps.edn" 69 | 70 | ```sh 71 | clojure -X:snowball :deps deps.edn :view treemap-image :path sizes.png 72 | ``` 73 | 74 | Use a specific Maven version 75 | 76 | ```sh 77 | clojure -X:snowball :lib cnuernber/dtype-next :mvn/version '"9.011"' 78 | ``` 79 | 80 | Use a specific git version 81 | 82 | ```sh 83 | clojure -X:snowball :lib thheller/shadow-cljs :git/url '"https://github.com/thheller/shadow-cljs"' :git/sha '"46b73e161732d3a38a0c797119260775b78c8e93"' 84 | ``` 85 | 86 | Use a local root in the current directory "." 87 | 88 | ```sh 89 | clojure -X:snowball :lib my-local/lib :local/root . 90 | ``` 91 | 92 | 93 | ## Options 94 | 95 | ### Required 96 | 97 | Either `:deps` or `:lib` must be specified. 98 | 99 | `:lib`: The lib coordinate name. Example: `org.clojure/clojure`. 100 | `:deps`: The path to a `deps.edn` path. 101 | 102 | ### Optional 103 | 104 | `:view`: One of the following: 105 | * `treemap` (default): This will open a Swing window that shows a treemap of the dependencies. Area corresponds to size. 106 | * `treemap-image`: Save a treemap as an image to :path. Default :path is "snowball.png". 107 | * `print`: Prints a table of dependencies and sizes sorted by size. 108 | * `csv`: Prints a csv list of dependencies and sizes. 109 | * `edn`: Prints an edn map of dependencies and sizes. 110 | * `json`: Prints a json map of dependencies and sizes. 111 | * `treemap-json`: Prints a the treemap layout as json. See [description](https://github.com/phronmophobic/treemap-clj#rendering-your-own-treemap-layers). 112 | * `treemap-edn`: Prints a the treemap layout as edn. See [description](https://github.com/phronmophobic/treemap-clj#rendering-your-own-treemap-layers). 113 | 114 | `:path`: Specify a path when used with :view treemap-image. Ignored otherwise. 115 | 116 | #### Specifying a version 117 | 118 | You can use a specific version by providing one of the following. If no version is provided, then `{:mvn/version "RELEASE"}` is used. 119 | 120 | `:mvn/version`: mvn-version 121 | 122 | `:git/sha`: sha 123 | `:git/url`: url 124 | 125 | `:local/root`: root 126 | 127 | `:deps`: path-to-deps.edn 128 | 129 | ## Usage with lein projects 130 | 131 | Snowball doesn't work directly with lein projects, but it can be run against any mvn library so the workaround is something like: 132 | 133 | ```sh 134 | $ lein install 135 | Created /Users/adrian/workspace/membrane-re-frame-example/target/membrane-re-frame-example-0.1.0-SNAPSHOT.jar 136 | Wrote /Users/adrian/workspace/membrane-re-frame-example/pom.xml 137 | Installed jar and pom into local repo. 138 | # Note the project name and version 139 | $ clojure -X:snowball :lib membrane-re-frame-example :mvn/version '"0.1.0-SNAPSHOT"' 140 | ``` 141 | 142 | 143 | ## Related 144 | 145 | [tools.deps.graph](https://github.com/clojure/tools.deps.graph): A tool for making deps.edn dependency graphs. 146 | 147 | ## License 148 | 149 | Copyright © 2021 Adrian 150 | 151 | Distributed under the Eclipse Public License version 1.0. 152 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src" "resources"] 2 | :deps {org.clojure/clojure {:mvn/version "1.10.3"} 3 | io.github.clojure/tools.build {:git/tag "v0.7.5" :git/sha "34727f7"} 4 | org.clojure/tools.deps.alpha {:mvn/version "0.12.1148", :deps/manifest :mvn} 5 | org.clojure/data.json {:mvn/version "2.4.0"} 6 | com.phronemophobic/treemap-clj {:mvn/version "0.2.6" 7 | ;; :local/root "../treemap-clj" 8 | } 9 | com.phronemophobic/membrane {:mvn/version "0.10.1-beta" 10 | ;; :local/root "../membrane2" 11 | } 12 | } 13 | :aliases 14 | { 15 | :snowball {:exec-fn com.phronemophobic.snowball/-main 16 | :exec-args {}} 17 | 18 | :project 19 | {:extra-deps {com.phronemophobic.membrane/skialib-macosx-aarch64 20 | {:mvn/version "0.9.31.0-beta" 21 | ;;:local/root "../membrane2/csource/macos-aarch64" 22 | }}} 23 | 24 | :jar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}} 25 | :exec-fn hf.depstar/jar 26 | :exec-args {:jar "snowball.jar" :sync-pom true}} 27 | 28 | :deploy {:replace-deps {slipset/deps-deploy {:mvn/version "0.2.0"}} 29 | :exec-fn deps-deploy.deps-deploy/deploy 30 | :exec-args {:installer :remote :artifact "snowball.jar"}}}} 31 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.phronemophobic 5 | snowball 6 | 1.3 7 | com.phronemophobic/snowball 8 | View the sizes of your dependencies. 9 | https://github.com/com.phronemophobic/snowball 10 | 11 | 12 | Eclipse Public License 13 | http://www.eclipse.org/legal/epl-v10.html 14 | 15 | 16 | 17 | 18 | Adrian 19 | 20 | 21 | 22 | https://github.com/com.phronemophobic/snowball 23 | scm:git:git://github.com/com.phronemophobic/snowball.git 24 | scm:git:ssh://git@github.com/com.phronemophobic/snowball.git 25 | v1.3 26 | 27 | 28 | 29 | org.clojure 30 | data.json 31 | 2.4.0 32 | 33 | 34 | org.clojure 35 | clojure 36 | 1.10.3 37 | 38 | 39 | com.phronemophobic 40 | membrane 41 | 0.9.31.8-beta 42 | 43 | 44 | org.clojure 45 | tools.deps.alpha 46 | 0.12.1148 47 | 48 | 49 | com.phronemophobic 50 | treemap-clj 51 | 0.2.5 52 | 53 | 54 | 55 | src 56 | 57 | 58 | 59 | clojars 60 | https://repo.clojars.org/ 61 | 62 | 63 | 64 | 65 | clojars 66 | Clojars repository 67 | https://clojars.org/repo 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /resources/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phronmophobic/snowball/e1dccb3eb8d72915dc214b8f13149852e90e5bcc/resources/.keep -------------------------------------------------------------------------------- /resources/usage.txt: -------------------------------------------------------------------------------- 1 | Usage: 2 | clojure -X:snowball :lib lib-coord 3 | 4 | Snowball will display dependency sizes. 5 | 6 | --------------------------------------- 7 | Additional Args 8 | --------------------------------------- 9 | 10 | :view view 11 | 12 | There are 3 ways to view dependency sizes. 13 | The view can be selected with the :view option. 14 | 15 | The valid :view values are: 16 | 17 | :view treemap 18 | 19 | (default) 20 | This will open a Swing window that shows a treemap of the dependencies. 21 | Area corresponds to size. 22 | 23 | :view treemap-image 24 | 25 | Save a treemap as an image to :path. Default :path is "snowball.png". 26 | 27 | :view print 28 | 29 | Prints a table of dependencies and sizes sorted by size. 30 | 31 | :view csv 32 | 33 | Prints a csv list of dependencies and sizes. 34 | 35 | :view edn 36 | 37 | Prints an edn map of dependencies and sizes. 38 | 39 | :view json 40 | 41 | Prints an edn map of dependencies and sizes. 42 | 43 | :view treemap-json 44 | 45 | Prints a the treemap layout as json. See https://github.com/phronmophobic/treemap-clj#rendering-your-own-treemap-layers. 46 | 47 | :view treemap-edn 48 | 49 | Prints a the treemap layout as edn. See https://github.com/phronmophobic/treemap-clj#rendering-your-own-treemap-layers. 50 | 51 | :path image-path.png 52 | 53 | Specify a path when used with :view treemap-image. Ignored otherwise. 54 | 55 | -------------------------------------- 56 | Version Specification 57 | -------------------------------------- 58 | 59 | There are 4 ways to specify a specific lib version. 60 | If no version if is provided, {:mvn/version "RELEASE"} is used. 61 | 62 | :mvn/version mvn-version 63 | 64 | :git/sha sha 65 | :git/url url 66 | 67 | :local/root root 68 | 69 | :deps path-to-deps.edn 70 | 71 | -------------------------------------- 72 | Examples 73 | ------------------------------------- 74 | 75 | * Show the treemap for the local deps file "deps.edn" 76 | 77 | clojure -X:snowball :deps deps.edn 78 | 79 | * Print the sizes for the local deps file "deps.edn" 80 | 81 | clojure -X:snowball :deps deps.edn :view print 82 | 83 | * Save a treemap image to "sizes.png" for the local deps file "deps.edn" 84 | 85 | clojure -X:snowball :deps deps.edn :view treemap-image :path sizes.png 86 | 87 | * Use a specific Maven version 88 | 89 | clojure -X:snowball :lib cnuernber/dtype-next :mvn/version '"9.011"' 90 | 91 | * Use a specific git version 92 | 93 | clojure -X:snowball :lib thheller/shadow-cljs :git/url '"https://github.com/thheller/shadow-cljs"' :git/sha '"46b73e161732d3a38a0c797119260775b78c8e93"' 94 | 95 | * Use a local root in the current directory "." 96 | 97 | clojure -X:snowball :lib my-local/lib :local/root . 98 | 99 | -------------------------------------------------------------------------------- /snowball-print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phronmophobic/snowball/e1dccb3eb8d72915dc214b8f13149852e90e5bcc/snowball-print.png -------------------------------------------------------------------------------- /snowball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phronmophobic/snowball/e1dccb3eb8d72915dc214b8f13149852e90e5bcc/snowball.png -------------------------------------------------------------------------------- /src/com/phronemophobic/snowball.clj: -------------------------------------------------------------------------------- 1 | (ns com.phronemophobic.snowball 2 | (:require [clojure.tools.build.api :as b] 3 | clojure.tools.deps.alpha 4 | [treemap-clj.core :as treemap] 5 | [clojure.zip :as z] 6 | [membrane.ui :as ui] 7 | [clojure.data.json :as json] 8 | ;; (require 'membrane.java2d) 9 | [treemap-clj.rtree :as rtree] 10 | [membrane.component :as component]) 11 | (:import com.github.davidmoten.rtree.geometry.Geometries 12 | com.github.davidmoten.rtree.RTree) 13 | (:gen-class)) 14 | 15 | 16 | (defn human-readable [size] 17 | (or (some (fn [[num suffix]] 18 | (when (>= size num) 19 | (let [coefficient (double (/ size num)) 20 | num-str (if (< coefficient 10) 21 | (format "%.1f" coefficient) 22 | (-> coefficient (Math/round) int))] 23 | (str num-str suffix)))) 24 | [[1e12 "T"] 25 | [1e9 "G"] 26 | [1e6 "M"] 27 | [1e3 "k"]]) 28 | (str size))) 29 | 30 | (defn overlaps? [rt [x y w h]] 31 | (-> (.search ^RTree rt (Geometries/rectangle 32 | (double x) (double y) 33 | (double (+ x w)) (double (+ y h)))) 34 | (.toBlocking) 35 | (.toIterable) 36 | seq)) 37 | 38 | 39 | 40 | (defn rects->absolute [tm] 41 | (let [zip (treemap/treezip tm)] 42 | (loop [zip zip] 43 | (if (z/end? zip) 44 | (z/root zip) 45 | (recur (-> zip 46 | (z/edit (fn [rect] 47 | (let [x (if (z/end? zip) 48 | 0 49 | (loop [x 0 50 | zip zip] 51 | (if-not zip 52 | x 53 | (recur (+ x (:x (z/node zip))) 54 | (z/up zip))))) 55 | y (if (z/end? zip) 56 | 0 57 | (loop [y 0 58 | zip zip] 59 | (if-not zip 60 | y 61 | (recur (+ y (:y (z/node zip))) 62 | (z/up zip)))))] 63 | (assoc rect 64 | :ax x 65 | :ay y)))) 66 | (z/next))))))) 67 | 68 | (defn render-labels [tm] 69 | (let [tm (rects->absolute tm) 70 | rects (reverse (sort-by #(-> % :obj :size) (tree-seq :children :children tm))) 71 | 72 | [rtree labels] 73 | (reduce 74 | (fn [[rt labels] rect] 75 | (let [label (ui/label 76 | (clojure.string/join "\n" 77 | [(human-readable (-> rect :obj (:size 0))) 78 | (-> rect :obj :name name)]) 79 | (ui/font "monospace" 12)) 80 | [w h] (ui/bounds label) 81 | x (:ax rect) 82 | y (:ay rect)] 83 | (if (overlaps? rt [x y w h]) 84 | [rt labels] 85 | [(rtree/add! rt {:x x 86 | :y y 87 | :w w 88 | :h h}) 89 | (conj labels 90 | (ui/with-color [1 1 1 0.2] 91 | [(ui/translate (inc x) (inc y) 92 | label) 93 | (ui/translate (inc x) (dec y) 94 | label) 95 | (ui/translate (dec x) (dec y) 96 | label) 97 | (ui/translate (dec x) (inc y) 98 | label)]) 99 | 100 | (ui/translate x y 101 | label) 102 | )]))) 103 | [(rtree/rtree) 104 | []] 105 | rects)] 106 | labels)) 107 | 108 | 109 | 110 | (defn coord-size [lib-tree {:keys [children paths] 111 | :as coord}] 112 | (transduce (comp (map clojure.java.io/file) 113 | (map #(.length %))) 114 | + 115 | 0 116 | paths)) 117 | 118 | (defn transitive-coord-size [lib-tree {:keys [children paths] 119 | :as coord}] 120 | (let [transitive-coords (tree-seq :children 121 | (fn [coord] 122 | (map #(get lib-tree %) (:children coord))) 123 | coord)] 124 | (transduce (map :size) 125 | + 126 | 0 127 | transitive-coords))) 128 | 129 | (defn top-level-coord? [coord] 130 | (-> coord :dependents nil?)) 131 | 132 | (defn top-level-deps [lib-tree] 133 | (->> lib-tree 134 | (keep (fn [[lib coord]] 135 | ;; based on clojure.tools.deps.alpha/print-tree 136 | ;; implementation for finding root deps 137 | (when (top-level-coord? coord) 138 | lib))))) 139 | 140 | (defn root-coord [lib-tree] 141 | (let [coord {:children (top-level-deps lib-tree) 142 | :name 'root/root 143 | ;; don't try co calculate root deps since 144 | ;; they're most likely unzipped 145 | :size 0 146 | :paths []}] 147 | (assoc coord 148 | :transitive-size (transitive-coord-size lib-tree coord)))) 149 | 150 | (defn basis->size-tree [basis] 151 | ;; if clojure.tools.deps.alpha/make-tree breaks, 152 | ;; check clojure.tools.deps.alpha/print-tree 153 | (let [tree (#'clojure.tools.deps.alpha/make-tree (:libs basis)) 154 | tree+names+sizes (into 155 | {} 156 | (map (fn [[k v]] 157 | (let [size (coord-size tree v)] 158 | [k (assoc v 159 | :name k 160 | :top-level (top-level-coord? v) 161 | :size-readable (human-readable size) 162 | :size size)]))) 163 | tree) 164 | tree+names+sizes+transitive-sizes 165 | (into 166 | {} 167 | (map (fn [[k v]] 168 | (let [transitive-size (transitive-coord-size tree+names+sizes v)] 169 | [k (assoc v 170 | :transitive-size transitive-size 171 | :transitive-size-readable (human-readable transitive-size))]))) 172 | tree+names+sizes)] 173 | tree+names+sizes+transitive-sizes)) 174 | 175 | (defn render-depth 176 | "Draw filled rectangles of leaf rects 177 | with colors corresponding to the depth." 178 | ([rect] 179 | (render-depth rect 0.2)) 180 | ([rect opacity] 181 | (let [mdepth (treemap/max-depth rect) 182 | color-gradient (requiring-resolve 'treemap-clj.view/color-gradient)] 183 | (loop [to-visit (seq [[0 0 0 rect]]) 184 | view []] 185 | (if to-visit 186 | (let [[depth ox oy rect] (first to-visit) 187 | to-visit (if-let [children (:children rect)] 188 | (let [ox (+ ox (:x rect)) 189 | oy (+ oy (:y rect))] 190 | (into (next to-visit) 191 | (map #(vector (inc depth) ox oy %) children))) 192 | (next to-visit))] 193 | (recur to-visit 194 | (conj view 195 | (let [children? (:children rect) 196 | opacity (if children? 197 | ;;(max 0.2 (- 0.8 (* depth 0.2))) 198 | 0.2 199 | opacity) 200 | style (if children? 201 | :membrane.ui/style-stroke 202 | :membrane.ui/style-fill)] 203 | (ui/with-style style 204 | (ui/with-stroke-width 2 205 | (ui/translate (+ (:x rect) ox) (+ (:y rect) oy) 206 | [(ui/with-color (conj (if children? 207 | [0 0 0] 208 | (color-gradient (/ depth mdepth)) 209 | ) 210 | opacity) 211 | (ui/rectangle (max 1 (dec (:w rect))) 212 | (max 1 (dec (:h rect)))) 213 | )])))))) 214 | ) 215 | view))))) 216 | 217 | (defn basis->treemap [basis] 218 | (let [lib-tree (basis->size-tree basis) 219 | tm (treemap/treemap (root-coord lib-tree) 220 | (treemap/make-rect 600 600) 221 | (merge 222 | treemap/treemap-options-defaults 223 | {:size (fn [coord] 224 | (max 1 (:transitive-size coord) )) 225 | :keypath-fn 226 | (fn [coord] 227 | (cons 228 | '(find self) 229 | (map #(list 'find %) 230 | (:children coord)))) 231 | :branch? #(-> % :children seq) 232 | :children (fn [coord] 233 | (cons 234 | {:name (symbol 235 | (namespace (:name coord)) 236 | (str "self:" 237 | (name (:name coord)))) 238 | :size (:size coord) 239 | :transitive-size (:size coord)} 240 | (->> coord 241 | :children 242 | (map #(get lib-tree %)))))}))] 243 | tm)) 244 | 245 | (defn render-treemap [tm] 246 | (let [rendered [(render-depth tm) 247 | (render-labels tm)]] 248 | ((requiring-resolve 'treemap-clj.view/wrap-treemap-events) tm rendered))) 249 | 250 | (defn size-treemap [basis] 251 | ((requiring-resolve 'membrane.java2d/run-sync) 252 | (component/make-app (requiring-resolve 'treemap-clj.view/treemap-explore) {:tm-render (-> (basis->treemap basis) 253 | (render-treemap))}) 254 | {:window-start-width 1350 255 | :window-start-height 700 256 | :window-title "Snowball"})) 257 | 258 | (defn treemap-image [basis fname] 259 | ((requiring-resolve 'membrane.java2d/save-to-image!) 260 | fname 261 | (-> (basis->treemap basis) 262 | (render-treemap))) 263 | (println "Saved to " fname ".")) 264 | 265 | (defn treemap-edn [basis] 266 | (binding [*print-length* false] 267 | (prn (clojure.walk/prewalk 268 | (fn [obj] 269 | (if (record? obj) 270 | (into {} obj) 271 | obj)) 272 | (basis->treemap basis))))) 273 | 274 | (defn treemap-json [basis] 275 | (json/write (basis->treemap basis) *out*)) 276 | 277 | (defn opts->basis [{version :mvn/version 278 | sha :git/sha 279 | url :git/url 280 | root :local/root 281 | deps :deps 282 | 283 | lib :lib}] 284 | (when (not deps) 285 | (assert lib "Lib coordinate is required")) 286 | (when sha 287 | (assert url ":git/sha provided, but :git/url not provided")) 288 | (when url 289 | (assert sha ":git/url provided, but :git/sha not provided")) 290 | (let [deps (cond 291 | version {:deps {lib {:mvn/version (name version)}}} 292 | sha {:deps {lib {:git/sha (name sha) 293 | :git/url (name url)}}} 294 | root {:deps {lib {:local/root (name root)}}} 295 | deps (name deps) 296 | :else {:deps {lib {:mvn/version "RELEASE"}}})] 297 | (b/create-basis {:project deps}))) 298 | 299 | (defn print-sizes [basis] 300 | (let [lib-tree (basis->size-tree basis) 301 | top-libs (->> lib-tree ;; (select-keys lib-tree (top-level-deps lib-tree)) 302 | vals 303 | (sort-by :size) 304 | reverse) 305 | max-namespace-width (->> top-libs 306 | (map :name) 307 | (map namespace) 308 | (map count) 309 | (apply max)) 310 | max-name-width (->> top-libs 311 | (map :name) 312 | (map name) 313 | (map count) 314 | (apply max)) 315 | header (clojure.string/join " | " [(format (str "%" max-namespace-width "s") "namespace") 316 | (format (str "%" max-name-width "s") "name") 317 | (format (str "%13s") "transitive-size") 318 | (format (str "%13s") "self-size")]) 319 | ] 320 | (println header) 321 | (println (clojure.string/join (repeat (count header) "-" ))) 322 | (doseq [{:keys [transitive-size size name]} top-libs] 323 | (println (clojure.string/join " | " [(format (str "%" max-namespace-width "s") (namespace name)) 324 | (format (str "%" max-name-width "s") (clojure.core/name name)) 325 | (format (str "%,15d") transitive-size) 326 | (format (str "%,13d") size)]))))) 327 | 328 | (defn print-csv [basis] 329 | (let [lib-tree (basis->size-tree basis) 330 | top-libs (->> lib-tree 331 | vals 332 | (sort-by :size) 333 | reverse) 334 | columns [ 335 | ["namespace" #(-> % :name namespace)] 336 | ["name" #(-> % :name name)] 337 | ["transitive-size" :transitive-size] 338 | ["transitive-size-readable" :transitive-size-readable] 339 | ["self-size" :size] 340 | ["self-size-readable" :size-readable]] 341 | ] 342 | (println (clojure.string/join "," (map first columns))) 343 | (doseq [lib top-libs] 344 | (println (clojure.string/join "," (map (fn [[_column-name f]] 345 | (str (f lib))) 346 | columns)))))) 347 | 348 | (defn print-edn [basis] 349 | (binding [*print-length* false] 350 | (prn (basis->size-tree basis)))) 351 | 352 | (defn print-json [basis] 353 | (json/write (basis->size-tree basis) *out*)) 354 | 355 | (defn print-usage [] 356 | (print (slurp ((requiring-resolve 'clojure.java.io/resource) "usage.txt")))) 357 | 358 | (defn -main [{:keys [view path lib deps] 359 | :or {view :treemap 360 | path "snowball.png"} 361 | :as m}] 362 | (if (or lib deps) 363 | (case (name view) 364 | "treemap" (size-treemap (opts->basis m)) 365 | "treemap-image" (treemap-image (opts->basis m) (str path)) 366 | "print" (print-sizes (opts->basis m)) 367 | "csv" (print-csv (opts->basis m)) 368 | "edn" (print-edn (opts->basis m)) 369 | "json" (print-json (opts->basis m)) 370 | "treemap-edn" (treemap-edn (opts->basis m)) 371 | "treemap-json" (treemap-json (opts->basis m)) 372 | ;; else 373 | (print-usage)) 374 | (print-usage)) 375 | 376 | ) 377 | --------------------------------------------------------------------------------