Skip to content

Commit

Permalink
Merge pull request #122 from liquidz/dev
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
liquidz authored Nov 17, 2021
2 parents 9b664f4 + e10457a commit 6c0d217
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: 0918nobita/setup-cljstyle@v0.5.2
- uses: 0918nobita/setup-cljstyle@v0.5.4
with:
cljstyle-version: 0.15.0
- run: cljstyle check
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ All notable changes to this project will be documented in this file. This change

== Unreleased (dev)

== 1.3.0 (2021-11-18)

// {{{
=== Added
* https://github.com/liquidz/antq/issues/115[#115]: Added support for detecting libraries in `:local/root` dependencies.

=== Changed
* Bumped tools.deps.alpha to 0.12.1071.

=== Fixed
* https://github.com/liquidz/antq/issues/109[#109]: Fixed to correctly check versions of libraries in private repositories.
// }}}

== 1.2.0 (2021-11-06)
// {{{
=== Added
Expand Down
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ From Clojure CLI ver `1.10.3.933`, https://clojure.org/reference/deps_and_cli#to
[source,sh]
----
# install
clojure -Ttools install com.github.liquidz/antq '{:git/tag "1.2.0"}' :as antq
clojure -Ttools install com.github.liquidz/antq '{:git/tag "1.3.0"}' :as antq
# uninstall
clojure -Ttools remove :tool antq
# execute
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
org.clojure/data.xml {:mvn/version "0.2.0-alpha6"}
org.clojure/data.zip {:mvn/version "1.0.0"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/tools.deps.alpha {:mvn/version "0.12.1067"}
org.clojure/tools.deps.alpha {:mvn/version "0.12.1071"}
org.clojure/data.json {:mvn/version "2.4.0"}
clj-commons/clj-yaml {:mvn/version "0.7.107"}
version-clj/version-clj {:mvn/version "2.0.2"}
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.liquidz</groupId>
<artifactId>antq</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<name>antq</name>
<description>Point out your outdated dependencies</description>
<url>https://github.com/liquidz/antq</url>
Expand Down Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>org.clojure</groupId>
<artifactId>tools.deps.alpha</artifactId>
<version>0.12.1067</version>
<version>0.12.1071</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
Expand Down
98 changes: 73 additions & 25 deletions src/antq/dep/clojure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
[antq.util.dep :as u.dep]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.deps.alpha :as alpha]
[clojure.tools.deps.alpha.extensions.git :as git]
[clojure.walk :as walk]))

(def ^:private project-file "deps.edn")

(defn- ignore?
[opt]
(and (map? opt)
(contains? opt :local/root)))
(declare load-deps)

(defn user-deps-repository
[]
(let [file (io/file (alpha/user-deps-path))]
(when (.exists file)
(-> file slurp edn/read-string :mvn/repos))))

(defmulti extract-type-and-version
(fn [opt]
Expand Down Expand Up @@ -67,10 +72,33 @@
opt)
opt))

(defn- get-relative-path-by-current-working-directory
[current-working-directory relative-path]
(let [file (io/file current-working-directory)
dir (if (.isDirectory file)
file
(.getParentFile file))]
(if dir
(-> (str (u.dep/relative-path dir)
(System/getProperty "file.separator")
relative-path)
(str/replace #"\./" ""))
relative-path)))

(defn- get-local-root-relative-path
[current-file-path opt]
(let [local-root (:local/root opt)]
(if (str/starts-with? local-root "/")
local-root
(get-relative-path-by-current-working-directory
current-file-path local-root))))

(defn extract-deps
[file-path deps-edn-content-str]
[file-path deps-edn-content-str & [loaded-dir-set]]
(let [deps (atom [])
edn (edn/read-string deps-edn-content-str)]
edn (edn/read-string deps-edn-content-str)
loaded-dir-set (or loaded-dir-set (atom #{}))
cross-project-repositories (user-deps-repository)]
(walk/postwalk (fn [form]
(when (and (sequential? form)
(#{:deps :extra-deps :replace-deps :override-deps} (first form))
Expand All @@ -81,25 +109,45 @@
(swap! deps concat)))
form)
edn)
(for [[dep-name opt] @deps
:let [opt (adjust-version-via-deduction dep-name opt)
type-and-version (extract-type-and-version opt)]
:when (and (not (ignore? opt))
(string? (:version type-and-version))
(seq (:version type-and-version)))]
(-> {:project :clojure
:file file-path
:name (if (qualified-symbol? dep-name)
(str dep-name)
(str dep-name "/" dep-name))
:repositories (:mvn/repos edn)}
(merge type-and-version)
(r/map->Dependency)))))
(->> @deps
(mapcat (fn [[dep-name opt]]
(let [opt (adjust-version-via-deduction dep-name opt)
type-and-version (extract-type-and-version opt)]
(cond
(not (map? opt))
[nil]

(contains? opt :local/root)
(let [path (get-local-root-relative-path file-path opt)]
(load-deps path loaded-dir-set))

(and (string? (:version type-and-version))
(seq (:version type-and-version)))
(-> {:project :clojure
:file file-path
:name (if (qualified-symbol? dep-name)
(str dep-name)
(str dep-name "/" dep-name))
:repositories (merge cross-project-repositories
(:mvn/repos edn))}
(merge type-and-version)
(r/map->Dependency)
(vector))

:else
[nil]))))
(remove nil?))))

(defn load-deps
([] (load-deps "."))
([dir]
(let [file (io/file dir project-file)]
(when (.exists file)
(extract-deps (u.dep/relative-path file)
(slurp file))))))
([dir] (load-deps dir (atom #{})))
([dir loaded-dir-set]
(let [dir (u.dep/normalize-path dir)]
;; Avoid infinite loop
(when-not (contains? @loaded-dir-set dir)
(swap! loaded-dir-set conj dir)
(let [file (io/file dir project-file)]
(when (.exists file)
(extract-deps (u.dep/relative-path file)
(slurp file)
loaded-dir-set)))))))
19 changes: 19 additions & 0 deletions src/antq/util/dep.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,22 @@
(defmethod normalize-by-name :default
[dep]
dep)

(defn normalize-path
[^String path]
(let [sep (System/getProperty "file.separator")]
(loop [[v :as elements] (seq (.split path sep))
accm []]
(if-not v
(str/join sep accm)
(recur (rest elements)
(condp = v
"." (cond
(seq accm) accm
(seq (rest elements)) accm
:else (conj accm v))

".." (if (seq accm)
(vec (butlast accm))
(conj accm v))
(conj accm v)))))))
5 changes: 5 additions & 0 deletions src/antq/util/env.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns antq.util.env)

(defn getenv
[x]
(System/getenv x))
23 changes: 23 additions & 0 deletions src/antq/util/leiningen.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns antq.util.leiningen
(:require
[antq.util.env :as u.env]
[clojure.string :as str]))

(defn- env-name
"cf. https://github.com/technomancy/leiningen/blob/master/doc/DEPLOY.md#credentials-in-the-environment"
[kw]
(cond
(and (qualified-keyword? kw)
(= "env" (namespace kw)))
(str/upper-case (name kw))

(= :env kw)
"LEIN_PASSWORD"

:else
nil))

(defn env
[kw]
(some-> (env-name kw)
(u.env/getenv)))
42 changes: 39 additions & 3 deletions src/antq/util/maven.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[antq.constant :as const]
[antq.log :as log]
[antq.util.leiningen :as u.lein]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.deps.alpha.util.maven :as deps.util.maven]
Expand All @@ -11,6 +12,9 @@
Model
Scm)
org.apache.maven.model.io.xpp3.MavenXpp3Reader
(org.apache.maven.settings
Server
Settings)
(org.eclipse.aether
DefaultRepositorySystemSession
RepositorySystem)
Expand Down Expand Up @@ -49,6 +53,35 @@
(normalize-repos))
:snapshots? (snapshot? (:version dep))})

(defn ensure-username-or-password
[x]
(if (string? x)
x
(or (u.lein/env x)
(str x))))

(defn- ^Server new-repository-server
[{:keys [id username password]}]
(doto (Server.)
(.setId id)
(.setUsername (ensure-username-or-password username))
(.setPassword (ensure-username-or-password password))))

(defn ^Settings get-maven-settings
[opts]
(let [settings ^Settings (deps.util.maven/get-settings)
server-ids (set (map #(.getId %) (.getServers settings)))]
;; NOTE
;; In Leiningen, authentication information is defined in project.clj instead of ~/.m2/settings.xml,
;; so if there is authentication information in `:repositories`, apply to `settings`
(doseq [[id {:keys [username password]}] (:repositories opts)]
(when (and username
password
(not (contains? server-ids id)))
(.addServer settings
(new-repository-server {:id id :username username :password password}))))
settings))

(def ^TransferListener custom-transfer-listener
"Copy from clojure.tools.deps.alpha.util.maven/console-listener
But no outputs for `transferStarted`"
Expand All @@ -68,12 +101,13 @@
(let [lib (cond-> name (string? name) symbol)
local-repo deps.util.maven/default-local-repo
system ^RepositorySystem (deps.util.session/retrieve :mvn/system #(deps.util.maven/make-system))
session ^DefaultRepositorySystemSession (deps.util.maven/make-session system local-repo)
settings ^Settings (get-maven-settings opts)
session ^DefaultRepositorySystemSession (deps.util.maven/make-session system settings local-repo)
;; Overwrite TransferListener not to show "Downloading" messages
_ (.setTransferListener session custom-transfer-listener)
;; c.f. https://stackoverflow.com/questions/35488167/how-can-you-find-the-latest-version-of-a-maven-artifact-from-java-using-aether
artifact (deps.util.maven/coord->artifact lib {:mvn/version version})
remote-repos (deps.util.maven/remote-repos (:repositories opts))]
remote-repos (deps.util.maven/remote-repos (:repositories opts) settings)]
{:system system
:session session
:artifact artifact
Expand All @@ -93,7 +127,9 @@
(catch java.net.ConnectException e
(if (= "Operation timed out" (.getMessage e))
(log/warning (str "Fetching pom from " url " failed because it timed out, retrying"))
(throw e))))
(throw e)))
(catch java.io.IOException e
(log/warning (str "Fetching pom from " url " failed because of the following error: " (.getMessage e)))))
(recur (inc i))))))

(defn ^String get-url
Expand Down
3 changes: 2 additions & 1 deletion test/antq/dep/babashka_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
:file "test/resources/dep/bb.edn"
:name "bb/core"
:version "1.0.0"
:project :clojure})]
:project :clojure
:repositories nil})]
(sut/load-deps "test/resources/dep"))))
35 changes: 29 additions & 6 deletions test/antq/dep/clojure_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
[antq.dep.clojure :as sut]
[antq.record :as r]
[clojure.java.io :as io]
[clojure.test :as t]))
[clojure.test :as t]
[clojure.tools.deps.alpha :as alpha]))

(def ^:private file-path
"path/to/deps.edn")
;; "path/to/deps.edn"
(.getAbsolutePath (io/file (io/resource "dep/deps.edn"))))

(defn- java-dependency
[m]
Expand Down Expand Up @@ -47,17 +49,38 @@
:extra {:url "https://github.com/example/sha.git"}})
(git-sha-dependency {:name "git-sha/git-sha" :version "dummy-git-sha"
:extra {:url "https://github.com/example/git-sha.git"}})
(git-sha-dependency {:name "com.github.liquidz/dummy"
:version "dummy-inferring-url"
:extra {:url "https://github.com/liquidz/dummy.git"}})
(git-tag-dependency {:name "tag-short-sha/tag-short-sha" :version "v1.2.3"
:extra {:url "https://github.com/example/tag-short.git"
:sha "123abcd"}})
(git-tag-dependency {:name "git-tag-long-sha/git-tag-long-sha" :version "v2.3.4"
:extra {:url "https://github.com/example/git-tag-long.git"
:sha "1234567890abcdefghijklmnopqrstuvwxyz1234"}})}
:sha "1234567890abcdefghijklmnopqrstuvwxyz1234"}})
(git-sha-dependency {:name "com.github.liquidz/dummy"
:version "dummy-inferring-url"
:extra {:url "https://github.com/liquidz/dummy.git"}})
(java-dependency {:name "local/core" :version "9.9.9"
:file (.getAbsolutePath (io/file (io/resource "dep/local/deps.edn")))
:repositories nil})
(java-dependency {:name "local/nested-core" :version "8.8.8"
:file (.getAbsolutePath (io/file (io/resource "dep/local/nested/deps.edn")))
:repositories nil})}
(set deps)))))

(t/deftest extract-deps-cross-project-configuration-test
(let [cross-project-path (.getAbsolutePath
(io/file
(.getParentFile (io/file (io/resource "dep/deps.edn")))
"cross-project"
"deps.edn"))
content (pr-str '{:deps {foo/bar {:mvn/version "0.0.1"}}})]
(with-redefs [alpha/user-deps-path (constantly cross-project-path)]
(t/is (= [(java-dependency
{:name "foo/bar"
:version "0.0.1"
:file "dummy"
:repositories {"cross-project" {:url "https://cross-project.example.com"}}})]
(sut/extract-deps "dummy" content))))))

(t/deftest extract-deps-unexpected-test
(t/is (empty? (sut/extract-deps file-path "[:deps \"foo\"]")))
(t/is (empty? (sut/extract-deps file-path "{:deps \"foo\"}")))
Expand Down
Loading

0 comments on commit 6c0d217

Please sign in to comment.