├── .VERSION_PREFIX ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bb.edn ├── bin └── proj ├── deps.edn ├── doc └── intro.md ├── pom.xml └── src └── garden_watcher ├── core.clj ├── def.clj └── main.clj /.VERSION_PREFIX: -------------------------------------------------------------------------------- 1 | 1.0 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml.asc 5 | *.jar 6 | *.class 7 | /.lein-* 8 | /.nrepl-port 9 | .hgignore 10 | .hg/ 11 | .cpcache 12 | .store 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Unreleased 2 | 3 | ## Added 4 | 5 | ## Fixed 6 | 7 | ## Changed 8 | 9 | # 1.0.45 (2021-09-13 / aba992d) 10 | 11 | ## Added 12 | 13 | - Support for `hawk` options 14 | 15 | ## Changed 16 | 17 | - Make `new-garden-watcher` function accept optional argument with `hawk` options. 18 | Example of using `hawk` options can be found in [Polling Watches](https://github.com/wkf/hawk#polling-watches). 19 | 20 | # 1.0.36 (2021-01-14 / 4628d5b) 21 | 22 | ## Changed 23 | 24 | - Update dependencies 25 | - com.stuartsierra/component {:mvn/version "0.4.0"} -> {:mvn/version "1.0.0"} 26 | - org.clojure/java.classpath {:mvn/version "0.3.0"} -> {:mvn/version "1.0.0"} 27 | - garden {:mvn/version "1.3.9"} -> {:mvn/version "1.3.10"} 28 | 29 | # 1.0.33 (2021-01-14 / 777c23b) 30 | 31 | ## Added 32 | 33 | - Support for *.cljc files 34 | 35 | ## Changed 36 | 37 | - Make namespace to file path conversion (and vice versa) more robust 38 | 39 | # 1.0.27 (2020-10-30 / 634bccc) 40 | 41 | ## Fixed 42 | 43 | - Make classpath detection more robust 44 | 45 | # [0.3.5] - 2019-11-09 46 | 47 | ## Fixed 48 | 49 | - Fixed order or `start-garden-watcher!` and `compile-garden-namespaces` 50 | 51 | # [0.3.4] - 2019-11-08 52 | 53 | ## Changed 54 | 55 | - Upgrade dependencies 56 | 57 | ## Added 58 | 59 | - `start-garden-watcher!` / `stop-garden-watcher!`, for people using e.g. 60 | Integrant instead of Component 61 | 62 | # [0.3.3] - 2018-10-15 63 | 64 | ## Changed 65 | 66 | - Upgrade dependencies 67 | 68 | # [0.3.2] - 2017-08-14 69 | 70 | ## Changed 71 | - Remove second println when stopping the component 72 | 73 | # [0.3.1] - 2017-03-09 74 | 75 | ## Fixed 76 | - Added missing dependency: `org.clojure/java.classpath` 77 | 78 | # [0.3.0] - 2017-02-10 79 | 80 | ## Changed 81 | - Made component idempotent 82 | 83 | # [0.2.0] - 2016-12-29 84 | 85 | ## Changed 86 | - Renamed to `lambdaisland/garden-watcher` 87 | 88 | # 0.1.0 - 2016-12-29 89 | - Initial version, released as `lambdaisland/garden-reloader` 90 | 91 | [Unreleased]: https://github.com/plexus/garden-watcher/compare/v0.3.5...HEAD 92 | [0.3.5]: https://github.com/plexus/garden-watcher/compare/v0.3.4...v0.3.5 93 | [0.3.4]: https://github.com/plexus/garden-watcher/compare/v0.3.3...v0.3.4 94 | [0.3.3]: https://github.com/plexus/garden-watcher/compare/v0.3.2...v0.3.3 95 | [0.3.2]: https://github.com/plexus/garden-watcher/compare/v0.3.1...v0.3.2 96 | [0.3.1]: https://github.com/plexus/garden-watcher/compare/v0.3.0...v0.3.1 97 | [0.3.0]: https://github.com/plexus/garden-watcher/compare/v0.2.0...v0.3.0 98 | [0.2.0]: https://github.com/plexus/garden-watcher/compare/v0.1.0...v0.2.0 -------------------------------------------------------------------------------- /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 tocontrol, 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 | # Garden reloader component 2 | 3 | Watch your [Garden](https://github.com/noprompt/garden) stylesheets for changes, 4 | and compiles them to CSS. 5 | 6 | ## Quickstart 7 | 8 | Add `lambdaisland/garden-watcher` as a dependency in `deps.edn` (Clojure CLI), 9 | `project.clj` (Leiningen) or `build.boot` (Boot). 10 | 11 | ``` 12 | ;; deps.edn 13 | {lambdaisland/garden-watcher {:mvn/version "1.0.45"}} 14 | 15 | ;; project.clj/build.boot 16 | [lambdaisland/garden-watcher "1.0.45"] 17 | ``` 18 | 19 | Create vars containing Garden-style declarations, and add a `^:garden` metadata 20 | to the var. 21 | 22 | ``` clojure 23 | (ns sesame.styles) 24 | 25 | (def ^:garden main 26 | (list 27 | [:h1 {:color "blue"}] 28 | [:h2 {:color "green"}] 29 | [:h3 {:color "red"}])) 30 | ``` 31 | 32 | Now you start garden-watcher, passing it your namespace name, and it will 33 | recompile the stylesheet and create a `main.css` file whenever the namespace 34 | changes. You can start garden-watcher in several ways. For more metadata options 35 | (e.g. to control the output file) and a convenience macro (`defstyles`), see 36 | below. 37 | 38 | ### Use with `garden-watcher.main` 39 | 40 | There's a main namespace that's convenient if you want to simply run the watcher 41 | as its own process. You pass one or more namespace names as arguments. 42 | 43 | ``` 44 | ;; leiningen 45 | lein run -m garden-watcher.main name.of.your.namespace 46 | 47 | ;; clojure CLI 48 | clj -m garden-watcher.main name.of.your.namespace 49 | ``` 50 | 51 | ### Use as a library 52 | 53 | There are start/stop functions so you can hook this up however you like. Useful 54 | for use with Integrant, Mount, etc. 55 | 56 | ``` clojure 57 | (require '[garden-watcher.core :as gw]) 58 | 59 | (def watcher (gw/start-garden-watcher! '[name.of.namespace name.of.other.namespace])) 60 | (gw/stop-garden-watcher! watcher) 61 | ``` 62 | 63 | ### Use as a Component 64 | 65 | If you're using `com.stuartsierra.component` then you can use the built-in 66 | component directly. 67 | 68 | Use `garden-watcher.core/new-garden-watcher` to create the component, passing it 69 | a vector of namespace names. Once started this will watch and compile each var 70 | with a `:garden` metadata key in the given namespaces to a CSS file. 71 | Also, [hawk](https://github.com/wkf/hawk) options map can be passed as a second argument. 72 | 73 | ``` clojure 74 | (ns user 75 | (:require [com.stuartsierra.component :as component] 76 | [figwheel-sidecar.config :as fw-conf] 77 | [figwheel-sidecar.system :as fw-sys] 78 | [garden-watcher.core :refer [new-garden-watcher]])) ;; <------ 79 | 80 | (defn dev-system [] 81 | (component/system-map 82 | :figwheel-system (fw-sys/figwheel-system (fw-conf/fetch-config)) 83 | :css-watcher (fw-sys/css-watcher {:watch-paths ["resources/public/css"]}) 84 | :garden-watcher (new-garden-watcher '[sesame.styles]))) ;; <------ 85 | ``` 86 | 87 | The above will generate `resources/public/css/main.css`, and recreate it 88 | whenever `styles.clj` is saved. By combining it with Figwheel's CSS reloader you 89 | get instant reloading in the browser. 90 | 91 | ## Use of metadata 92 | 93 | Garden provides a macro, `garden.def/defstylesheet`, that allows you to pass 94 | compiler options to `garde.core/css`. By adding `:output-to` this allows writing 95 | stylesheets that "automatically" create the corresponding CSS files. When using 96 | garden-watcher you should *not* use `defstylesheet`. 97 | 98 | With `defstylesheet` the compile-to-CSS happens as a side effect of loading the 99 | namespace, which makes it impossible to load the namespace without generating 100 | the CSS file, or generating the CSS without reloading the namespace. Therefore 101 | `garden-watcher` chose a different approach. 102 | 103 | Instead create regular vars, adn tag them with a `:garden` metadata key. You can 104 | use a map to add compiler options, including `:output-to`. 105 | 106 | ``` clojure 107 | (def 108 | ^{:garden {:output-to "resources/public/styles.css" 109 | :vendors ["webkit"]}} 110 | my-stylesheet 111 | (list 112 | [:h1 {:color "blue"}])) 113 | ``` 114 | 115 | If you don't specify an output file, it defaults to `resources/public/css/{{var-name}}.css` 116 | 117 | ``` clojure 118 | ;; This creates resources/public/css/anja.css 119 | (def ^:garden anja 120 | (list 121 | [:h1 {:color "blue"}])) 122 | ``` 123 | 124 | Note that Garden provides `garden.def/defstyles` macro that allows you to get 125 | rid of the `(list ,,,)` (that's all that macro does), so this code is equivalent: 126 | 127 | ``` clojure 128 | (require '[garden.def :refer [defstyles]]) 129 | 130 | (defstyles ^:garden anja 131 | [:h1 {:color "blue"}]) 132 | ``` 133 | 134 | Or you can use `garden-watcher.def/defstyles`, which does the exact same thing, 135 | but automatically adds the `:garden` metadata, so this code is equivalent again: 136 | 137 | ``` clojure 138 | (require '[garden-watcher.def :refer [defstyles]]) ;; <-- different namespace 139 | 140 | (defstyles anja 141 | [:h1 {:color "blue"}]) 142 | ``` 143 | 144 | ## One-off Compiling to CSS 145 | 146 | To generate CSS files from these vars, use 147 | `garden-watcher.core/compile-garden-namespaces`: 148 | 149 | ``` clojure 150 | (compile-garden-namespaces '[sesame.styles]) 151 | ``` 152 | 153 | garden-watcher also includes a "main" entry point to make it easy to invoke 154 | this as a build step. 155 | 156 | ``` 157 | lein run -m garden-watcher.main sesame.styles 158 | ``` 159 | 160 | E.g. say you're building an uberjar containing compiled ClojureScript and CSS. 161 | 162 | ``` clojure 163 | (defproject sesame "0.1.0" 164 | ,,, 165 | :uberjar-name "sesame.jar" 166 | 167 | :profiles {,,, 168 | 169 | :uberjar 170 | {:prep-tasks ["compile" 171 | ["cljsbuild" "once" "min"] 172 | ["run" "-m" "garden-watcher.main" "sesame.styles"]] 173 | :omit-source true 174 | :aot :all}}) 175 | ``` 176 | 177 | ## CLJC support 178 | 179 | In order to share code with Clojurescript, `cljc` files are fully supported. 180 | However, note that priority is given to regular `clj` files when both kind 181 | exist for a given namespace. 182 | 183 | ## License 184 | 185 | Copyright © 2016-2019 Arne Brasseur 186 | 187 | Distributed under the Mozilla Public License 2.0 (https://www.mozilla.org/en-US/MPL/2.0/) 188 | -------------------------------------------------------------------------------- /bb.edn: -------------------------------------------------------------------------------- 1 | {:deps 2 | {lambdaisland/open-source {:git/url "https://github.com/lambdaisland/open-source" 3 | :git/sha "4f436dd93a2c27330d82850cdc8afeec0de9a53c"}} 4 | :min-bb-version "0.6.0"} 5 | -------------------------------------------------------------------------------- /bin/proj: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bb 2 | 3 | (ns proj (:require [lioss.main :as lioss])) 4 | 5 | (lioss/main 6 | {:license :mpl 7 | :inception-year 2016 8 | :description "Hot-reload your Garden stylesheets" 9 | :group-id "lambdaisland"}) 10 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src"] 2 | :deps {com.stuartsierra/component {:mvn/version "1.0.0"}, 3 | org.clojure/java.classpath {:mvn/version "1.0.0"}, 4 | hawk/hawk {:mvn/version "0.2.11"}, 5 | garden/garden {:mvn/version "1.3.10"}}} 6 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to garden-component 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | lambdaisland 5 | garden-watcher 6 | 1.0.45 7 | garden-watcher 8 | Hot-reload your Garden stylesheets 9 | https://github.com/lambdaisland/garden-watcher 10 | 2016 11 | 12 | Lambda Island 13 | https://lambdaisland.com 14 | 15 | 16 | 17 | MPL-2.0 18 | https://www.mozilla.org/media/MPL/2.0/index.txt 19 | 20 | 21 | 22 | https://github.com/lambdaisland/garden-watcher 23 | scm:git:git://github.com/lambdaisland/garden-watcher.git 24 | scm:git:ssh://git@github.com/lambdaisland/garden-watcher.git 25 | df2e726ad04435e7a006d405159fb75afbc37748 26 | 27 | 28 | 29 | com.stuartsierra 30 | component 31 | 1.0.0 32 | 33 | 34 | org.clojure 35 | java.classpath 36 | 1.0.0 37 | 38 | 39 | hawk 40 | hawk 41 | 0.2.11 42 | 43 | 44 | garden 45 | garden 46 | 1.3.10 47 | 48 | 49 | 50 | src 51 | 52 | 53 | src 54 | 55 | 56 | 57 | 58 | org.apache.maven.plugins 59 | maven-compiler-plugin 60 | 3.8.1 61 | 62 | 63 | org.apache.maven.plugins 64 | maven-jar-plugin 65 | 3.2.0 66 | 67 | 68 | 69 | df2e726ad04435e7a006d405159fb75afbc37748 70 | 71 | 72 | 73 | 74 | 75 | org.apache.maven.plugins 76 | maven-gpg-plugin 77 | 1.6 78 | 79 | 80 | sign-artifacts 81 | verify 82 | 83 | sign 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | clojars 93 | https://repo.clojars.org/ 94 | 95 | 96 | 97 | 98 | clojars 99 | Clojars repository 100 | https://clojars.org/repo 101 | 102 | 103 | -------------------------------------------------------------------------------- /src/garden_watcher/core.clj: -------------------------------------------------------------------------------- 1 | (ns garden-watcher.core 2 | (:require [clojure.java.io :as io] 3 | [clojure.java.classpath :as cp] 4 | [clojure.string :as str] 5 | [com.stuartsierra.component :as component] 6 | [garden.core :refer [css]] 7 | [hawk.core :as hawk]) 8 | (:import java.io.File)) 9 | 10 | (defn- -ns->path 11 | "Given a list of namespace symbols, returns a map of namespace symbol -> path of corresponding 12 | .clj or .cljc file." 13 | [sym-nses] 14 | (let [cp-dirs (into #{} 15 | (comp (filter #(.isDirectory %)) 16 | (map #(.getCanonicalPath %))) 17 | (concat (cp/classpath (clojure.lang.RT/baseLoader)) 18 | (cp/system-classpath)))] 19 | (reduce (fn [ns->paths sym-ns] 20 | (let [clj-path (str "/" 21 | (-> (str sym-ns) 22 | (str/replace "-" "_") 23 | (str/replace "." File/separator)) 24 | ".clj") 25 | path (first (sequence (comp (map (fn [cp-dir] 26 | (let [canonical-clj-path (str cp-dir 27 | clj-path)] 28 | (if (.exists (io/file canonical-clj-path)) 29 | canonical-clj-path 30 | (let [canonical-cljc-path (str canonical-clj-path 31 | "c")] 32 | (when (.exists (io/file canonical-cljc-path)) 33 | canonical-cljc-path)))))) 34 | (filter some?)) 35 | cp-dirs))] 36 | (cond-> 37 | ns->paths 38 | path 39 | (assoc sym-ns 40 | path)))) 41 | {} 42 | sym-nses))) 43 | 44 | (defn- -reload-and-compile! 45 | "Reloads the given namespace, then finds all vars with a :garden metadata in that 46 | namespace, and compiles those to CSS. The target path is either defined in the 47 | :garden metadata as :output-to, or it iss derived from the var name as 48 | resources/public/css/.css 49 | 50 | Throws when namespace is not found." 51 | [sym-ns] 52 | (require sym-ns :reload) 53 | (doseq [[sym var] (ns-publics sym-ns)] 54 | (when-let [garden-meta (-> var meta :garden)] 55 | (let [garden-meta (if (map? garden-meta) garden-meta {})] 56 | (let [target (:output-to garden-meta (str "resources/public/css/" sym ".css"))] 57 | (println (str "Garden: compiling #'" sym-ns "/" sym)) 58 | (io/make-parents target) 59 | (css (assoc garden-meta :output-to target) @var)))))) 60 | 61 | (defn- -garden-reloader-handler 62 | "Handler for Hawk which reloads a namespace when its corresponding source file changes." 63 | [path->ns _ctx event] 64 | (when (= (:kind event) :modify) 65 | (-reload-and-compile! (get path->ns 66 | (.getCanonicalPath (:file event)))))) 67 | 68 | (defn compile-garden-namespaces 69 | "Given a list of namespace symbols, reloads those namespaces, finds all 70 | definitiions with a :garden metadata key, and compiles them to CSS." 71 | [sym-nses] 72 | (run! -reload-and-compile! 73 | (keys (-ns->path sym-nses)))) 74 | 75 | (defn start-garden-watcher! 76 | "Starts a watcher which generates new CSS files when any source file associated with the given 77 | namespaces changes. 78 | See `compile-garden-namespaces`." 79 | [sym-nses hawk-options] 80 | (let [ns->path (-ns->path sym-nses)] 81 | (if (seq ns->path) 82 | (let [paths (vals ns->path)] 83 | (println "Garden: watching" (str/join ", " paths)) 84 | (run! -reload-and-compile! 85 | (keys ns->path)) 86 | (hawk/watch! hawk-options 87 | [{:handler (partial -garden-reloader-handler 88 | (into {} 89 | (map (fn [[sym-ns path]] 90 | [path sym-ns])) 91 | ns->path)) 92 | :paths paths}])) 93 | (println "No files found for the given namespaces")))) 94 | 95 | (defn stop-garden-watcher! [hawk] 96 | (hawk/stop! hawk) 97 | (println "Garden: stopped watching namespaces.")) 98 | 99 | (defrecord GardenWatcherComponent [namespaces hawk-options] 100 | component/Lifecycle 101 | (start [this] 102 | (if (:garden-watcher-hawk this) 103 | (do 104 | (println "Garden: watcher already running.") 105 | this) 106 | (assoc this :garden-watcher-hawk (start-garden-watcher! namespaces hawk-options)))) 107 | (stop [this] 108 | (if-let [hawk (:garden-watcher-hawk this)] 109 | (do 110 | (stop-garden-watcher! hawk) 111 | (dissoc this :garden-watcher-hawk)) 112 | (do 113 | (println "Garden: watcher not running") 114 | this)))) 115 | 116 | (defn new-garden-watcher 117 | "Create a new Sierra Component that watches the given namespaces for changes, 118 | and upon change compiles any definitions with a :garden metadata key to CSS." 119 | ([namespaces hawk-options] 120 | (->GardenWatcherComponent namespaces hawk-options)) 121 | ([namespaces] 122 | (new-garden-watcher namespaces {}))) 123 | -------------------------------------------------------------------------------- /src/garden_watcher/def.clj: -------------------------------------------------------------------------------- 1 | (ns garden-watcher.def 2 | (:require [garden.core])) 3 | 4 | (defmacro defstyles 5 | "Convenience macro equivalent to `(def name (list styles*))`. Equivalent to 6 | garden.def/defstyles but adds a :garden metadata key if it's not there 7 | already." 8 | [name & styles] 9 | `(def ~(vary-meta name #(merge {:garden true} %)) (list ~@styles))) 10 | -------------------------------------------------------------------------------- /src/garden_watcher/main.clj: -------------------------------------------------------------------------------- 1 | (ns garden-watcher.main 2 | (:gen-class) 3 | (:require [garden-watcher.core :refer [compile-garden-namespaces]])) 4 | 5 | (defn -main [& args] 6 | (compile-garden-namespaces (map symbol args))) 7 | --------------------------------------------------------------------------------