Skip to content

Commit

Permalink
Added an option to specify the builds as a hash-map with keys as the …
Browse files Browse the repository at this point in the history
…build ids instead of vectors. Also added a keyword `:output-dir` as an alternative to `:output-directory` in top-level Tornado setup.
  • Loading branch information
JanSuran03 committed Feb 22, 2023
1 parent 7b8405c commit 97efaae
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## 0.2.4
Added an option to specify the builds as a hash-map with keys as the build ids instead of vectors.

Also added a keyword `:output-dir` as an alternative to `:output-directory` in top-level Tornado setup.

## 0.2.3
Added an option for all stylesheets to share a common parent directory (via `:tornado {:output-directory "...", builds {...}, ...}`)

Expand Down
56 changes: 44 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@ A Clojure plugin for automatic compilation of [Tornado](https://github.com/JanSu

[![Clojars Project](https://img.shields.io/clojars/v/org.clojars.jansuran03/lein-tornado.svg)](https://clojars.org/org.clojars.jansuran03/lein-tornado)


# Tutorial

### Example Leiningen config:

```clojure
(defproject ...
...
:plugins [...
[org.clojars.jansuran03/lein-tornado "0.2.3"]]
[org.clojars.jansuran03/lein-tornado "0.2.4"]]
:tornado {:source-paths ["src/my-app/css"]
:output-directory "resources/public/css"
:builds {:main {:stylesheet my-app.css.core
:compiler {:pretty-print? false
:output-to "main.css"}}
{:foo {:source-paths ["src/my-app/css/foo"]
:stylesheet my-app.css.foo.core
:compiler {:output-to "foo.css"}}}
{:common {:source-paths ["src/my-app/common"]
:stylesheet my-app.common.css
:compiler {:output-to "common.css"
:indent-length 3}}}}
...)
```

The old (deprecated) setup is still working fine:

```clojure
(defproject ...
...
:plugins [...
[org.clojars.jansuran03/lein-tornado "0.2.4"]]
:tornado {:source-paths ["src/my-app/css"]
:output-directory "resources/public/css"
:builds [{:id :main
Expand All @@ -33,28 +55,38 @@ A Clojure plugin for automatic compilation of [Tornado](https://github.com/JanSu
...)
```

The `:source-paths` (if specified) is a vector of common source paths for all builds. Specifying `:source-paths` directly in a build
The `:source-paths` (if specified) is a vector of common source paths for all builds. Specifying `:source-paths`
directly in a build
configuration will override the tornado-global `:source-paths`.

The `:output-directory` (if specified) is where all compiled stylesheets will be saved. The `:compiler -> :output-to` value will be appended
to this common path. If `:output-directory` is specified, you cannot (yet) tell this Tornado if you only want to go for paths
without this tornado-global prefix for specific builds. If you'd like to add this option (or anything else), please create an issue.
The `:output-directory` (if specified) is where all compiled stylesheets will be saved. The `:compiler -> :output-to`
value will be appended
to this common path. If `:output-directory` is specified, you cannot (yet) tell this Tornado if you only want to go for
paths
without this tornado-global prefix for specific builds. If you'd like to add this option (or anything else), please
create an issue.

Each build must contain `:id`, `:stylesheet` and `:compiler -> :output-to`
- The `:id` is a key for Lein-tornado to know which build to compile. Even if you always compile all builds, you still have to specify the id.

- The `:id` is a key for Lein-tornado to know which build to compile. Even if you always compile all builds, you still
have to specify the id.
- The `:stylesheet` is a fully-qualified symbol of the stylesheet you would like to be compiled.
- If build-specific `:source-paths` are specified, they have higher priority over the global ones, however,
- if no source paths are specified, it is an error.
- The `:compiler` specifies the options for the Tornado compiler:
* `:output-path` is a relative path from the project root if no `:output-directory` is specified, otherwise a relative
path to that directory is used.
* `:pretty-print?` is an option whether you want to keep the CSS to be nicely formatted or compressed by the compiler.
* `:indent-length` is an option where you can specify the indent length if the CSS is pretty-printed.
* `:output-path` is a relative path from the project root if no `:output-directory` is specified, otherwise a
relative
path to that directory is used.
* `:pretty-print?` is an option whether you want to keep the CSS to be nicely formatted or compressed by the
compiler.
* `:indent-length` is an option where you can specify the indent length if the CSS is pretty-printed.

### There are several compilation options:

- `lein tornado once` - compiles all stylesheets once
- `lein tornado auto` - compiles all stylesheets, then watches for changes to compile them again
- `lein tornado release` - compiles all stylesheets once and compresses them regardless on the `:compiler -> :pretty-print?` option
- `lein tornado release` - compiles all stylesheets once and compresses them regardless on
the `:compiler -> :pretty-print?` option
- `lein tornado <command> main foo` - only builds with ids `:main` and `:foo` will be affected

## License
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.jansuran03/lein-tornado "0.2.3"
(defproject org.clojars.jansuran03/lein-tornado "0.2.4"
:description "A Clojure plugin for automatic compilation of Tornado stylesheets."
:url "https://github.com/JanSuran03/lein-tornado"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
39 changes: 21 additions & 18 deletions src/leiningen/tornado.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require [me.raynes.fs :as fs]
[leiningen.core.main :as lein]
[clojure.java.io :as io]
[clojure.pprint :as pp]
[leiningen.core.project :as project]
[leiningen.core.eval :refer [eval-in-project]]
[leiningen.help :as help]))
Expand All @@ -14,10 +13,28 @@
(apply lein/info args)
(lein/abort))

(defn- validate-builds
"For each build, validates id, stylesheet and source paths. Throws an exception if some builds are not valid."
[project builds]
(doseq [{:keys [id stylesheet source-paths compiler]} builds
:let [{:keys [output-to]} compiler]]
(cond (not ((some-fn keyword? string?) id)) (error "The build id must be a string or a keyword: " id)
(and (nil? source-paths) (nil? (:source-paths project))) (error (str "No source paths specified for a build: " (name id) "."
"Please specify global or build-specific source paths."))
(nil? stylesheet) (error "No stylesheet specified for a build: " (name id))
(not (symbol? stylesheet)) (error "Stylesheet value must be a symbol: " stylesheet " in a build: " (name id))
(nil? output-to) (error "No specified output path (:output-to) for a compiled CSS build: " (name id)))))

(defn- all-builds
"Returns a sequence of Tornado builds."
[project]
(-> project :tornado :builds))
(let [builds (-> project :tornado :builds)
builds (cond->> builds
(map? builds)
(map (fn [[build-id build]]
(assoc build :id build-id))))]
(validate-builds project builds)
builds))

(defn- find-builds
"For each of the given build ids, if each of the builds exists, returns a complete
Expand All @@ -31,19 +48,6 @@
build
(error "Unknown build id: " build-id)))))

(defn- validate-builds
"For each build, validates id, stylesheet and source paths. Throws an exception if some
of the builds are not valid."
[project]
(doseq [{:keys [id stylesheet source-paths compiler]} (all-builds project)
:let [{:keys [output-to]} compiler]]
(cond (not ((some-fn keyword? string?) id)) (error "The build id must be a string or a keyword: " id)
(and (nil? source-paths) (nil? (:source-paths project))) (error (str "No source paths specified for a build: " (name id) "."
"Please specify global or build-specific source paths."))
(nil? stylesheet) (error "No stylesheet specified for a build: " (name id))
(not (symbol? stylesheet)) (error "Stylesheet value must be a symbol: " stylesheet " in a build: " (name id))
(nil? output-to) (error "No specified output path (:output-to) for a compiled CSS build: " (name id)))))

(defn- load-namespaces [stylesheets]
`(require ~@(for [stylesheet stylesheets]
`'~(-> stylesheet namespace symbol))))
Expand Down Expand Up @@ -90,11 +94,11 @@

(defn- run-compiler
"Starts the compilation process."
[{{:keys [output-directory source-paths]} :tornado :as project} args compilation-type]
[{{:keys [output-dir output-directory source-paths]} :tornado :as project} args compilation-type]
(let [builds (if (seq args)
(find-builds project args)
(all-builds project))
_ (pp/pprint (:tornado project))
output-directory (or output-directory output-dir)
builds (cond->> builds
(= compilation-type :release) (map #(update % :compiler assoc :pretty-print? false))
;; build outputs can share the same parent folder
Expand Down Expand Up @@ -150,7 +154,6 @@
:subtasks [#'once #'auto #'release]}
[project command & builds?]
(let [project (project/merge-profiles project [tornado-profile])]
(validate-builds project)
(case command "once" (once project builds?)
"auto" (auto project builds?)
"release" (release project builds?)
Expand Down

0 comments on commit 97efaae

Please sign in to comment.