Skip to content
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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ $ gitwerk semver major
v1.0.0
```

### sci

gitwerk supports to run user-defined scripts written in clojure (powered by [borkdude/sci](https://github.com/borkdude/sci)).

```
## can read stdin
$ echo '(semver ctx ["patch"])' | gitwerk sci
v0.0.2

## also read a file as a script
$ cat examples/example1.clj
(semver ctx ["patch"])
$ gitwerk sci examples/example1.clj
v0.0.3

## fetch executed command result and modify returned message
$ cat examples/example2.clj
(let [res (semver-auto ctx nil)
status (get-in res [:console-out :status])
oldv (get-in res [:console-out :old-version])
newv (get-in res [:console-out :new-version])]
(if (= status :updated)
(str "Version updated: " oldv " -> " newv)
"Version not updated"))
$ gitwerk sci examples/example2.clj
Version not updated

$ git commit --allow-empty -m "[patch] version updated"
$ gitwerk sci examples/example2.clj
Version updated: v0.0.3 -> v0.0.4
```

## License

Copyright © 2019 rinx
Expand Down
1 change: 1 addition & 0 deletions examples/example1.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(semver ctx ["patch"])
7 changes: 7 additions & 0 deletions examples/example2.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(let [res (semver-auto ctx nil)
status (get-in res [:console-out :status])
oldv (get-in res [:console-out :old-version])
newv (get-in res [:console-out :new-version])]
(if (= status :updated)
(str "Version updated: " oldv " -> " newv)
"Version not updated"))
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[metosin/jsonista "0.2.2"]
[camel-snake-kebab "0.4.0"]
[io.quarkus/quarkus-jgit "1.1.0.CR1"]
[borkdude/sci "0.0.12-alpha.7"]
[org.martinklepsch/clj-http-lite "0.4.3"]]
:profiles {:dev {:dependencies [[org.clojure/tools.namespace "0.2.11"]
[orchestra "2019.02.06-1"]]
Expand Down
34 changes: 34 additions & 0 deletions src/gitwerk/command/sci.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(ns gitwerk.command.sci
(:require
[clojure.spec.alpha :as spec]
[clojure.pprint :as pprint]
[sci.core :as sci]
[gitwerk.internal.io :as internal.io]))

(def clj-primitives
{'println println
'pprint pprint/pprint})

(defn run-fn [sci-opts]
(fn [ctx args]
(try
(let [file (first args)
body (if (nil? file)
(internal.io/read-from-stdin)
(internal.io/safe-read file))
ctx (dissoc ctx :command :summary)
opts (-> sci-opts
(update
:bindings
#(-> %
(merge clj-primitives)
(merge {'ctx ctx}))))
res (sci/eval-string body opts)]
(if (and (map? res) (:status res))
res
{:status 0
:console-out res}))
(catch Exception e
{:status 1
:console-out
{:error (.getMessage e)}}))))
39 changes: 21 additions & 18 deletions src/gitwerk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
[clojure.spec.alpha :as spec]
[clojure.tools.cli :as cli]
[clojure.string :as string]
[clojure.pprint :as pprint]
[clojure.edn :as edn]
[clojure.java.io :as io]
[gitwerk.service.runner :as runner])
(:gen-class))

(def cli-header
(string/join
"\n"
"\n"
(concat
["gitwerk is a CLI tool for supporting Git(Hub) operations on CI."
""
"Usage: gitwerk [command] [options]"
""
"Available commands:"
" clone [url] clone git repository"
" log show git logs of current directory"
" semver [type] print incremented version"
" semver-auto increment version by latest git log message contexts"
" tag show git tags of current directory"
""
"Options:"]))
"Available commands:"]
(mapv (fn [[k v]]
(let [name (format " %-12s " (name k))
desc (:description v)]
(str name desc))) runner/definitions)
[""
"Options:"])))
(def cli-options
[["-f" "--file PATH" "config"
:id :config-filename
Expand All @@ -33,19 +34,21 @@

(defn edn-output
[ctx res]
(println res))
(pprint/pprint res))

(defn std-output
[{:keys [summary] :as ctx}
{:keys [status invalid-arg? console-out]}]
(when console-out
(println console-out))
(when invalid-arg?
(println cli-header)
(println summary))
(if status
(System/exit status)
(System/exit 1)))
(when console-out
(if (map? console-out)
(pprint/pprint console-out)
(println console-out)))
(when invalid-arg?
(println cli-header)
(println summary))
(if status
(System/exit status)
(System/exit 1)))

(defn run
[{:keys [options] :as ctx}]
Expand Down
20 changes: 20 additions & 0 deletions src/gitwerk/internal/io.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns gitwerk.internal.io
(:require
[clojure.spec.alpha :as spec]
[clojure.java.io :as io]
[clojure.string :as string])
(:import
[java.io BufferedReader]))

(defn read-from-stdin []
(->> (BufferedReader. *in*)
(line-seq)
(string/join "\n")))

(defn safe-read
[file]
(if (.exists (io/file file))
(slurp file)
(throw
(Exception.
(str "File not found: " file)))))
39 changes: 32 additions & 7 deletions src/gitwerk/service/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,47 @@
[camel-snake-kebab.core :as csk]
[gitwerk.command.clone :as command.clone]
[gitwerk.command.log :as command.log]
[gitwerk.command.sci :as command.sci]
[gitwerk.command.semver :as command.semver]
[gitwerk.command.semver-auto :as command.semver-auto]
[gitwerk.command.tag :as command.tag]))

(def primitives
{:clone
{:command command.clone/run
:description "clone git repository"}
:log
{:command command.log/run
:description "show git logs of current directory"}
:semver
{:command command.semver/run
:description "print incremented version"}
:semver-auto
{:command command.semver-auto/run
:description "increment version by latest git log message contexts"}
:tag
{:command command.tag/run
:description "show git tags of current directory"}})

(def definitions
(let [->binding (fn [[k v]]
(let [sym (symbol (name k))
com (:command v)]
[sym com]))
bindings (->> primitives
(map ->binding)
(into {}))
sci-opts {:bindings bindings}]
(into primitives
{:sci {:command (command.sci/run-fn sci-opts)
:description "run user-defined script (written in clojure)"}})))

(defn dispatch
[{:keys [args] :as ctx} cmd]
(let [default (fn [_ _]
{:status 1
:invalid-arg? true})
cmd (case cmd
:clone command.clone/run
:log command.log/run
:semver command.semver/run
:semver-auto command.semver-auto/run
:tag command.tag/run
default)]
cmd (or (:command (cmd definitions)) default)]
(cmd ctx args)))

(defn run
Expand Down