Skip to content

Commit 0a7dfa3

Browse files
committed
Add support for auto-prefixes via cljs.storm.instrumentAutoPrefixes
1 parent bc16feb commit 0a7dfa3

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
## master (unreleased)
44

55
### New Features
6+
7+
- Add support for auto-prefixes via cljs.storm.instrumentAutoPrefixes
68
79
### Changes
10+
11+
- Instrumentation is enable by default unless cljs.storm.instrumentEnable is set to false
812
913
### Bugs fixed
1014

src/main/clojure/cljs/storm/emitter.cljc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
(ns cljs.storm.emitter
2-
(:require [clojure.string :as str]))
3-
4-
#?(:clj
5-
(defn prefixes-for-prop-starting-with [prop-prefix]
6-
(reduce-kv (fn [prefixes prop-name prop-val]
7-
(if (str/starts-with? prop-name prop-prefix)
8-
(->> (str/split prop-val #",")
9-
(map str/trim)
10-
(remove str/blank?)
11-
(into prefixes))
12-
prefixes))
13-
[]
14-
(into {} (System/getProperties)))))
2+
(:require [clojure.string :as str]
3+
[cljs.storm.utils :as utils]))
154

165
#?(:clj (def instrument-enable
17-
(some-> (System/getProperty "cljs.storm.instrumentEnable")
18-
Boolean/parseBoolean))
6+
(if-let [prop (System/getProperty "cljs.storm.instrumentEnable")]
7+
(Boolean/parseBoolean prop)
8+
true))
199
:cljs (def instrument-enable nil))
2010

11+
#?(:clj (def auto-prefixes?
12+
(if-let [prop (System/getProperty "cljs.storm.instrumentAutoPrefixes")]
13+
(Boolean/parseBoolean prop)
14+
true))
15+
:cljs (def auto-prefixes? nil))
16+
2117
#?(:clj (def instrument-only-prefixes
22-
(prefixes-for-prop-starting-with "cljs.storm.instrumentOnlyPrefixes"))
18+
(cond-> (utils/prefixes-for-prop-starting-with "cljs.storm.instrumentOnlyPrefixes")
19+
auto-prefixes? (into (utils/classpath-src-dirs-prefixes))))
2320
:cljs (def instrument-only-prefixes nil))
2421

2522
#?(:clj (def instrument-skip-prefixes
26-
(prefixes-for-prop-starting-with "cljs.storm.instrumentSkipPrefixes"))
23+
(utils/prefixes-for-prop-starting-with "cljs.storm.instrumentSkipPrefixes"))
2724
:cljs (def instrument-skip-prefixes nil))
2825

2926
#?(:clj (def instrument-skip-regex

src/main/clojure/cljs/storm/utils.cljc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,50 @@
116116
(defn original-source-form [env]
117117
(or (get-in env [:root-source-info :source-form]) ;; root form when called from vanilla cljs compiler
118118
(:shadow.build/root-form env))) ;; root form when called from shadow
119+
120+
#?(:clj
121+
(defn src-dir-root-namespaces [dir-file]
122+
(let [dir-path (.getAbsolutePath dir-file)]
123+
(->> (file-seq dir-file)
124+
(filterv (fn [f]
125+
(or (str/ends-with? (.getName f) ".clj")
126+
(str/ends-with? (.getName f) ".cljc"))))
127+
(keep (fn [f]
128+
(let [f-path (-> (.getAbsolutePath f)
129+
(.replace "\\" "/"))
130+
[_ top-dir] (re-find (re-pattern (str dir-path "/(.+?)/.*")) f-path)]
131+
(when top-dir
132+
(Compiler/demunge top-dir)))))
133+
(into #{})))))
134+
135+
#?(:clj
136+
(defn classpath-src-dirs-root-namespaces []
137+
(let [class-path (System/getProperty "java.class.path")
138+
class-path-separator (System/getProperty "path.separator")
139+
cp-dir-entries (->> (.split class-path class-path-separator)
140+
(mapv io/file)
141+
(filterv #(.isDirectory %)))]
142+
(reduce (fn [root-namespaces cp-dir-file]
143+
(into root-namespaces (src-dir-root-namespaces cp-dir-file)))
144+
#{}
145+
cp-dir-entries))))
146+
147+
#?(:clj
148+
(defn classpath-src-dirs-prefixes []
149+
(->> (classpath-src-dirs-root-namespaces)
150+
(remove (fn [root-ns-name]
151+
(or (= "flow-storm" root-ns-name)
152+
(= "clojure" root-ns-name)
153+
(= "cljs" root-ns-name)))))))
154+
155+
#?(:clj
156+
(defn prefixes-for-prop-starting-with [prop-prefix]
157+
(reduce-kv (fn [prefixes prop-name prop-val]
158+
(if (str/starts-with? prop-name prop-prefix)
159+
(->> (str/split prop-val #",")
160+
(map str/trim)
161+
(remove str/blank?)
162+
(into prefixes))
163+
prefixes))
164+
[]
165+
(into {} (System/getProperties)))))

0 commit comments

Comments
 (0)