|
23 | 23 | (s/def ::old-string string?) |
24 | 24 | (s/def ::new-string string?) |
25 | 25 | (s/def ::nrepl-client-atom (s/nilable #(instance? clojure.lang.Atom %))) |
| 26 | + |
| 27 | +(s/def ::dry-run (s/nilable #{"diff" "new-source"})) |
26 | 28 | ;; Pipeline specific steps |
27 | 29 |
|
28 | 30 | ;; Using check-file-modified from form-edit/pipeline instead |
|
117 | 119 | - file-path: Path to the file to edit |
118 | 120 | - old-string: String to replace |
119 | 121 | - new-string: New string to insert |
120 | | - - nrepl-client-atom: Atom containing the nREPL client (optional) |
121 | | - - config: Optional tool configuration map |
| 122 | + - dry-run: Optional string, either \"diff\" or \"new-source\" to skip actual file write |
| 123 | + - config: Optional tool configuration map with :nrepl-client-atom |
122 | 124 | |
123 | 125 | Returns: |
124 | 126 | - A context map with the result of the operation" |
125 | | - [file-path old-string new-string {:keys [nrepl-client-atom] :as config}] |
| 127 | + [file-path old-string new-string dry-run {:keys [nrepl-client-atom] :as config}] |
126 | 128 | (let [initial-ctx {::form-pipeline/file-path file-path |
127 | 129 | ::old-string old-string |
128 | 130 | ::new-string new-string |
| 131 | + ::dry-run dry-run |
129 | 132 | ::form-pipeline/nrepl-client-atom nrepl-client-atom |
130 | 133 | ::form-pipeline/config config}] |
131 | 134 | ;; Pipeline for existing file edit |
|
147 | 150 | format-clojure-content ;; Format Clojure files automatically |
148 | 151 | form-pipeline/determine-file-type ;; This will mark as "update" |
149 | 152 | form-pipeline/generate-diff ;; Generate diff between old and new |
150 | | - form-pipeline/save-file ;; Save the file |
151 | | - form-pipeline/update-file-timestamp ;; Update the timestamp after save |
152 | | - form-pipeline/highlight-form))) ;; Update the timestamp after save ;; Update the timestamp after save |
| 153 | + ;; Skip file operations if dry-run is set |
| 154 | + (fn [ctx] |
| 155 | + (if (::dry-run ctx) |
| 156 | + ctx |
| 157 | + (-> ctx |
| 158 | + form-pipeline/save-file |
| 159 | + form-pipeline/update-file-timestamp |
| 160 | + form-pipeline/highlight-form)))))) ;; Update the timestamp after save ;; Update the timestamp after save |
153 | 161 |
|
154 | 162 | ;; Format result for tool consumption |
155 | 163 | (defn format-result |
|
159 | 167 | - ctx: The final context map from the pipeline |
160 | 168 | |
161 | 169 | Returns: |
162 | | - - A map with :error, :message, and :diff keys, and potentially :repaired" |
| 170 | + - A map with :error, :message, and :diff or :new-source keys, and potentially :repaired" |
163 | 171 | [ctx] |
164 | 172 | (if (::form-pipeline/error ctx) |
165 | 173 | {:error true |
166 | 174 | :message (::form-pipeline/message ctx)} |
167 | | - (cond-> {:error false |
168 | | - :diff (::form-pipeline/diff ctx) |
169 | | - :type (::form-pipeline/type ctx)} |
170 | | - ;; Include repaired flag if present |
171 | | - (::form-pipeline/repaired ctx) |
172 | | - (assoc :repaired true)))) |
| 175 | + (let [dry-run (::dry-run ctx)] |
| 176 | + (cond-> {:error false |
| 177 | + :type (::form-pipeline/type ctx)} |
| 178 | + ;; Include repaired flag if present |
| 179 | + (::form-pipeline/repaired ctx) |
| 180 | + (assoc :repaired true) |
| 181 | + |
| 182 | + ;; Return new-source if dry-run is "new-source" |
| 183 | + (= dry-run "new-source") |
| 184 | + (assoc :new-source (::form-pipeline/output-source ctx)) |
| 185 | + |
| 186 | + ;; Otherwise return diff (default behavior and for "diff" dry-run) |
| 187 | + (not= dry-run "new-source") |
| 188 | + (assoc :diff (::form-pipeline/diff ctx)))))) |
173 | 189 |
|
174 | 190 | (comment |
175 | 191 | ;; === Examples of using the file-edit pipeline directly === |
|
0 commit comments