Skip to content

Commit

Permalink
runtime: expose dirac.runtime/available? and revamp internals
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed May 30, 2016
1 parent 679ca33 commit 560c96d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/runtime/dirac/runtime.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

(def ^:export get-version get-current-version)

(def ^:export available? core/available?)
(def ^:export installed? core/installed?)
(def ^:export install! core/install!)
(def ^:export uninstall! core/uninstall!)

(def ^:export get-tag core/get-tag)

(def ^:export set-prefs! prefs/set-prefs!)
(def ^:export get-prefs prefs/get-prefs)
(def ^:export set-prefs! prefs/set-prefs!)
(def ^:export set-pref! prefs/set-pref!)

; -- DEPRECATED API ---------------------------------------------------------------------------------------------------------

(defn ^:export is-feature-available? [& args]
Expand Down
35 changes: 23 additions & 12 deletions src/runtime/dirac/runtime/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,47 @@
(:require-macros [dirac.runtime.core :refer [get-current-browser-name get-current-platform-name]])
(:require [dirac.project :refer [get-current-version]]
[dirac.runtime.repl :as repl]
[dirac.runtime.util :refer [display-banner-if-needed! report-unknown-features! install-feature! make-version-info
make-lib-info]]
[dirac.runtime.util :refer [display-banner-if-needed! install-feature! resolve-features! get-lib-info]]
[dirac.runtime.prefs :as prefs]
[goog.labs.userAgent.browser :as ua-browser]
[goog.labs.userAgent.platform :as ua-platform]))

(def known-features [:repl])
(def features-to-install-by-default [:repl])
(def default-features [:repl])
(def feature-groups {:all known-features
:default default-features})

; -- CORE API ---------------------------------------------------------------------------------------------------------------

(defn is-feature-available? [feature]
(case feature
:repl (repl/available?)))

(defn available?
([] (available? :default))
([features-desc]
(let [features (resolve-features! features-desc feature-groups)]
(if (empty? features)
false
(every? is-feature-available? features)))))

(defn is-feature-installed? [feature]
(case feature
:repl (repl/installed?)))

(defn installed? [feature-or-features]
(let [features (if (seqable? feature-or-features) feature-or-features [feature-or-features])]
(every? is-feature-installed? features)))
(defn installed?
([] (installed? :default))
([features-desc]
(let [features (resolve-features! features-desc feature-groups)]
(if (empty? features)
false
(every? is-feature-installed? features)))))

(defn install!
([] (install! features-to-install-by-default))
([features-to-install]
(let [features (if (some? features-to-install) features-to-install features-to-install-by-default)
lib-info (make-lib-info (get-current-version))]
(report-unknown-features! features known-features lib-info)
(display-banner-if-needed! features known-features lib-info)
([] (install! :default))
([features-desc]
(let [features (resolve-features! features-desc feature-groups)]
(display-banner-if-needed! features feature-groups)
(install-feature! :repl features is-feature-available? repl/install!))))

(defn uninstall! []
Expand Down
41 changes: 31 additions & 10 deletions src/runtime/dirac/runtime/util.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns dirac.runtime.util
(:require [goog.userAgent :as ua]
[dirac.project :refer [get-current-version]]
[dirac.runtime.prefs :as prefs]))

(defn ^:dynamic make-version-info [version]
Expand All @@ -16,37 +17,57 @@
(str "Feature " feature " cannot be installed. "
"Unsupported browser " (ua/getUserAgentString) "."))

(defn get-lib-info []
(make-lib-info (get-current-version)))

; -- banner -----------------------------------------------------------------------------------------------------------------

(defn feature-for-display [installed-features feature]
(let [color (if (some #{feature} installed-features) "color:#0000ff" "color:#ccc")]
["%c%s" [color (str feature)]]))

(defn feature-list-display [installed-features known-features]
(let [labels (map (partial feature-for-display installed-features) known-features)
(defn feature-list-display [installed-features feature-groups]
(let [labels (map (partial feature-for-display installed-features) (:all feature-groups))
* (fn [accum val]
[(str (first accum) " " (first val))
(concat (second accum) (second val))])]
(reduce * (first labels) (rest labels))))

(defn display-banner! [installed-features known-features fmt & params]
(let [[fmt-str fmt-params] (feature-list-display installed-features known-features)
(defn display-banner! [installed-features feature-groups fmt & params]
(let [[fmt-str fmt-params] (feature-list-display installed-features feature-groups)
items (concat [(str fmt " " fmt-str)] params fmt-params)]
(.apply (.-info js/console) js/console (into-array items))))

(defn display-banner-if-needed! [features-to-install known-features lib-info]
(defn display-banner-if-needed! [features-to-install feature-groups]
(when-not (prefs/pref :dont-display-banner)
(let [banner (str "Installing %c%s%c and enabling features")
lib-info-style "color:black;font-weight:bold;"
reset-style "color:black"]
(display-banner! features-to-install known-features banner lib-info-style lib-info reset-style))))
(display-banner! features-to-install feature-groups banner lib-info-style (get-lib-info) reset-style))))

; -- unknown features -------------------------------------------------------------------------------------------------------

(defn report-unknown-features! [features known-features lib-info]
(doseq [feature features]
(if-not (some #{feature} known-features)
(.warn js/console (unknown-feature-msg feature known-features lib-info)))))
(defn report-unknown-features! [features known-features]
(let [lib-info (get-lib-info)]
(doseq [feature features]
(if-not (some #{feature} known-features)
(.warn js/console (unknown-feature-msg feature known-features lib-info))))))

(defn is-known-feature? [known-features feature]
(boolean (some #{feature} known-features)))

(defn sanititze-features! [features feature-groups]
(let [known-features (:all feature-groups)]
(report-unknown-features! features known-features)
(filter (partial is-known-feature? known-features) features)))

(defn resolve-features! [features-desc feature-groups]
(let [features (cond
(and (keyword? features-desc) (features-desc feature-groups)) (features-desc feature-groups)
(nil? features-desc) (:default feature-groups)
(seqable? features-desc) features-desc
:else [features-desc])]
(sanititze-features! features feature-groups)))

; -- installer --------------------------------------------------------------------------------------------------------------

Expand Down

2 comments on commit 560c96d

@arichiardi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the commit to plug in boot-cljs-source right?

@darwin
Copy link
Member Author

@darwin darwin commented on 560c96d May 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to use dirac.runtime/installed? that was introduced a few commits earlier. I think.

Please sign in to comment.