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

Add pgns command #26

Merged
merged 3 commits into from
Nov 14, 2022
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
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension points (such as readers, outputs, AST nodes), are subject to change.
- [Partials](#partials)
- [Error messages](#error-messages)
- [CLI usage](#cli-usage)
- [Babashka support](#babashka-support)

## Installation

Expand Down Expand Up @@ -399,6 +400,44 @@ For example, the following command will check three template files named `foo.mu
$ clojure -X:template check :file '"foo.mustache:bar.mustache:baz.mustache"'
```

### Babashka support

Pogonos 0.2.1+ supports Babashka. It's supported in the following three ways:

1. All the public function provided in `pogonos.core` can be used on Babashka, just like on the JVM
2. The API for the CLI usage can also be used on Babashka
3. Pogonos provides a standalone command that can be installed with [`bbin`](https://github.com/babashka/bbin) and works just like the [CLI tool](#cli-usage)

The following sections will focus on 3.

#### Installation

Pogonos provides a standalone command named `pgns`. To install the `pgns` command, run the following:

```console
bbin install io.github.athos/pogonos
```

#### Usage

The `pgns` command can be used exactly like the CLI tool:

```console
$ echo 'Hello, {{name}}!' | pgns render :data '{:name "Clojurian"}'
Hello, Clojurian!
$
```

Backed by [babashka.cli](https://github.com/babashka/cli), the `pgns` command also provides more UNIXy options:

```console
$ echo 'Hello, {{name}}!' | pgns render --data '{:name "Clojurian"}'
Hello, Clojurian!
$
```

Run `pgns help` for more detailed usage.

## License

Copyright © 2020 Shogo Ohta
Expand Down
1 change: 1 addition & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{:paths ["src" "test" "test-resources"]
:deps {org.clojure/data.json {:mvn/version "2.4.0"}}
:bbin/bin {pgns {:main-opts ["-m" "pogonos.main"]}}
:tasks
{test pogonos.test-runner/-main}}
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.babashka/cli {:mvn/version "0.4.39"}}
:tools/usage {:ns-default pogonos.api}
:aliases {:check
{:extra-deps
Expand Down
51 changes: 49 additions & 2 deletions src/pogonos/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,28 @@
- :data Map of the values passed to the template
- :data-file If specified, reads an edn map from the file specified by that
path and pass it to the template"
{:added "0.2.0"}
{:added "0.2.0"
:org.babashka/cli
{:spec
{:string
{:desc "Renders the given template string",
:ref "<template string>", :coerce :string, :alias :s}
:file
{:desc "Renders the specified template file",
:ref "<file>", :coerce :string, :alias :f}
:resource
{:desc "Renders the specified template resource on the classpath",
:ref "<resource path>", :coerce :string, :alias :r}
:output
{:desc "Path to the output file. If not specified, the rendering result will be emitted to stdout by default."
:ref "<file>", :coerce :string, :alias :o}
:data
{:desc "Map of the values passed to the template",
:ref "<edn>", :coerce :edn, :alias :d}
:data-file
{:desc "If specified, reads an edn map from the file specified by that path and pass it to the template",
:ref "<file>", :coerce :string, :alias :D}}
:order [:string :file :resource :output :data :data-file]}}
[{:keys [string file resource data data-file output] :as opts}]
(let [data (or (when data-file
(with-open [r (-> (io/reader (str data-file))
Expand Down Expand Up @@ -127,7 +148,33 @@
the following options:
- :only-show-errors Hides progress messages
- :suppress-verbose-errors Suppresses verbose error messages"
{:added "0.2.0"}
{:added "0.2.0"
:org.babashka/cli
{:spec
{:string
{:desc "Checks the given template string",
:ref "<template string>", :coerce :string, :alias :s}
:file
{:desc "Checks the specified template file(s)",
:ref "<file>", :coerce :string, :alias :f}
:dir
{:desc "Checks the template files in the specified directory",
:ref "<dir>", :coerce :string, :alias :d}
:resource
{:desc "Checks the specified template resource(s) on the classpath",
:ref "<resource path>", :coerce :string, :alias :r}
:include-regex
{:desc "Regex pattern for paths of templates to be checked",
:ref "<regex>", :coerce :string, :alias :i}
:exclude-regex
{:desc "Regex pattern for paths of templates not to be checked",
:ref "<regex>", :coerce :string, :alias :e}
:only-show-errors
{:desc "Hides progress messages", :coerce :boolean, :alias :S}
:suppress-verbose-errors
{:desc "Suppress verbose error messages" :coerce :boolean, :alias :E}}
:order [:string :file :dir :resource
:only-show-errors :suppress-verbose-errors]}}
[{:keys [string file dir resource on-failure] :or {on-failure :exit} :as opts}]
(binding [*errors* []]
(cond string (with-error-handling opts #(pg/check-string string opts))
Expand Down
62 changes: 62 additions & 0 deletions src/pogonos/main.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(ns pogonos.main
;;; This namespace is only available for clj/bb, and so all the following
;;; reader conditionals are effectively unnecessary and are only used
;;; to suppress linter warnings.
#?(:clj
(:require [babashka.cli :as cli]
pogonos.api)))

#?(:clj
(do
(defn- print-usage []
(println
(str #?@(:bb
["Usage: pgns <command> <options>\n"
" bb -m pogonos.main <command> <options>\n"]
:clj
["Usage: clojure -M -m pogonos.main <command> <options>\n"])
"\n"
"Commands:\n"
" render Renders the given Mustache template\n"
" check Checks if the given Mustache template contains any syntax error\n"
" help Prints this help message\n"
"\n"
#?(:bb
"Run `pgns help <command>` or `bb -m pogonos.main help <command>` to see more details for each command."
:clj
"Run `clojure -M -m pogonos.main help <command>` to see more details for each command."))))

(defn- resolve-command [cmd]
(when-let [v (some->> cmd
symbol
(ns-resolve 'pogonos.api))]
{:cmd v, :opts (:org.babashka/cli (meta v))}))

(defn- help [args]
(if (empty? args)
(print-usage)
(let [cmd (first args)]
(if-let [{cli-opts :opts} (resolve-command cmd)]
(println
(str #?@(:bb
["Usage: pgns" cmd " <options>\n"
" bb -m pogonos.main " cmd " <options>\n"]
:clj
["Usage: clojure -M -m pogonos.main " cmd " <options>\n"])
"\n"
"Options:\n"
(cli/format-opts cli-opts)))
(do (print-usage)
(System/exit 1))))))

(defn -main [& [cmd & args]]
(if (= cmd "help")
(help args)
(if-let [{f :cmd cli-opts :opts} (resolve-command cmd)]
(let [opts (cli/parse-opts args cli-opts)
cli-exec-args (:exec-args opts)
opts (cli/merge-opts cli-exec-args opts)]
(f opts))
(do (print-usage)
(System/exit 1)))))
))