Skip to content

Commit

Permalink
Use sources defined in deps.edn instead of hard coded values (#224)
Browse files Browse the repository at this point in the history
Use sources defined in `deps.edn` files + use transducers (thanks @imrekoszo).
  • Loading branch information
tengstrand authored May 31, 2022
1 parent 0904efc commit adf957c
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 174 deletions.
4 changes: 0 additions & 4 deletions components/common/src/polylith/clj/core/common/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require [polylith.clj.core.common.class-loader :as class-loader]
[polylith.clj.core.common.core :as core]
[polylith.clj.core.common.config :as config]
[polylith.clj.core.common.leiningen :as leiningen]
[polylith.clj.core.common.ns-extractor :as ns-extractor]
[polylith.clj.core.common.validate-args :as validate-args]
[polylith.clj.core.common.ws-dir :as ws-dir]))
Expand Down Expand Up @@ -42,9 +41,6 @@
(defn compact? [workspace view]
(core/compact? workspace view))

(defn leiningen-config-key [config-path key]
(leiningen/config-key config-path key))

(defn ns-to-path [namespace]
(core/ns-to-path namespace))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns polylith.clj.core.common.interface.config)

(defn src-paths [config]
(-> config :paths))

(defn test-paths [config]
(-> config :aliases :test :extra-paths))

(defn source-paths [config]
(concat (src-paths config)
(test-paths config)))
3 changes: 3 additions & 0 deletions components/file/src/polylith/clj/core/file/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
(defn files-and-dirs [dir home-dir]
(core/dirs-and-files dir home-dir))

(defn not-hidden? [name]
(core/not-hidden? name))

(defn read-deps-file [path]
(core/read-deps-file path))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@
(defn not-exists? [{:keys [exists?]}]
(not exists?))

(defn src-path? [{:keys [path]}]
(str/ends-with? path "/src"))
(defn resources? [path]
(or (= path "resources")
(str/ends-with? path "/resources")))

(defn test-path? [{:keys [path]}]
(str/ends-with? path "/test"))
(defn src-path? [{:keys [path test?]}]
(and (not test?)
(not (resources? path))))

(defn resources-path? [{:keys [path]}]
(str/ends-with? path "/resources"))
(defn test-path? [{:keys [path test?]}]
(and test?
(not (resources? path))))

(defn not-test-or-resources-path [entry]
(and (not (test-path? entry))
(not (resources-path? entry))))
(defn resources-path? [{:keys [path]}]
(resources? path))

(defn profile? [{:keys [profile?]}]
profile?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
(when paths
(into []
(comp
(map #(path-entry missing-paths % profile? test?))
(filter :name))
(map #(path-entry missing-paths % profile? test?))
(filter :name))
paths)))

(defn path-entries [src-paths test-paths profile-src-paths profile-test-paths disk-paths]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
(ns polylith.clj.core.path-finder.sources-on-disk
(:require [polylith.clj.core.file.interface :as file]))

(defn entity-source-paths [ws-dir entity-type entity]
(map #(str entity-type "/" entity "/" %)
(filter #(contains? #{"src" "resources" "test"} %)
(file/directories (str ws-dir "/" entity-type "/" entity)))))
(defn- entity-source-paths [ws-dir entity-type entity]
(eduction
(filter file/not-hidden?)
(map #(str entity-type "/" entity "/" %))
(file/directories (str ws-dir "/" entity-type "/" entity))))

(defn entity-paths [ws-dir entity-type]
(mapcat #(entity-source-paths ws-dir entity-type %)
(file/directories (str ws-dir "/" entity-type))))
(defn- entity-paths [ws-dir entity-type]
(eduction
(mapcat #(entity-source-paths ws-dir entity-type %))
(file/directories (str ws-dir "/" entity-type))))

(defn source-paths [ws-dir]
(vec (sort (mapcat #(entity-paths ws-dir %)
["bases" "components" "projects"]))))
(->> ["bases" "components" "projects"]
(into [] (mapcat #(entity-paths ws-dir %)))
(sort)
(vec)))
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
(def major 0)
(def minor 2)
(def patch 15)
(def revision "alpha-issue227-01")
(def revision "alpha-issue206-01")
(def name (str major "." minor "." patch "-" revision))

(def date "2022-05-29")
(def date "2022-05-31")

(defn version
([ws-type]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
(ns polylith.clj.core.workspace-clj.bases-from-disk
(:require [polylith.clj.core.file.interface :as file]
(:require [polylith.clj.core.common.interface.config :as config]
[polylith.clj.core.file.interface :as file]
[polylith.clj.core.lib.interface :as lib]
[polylith.clj.core.util.interface :as util]
[polylith.clj.core.workspace-clj.brick-dirs :as brick-dirs]
[polylith.clj.core.workspace-clj.brick-paths :as brick-paths]
[polylith.clj.core.workspace-clj.config-from-disk :as config-from-disk]
[polylith.clj.core.workspace-clj.namespaces-from-disk :as ns-from-disk]))
[polylith.clj.core.workspace-clj.namespaces-from-disk :as ns-from-disk]
[polylith.clj.core.workspace-clj.non-top-namespace :as non-top-ns]))

(defn read-base [ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir brick->non-top-namespaces base-name]
(defn read-base [ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir base-name]
(let [base-dir (str ws-dir "/bases/" base-name)
src-dir (str base-dir "/src/" top-src-dir)
test-dir (str base-dir "/test/" top-src-dir)
namespaces (ns-from-disk/namespaces-from-disk src-dir test-dir)
config (config-from-disk/read-config-file ws-type base-dir)
base-src-dirs (brick-dirs/top-src-dirs base-dir top-src-dir config)
base-test-dirs (brick-dirs/top-test-dirs base-dir top-src-dir config)
namespaces (ns-from-disk/namespaces-from-disk base-src-dirs base-test-dirs)
entity-root-path (str "bases/" base-name)
lib-deps (lib/brick-lib-deps ws-dir ws-type config top-namespace ns-to-lib namespaces entity-root-path user-home)]
lib-deps (lib/brick-lib-deps ws-dir ws-type config top-namespace ns-to-lib namespaces entity-root-path user-home)
source-paths (config/source-paths config)
non-top-namespaces (non-top-ns/non-top-namespaces "base" base-name base-dir top-src-dir source-paths)]
(util/ordered-map :name base-name
:type "base"
:maven-repos (:mvn/repos config)
:paths (brick-paths/source-paths base-dir config)
:namespaces namespaces
:non-top-namespaces (brick->non-top-namespaces base-name)
:non-top-namespaces non-top-namespaces
:lib-deps lib-deps)))

(defn read-bases
[ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir brick->non-top-namespaces]
(vec (sort-by :name (map #(read-base ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir brick->non-top-namespaces %)
[ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir]
(vec (sort-by :name (map #(read-base ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir %)
(file/directories (str ws-dir "/bases"))))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns polylith.clj.core.workspace-clj.brick-dirs
(:require [polylith.clj.core.common.interface.config :as config]))

(defn source-dirs [brick-dir paths]
(into []
(comp (filter #(not= "resources" %))
(map #(str brick-dir "/" %)))
paths))

(defn top-source-dirs [brick-dir top-src-dir paths]
(mapv #(str % "/" top-src-dir)
(source-dirs brick-dir paths)))

(defn top-src-dirs [brick-dir top-src-dir config]
(top-source-dirs brick-dir top-src-dir (config/src-paths config)))

(defn top-test-dirs [brick-dir top-src-dir config]
(top-source-dirs brick-dir top-src-dir (config/test-paths config)))
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
(ns polylith.clj.core.workspace-clj.components-from-disk
(:require [polylith.clj.core.common.interface :as common]
[polylith.clj.core.common.interface.config :as config]
[polylith.clj.core.file.interface :as file]
[polylith.clj.core.lib.interface :as lib]
[polylith.clj.core.util.interface :as util]
[polylith.clj.core.workspace-clj.brick-dirs :as brick-dirs]
[polylith.clj.core.workspace-clj.brick-paths :as brick-paths]
[polylith.clj.core.workspace-clj.non-top-namespace :as non-top-ns]
[polylith.clj.core.workspace-clj.config-from-disk :as config-from-disk]
[polylith.clj.core.workspace-clj.namespaces-from-disk :as ns-from-disk]
[polylith.clj.core.workspace-clj.interface-defs-from-disk :as defs-from-disk]))

(defn read-component [ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir component-name interface-ns brick->non-top-namespaces]
(defn read-component [ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir component-name interface-ns]
(let [component-dir (str ws-dir "/components/" component-name)
component-src-dir (str component-dir "/src/" top-src-dir)
component-test-dir (str component-dir "/test/" top-src-dir)
interface-path-name (-> component-src-dir file/directories first)
interface-name (common/path-to-ns interface-path-name)
src-dir (str component-src-dir interface-path-name)
namespaces (ns-from-disk/namespaces-from-disk component-src-dir component-test-dir)
definitions (defs-from-disk/defs-from-disk src-dir interface-ns)
config (config-from-disk/read-config-file ws-type component-dir)
component-top-src-dirs (brick-dirs/top-src-dirs component-dir top-src-dir config)
component-top-test-dirs (brick-dirs/top-test-dirs component-dir top-src-dir config)
interface-path-name (first (mapcat file/directories component-top-src-dirs))
interface-name (common/path-to-ns interface-path-name)
src-dirs (mapv #(str % interface-path-name)
component-top-src-dirs)
namespaces (ns-from-disk/namespaces-from-disk component-top-src-dirs component-top-test-dirs)
definitions (defs-from-disk/defs-from-disk src-dirs interface-ns)
entity-root-path (str "components/" component-name)
lib-deps (lib/brick-lib-deps ws-dir ws-type config top-namespace ns-to-lib namespaces entity-root-path user-home)]
(util/ordered-map :name component-name
:type "component"
:maven-repos (:mvn/repos config)
:paths (brick-paths/source-paths component-dir config)
:namespaces namespaces
:non-top-namespaces (brick->non-top-namespaces component-name)
:lib-deps lib-deps
:interface (util/ordered-map :name interface-name
:definitions definitions))))
lib-deps (lib/brick-lib-deps ws-dir ws-type config top-namespace ns-to-lib namespaces entity-root-path user-home)
paths (brick-paths/source-paths component-dir config)
source-paths (config/source-paths config)
non-top-namespaces (non-top-ns/non-top-namespaces "component" component-name component-dir top-src-dir source-paths)]
(util/ordered-map :name component-name
:type "component"
:maven-repos (:mvn/repos config)
:paths paths
:namespaces namespaces
:non-top-namespaces non-top-namespaces
:lib-deps lib-deps
:interface (util/ordered-map :name interface-name
:definitions definitions))))

(defn read-components [ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir interface-ns brick->non-top-namespaces]
(vec (sort-by :name (map #(read-component ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir % interface-ns brick->non-top-namespaces)
(defn read-components [ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir interface-ns]
(vec (sort-by :name (map #(read-component ws-dir ws-type user-home top-namespace ns-to-lib top-src-dir % interface-ns)
(file/directories (str ws-dir "/components"))))))
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[polylith.clj.core.workspace-clj.profile :as profile]
[polylith.clj.core.workspace-clj.ws-reader :as ws-reader]
[polylith.clj.core.workspace-clj.tag-pattern :as tag-pattern]
[polylith.clj.core.workspace-clj.non-top-namespace :as non-top-ns]
[polylith.clj.core.workspace-clj.bases-from-disk :as bases-from-disk]
[polylith.clj.core.workspace-clj.project-settings :as project-settings]
[polylith.clj.core.workspace-clj.projects-from-disk :as projects-from-disk]
Expand Down Expand Up @@ -98,11 +97,10 @@
user-home (user-config/home-dir)
thousand-separator (user-config/thousand-separator)
user-config-filename (user-config/config-file-path)
brick->non-top-namespaces (non-top-ns/brick->non-top-namespaces ws-dir top-namespace)
project->settings (project-settings/convert ws-config)
ns-to-lib-str (stringify ws-type (or ns-to-lib {}))
components (components-from-disk/read-components ws-dir ws-type user-home top-namespace ns-to-lib-str top-src-dir interface-ns brick->non-top-namespaces)
bases (bases-from-disk/read-bases ws-dir ws-type user-home top-namespace ns-to-lib-str top-src-dir brick->non-top-namespaces)
components (components-from-disk/read-components ws-dir ws-type user-home top-namespace ns-to-lib-str top-src-dir interface-ns)
bases (bases-from-disk/read-bases ws-dir ws-type user-home top-namespace ns-to-lib-str top-src-dir)
name->brick (into {} (comp cat (map (juxt :name identity))) [components bases])
projects (projects-from-disk/read-projects ws-dir ws-type name->brick project->settings user-input user-home)
profile-to-settings (profile/profile-to-settings ws-dir aliases name->brick user-home)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@

(defn defs-from-disk
"Example of a src-dir: ./components/workspace-clj/src/polylith/clj/core/workspace_clj"
[src-dir interface-ns]
(vec (sort-by (juxt :sub-ns :type :name params)
(mapcat #(interface-from-disk % interface-ns)
(interface-namespaces src-dir interface-ns)))))
[src-dirs interface-ns]
(->> src-dirs
(into []
(comp
(mapcat #(interface-namespaces % interface-ns))
(mapcat #(interface-from-disk % interface-ns))))
(sort-by (juxt :sub-ns :type :name params))
(vec)))
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,37 @@
(defn imports [ns-statements]
(vec (sort (mapcat import (filterv import? ns-statements)))))

(defn skip-slash [path]
(or (str-util/skip-until path "/")
path))

(defn namespace-name [root-dir path]
(when path
(when-let [file-path (-> (subs path (count root-dir))
(str-util/skip-until "/"))]
(skip-slash))]
(-> file-path
(str-util/skip-suffixes [".clj" ".cljc"])
(str/replace "/" ".")
(str/replace "_" "-")))))

(defn ->namespace [root-dir file-path]
(defn ->namespace [source-dir file-path]
(let [content (file/read-file file-path)]
{:name (namespace-name root-dir file-path)
{:name (namespace-name source-dir file-path)
:namespace (-> content first second str)
:file-path file-path
:imports (-> content first imports)}))

(defn source-namespaces-from-disk [root-dir]
(mapv #(->namespace root-dir %)
(-> root-dir
(defn source-namespaces-from-disk [source-dir]
(mapv #(->namespace source-dir %)
(-> source-dir
file/paths-recursively
common/filter-clojure-paths)))

(defn namespaces-from-disk [src-dir test-dir]
(let [src (source-namespaces-from-disk src-dir)
test (source-namespaces-from-disk test-dir)]
(defn namespaces-from-disk [src-dirs test-dirs]
(let [src (vec (mapcat #(source-namespaces-from-disk %)
src-dirs))
test (vec (mapcat #(source-namespaces-from-disk %)
test-dirs))]
(cond-> {}
(seq src) (assoc :src src)
(seq test) (assoc :test test))))
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@
[polylith.clj.core.common.interface :as common]
[polylith.clj.core.util.interface.str :as str-util]))

(defn brick? [path]
(or (str/starts-with? path "components")
(str/starts-with? path "bases")))
(defn is-not-top-ns? [path top-src-dir]
(not (str/starts-with? path top-src-dir)))

(defn as-namespaces [brick-nss]
(map #(str/replace % "_" "-") brick-nss))
(defn non-top-ns-map [brick-type brick-name source-dir path]
(let [non-top-ns (if-let [rev-path (str-util/skip-until (str/reverse path) "/")]
(common/path-to-ns (str/reverse rev-path))
(common/path-to-ns path))]
{:non-top-ns non-top-ns
:file (str brick-type "s/" brick-name "/" source-dir "/" path)}))

(defn non-top-namespace [path top-nss]
(let [parts (str/split path #"/")
brick (first (drop 1 parts))
n#nss (count top-nss)
brick-nss (as-namespaces (take n#nss (drop 3 parts)))]
(when (and (not= brick-nss top-nss)
(not (contains? #{"data_readers.clj"
"data_readers.cljc"} (last parts))))
[{:brick brick
:file path
:non-top-ns (str/join "." brick-nss)}])))
(defn non-data-reader-file? [path]
(let [filename (last (str/split path #"/"))]
(not (contains? #{"data_readers.clj"
"data_readers.cljc"} filename))))

(defn add-non-top-ns [result {:keys [brick non-top-ns file]}]
(if (contains? result brick)
(assoc result brick (conj (result brick) {:non-top-ns non-top-ns :file file}))
(assoc result brick #{{:non-top-ns non-top-ns :file file}})))
(defn non-top-namespaces-for-source [brick-type brick-name brick-dir top-src-dir source-dir]
(let [path (str brick-dir "/" source-dir)
path-with-slash (str path "/")]
(->> path
(file/files-recursively)
(into []
(comp
(filter #(and (not (file/directory? %))
(non-data-reader-file? (str %))))
(map #(str-util/skip-prefix (str %) path-with-slash))
(filter #(is-not-top-ns? % top-src-dir))
(map #(non-top-ns-map brick-type brick-name source-dir %)))))))

(defn brick->non-top-namespaces [ws-dir top-namespace]
(let [top-nss (str/split top-namespace #"\.")
non-nss (set (mapcat #(non-top-namespace % top-nss)
(filterv brick?
(common/filter-clojure-paths
(map #(str-util/skip-prefix % (str ws-dir "/"))
(file/paths-recursively ws-dir))))))]
(reduce add-non-top-ns {} non-nss)))
(defn non-top-namespaces [brick-type brick-name brick-dir top-src-dir source-paths]
(let [namespaces (mapcat #(non-top-namespaces-for-source brick-type brick-name brick-dir top-src-dir %)
(filter #(not= "resources" %)
source-paths))]
(when (seq namespaces)
(vec namespaces))))
Loading

0 comments on commit adf957c

Please sign in to comment.