Skip to content

Commit 62f3fe1

Browse files
committed
Add forced focus option
1 parent 197c707 commit 62f3fe1

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ NOTE: You must specify `groupId/artifactId` for Java dependencies.
210210

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

213+
If you want to focus the upgrade on specific version of dependency, you can use --focus=ARTIFACT_NAME[@VERSION]
214+
E.g. `--focus=com.github.liquidz/[email protected]`
215+
Will set antq dep to version 50.2.0, even if that version doesn't exist
216+
213217
=== --skip=PROJECT_TYPE
214218
Skip to search specified project files.
215219
Must be one of `boot`, `clojure-cli`, `github-action`, `pom`, `shadow-cljs` and `leiningen`.

src/antq/core.clj

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,40 @@
101101
[nil "--changes-in-table"]
102102
[nil "--transitive"]])
103103

104+
(defn- retrieve-artifact-name
105+
"Retrieve artifact name from artifact string, which may contain version after `@` sign"
106+
[artifact]
107+
(let [specific-version (str/split (str artifact) #"@" 2)]
108+
(if (some? (second specific-version)) (first specific-version) artifact)))
109+
110+
(defn forced-artifacts
111+
"Forced artifacts are coming from focus param and contain specific version targeted with @"
112+
[options]
113+
(->> (:focus options)
114+
(remove (fn [artifact]
115+
(-> (str/split (str artifact) #"@" 2)
116+
second
117+
not)))
118+
(map (fn [art]
119+
(let [[name ver] (str/split (str art) #"@" 2)]
120+
{:name name, :latest-version ver})))))
121+
122+
(defn forced-version
123+
"Returns forced version if exists for `dep`, otherwise nil"
124+
[dep forced-artifacts]
125+
(when-let [matching-artifact (some (fn [artifact]
126+
(when (= (:name artifact) (:name dep))
127+
artifact))
128+
forced-artifacts)]
129+
(:latest-version matching-artifact)))
130+
104131
(defn skip-artifacts?
105132
[dep options]
106133
(let [exclude-artifacts (set (:exclude options []))
107-
focus-artifacts (set (:focus options []))]
134+
focus-artifacts (->> []
135+
(:focus options)
136+
(map retrieve-artifact-name)
137+
set)]
108138
(cond
109139
;; `focus` is prefer than `exclude`
110140
(seq focus-artifacts)
@@ -151,13 +181,16 @@
151181
(log/info))))
152182

153183
(defn- assoc-latest-version
154-
[dep options]
184+
[dep options forced-artifacts]
155185
(let [vers (cond->> (:_versions dep)
156186
(not (ver/under-development? (:version dep)))
157187
(drop-while ver/under-development?))
158188
vers (remove-skipping-versions vers dep options)
159189
latest-version (first vers)]
160-
(assoc dep :latest-version latest-version)))
190+
(if-let [forced-version (and (seq forced-artifacts)
191+
(forced-version dep forced-artifacts))]
192+
(assoc dep :latest-version forced-version :forced? true)
193+
(assoc dep :latest-version latest-version))))
161194

162195
(defn- dissoc-no-longer-used-keys
163196
[dep]
@@ -194,7 +227,7 @@
194227
_ (report/init-progress uniq-deps options)
195228
uniq-deps-with-vers (doall (pmap #(assoc-versions % options) uniq-deps))
196229
_ (report/deinit-progress uniq-deps options)
197-
assoc-latest-version* #(assoc-latest-version % options)
230+
assoc-latest-version* #(assoc-latest-version % options (forced-artifacts options))
198231
version-checked-deps (->> org-deps
199232
(pmap #(complete-versions-by % uniq-deps-with-vers))
200233
(map (comp dissoc-no-longer-used-keys
@@ -204,7 +237,8 @@
204237
(keep :parent)
205238
(set))]
206239
(->> version-checked-deps
207-
(remove #(and (ver/latest? %)
240+
(remove #(and (not (:forced? %))
241+
(ver/latest? %)
208242
(not (contains? parent-dep-names (:name %))))))))
209243

210244
(defn assoc-changes-url

test/antq/core_test.clj

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@
112112
false "foo"
113113
true "foo/bar"))
114114

115+
(t/testing "focus works with specified version `@`"
116+
(t/are [expected in] (= expected (sut/skip-artifacts? (r/map->Dependency {:name in})
117+
{:focus ["org.clojure/clojure" "[email protected]"]}))
118+
false "org.clojure/clojure"
119+
true "org.clojure/foo"
120+
true "foo/clojure"
121+
false "foo"
122+
true "foo/bar"))
115123
(t/testing "`focus` shoud be prefer than `exclude`"
116124
(t/is (false? (sut/skip-artifacts? (r/map->Dependency {:name "org.clojure/clojure"})
117125
{:exclude ["org.clojure/clojure"]
@@ -184,7 +192,13 @@
184192
(t/testing "[email protected] should be excluded"
185193
(t/is (= [(test-dep {:name "alice" :version "1.0.0" :latest-version "2.0.0"})
186194
(test-dep {:name "bob" :version "2.0.0" :latest-version "3.0.0"})]
187-
(sut/outdated-deps deps {:exclude ["[email protected]"]}))))))
195+
(sut/outdated-deps deps {:exclude ["[email protected]"]}))))
196+
(t/testing "alice is focused so only this dep should be kept"
197+
(t/is (= [(test-dep {:name "alice" :version "1.0.0" :latest-version "3.0.0"})]
198+
(sut/outdated-deps deps {:focus ["alice"]}))))
199+
(t/testing "focus containing specific version, should force it (0.5.0) even when newer exists (3.0.0)"
200+
(t/is (= [(test-dep {:name "alice" :version "1.0.0" :latest-version "0.5.0" :forced? true})]
201+
(sut/outdated-deps deps {:focus ["[email protected]"]}))))))
188202

189203
(t/deftest assoc-changes-url-test
190204
(let [dummy-dep {:type :java :name "foo/bar" :version "1" :latest-version "2"}]
@@ -309,3 +323,10 @@
309323
(str/trim
310324
(with-out-str
311325
(sut/latest {:type :test :name 'foo/bar}))))))
326+
327+
(t/deftest forced-artifacts-test
328+
(t/testing "default"
329+
(t/is [] (sut/forced-artifacts {:focus ["foo"]}))
330+
(t/is [{:name "foo" :latest-version "2.0.0"}] (sut/forced-artifacts {:focus ["[email protected]"]}))
331+
(t/is [{:name "foo" :latest-version "2.0.0"}
332+
{:name "foo/zbar2" :latest-version "2"}] (sut/forced-artifacts {:focus ["[email protected]" "foo" "foo/bar" "foo/zbar2@2"]}))))

0 commit comments

Comments
 (0)