Skip to content

Commit

Permalink
Add forced focus option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaspazza committed Sep 17, 2024
1 parent 197c707 commit 62f3fe1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ NOTE: You must specify `groupId/artifactId` for Java dependencies.

WARNING: `focus` option is prefer than `exclude` option.

If you want to focus the upgrade on specific version of dependency, you can use --focus=ARTIFACT_NAME[@VERSION]
E.g. `--focus=com.github.liquidz/[email protected]`
Will set antq dep to version 50.2.0, even if that version doesn't exist

=== --skip=PROJECT_TYPE
Skip to search specified project files.
Must be one of `boot`, `clojure-cli`, `github-action`, `pom`, `shadow-cljs` and `leiningen`.
Expand Down
44 changes: 39 additions & 5 deletions src/antq/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,40 @@
[nil "--changes-in-table"]
[nil "--transitive"]])

(defn- retrieve-artifact-name
"Retrieve artifact name from artifact string, which may contain version after `@` sign"
[artifact]
(let [specific-version (str/split (str artifact) #"@" 2)]
(if (some? (second specific-version)) (first specific-version) artifact)))

(defn forced-artifacts
"Forced artifacts are coming from focus param and contain specific version targeted with @"
[options]
(->> (:focus options)
(remove (fn [artifact]
(-> (str/split (str artifact) #"@" 2)
second
not)))
(map (fn [art]
(let [[name ver] (str/split (str art) #"@" 2)]
{:name name, :latest-version ver})))))

(defn forced-version
"Returns forced version if exists for `dep`, otherwise nil"
[dep forced-artifacts]
(when-let [matching-artifact (some (fn [artifact]
(when (= (:name artifact) (:name dep))
artifact))
forced-artifacts)]
(:latest-version matching-artifact)))

(defn skip-artifacts?
[dep options]
(let [exclude-artifacts (set (:exclude options []))
focus-artifacts (set (:focus options []))]
focus-artifacts (->> []
(:focus options)
(map retrieve-artifact-name)
set)]
(cond
;; `focus` is prefer than `exclude`
(seq focus-artifacts)
Expand Down Expand Up @@ -151,13 +181,16 @@
(log/info))))

(defn- assoc-latest-version
[dep options]
[dep options forced-artifacts]
(let [vers (cond->> (:_versions dep)
(not (ver/under-development? (:version dep)))
(drop-while ver/under-development?))
vers (remove-skipping-versions vers dep options)
latest-version (first vers)]
(assoc dep :latest-version latest-version)))
(if-let [forced-version (and (seq forced-artifacts)
(forced-version dep forced-artifacts))]
(assoc dep :latest-version forced-version :forced? true)
(assoc dep :latest-version latest-version))))

(defn- dissoc-no-longer-used-keys
[dep]
Expand Down Expand Up @@ -194,7 +227,7 @@
_ (report/init-progress uniq-deps options)
uniq-deps-with-vers (doall (pmap #(assoc-versions % options) uniq-deps))
_ (report/deinit-progress uniq-deps options)
assoc-latest-version* #(assoc-latest-version % options)
assoc-latest-version* #(assoc-latest-version % options (forced-artifacts options))
version-checked-deps (->> org-deps
(pmap #(complete-versions-by % uniq-deps-with-vers))
(map (comp dissoc-no-longer-used-keys
Expand All @@ -204,7 +237,8 @@
(keep :parent)
(set))]
(->> version-checked-deps
(remove #(and (ver/latest? %)
(remove #(and (not (:forced? %))
(ver/latest? %)
(not (contains? parent-dep-names (:name %))))))))

(defn assoc-changes-url
Expand Down
23 changes: 22 additions & 1 deletion test/antq/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@
false "foo"
true "foo/bar"))

(t/testing "focus works with specified version `@`"
(t/are [expected in] (= expected (sut/skip-artifacts? (r/map->Dependency {:name in})
{:focus ["org.clojure/clojure" "[email protected]"]}))
false "org.clojure/clojure"
true "org.clojure/foo"
true "foo/clojure"
false "foo"
true "foo/bar"))
(t/testing "`focus` shoud be prefer than `exclude`"
(t/is (false? (sut/skip-artifacts? (r/map->Dependency {:name "org.clojure/clojure"})
{:exclude ["org.clojure/clojure"]
Expand Down Expand Up @@ -184,7 +192,13 @@
(t/testing "[email protected] should be excluded"
(t/is (= [(test-dep {:name "alice" :version "1.0.0" :latest-version "2.0.0"})
(test-dep {:name "bob" :version "2.0.0" :latest-version "3.0.0"})]
(sut/outdated-deps deps {:exclude ["[email protected]"]}))))))
(sut/outdated-deps deps {:exclude ["[email protected]"]}))))
(t/testing "alice is focused so only this dep should be kept"
(t/is (= [(test-dep {:name "alice" :version "1.0.0" :latest-version "3.0.0"})]
(sut/outdated-deps deps {:focus ["alice"]}))))
(t/testing "focus containing specific version, should force it (0.5.0) even when newer exists (3.0.0)"
(t/is (= [(test-dep {:name "alice" :version "1.0.0" :latest-version "0.5.0" :forced? true})]
(sut/outdated-deps deps {:focus ["[email protected]"]}))))))

(t/deftest assoc-changes-url-test
(let [dummy-dep {:type :java :name "foo/bar" :version "1" :latest-version "2"}]
Expand Down Expand Up @@ -309,3 +323,10 @@
(str/trim
(with-out-str
(sut/latest {:type :test :name 'foo/bar}))))))

(t/deftest forced-artifacts-test
(t/testing "default"
(t/is [] (sut/forced-artifacts {:focus ["foo"]}))
(t/is [{:name "foo" :latest-version "2.0.0"}] (sut/forced-artifacts {:focus ["[email protected]"]}))
(t/is [{:name "foo" :latest-version "2.0.0"}
{:name "foo/zbar2" :latest-version "2"}] (sut/forced-artifacts {:focus ["[email protected]" "foo" "foo/bar" "foo/zbar2@2"]}))))

0 comments on commit 62f3fe1

Please sign in to comment.