Skip to content

Commit ea23abb

Browse files
committed
Support nested folder for rules and commands
Fixes #220
1 parent 60fb986 commit ea23abb

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Support nested folder for rules and commands. #220
6+
57
## 0.81.0
68

79
- Support rollback file changes done by `write_file`, `edit_file` and `move_file`. #218

src/eca/features/commands.clj

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@
2727
(io/file (config/get-property "user.home") ".config"))
2828
commands-dir (io/file xdg-config-home "eca" "commands")]
2929
(when (fs/exists? commands-dir)
30-
(map (fn [file]
31-
{:name (normalize-command-name file)
32-
:path (str (fs/canonicalize file))
33-
:type :user-global-file
34-
:content (slurp (fs/file file))})
35-
(fs/list-dir commands-dir)))))
30+
(keep (fn [file]
31+
(when-not (fs/directory? file)
32+
{:name (normalize-command-name file)
33+
:path (str (fs/canonicalize file))
34+
:type :user-global-file
35+
:content (slurp (fs/file file))}))
36+
(fs/glob commands-dir "**" {:follow-links true})))))
3637

3738
(defn ^:private local-file-commands [roots]
3839
(->> roots
3940
(mapcat (fn [{:keys [uri]}]
4041
(let [commands-dir (fs/file (shared/uri->filename uri) ".eca" "commands")]
4142
(when (fs/exists? commands-dir)
42-
(fs/list-dir commands-dir)))))
43-
(map (fn [file]
44-
{:name (normalize-command-name file)
45-
:path (str (fs/canonicalize file))
46-
:type :user-local-file
47-
:content (slurp (fs/file file))}))))
43+
(fs/glob commands-dir "**" {:follow-links true})))))
44+
(keep (fn [file]
45+
(when-not (fs/directory? file)
46+
{:name (normalize-command-name file)
47+
:path (str (fs/canonicalize file))
48+
:type :user-local-file
49+
:content (slurp (fs/file file))})))))
4850

4951
(defn ^:private config-commands [config roots]
5052
(->> (get config :commands)

src/eca/features/rules.clj

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@
1212
(io/file (config/get-property "user.home") ".config"))
1313
rules-dir (io/file xdg-config-home "eca" "rules")]
1414
(when (fs/exists? rules-dir)
15-
(map (fn [file]
16-
{:name (fs/file-name file)
17-
:path (str (fs/canonicalize file))
18-
:type :user-global-file
19-
:content (slurp (fs/file file))})
20-
(fs/list-dir rules-dir)))))
15+
(keep (fn [file]
16+
(when-not (fs/directory? file)
17+
{:name (fs/file-name file)
18+
:path (str (fs/canonicalize file))
19+
:type :user-global-file
20+
:content (slurp (fs/file file))}))
21+
(fs/glob rules-dir "**" {:follow-links true})))))
2122

2223
(defn ^:private local-file-rules [roots]
2324
(->> roots
2425
(mapcat (fn [{:keys [uri]}]
2526
(let [rules-dir (fs/file (shared/uri->filename uri) ".eca" "rules")]
2627
(when (fs/exists? rules-dir)
27-
(fs/list-dir rules-dir)))))
28-
(map (fn [file]
29-
{:name (fs/file-name file)
30-
:path (str (fs/canonicalize file))
31-
:type :user-local-file
32-
:content (slurp (fs/file file))}))))
28+
(fs/glob rules-dir "**" {:follow-links true})))))
29+
(keep (fn [file]
30+
(when-not (fs/directory? file)
31+
{:name (fs/file-name file)
32+
:path (str (fs/canonicalize file))
33+
:type :user-local-file
34+
:content (slurp (fs/file file))})))))
3335

3436
(defn ^:private config-rules [config roots]
3537
(->> (get config :rules)

test/eca/features/rules_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
(with-redefs [fs/absolute? (constantly false)
2828
fs/exists? #(contains? #{(h/file-path "/my/project/.eca/rules")
2929
(h/file-path "/my/project/.foo/cool-rule.md")} (str %))
30-
fs/list-dir (constantly [])
30+
fs/glob (constantly [])
3131
fs/canonicalize identity
3232
fs/file-name (constantly "cool-name")
3333
clojure.core/slurp (constantly "MY_RULE_CONTENT")]
@@ -42,7 +42,7 @@
4242

4343
(testing "local file rules"
4444
(with-redefs [fs/exists? #(= (h/file-path "/my/project/.eca/rules") (str %))
45-
fs/list-dir (constantly [(fs/path "cool.md")])
45+
fs/glob (constantly [(fs/path "cool.md")])
4646
fs/canonicalize identity
4747
fs/file-name (constantly "cool-name")
4848
clojure.core/slurp (constantly "MY_RULE_CONTENT")]
@@ -56,7 +56,7 @@
5656
(testing "global file rules"
5757
(with-redefs [config/get-env (constantly (h/file-path "/home/someuser/.config"))
5858
fs/exists? #(= (h/file-path "/home/someuser/.config/eca/rules") (str %))
59-
fs/list-dir (constantly [(fs/path "cool.md")])
59+
fs/glob (constantly [(fs/path "cool.md")])
6060
fs/canonicalize identity
6161
fs/file-name (constantly "cool-name")
6262
clojure.core/slurp (constantly "MY_RULE_CONTENT")]

0 commit comments

Comments
 (0)