Skip to content

Commit

Permalink
Cleanup (#473)
Browse files Browse the repository at this point in the history
* Also check for bases when deciding if an import is a library or not.
* Changed from ffil and ddir in switch-ws to via.
* 0.2.20-SNAPSHOT #22
  • Loading branch information
tengstrand authored Jun 22, 2024
1 parent 7e7db4f commit 3b64a09
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 101 deletions.
3 changes: 1 addition & 2 deletions components/help/src/polylith/clj/core/help/summary.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
" Example (shell only):\n"
" switch-ws dir:~/myworkspace\n"
" switch-ws file:../../another/ws.edn\n"
" switch-ws ddir:myws\n"
" switch-ws ffile:myfile\n"
" switch-ws via:myworkspace\n"
" tap\n"
" tap open\n"
" tap clean\n"
Expand Down
12 changes: 4 additions & 8 deletions components/help/src/polylith/clj/core/help/switch_ws.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
" After we have switched workspace, all subsequent commands will append either\n"
" ws-dir:" (s/key "DIR" cm) " or ws-file:" (s/key "FILE" cm) ", depending on whether we switch to a directory or a file.\n"
"\n"
" switch-ws [dir:" (s/key "DIR" cm) "] [file:" (s/key "FILE" cm) "] [ddir:" (s/key "DIRNAME" cm) "] [ffile:" (s/key "FILENAME" cm) "]\n"
" switch-ws [dir:" (s/key "DIR" cm) "] [file:" (s/key "FILE" cm) "] [via:" (s/key "SHORTCUT" cm) "]\n"
"\n"
" " (s/key "DIR" cm) " = Switches to the given workspace directory.\n"
" The prompt will be prefixed with 'dir:' to show this.\n"
Expand All @@ -15,20 +15,16 @@
" created by something like 'poly ws out:ws.edn'.\n"
" The prompt will be prefixed with 'file:' to show this.\n"
"\n"
" " (s/key "DIRNAME" cm) " = Switches to the workspace specified with " (s/key ":dir" cm) " in\n"
" ~/.config/polylith/config.edn under the " (s/key ":ws-shortcuts" cm) " key.\n"
"\n"
" " (s/key "FILENAME" cm) " = Switches to the workspace specified with " (s/key ":file" cm) " in\n"
" ~/.config/polylith/config.edn under the " (s/key ":ws-shortcuts" cm) " key.\n"
" " (s/key "SHORTCUT" cm) " = Switches to the workspace specified under the " (s/key ":ws-shortcuts" cm) " key in\n"
" ~/.config/polylith/config.edn\n"
"\n"
" Example of a config file in ~/.config/polylith/config.edn:\n"
" \n"
"\n"
" Example:\n"
" switch-ws dir:~/myworkspace\n"
" switch-ws file:../../another/ws.edn\n"
" switch-ws ddir:myws\n"
" switch-ws ffile:myfile"))
" switch-ws via:myworkspace"))

(defn print-help [color-mode]
(println (help color-mode)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
(defn description [{:keys [file dir]}]
{:description (or file dir)})

(defn select-dirs [candidate _ _]
(defn select [candidate _ _]
(let [group-id (-> candidate :group :id)]
(map #(c/single-txt (:name %) (c/group group-id) (description %))
(filter :dir (user-config/ws-shortcuts-paths)))))

(defn select-files [candidate _ _]
(let [group-id (-> candidate :group :id)]
(map #(c/single-txt (:name %) (c/group group-id) (description %))
(filter :file (user-config/ws-shortcuts-paths)))))
(user-config/ws-shortcuts-paths))))
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,12 @@
;; switch-ws
(def switch-ws-dir (c/fn-explorer "dir" :switch-ws #'file-explorer/select-edn))
(def switch-ws-file (c/fn-explorer "file" :switch-ws #'file-explorer/select-edn))
(def switch-ws-ddir (c/fn-explorer "ddir" :switch-ws #'ws-shortcuts/select-dirs))
(def switch-ws-ffile (c/fn-explorer "ffile" :switch-ws #'ws-shortcuts/select-files))
(def switch-ws-via (c/fn-explorer "via" :switch-ws #'ws-shortcuts/select))

(defn switch-ws [ws-shortcuts]
(c/single-txt "switch-ws" :switch-ws
(concat [switch-ws-file switch-ws-dir]
(when ws-shortcuts [switch-ws-ddir switch-ws-ffile]))))
(when ws-shortcuts [switch-ws-via]))))

(defn ->profiles [group-id profiles all?]
(let [profile-keys (map :name profiles)]
Expand Down
18 changes: 9 additions & 9 deletions components/shell/src/polylith/clj/core/shell/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
file (assoc :ws-file file)
branch (assoc :branch branch)))

(defn select-file-and-dir [name->config dir file ddir ffile]
(if-let [shortcut (or ddir ffile)]
(let [{:keys [dir file]} (get name->config shortcut)]
(defn select-file-and-dir [name->config dir file via]
(if via
(let [{:keys [dir file]} (get name->config via)]
{:dir (user-config/with-shortcut-root-dir dir)
:file (user-config/with-shortcut-root-dir file)})
{:dir dir :file file}))

(defn switch-ws [user-input dir file ddir ffile local? github? branch]
(defn switch-ws [user-input dir file via local? github? branch]
(let [paths (user-config/ws-shortcuts-paths)
name->config (into {} (map (juxt :name identity) paths))
{:keys [file dir]} (select-file-and-dir name->config dir file ddir ffile)
{:keys [file dir]} (select-file-and-dir name->config dir file via)
input (enhance user-input dir file local? github? branch)]
(reset! ws-dir (if (= "." dir) nil dir))
(reset! ws-file file)
Expand All @@ -75,7 +75,7 @@
(git/current-branch)
"master"))))

(defn start [command-executor {:keys [ws-dir ws-file ddir ffile is-local is-github branch is-tap is-fake-poly] :as user-input}
(defn start [command-executor {:keys [ws-dir ws-file via is-local is-github branch is-tap is-fake-poly] :as user-input}
workspace
color-mode]
(let [reader (jline/reader)
Expand All @@ -86,13 +86,13 @@
(tap/execute "open"))
(println (logo is-fake-poly color-mode))
(reset! engine/ws workspace)
(switch-ws user-input ws-dir ws-file ddir ffile is-local is-github branch)
(switch-ws user-input ws-dir ws-file via is-local is-github branch)
(tap> {:workspace workspace})
(try
(loop []
(flush)
(when-let [line (.readLine reader (prompt))]
(let [{:keys [branch cmd unnamed-args is-local is-github file dir ddir ffile color-mode] :as input} (user-input/extract-arguments (str-util/split-text line))
(let [{:keys [branch cmd unnamed-args is-local is-github file dir via color-mode] :as input} (user-input/extract-arguments (str-util/split-text line))
color-mode (or color-mode (common/color-mode input))
is-local (or is-local local?)
is-github (or is-github github?)
Expand All @@ -101,7 +101,7 @@
(when-not (contains? #{"exit" "quit"} cmd)
(cond
(= "shell" cmd) (println " Can't start a shell inside another shell.")
(= "switch-ws" cmd) (switch-ws input dir file ddir ffile is-local is-github branch)
(= "switch-ws" cmd) (switch-ws input dir file via is-local is-github branch)
(= "tap" cmd) (tap/execute (first unnamed-args))
(str/blank? line) nil
:else (execute-command command-executor input is-local is-github branch color-mode))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@
compact!
dev!
dir
ddir
fake-poly!
fake-sha
fake-tag
file
ffile
get
git-add!
github!
Expand Down Expand Up @@ -103,6 +101,7 @@
top-ns
update!
verbose!
via
with
workspace!
ws
Expand All @@ -116,11 +115,9 @@
:color-mode (as-value color-mode)
:changed-files (as-vector changed-files)
:dir dir
:ddir ddir
:fake-sha fake-sha
:fake-tag fake-tag
:file file
:ffile ffile
:help (as-value help)
:interface interface
:is-all (= "true" all!)
Expand Down Expand Up @@ -163,6 +160,7 @@
:since since
:skip (as-vector skip)
:top-ns top-ns
:via via
:with (as-vector with)
:ws (as-value ws)
:ws-dir ws-dir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(def minor 2)
(def patch 20)
(def revision SNAPSHOT) ;; Set to SNAPSHOT or RELEASE.
(def snapshot 21) ;; Increase by one for every snapshot release, or set to 0 if a release.
(def snapshot 22) ;; Increase by one for every snapshot release, or set to 0 if a release.
;; Also update :snapshot-version: at the top of readme.adoc.
(def snapshot? (= SNAPSHOT revision))

Expand All @@ -37,7 +37,7 @@

(def tool (if system/extended? "polyx" "poly"))

(def date "2024-06-17")
(def date "2024-06-22")

;; Execute 'poly doc version' to see when different changes was introduced.
(def api-version {:breaking 1, :non-breaking 0})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(defn enrich [ws-dir suffixed-top-ns interface-names base-names outdated-libs library->latest-version user-input name-type->keep-lib-version interface-ns
{:keys [name type namespaces lib-deps] :as base}]
(let [{:keys [interface-deps base-deps illegal-deps]} (deps/brick-deps suffixed-top-ns interface-names base-names interface-ns base)
lib-imports (lib-imp/lib-imports suffixed-top-ns interface-names base)
lib-imports (lib-imp/lib-imports suffixed-top-ns interface-names base-names base)
lib-deps (lib/lib-deps-with-latest-version name type lib-deps outdated-libs library->latest-version user-input name-type->keep-lib-version)]
(cond-> (assoc base :lib-deps lib-deps
:lines-of-code (loc/lines-of-code ws-dir namespaces)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(defn enrich [ws-dir suffixed-top-ns interface-names base-names outdated-libs library->latest-version user-input name-type->keep-lib-versions interface-ns
{:keys [name type namespaces lib-deps] :as component}]
(let [{:keys [interface-deps _ illegal-deps]} (deps/brick-deps suffixed-top-ns interface-names base-names interface-ns component)
lib-imports (lib-imp/lib-imports suffixed-top-ns interface-names component)
lib-imports (lib-imp/lib-imports suffixed-top-ns interface-names base-names component)
lines-of-code (loc/lines-of-code ws-dir namespaces)
lib-deps (lib/lib-deps-with-latest-version name type lib-deps outdated-libs library->latest-version user-input name-type->keep-lib-versions)]
(cond-> (assoc component :lib-deps lib-deps
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
(ns ^:no-doc polylith.clj.core.workspace.enrich.lib-imports
(:require [clojure.string :as str]))

(defn library? [import suffixed-top-ns interface-names]
(defn library? [import suffixed-top-ns brick-names]
(if (str/starts-with? import suffixed-top-ns)
(let [interface-ns (subs import (count suffixed-top-ns))
index (str/index-of interface-ns ".")
interface (if (nil? index)
interface-ns
(subs interface-ns 0 index))]
(not (contains? interface-names interface)))
(not (contains? brick-names interface)))
true))

(defn lib-imports-source [suffixed-top-ns interface-names brick source-key]
(vec (sort (filter #(library? % suffixed-top-ns interface-names)
(defn lib-imports-source [suffixed-top-ns brick-names brick source-key]
(vec (sort (filter #(library? % suffixed-top-ns brick-names)
(set (mapcat :imports (-> brick :namespaces source-key)))))))

(defn lib-imports [suffixed-top-ns interface-names brick]
(let [src (lib-imports-source suffixed-top-ns interface-names brick :src)
test (lib-imports-source suffixed-top-ns interface-names brick :test)]
(defn lib-imports [suffixed-top-ns interface-names base-names brick]
(let [brick-names (set (concat interface-names base-names))
src (lib-imports-source suffixed-top-ns brick-names brick :src)
test (lib-imports-source suffixed-top-ns brick-names brick :test)]
(cond-> {}
(seq src) (assoc :src src)
(seq test) (assoc :test test))))
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
[polylith.clj.core.workspace.enrich.lib-imports :as lib-imports]))

(def interface-names #{"article" "comment" "database" "log" "profile" "spec" "tag" "user"})
(def base-names #{"mybase" "thatbase"})

(def component {:name "article"
:type "component"
:namespaces {:src [{:name "article/spec.clj"
:imports ["clojure.realworld.profile.interface"
"clojure.realworld.spec.interface"
"spec-tools.data-spec"]}
{:name "article/interface.clj"
:imports ["clojure.realworld.article.core"
"clojure.realworld.article.spec"]}
{:name "article/store.clj"
:imports ["clojure.java.jdbc"
"clojure.realworld.database.interface"
"clojure.string"
"honeysql.core"]}
{:name "article/core.clj"
:imports ["clj-time.coerce"
"clj-time.core"
"clojure.realworld.article.store"
"clojure.realworld.profile.interface"
"slugger.core"]}]}})
(def mybase {:name "mybase"
:type "base"
:namespaces {:src [{:name "mybase/core.clj"
:imports ["clojure.realworld.profile.interface"
"clojure.realworld.spec.interface"
"clojure.realworld.thatbase.api"
"spec-tools.data-spec"]}
{:name "mybase/interface.clj"
:imports ["clojure.realworld.article.core"
"clojure.realworld.article.spec"]}
{:name "mybase/store.clj"
:imports ["clojure.java.jdbc"
"clojure.realworld.database.interface"
"clojure.string"
"honeysql.core"]}
{:name "mybase/core.clj"
:imports ["clj-time.coerce"
"clj-time.core"
"clojure.realworld.article.store"
"clojure.realworld.profile.interface"
"slugger.core"]}]}})

(deftest lib-imports-src--given-a-component-with-interface-and-library-imports--return-all-imports-except-components
(is (= {:src ["clj-time.coerce"
Expand All @@ -33,4 +35,4 @@
"honeysql.core"
"slugger.core"
"spec-tools.data-spec"]}
(lib-imports/lib-imports "clojure.realworld." interface-names component))))
(lib-imports/lib-imports "clojure.realworld." interface-names base-names mybase))))
15 changes: 5 additions & 10 deletions doc/commands.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ poly help
Example (shell only):
switch-ws dir:~/myworkspace
switch-ws file:../../another/ws.edn
switch-ws ddir:myws
switch-ws ffile:myfile
switch-ws via:myworkspace
tap
tap open
tap clean
Expand Down Expand Up @@ -1025,7 +1024,7 @@ poly help
After we have switched workspace, all subsequent commands will append either
ws-dir:DIR or ws-file:FILE, depending on whether we switch to a directory or a file.
switch-ws [dir:DIR] [file:FILE] [ddir:DIRNAME] [ffile:FILENAME]
switch-ws [dir:DIR] [file:FILE] [via:SHORTCUT]
DIR = Switches to the given workspace directory.
The prompt will be prefixed with 'dir:' to show this.
Expand All @@ -1034,20 +1033,16 @@ poly help
created by something like 'poly ws out:ws.edn'.
The prompt will be prefixed with 'file:' to show this.
DIRNAME = Switches to the workspace specified with :dir in
~/.config/polylith/config.edn under the :ws-shortcuts key.
FILENAME = Switches to the workspace specified with :file in
~/.config/polylith/config.edn under the :ws-shortcuts key.
DIRNAME = Switches to the workspace specified under the :ws-shortcuts key in
~/.config/polylith/config.edn.
Example of a config file in ~/.config/polylith/config.edn:
Example:
switch-ws dir:~/myworkspace
switch-ws file:../../another/ws.edn
switch-ws ddir:myws
switch-ws ffile:myfile
switch-ws via:myworkspace
----

[#tap]
Expand Down
2 changes: 1 addition & 1 deletion doc/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ a| Tells the xref:commands.adoc#libs[libs] where it can find your local Maven re

[[ws-shortcuts]]
a| `:ws-shortcuts`
a| Contains a map with the keys `:root-dir` and `:paths` which are used by the xref:shell#switch-workspace[switching workspace] functionality in the shell. The `:root-dir` stores the root of the paths specified in `:paths` and is prepended to the paths. Each map in `:paths` has either a `:dir` or `:file` key which specifies a path for a workspace. The name of the directory or file will be used by the `ddir:NAME` and `ffile:NAME` arguments, but can be overridden by `:name`.
a| Contains a map with the keys `:root-dir` and `:paths` which are used by the xref:shell#switch-workspace[switching workspace] functionality in the shell. The `:root-dir` stores the root of the paths specified in `:paths` and is prepended to the paths. Each map in `:paths` has either a `:dir` or `:file` key which specifies a path for a workspace. The name of the directory or file will be used by the `via:NAME` argument, but can be overridden by `:name`.

|===

Expand Down
12 changes: 7 additions & 5 deletions doc/shell.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,23 @@ This example assumes that you have cloned the repos https://github.com/polyfy/po

The value of `:root-dir` is added to each path to get a full path.

Now we can switch workspace from the xref:shell.adoc[shell] with `ddir:NAME` or `ffile:NAME` where NAME is the name of the directory/file:
Now we can switch workspace from the xref:shell.adoc[shell] with `via:NAME`
where NAME is the name of the directory/file:

[source,text]
----
polylith$ switch-ws ddir:
polylith$ switch-ws via:
another-ws (../sandbox/another-ws.edn)
backend (examples/multiple-workspaces2/backend)
doc-example (examples/doc-example)
realworld (../clojure-polylith-realworld-example-app)
----

If we type `d` and press kbd:[Tab] followed by kbd:[Enter] we switch to the `doc-example` workspace directory (full path is `/Users/joakimtengstrand/source/polylith/examples/doc-example`):
If we type `d` and press kbd:[Tab] followed by kbd:[Enter] we switch to the `examples/doc-example` workspace directory:

[source,text]
----
polylith$ switch-ws ddir:doc
polylith$ switch-ws via:doc
dir:doc-example$
----

Expand All @@ -244,7 +246,7 @@ A workspace that has been exported to a file, e.g. `poly ws out:another-ws.edn`,

[source,text]
----
polylith$ switch-ws ffile:another-ws
polylith$ switch-ws via:another-ws
file:another-ws
----

Expand Down
Loading

0 comments on commit 3b64a09

Please sign in to comment.