Skip to content

Commit

Permalink
Merge pull request #17 from cdorrat/master
Browse files Browse the repository at this point in the history
Added "print" command to print a templates value to stdout.  This is a great addition to the project.  It even supports doing something like 
    `lein resource print "user={{user.name.prop}} license={{license.name}}"`
  • Loading branch information
m0smith committed Dec 27, 2015
2 parents b36bcf0 + 67ab97c commit e00f0d6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
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))))

0 comments on commit e00f0d6

Please sign in to comment.