From 805b2b88421895115aa88bb55b160405520f78b5 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 31 Jan 2020 15:12:50 +0100 Subject: [PATCH] start refactoring for fs-split --- src/cryogen_core/classpath_able_io/fs.clj | 58 +++++++++++++++++++ .../classpath_able_io/fs_test.clj | 20 +++++++ 2 files changed, 78 insertions(+) create mode 100644 src/cryogen_core/classpath_able_io/fs.clj create mode 100644 test/cryogen_core/classpath_able_io/fs_test.clj diff --git a/src/cryogen_core/classpath_able_io/fs.clj b/src/cryogen_core/classpath_able_io/fs.clj new file mode 100644 index 0000000..2626212 --- /dev/null +++ b/src/cryogen_core/classpath_able_io/fs.clj @@ -0,0 +1,58 @@ +; Copyright (c) meissa. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns cryogen-core.classpath-able-io.fs + (:require [clojure.java.io :as io] + [clojure.string :as st] + [schema.core :as s]) + (:import [java.net URI] + [java.util.jar JarFile JarEntry] + [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption] + [java.nio.file.attribute FileAttribute])) + +; ----------------------- Domain functions ------------------------ +(def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) + +(s/defn path + [full-path] + (let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile + (try + (when (Files/exists path-from-fs no-link-option) + path-from-fs) + (catch Exception e + nil)))) + +(defn create-resource + ([virtual-path + java-path] + {:virtual-path virtual-path + :java-uri (.toUri java-path) + :java-path java-path + :source-type :filesystem + :resource-type (cond + (Files/isDirectory java-path no-link-option) :dir + (Files/isRegularFile java-path no-link-option) :file + :else :unknown)})) + +; ------------------- infra --------------------------------- +(defn user-dir [] + (java.lang.System/getProperty "user.dir")) + +(defn path-from-fs + [full-path] + (let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile + (try + (when (Files/exists path-from-fs no-link-option) + path-from-fs) + (catch Exception e + nil)))) + + (defn + list-entries-for-dir + [resource] + (.list (.toFile (:java-path resource)))) diff --git a/test/cryogen_core/classpath_able_io/fs_test.clj b/test/cryogen_core/classpath_able_io/fs_test.clj new file mode 100644 index 0000000..0bbe859 --- /dev/null +++ b/test/cryogen_core/classpath_able_io/fs_test.clj @@ -0,0 +1,20 @@ +; Copyright (c) meissa. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns cryogen-core.classpath-able-io.fs-test + (:require [clojure.test :refer :all] + [clojure.java.io :as io] + [schema.core :as s] + [cryogen-core.file-test-tools :as ftt] + [cryogen-core.classpath-able-io.fs :as sut])) + +(def fs-root "fs_root") + +(deftest test-path + (is + (sut/path (str fs-root "dummy/dummy_from_fs"))))