Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "print" command to print a templates value to stdout #17

Merged
merged 1 commit into from
Dec 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ that is passed to stencil contains a combination of:

To use from Leiningen add to `project.clj`:
```clojure
:plugins [ [lein-resource "15.10.1"] ]
:plugins [ [lein-resource "15.10.2"] ]
```
To have it run before the jar file creation:
```clojure
Expand Down Expand Up @@ -60,6 +60,13 @@ To see all the properties that are passed to stencil:

lein resource pprint


To print the value of a stencil passed as an argument (useful for build scripts & testing templates)

lein resource print "{{version}}"
export MY_PROJECT_VERSION=$(lein resource print "{{name}}:{{version}}")


To delete all the copied files and empty directories:

lein resource clean
Expand Down
8 changes: 7 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

(defproject lein-resource "15.10.1"
(defproject lein-resource "15.10.2"
:description
"
A task that copies the files for the resource-paths to the
Expand All @@ -21,6 +21,12 @@ Remove the files created by the plugin.
lein resource clean


### Print
Print the value of a stencil specified as an argument, useful for build scripts

lein resource print \"{{version}}\"
export MY_PROJECT_VERSION=$(lein resource print \"{{name}}:{{version}}\")

### Pretty Print
Dump the map of values sent to stencil.

Expand Down
21 changes: 13 additions & 8 deletions src/leiningen/resource.clj
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,19 @@ Return a FileSpec"
(verbose-msg project-info "file-spec-seq")
(map (partial copy-file-spec project-info)))))

(defn print-task [value-map patterns]
(doseq [patt patterns]
(println (stencil/render-string patt value-map))))

;; ## resource
;; This is the main entry point into the plugin. It supports 3 tasks:
;; copy, clean and pprint.
;; This is the main entry point into the plugin. It supports 4 tasks:
;; copy, clean, print and pprint.
;;
;; This function creates the `ProjectInfo`, determines which task is
;; needs and calls it.

(defn resource
"Task name can also be pprint or clean"
"Task name can also be print, pprint or clean"
[project & task-keys]
(stencil.loader/set-cache {})
(let [{:keys [resource-paths target-path extra-values excludes includes
Expand All @@ -245,11 +249,12 @@ Return a FileSpec"
project-info (ProjectInfo. resource-paths target-path value-map includes excludes skip-stencil update silent verbose)]
(verbose-msg project-info "project-info" project-info)
;;(println "TASK:" task-name)
(cond
(= "pprint" task-name) (pprint value-map)
(= "clean" task-name) (clean-task project-info)
(= "copy" task-name) (copy-task project-info)
:else (copy-task project-info)))))
(condp = task-name
"print" (print-task value-map (rest task-keys))
"pprint" (pprint value-map)
"clean" (clean-task project-info)
"copy" (copy-task project-info)
(copy-task project-info)))))

(defn compile-hook [task & [project & more-args :as args]]
(msg (:silent project) "Copying resources...")
Expand Down
6 changes: 6 additions & 0 deletions test/plugin/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@
(deftest test-pprint
(let [^String r (with-out-str (resource project "pprint"))]
(is (.contains r (str mykey)))))

(deftest test-print
(let [out-str (with-out-str (resource project "print" "{{resource.target-path}}={{resource.extra-values.key}}"))
expected (str (get-in project [:resource :target-path]) "="
(get-in project [:resource :extra-values mykey]) "\n")]
(is (= expected out-str))))