|
17 | 17 | [clojure.string :as string] |
18 | 18 | [clojure.walk :as walk] |
19 | 19 | [eca.features.agents :as agents] |
| 20 | + [rewrite-json.core :as rj] |
20 | 21 | [eca.interpolation :as interpolation] |
21 | 22 | [eca.logger :as logger] |
22 | 23 | [eca.messenger :as messenger] |
|
615 | 616 |
|
616 | 617 | (def ^:private config-schema-url "https://eca.dev/config.json") |
617 | 618 |
|
| 619 | +(defn ^:private flatten-to-paths |
| 620 | + "Recursively walks a nested map and returns a sequence of [path value] pairs, |
| 621 | + where path is a vector of string keys and value is a leaf (non-map) value. |
| 622 | + Mirrors deep-merge semantics: only the touched leaf paths are written." |
| 623 | + ([m] (flatten-to-paths [] m)) |
| 624 | + ([prefix m] |
| 625 | + (reduce-kv (fn [acc k v] |
| 626 | + (let [path (conj prefix (if (keyword? k) (name k) (str k)))] |
| 627 | + (if (and (map? v) (seq v)) |
| 628 | + (into acc (flatten-to-paths path v)) |
| 629 | + (conj acc [path v])))) |
| 630 | + [] |
| 631 | + m))) |
| 632 | + |
618 | 633 | (defn update-global-config! [config] |
619 | | - (let [global-config-file (global-config-file) |
620 | | - current-config (normalize-fields normalization-rules (config-from-global-file)) |
621 | | - new-config (deep-merge current-config |
622 | | - (normalize-fields normalization-rules config)) |
623 | | - new-config (assoc new-config "$schema" config-schema-url) |
624 | | - new-config-json (json/generate-string new-config {:pretty true})] |
625 | | - (io/make-parents global-config-file) |
626 | | - (spit global-config-file new-config-json))) |
627 | | - |
628 | | -(defn update-local-config! |
629 | | - "Deep-merges `config` into the local `.eca/config.json` for `workspace-root-uri`." |
630 | | - [workspace-root-uri config] |
631 | | - (let [config-dir (io/file (shared/uri->filename workspace-root-uri) ".eca") |
632 | | - config-file (io/file config-dir "config.json") |
633 | | - current-config (when (.exists config-file) |
634 | | - (normalize-fields normalization-rules |
635 | | - (safe-read-json-string (slurp config-file) (var *local-config-error*)))) |
636 | | - new-config (deep-merge (or current-config {}) |
637 | | - (normalize-fields normalization-rules config)) |
638 | | - new-config (assoc new-config "$schema" config-schema-url) |
639 | | - new-config-json (json/generate-string new-config {:pretty true})] |
640 | | - (io/make-parents config-file) |
641 | | - (spit config-file new-config-json))) |
| 634 | + (let [file (global-config-file) |
| 635 | + raw (if (.exists file) (slurp file) "{}") |
| 636 | + root (rj/parse-string raw) |
| 637 | + root (reduce (fn [r [path v]] (rj/assoc-in r path v)) |
| 638 | + root |
| 639 | + (flatten-to-paths config)) |
| 640 | + root (rj/assoc-in root ["$schema"] config-schema-url)] |
| 641 | + (io/make-parents file) |
| 642 | + (spit file (rj/to-string root)))) |
0 commit comments