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

Example of notifying with terminal-notifier. #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ in `project.clj`:

lein doo {js-env} {build-id} {watch-mode}

lein doo {js-env} {build-id} {watch-mode} {notify} {change-only}

* `js-env` can be any `chrome`, `firefox`, `ie`, `safari`, `opera`,
`slimer`, `phantom`, `node`, `rhino`, or `nashorn`. In the future it
is planned to support `v8`, `jscore`, and others.
* `watch-mode` (optional): either `auto` (default) or `once` which
exits with 0 if the tests were successful and 1 if they failed.
* `notify` (optional): specifies that
[terminal notifier](https://github.com/julienXX/terminal-notifier)
should be used to notify test run
* `change-only` (optional): when `notify?` will only notify when there is
a change in test status, e.g. from `OK` to `Failed`.
* `build-id` is one of your `cljsbuild` profiles. For example `test` from:

```clj
Expand Down
36 changes: 36 additions & 0 deletions library/src/doo/notifier.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns doo.notifier
(:require [clojure.java.shell :as shell]
[clojure.string :as str]))

(def last-run (atom nil))

(defn- escape [message]
(str/replace message "[" "\\["))

(defn- notify [title-postfix message]
{:pre [(string? title-postfix) (string? message)]}
(try
(shell/sh "terminal-notifier" "-message" (escape message) "-title" (str "doo - " (escape title-postfix)))
(catch Exception ex
(println "Problem communicating with notification center, please make sure you installed terminal-notifier (e.g. using 'brew install terminal-notifier'), exception:" (.getMessage ex)))))

(defn get-assertion-string [s]
(when s
(re-find #"Ran \d+ tests containing.+" s)))

(defn notify-title [s]
(when s
(condp re-find s
#"0 failures, 0 errors" "Ok"
#"0 errors" "Failed"
#"0 failures" "Errors"
s)))

(defn handle-notifications [out opts]
(when (:notify opts)
(let [old-status (notify-title @last-run)
new (reset! last-run (get-assertion-string out))
new-status (notify-title new)
changed (not= new-status old-status)]
(when (or changed (= :always (:notify opts)))
(notify new-status new)))))
21 changes: 14 additions & 7 deletions plugin/src/leiningen/doo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,25 @@
(defn default? [cli-opt]
(or (nil? cli-opt) (= :default cli-opt)))

(defn watch-mode? [arg]
(contains? #{"auto" "once"} arg))
(defn optional-args? [arg]
(contains? #{"auto" "once" "notify" "change-only"} arg))

(defn parse-notify [args]
(when (some #{"notify"} args)
(or (keyword (some #{"change-only"} args)) :always)))

(defn args->cli
"Parses & validates the cli arguments into a consistent format"
[args]
(let [[js-env build-id & xs] (remove watch-mode? args)]
(let [[js-env build-id & xs] (remove optional-args? args)
notify (parse-notify args)]
(assert (empty? xs)
(str "We couldn't parse " xs " as a watch-mode,"
" only auto or once are supported"))
{:alias (keyword (or js-env "default"))
:build (or build-id :default)
:watch-mode (keyword (or (first (filter watch-mode? args)) "auto"))}))
:notify notify
:watch-mode (keyword (or (first (filter #{"auto" "once"} args)) "auto"))}))

(defn cli->js-envs
"Returns the js-envs where doo should be run from the cli arguments
Expand Down Expand Up @@ -197,7 +203,7 @@ in project.clj.\n")
([project & args]
;; FIX: execute in a try catch like the one in run-local-project
(let [{:keys [alias watch-mode] :as cli} (args->cli args)
opts (:doo project)
opts (assoc (:doo project) :notify (:notify cli))
js-envs (cli->js-envs cli opts)
;; FIX: get the version dynamically
project' (-> project
Expand All @@ -211,7 +217,7 @@ in project.clj.\n")
;; FIX: there is probably a bug regarding the incorrect use of builds
;; Important to add sources to the classpath
(run-local-project (add-sources project' source-paths)
'(require 'cljs.build.api 'doo.core 'doo.karma)
'(require 'cljs.build.api 'doo.core 'doo.karma 'doo.notifier)
`(let [compiler# (cljs.build.api/add-implicit-options ~compiler)]
(doseq [js-env# ~js-envs]
(doo.core/assert-compiler-opts js-env# compiler#))
Expand All @@ -233,7 +239,8 @@ in project.clj.\n")
(Thread/sleep 1000))
(doseq [js-env# non-karma-envs#]
(doo.core/print-envs js-env#)
(doo.core/run-script js-env# compiler# ~opts))
(let [r# (doo.core/run-script js-env# compiler# ~opts)]
(doo.notifier/handle-notifications (:out r#) ~opts)))
(when @karma-on?#
(apply doo.core/print-envs karma-envs#)
(doo.core/karma-run! ~opts))))))
Expand Down
15 changes: 14 additions & 1 deletion plugin/test/clj/test/lein_doo/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,32 @@
(are [args opts] (= opts (doo/args->cli args))
[] {:build :default
:alias :default
:notify nil
:watch-mode :auto}
["chrome"] {:build :default
:alias :chrome
:notify nil
:watch-mode :auto}
["chrome" "none-test"] {:build "none-test"
:alias :chrome
:notify nil
:watch-mode :auto}
["chrome" "once"] {:build :default
:alias :chrome
:notify nil
:watch-mode :once}
["chrome" "none-test" "once"] {:build "none-test"
:alias :chrome
:watch-mode :once})
:notify nil
:watch-mode :once}
["chrome" "once" "notify"] {:build :default
:alias :chrome
:notify :always
:watch-mode :once}
["chrome" "once" "notify" "change-only"] {:build :default
:alias :chrome
:notify :change-only
:watch-mode :once})
(are [args] (is (thrown? java.lang.AssertionError (doo/args->cli args)))
["chrome" "none-test" "autoo"]
["chrome" "none-test" "auto" "advanced-test"])))
Expand Down