From 60026817c44015da8656925411d4af1d8210bad0 Mon Sep 17 00:00:00 2001 From: vemv Date: Tue, 14 Dec 2021 09:23:07 +0100 Subject: [PATCH] [Fix #12] Introduce feature flags to make extension of io/Coercions optional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These allow dependent libraries to use `fs` without the side-effect of extend. Otherwise one can unawarely create issues, as seen in https://github.com/clojure-emacs/clj-refactor.el/issues/508. https://clojure.org/reference/protocols#_guidelines_for_extension says: > If you don’t own the protocol or the target type, you should only extend in app (not public lib) code, and expect to maybe be broken by either owner. --- src/me/raynes/fs.clj | 12 +++++++----- src/me/raynes/fs/feature_flags.clj | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 src/me/raynes/fs/feature_flags.clj diff --git a/src/me/raynes/fs.clj b/src/me/raynes/fs.clj index 595662e..ccd2be5 100644 --- a/src/me/raynes/fs.clj +++ b/src/me/raynes/fs.clj @@ -3,7 +3,8 @@ (:refer-clojure :exclude [name parents]) (:require [clojure.zip :as zip] [clojure.java.io :as io] - [clojure.java.shell :as sh]) + [clojure.java.shell :as sh] + [me.raynes.fs.feature-flags :as feature-flags]) (:import [java.io File FilenameFilter] [java.nio.file Files Path LinkOption CopyOption] [java.nio.file.attribute FileAttribute])) @@ -141,10 +142,11 @@ [path] (predicate isHidden (file path))) -(extend-protocol io/Coercions - Path - (as-file [this] (.toFile this)) - (as-url [this] (.. this (toFile) (toURL)))) +(when feature-flags/extend-coercions? + (extend-protocol io/Coercions + Path + (as-file [this] (.toFile this)) + (as-url [this] (.. this (toFile) (toURL))))) (defn- ^Path as-path "Convert `path` to a `java.nio.file.Path`. diff --git a/src/me/raynes/fs/feature_flags.clj b/src/me/raynes/fs/feature_flags.clj new file mode 100644 index 0000000..8075255 --- /dev/null +++ b/src/me/raynes/fs/feature_flags.clj @@ -0,0 +1,12 @@ +(ns me.raynes.fs.feature-flags + "Compile-time feature flags. + + In order to use them: + + * `require` this ns before any other ns from this lib. + * `alter-var-root` a given feature flag within this ns to a different, desired value + * proceed to `require` the rest of this library.") + +(def extend-coercions? + "Should the clojure.java.io/Coercions protocol be extended by this library?" + true)