Skip to content

Commit

Permalink
Add support for updating libraries to latest version. Issue #376.
Browse files Browse the repository at this point in the history
  • Loading branch information
tengstrand committed Nov 9, 2023
1 parent f85a99a commit ab4f2f3
Show file tree
Hide file tree
Showing 24 changed files with 159 additions and 59 deletions.
1 change: 1 addition & 0 deletions bases/poly-cli/src/polylith/clj/core/poly_cli/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
(-main "ws" "get:components:account:namespaces:src:core:file-path" "ws-file:../sandbox/furkan.edn" ":no-exit")
(-main "ws" "get:components:api:namespaces:src:core:file-path" "ws-dir:../../Downloads/polylith-0.1.0-alpha9/" ":no-exit")
(-main "check" ":no-exit")
(-main "libs" ":update" ":no-exit")
(-main "libs" ":outdated" ":no-exit")
(-main "doc" "page:polylith-ci-setup" ":no-exit")
(-main "ws-dir:examples/doc-example" ":no-exit")
Expand Down
20 changes: 0 additions & 20 deletions components/antq/src/polylith/clj/core/antq/core.clj

This file was deleted.

8 changes: 6 additions & 2 deletions components/antq/src/polylith/clj/core/antq/ifc.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
;; This interface has the name ifc, just to check that it's allowed!
(ns ^:no-doc polylith.clj.core.antq.ifc
(:require [polylith.clj.core.antq.core :as core]))
(:require [polylith.clj.core.antq.latest :as latest]
[polylith.clj.core.antq.upgrade :as upgrade]))

(defn library->latest-version [configs]
(core/library->latest-version configs))
(latest/library->latest-version configs))

(defn upgrade-all-libs! [workspace color-mode]
(upgrade/upgrade-all-libs! workspace color-mode))
24 changes: 24 additions & 0 deletions components/antq/src/polylith/clj/core/antq/latest.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns ^:no-doc polylith.clj.core.antq.latest
(:require [antq.api :as antq]))

(defn truncate [version type]
(if (= :git-sha type)
(subs version 0 7)
version))

(defn key-value [{:keys [name version latest-version type]}]
[[name (truncate version type)]
(truncate latest-version type)])

(defn configs [{:keys [configs]}]
(let [{:keys [bases components projects]} configs]
(concat bases components projects)))

(defn library->latest-version
"Returns a map where the key is [lib-name lib-version]
and the value is the latest version of the library."
[workspace]
(into {} (map key-value)
(antq/outdated-deps
{:deps (apply merge (mapv #(-> % :deps :deps)
(configs workspace)))})))
46 changes: 46 additions & 0 deletions components/antq/src/polylith/clj/core/antq/upgrade.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(ns ^:no-doc polylith.clj.core.antq.upgrade
(:require [antq.api :as antq]
[clojure.set :as set]
[polylith.clj.core.antq.latest :as latest]
[polylith.clj.core.util.interface.color :as color]))

(defn outdated-libs-in-config [config outdated-libs]
(seq (set/intersection outdated-libs
(-> config :deps :deps keys set))))

(defn keep? [entity-name lib entity-config]
(contains? (set (get-in entity-config [entity-name :keep-lib-versions]))
lib))

(defn keep-lib-version? [entity-name type lib {:keys [bricks projects]}]
(if (= "project" type)
(keep? entity-name lib projects)
(keep? entity-name lib bricks)))

(defn upgrade-dep-in-config [entity-name type filename lib lib->latest-version settings color-mode]
(when-not (keep-lib-version? entity-name type lib settings)
(let [[[_ version] latest-version] (first (filter #(= lib (-> % ffirst symbol))
lib->latest-version))
ok? (get (antq/upgrade-deps! [{:file filename
:dependency {:project :clojure
:type :java
:name (str lib)
:version version
:latest-version latest-version}}])
true)]
(when ok?
(println (str " Updated " lib " to " latest-version " in " (color/entity type entity-name color-mode) "."))))))

(defn upgrade-deps-in-config [{:keys [name type] :as config} outdated-libs lib->latest-version ws-dir settings color-mode]
(let [filename (str ws-dir "/" type "s/" name "/deps.edn")]
(doseq [lib (outdated-libs-in-config config outdated-libs)]
(upgrade-dep-in-config name type filename lib lib->latest-version settings color-mode))))

(defn upgrade-all-libs! [{:keys [ws-dir settings] :as workspace} color-mode]
(let [lib->latest-version (latest/library->latest-version workspace)
outdated-libs (set (mapv #(-> % first symbol)
(keys lib->latest-version)))
outdated-configs (filter #(outdated-libs-in-config % outdated-libs)
(latest/configs workspace))]
(doseq [outdated-config outdated-configs]
(upgrade-deps-in-config outdated-config outdated-libs lib->latest-version ws-dir settings color-mode))))
4 changes: 2 additions & 2 deletions components/api/src/polylith/clj/core/api/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
any of the three APIs may change version, but the ambition is to never break `:api` and `:test-runner`.
With `:ws` it's different, and we know that the workspace structure will change over time, so pay extra
attention every time you bump `clj-poly` and have a look in the `version` component's interface namespace,
attention every time you bump `clj-poly` and have a look at the [versions](https://cljdoc.org/d/polylith/clj-poly/CURRENT/doc/versions) page,
which lists all the changes for different versions.
Examples of a breaking change of the workspace structure:
Expand All @@ -69,7 +69,7 @@
Any changes that only add functionality/attributes, will increase the `:non-breaking` number by one.
If you use a SNAPSHOT version, then you can check [next-release.txt](https://github.com/polyfy/polylith/blob/issue-318/next-release.txt)
to get a summary of all the changes that have been made since the last final release."
to get a summary of all the changes that have been made since the last stable/final release."
{:api version/api-version
:test-runner version/test-runner-api-version
:ws version/ws-api-version})
Expand Down
9 changes: 7 additions & 2 deletions components/command/src/polylith/clj/core/command/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@
":local" ["shell" (assoc user-input :is-local true)]
[cmd user-input])))

(defn execute [{:keys [cmd args name top-ns branch help is-local more page ws is-tap is-git-add is-github is-commit is-all is-outdated is-show-brick is-show-workspace is-show-project is-verbose is-fake-poly get out interface selected-bricks selected-projects unnamed-args ws-file] :as user-input}]
(defn libs [workspace is-outdated is-update color-mode]
(if is-update
(lib/update-all-libs! workspace color-mode)
(lib/print-lib-table workspace is-outdated)))

(defn execute [{:keys [cmd args name top-ns branch help is-local more page ws is-tap is-git-add is-github is-commit is-all is-outdated is-update is-show-brick is-show-workspace is-show-project is-verbose is-fake-poly get out interface selected-bricks selected-projects unnamed-args ws-file] :as user-input}]
(let [color-mode (common/color-mode user-input)
ws-dir (config-reader/workspace-dir user-input)
workspace-fn (workspace-reader-fn)
Expand All @@ -106,7 +111,7 @@
"diff" (diff workspace)
"help" (open-help args is-all is-show-project is-show-brick is-show-workspace toolsdeps1? is-fake-poly color-mode)
"info" (info/info workspace unnamed-args)
"libs" (lib/print-lib-table workspace is-outdated)
"libs" (libs workspace is-outdated is-update color-mode)
"migrate" (migrator/migrate ws-dir workspace)
"overview" (overview/print-table workspace)
"shell" (shell/start execute user-input workspace-fn workspace color-mode)
Expand Down
6 changes: 4 additions & 2 deletions components/help/src/polylith/clj/core/help/libs.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
(ns ^:no-doc polylith.clj.core.help.libs
(:require [polylith.clj.core.help.shared :as s]
[polylith.clj.core.system.interface :as system]
[polylith.clj.core.util.interface.color :as color]))

(defn help [extended? cm]
(str " Shows all libraries that are used in the workspace.\n"
"\n"
" poly libs [" (s/key ":compact" cm) "] [" (s/key ":outdated" cm) "] [out:" (s/key "FILENAME" cm) "]\n"
" poly libs [" (s/key ":compact" cm) "] [" (s/key ":outdated" cm) "] [" (s/key ":update" cm) "] [out:" (s/key "FILENAME" cm) "]\n"
"\n"
" " (s/key ":compact" cm) " = Shows the table in a more compact way.\n"
"\n"
" " (s/key ":outdated" cm) " = Shows the latest version of each library, or blank if up to date.\n"
"\n"
" " (s/key ":update" cm) " = Updates all libraries to the latest version.\n"
"\n"
(if extended?
(str " " (s/key "FILENAME" cm) " = The name of the text or image file to create, containing the\n"
" output from this command. If " (s/key "FILENAME" cm) " ends with .bmp, .wbmp,\n"
Expand Down Expand Up @@ -64,6 +65,7 @@
" poly libs\n"
" poly libs :compact\n"
" poly libs :outdated\n"
" poly libs :update\n"
" poly libs out:libs.txt\n"
" poly doc page:libraries"
(if extended?
Expand Down
1 change: 1 addition & 0 deletions components/help/src/polylith/clj/core/help/summary.clj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
" poly libs\n"
" poly libs :compact\n"
" poly libs :outdated\n"
" poly libs :update\n"
" poly libs out:libs.txt\n"
(if extended?
" poly libs out:libs.png\n"
Expand Down
6 changes: 5 additions & 1 deletion components/lib/src/polylith/clj/core/lib/interface.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns ^:no-doc polylith.clj.core.lib.interface
(:require [polylith.clj.core.lib.core :as core]
(:require [polylith.clj.core.antq.ifc :as antq]
[polylith.clj.core.lib.core :as core]
[polylith.clj.core.lib.size :as size]
[polylith.clj.core.lib.resolve-libs :as resolve-libs]
[polylith.clj.core.lib.text-table.lib-table :as lib-table]))
Expand All @@ -19,6 +20,9 @@
(defn resolve-libs [src-deps override-deps]
(resolve-libs/resolve-libs src-deps override-deps))

(defn update-all-libs! [workspace color-mode]
(antq/upgrade-all-libs! workspace color-mode))

(defn table [workspace]
(let [outdated? (-> workspace :user-input :is-outdated)]
(lib-table/table workspace outdated?)))
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,16 @@
(when (or all? extended?) [info-out]))))

;; libs
(def outdated (c/flag "outdated" :libs))
(def libs-update (c/flag "update" :libs))
(def libs-outdated (c/flag "outdated" :libs))
(def libs-out (c/fn-explorer "out" :libs (file-explorer/select-fn)))
(def libs-skip (c/fn-explorer "skip" :libs #'ws-projects/select))
(def libs-color-mode (c/fn-values "color-mode" :libs #'color-modes/select))

(defn libs [all? extended?]
(c/single-txt "libs" :libs
(concat [outdated]
(when all? [outdated compact libs-skip libs-color-mode])
(concat [libs-update libs-outdated]
(when all? [compact libs-skip libs-color-mode])
(when (or all? extended?) [libs-out]))))

;; test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@

(deftest libs
(is (= (candidates "libs")
[":outdated"])))
[":outdated" ":update"])))

(deftest libs-
(is (= (candidates "libs" :next "")
[":outdated"])))
[":outdated" ":update"])))

(deftest test
(is (= (candidates "test")
Expand Down
2 changes: 1 addition & 1 deletion components/tap/deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
:deps {djblue/portal {:mvn/version "0.48.0"}}
:deps {djblue/portal {:mvn/version "0.49.1"}}
:aliases {:test {:extra-paths []
:extra-deps {}}}}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
skip
tap!
top-ns
update!
verbose!
workspace!
ws
Expand Down Expand Up @@ -136,6 +137,7 @@
(= "true" resources!))
:is-show-workspace (= "true" workspace!)
:is-tap (= "true" tap!)
:is-update (= "true" update!)
:is-verbose (= "true" verbose!)
:more (as-more more)
:name name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(def minor 2)
(def patch 18)
(def revision SNAPSHOT) ;; Set to SNAPSHOT or RELEASE.
(def snapshot 11) ;; Increase by one for every snapshot release, or set to 0 if a release.
(def snapshot 12) ;; Increase by one for every snapshot release, or set to 0 if a release.
(def snapshot? (= SNAPSHOT revision))

(def name-without-rev (str major "." minor "." patch))
Expand All @@ -36,7 +36,7 @@

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

(def date "2023-11-07")
(def date "2023-11-09")

;; Execute 'poly doc version' to see when different changes was introduced.
(def api-version {:breaking 1, :non-breaking 0})
Expand Down
8 changes: 6 additions & 2 deletions doc/commands.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ poly help

[source,text]
----
poly 0.2.18 (2023-11-07) - https://github.com/polyfy/polylith
poly 0.2.18 (2023-11-09) - https://github.com/polyfy/polylith
poly CMD [ARGS] - where CMD [ARGS] are:
Expand Down Expand Up @@ -166,6 +166,7 @@ poly help
poly libs
poly libs :compact
poly libs :outdated
poly libs :update
poly libs out:libs.txt
poly migrate
poly shell
Expand Down Expand Up @@ -883,12 +884,14 @@ poly help
----
Shows all libraries that are used in the workspace.
poly libs [:compact] [:outdated] [out:FILENAME]
poly libs [:compact] [:outdated] [:update] [out:FILENAME]
:compact = Shows the table in a more compact way.
:outdated = Shows the latest version of each library, or blank if up to date.
:update = Updates all libraries to the latest version.
FILENAME = The name of the text file to create, containing the
output from this command.
u u
Expand Down Expand Up @@ -935,6 +938,7 @@ poly help
poly libs
poly libs :compact
poly libs :outdated
poly libs :update
poly libs out:libs.txt
poly doc page:libraries
----
Expand Down
22 changes: 21 additions & 1 deletion doc/libraries.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ image::images/libraries/libs.png[]

An `x` means that the library is added to the `src` context, while `t` means that it's only used from the `test` context.

We can list the outdated libraries by passing in `:outdated` (which uses https://github.com/liquidz/antq[antq] internally):
We can list the outdated libraries by passing in `:outdated`:

[source,shell]
----
Expand All @@ -38,6 +38,26 @@ image::images/libraries/libs-outdated.png[]

{nbsp} +

[#update]
We can also tell the tool to update all libraries to the latest version, in all `deps.edn` files for all bricks and projects:

[source,shell]
----
poly libs :update
----

If

We can ignore updating libraries for a brick or project by adding the `:keep-lib-versions` in `workspace.edn`:

[source,clojure]
----
{...
:bricks {"article" {:keep-lib-versions [djblue/portal]}}}
:projects {"realworld-backend" {:alias "rb"
:keep-lib-versions [clj-jwt/clj-jwt clj-time/clj-time]}
----

Libraries can be specified in three different ways in tools.deps:

[%autowidth]
Expand Down
7 changes: 6 additions & 1 deletion doc/workspace-structure.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ poly ws get:settings
* `:bricks` A map with configuration information per brick where the keys are brick names, specified in `workspace.edn`:
** `:ignore-files` A vector containing file or file paths to ignore, e.g.: `["myfile.clj" "myns/another_file.clj" "com/myns/a-thrird-file.clj"]`.
See xref:validations.adoc#ignore-files[Validations].
** `:keep-lib-versions` A vector containing the library names (as symbols) that should not be affected when executing `poly libs :update`.
See xref:libraries.adoc#update[update libs] and xref:validations.adoc#ignore-files[Validations].
[#color-mode]
* `:color-mode` The color mode specified in `~/.config/polylith/config.edn`.
Expand Down Expand Up @@ -716,6 +717,7 @@ Set to "~/.m2" by default, but can be overridden in `~/.config/polylith/config.e
** `:ignore-files` A vector containing file or file paths to ignore, e.g.: `["myfile.clj" "myns/another_file.clj" "com/myns/a-thrird-file.clj"]`.
All files ending with the specified files () will be ignored, or to be exact, if it's an exact match or if it ends with `/` + the string.
Dashes (-) will be replaced by underscores (_).
** `:keep-lib-versions` A vector containing the library names (as symbols) that should not be affected when executing `poly libs :update`.
** `:necessary` If we get xref:validations.adoc#warning206[warning 206 - Unreadable namespace in brick/project] and know that the brick(s) has to be included in the project, then we can add the necessary bricks(s) to the project in a vector for this key.
** `:test`
*** `:include` Specifies which bricks should be included when running the test command.
Expand Down Expand Up @@ -779,6 +781,7 @@ poly ws get:user-input
:is-show-resources false
:is-show-workspace false
:is-tap false
:is-update false
:is-verbose false
:selected-profiles #{}
:selected-projects #{}
Expand Down Expand Up @@ -898,6 +901,8 @@ Used by `poly help deps :workspace` to show help for the deps command when `work
* `:is-tap` Set to `true` if `:tap` is given.
* `:is-update` Set to `true` if `:update` is given. Used by the xref:commands.adoc#libs[libs] command.
* `:is-verbose` Used in combination with the `test` command to show extra information.
* `:out` Mainly used by the xref:commands.adoc#ws[ws] command, but can also be passed in to the xref:commands.adoc#info[info], xref:commands.adoc#deps[deps], and xref:commands.adoc#libs[libs] commands to generate a text file from the output.
Expand Down
Loading

0 comments on commit ab4f2f3

Please sign in to comment.