From e9b55e1a4b64b6154887413f34697a3b1d33e0de Mon Sep 17 00:00:00 2001 From: James Reeves Date: Mon, 29 May 2023 14:01:55 +0100 Subject: [PATCH] Ensure cljfmt fix always outputs for STDOUT Fixes #305. --- cljfmt/src/cljfmt/io.clj | 6 +++--- cljfmt/src/cljfmt/tool.clj | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cljfmt/src/cljfmt/io.clj b/cljfmt/src/cljfmt/io.clj index c788ca2..f2479e7 100644 --- a/cljfmt/src/cljfmt/io.clj +++ b/cljfmt/src/cljfmt/io.clj @@ -3,7 +3,7 @@ (defprotocol FileEntity (read-file [f]) - (write-file [f s]) + (update-file [f s changed?]) (exists? [f]) (directory? [f]) (list-files [f]) @@ -12,7 +12,7 @@ (extend-protocol FileEntity File (read-file [f] (slurp f)) - (write-file [f s] (spit f s)) + (update-file [f s changed?] (when changed? (spit f s))) (exists? [f] (.exists f)) (directory? [f] (.isDirectory f)) (list-files [f] (file-seq f)) @@ -24,7 +24,7 @@ (deftype StdIO [in out] FileEntity (read-file [_] (slurp in)) - (write-file [_ s] (binding [*out* out] (print s)) (flush)) + (update-file [_ s _] (binding [*out* out] (print s)) (flush)) (exists? [_] true) (directory? [_] false) (list-files [_] nil) diff --git a/cljfmt/src/cljfmt/tool.clj b/cljfmt/src/cljfmt/tool.clj index 9dffe59..29d6428 100644 --- a/cljfmt/src/cljfmt/tool.clj +++ b/cljfmt/src/cljfmt/tool.clj @@ -124,10 +124,11 @@ (trace "Processing file:" (project-path options file)) (let [original (io/read-file file)] (try - (let [revised (reformat-string options original)] - (if (not= original revised) - (do (io/write-file file revised) - {:file file :reformatted true}) + (let [revised (reformat-string options original) + changed? (not= original revised)] + (io/update-file file revised changed?) + (if changed? + {:file file :reformatted true} {:file file})) (catch Exception e {:file file :exception e}))))