Skip to content

Commit

Permalink
Merge pull request #91 from liquidz/feature/gradle
Browse files Browse the repository at this point in the history
Feature/gradle
  • Loading branch information
liquidz committed Jul 13, 2021
2 parents 19be2d1 + 7d8387c commit ecf3ecd
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .cljstyle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:rules {:indentation {:list-indent 1}
:blank-lines {:insert-padding? false}
:types {:enabled? false}
:namespaces {:indent-size 1}}}
:namespaces {:indent-size 1}}
:files {:ignore #{"target"}}}
; vim:ft=clojure
2 changes: 1 addition & 1 deletion .github/workflows/dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
- uses: actions/checkout@v2
- uses: liquidz/antq-action@v1
with:
excludes: 'clojure/brew-install'
excludes: 'clojure/brew-install com.github.seancorfield/depstar'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/target
**/.clj-kondo/.cache
**/.lsp
**/.gradle
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ outdated:
.PHONY: test
test: install
# NOTE: kaocha does not support Clojure 1.8
clojure -M:outdated:nop:1.8 --exclude=clojure/brew-install
clojure -M:outdated:nop:1.8 --exclude=clojure/brew-install --exclude=org.clojure/tools.deps.alpha --exclude=com.github.seancorfield/depstar --exclude=lambdaisland/kaocha
clojure -M:dev:1.9:test
clojure -M:dev:test
script/integration_test.sh
Expand Down
49 changes: 42 additions & 7 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,42 @@ If you'd like to use with 1.7.0 or earlier, please see link:./doc/clojure-1.7.ad

== Supported files

* deps.edn (https://clojure.org/guides/deps_and_cli[Clojure CLI])
* shadow-cljs.edn (http://shadow-cljs.org[Shadow-cljs])
* project.clj (https://leiningen.org[Leiningen])
* build.boot (https://boot-clj.com[Boot])
* pom.xml (https://maven.apache.org[Maven])
* .github/workflows/*.yml (https://github.com/features/actions[GitHub Actions])
* bb.edn (https://book.babashka.org/index.html#_bb_edn[Babashka])
|===
| File | Tool/Service | Note

| deps.edn
| https://clojure.org/guides/deps_and_cli[Clojure CLI]
|

| shadow-cljs.edn
| http://shadow-cljs.org[Shadow-cljs]
|

| project.clj
| https://leiningen.org[Leiningen]
| Provides 2 ways of program and plug-in.

| build.boot
| https://boot-clj.com[Boot]
|

| pom.xml
| https://maven.apache.org[Maven]
|

| .github/workflows/*.yml
| https://github.com/features/actions[GitHub Actions]
|

| bb.edn
| https://book.babashka.org/index.html#_bb_edn[Babashka]
|

| build.gradle
| https://gradle.org[Gradle]
| Experimental. `gradle` command must be installed.

|===

== Usage

Expand Down Expand Up @@ -89,6 +118,11 @@ In another way, you can use the following action.

* https://github.com/liquidz/antq-action

=== Gradle

Antq experimentally supports https://gradle.org[Gradle].
See link:./doc/gradle.adoc[here] for details.

== Options
=== --upgrade
Upgrade outdated versions interactively.
Expand Down Expand Up @@ -207,6 +241,7 @@ When you use `--error-format` option, this reporter will be used automatically.
* link:./doc/avoid-slf4j-warnings.adoc[Avoid SLF4J warnings]
* link:./doc/latest-version-of-a-specific-library.adoc[Latest version of a specific library]
* link:./doc/clojure-1.7.adoc[Antq with Clojure 1.7.0 or earlier]
* link:./doc/gradle.adoc[Work with Gradle]

== License

Expand Down
4 changes: 2 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
:main-opts ["-m" "cloverage.coverage" "--ns-exclude-regex" "leiningen.antq"]}

:depstar
{:extra-deps {com.github.seancorfield/depstar {:mvn/version "RELEASE"}}
{:extra-deps {com.github.seancorfield/depstar {:mvn/version "2.1.245"}}
:ns-default hf.depstar
:exec-args {}}

:deploy
{:extra-deps {slipset/deps-deploy {;:mvn/version "RELEASE"
{:extra-deps {slipset/deps-deploy {;; :mvn/version "RELEASE"
:git/url "https://github.com/slipset/deps-deploy"
:sha "b4359c5d67ca002d9ed0c4b41b710d7e5a82e3bf"}}
:exec-fn deps-deploy.deps-deploy/deploy
Expand Down
48 changes: 48 additions & 0 deletions doc/gradle.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
= Work with Gradle

WARNING: Gradle support is experimental

== Requirements

* `gradle` command must be installed.
** antq alone does not work.

== build.gradle

To work with gradle, you should update your `build.gradle` as following.

[source,build.gradle]
----
plugins {
id 'java-library'
}
repositories {
mavenCentral()
maven {
name = 'clojars'
url = 'https://repo.clojars.org'
}
}
dependencies {
runtimeOnly 'org.clojure:clojure:1.10.3'
runtimeOnly 'com.github.liquidz:antq:latest.release'
}
// OPTIONAL: Used to support detecting repositories
task antq_list_repositories {
doLast {
project.repositories.each { println "ANTQ;" + it.name + ";" + it.url }
}
}
// Main task to run antq
task outdated(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = "clojure.main"
args = ['-m', 'antq.core']
}
----

Then, run `gradle outdated`.
5 changes: 4 additions & 1 deletion src/antq/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
[antq.dep.boot :as dep.boot]
[antq.dep.clojure :as dep.clj]
[antq.dep.github-action :as dep.gh-action]
[antq.dep.gradle :as dep.gradle]
[antq.dep.leiningen :as dep.lein]
[antq.dep.pom :as dep.pom]
[antq.dep.shadow :as dep.shadow]
Expand Down Expand Up @@ -54,6 +55,7 @@
#{"boot"
"clojure-cli"
"github-action"
"gradle"
"pom"
"shadow-cljs"
"leiningen"
Expand Down Expand Up @@ -198,7 +200,8 @@
(when-not (skip "pom") (dep.pom/load-deps %))
(when-not (skip "shadow-cljs") (dep.shadow/load-deps %))
(when-not (skip "leiningen") (dep.lein/load-deps %))
(when-not (skip "babashka") (dep.bb/load-deps %)))
(when-not (skip "babashka") (dep.bb/load-deps %))
(when-not (skip "gradle") (dep.gradle/load-deps %)))
(distinct (:directory options)))))

(defn mark-only-newest-version-flag
Expand Down
69 changes: 69 additions & 0 deletions src/antq/dep/gradle.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(ns antq.dep.gradle
(:require
[antq.log :as log]
[antq.record :as r]
[antq.util.dep :as u.dep]
[clojure.java.io :as io]
[clojure.java.shell :as sh]
[clojure.string :as str]))

(def gradle-command "gradle")
(def ^:private project-file "build.gradle")
(def ^:private dep-regexp #"^[^-]\-+\s")

(defn- get-repositories
[file-path]
(let [{:keys [exit out]} (sh/sh gradle-command "--build-file" file-path
"antq_list_repositories")]
(when (= 0 exit)
(->> (str/split-lines out)
(filter #(str/starts-with? % "ANTQ;"))
(map #(str/split % #";" 3))
(reduce (fn [accm [_ repo-name url]]
(assoc accm repo-name {:url url})) {})))))

(defn- filter-deps-from-gradle-dependencies
[file-path]
(let [{:keys [exit out]} (sh/sh gradle-command
"--build-file" file-path
"--quiet"
"dependencies")]
(if (= 0 exit)
(->> (str/split-lines out)
(filter seq)
(filter #(re-seq dep-regexp %))
(map #(str/replace % dep-regexp ""))
(map #(first (str/split % #" " 2)))
(set))
(throw (ex-info "Failed to run gradle" {:exit exit})))))

(defn- convert-grandle-dependency
"e.g. dep-str: 'org.clojure:clojure:1.10.0'"
[file-path dep-str]
(let [[group-id artifact-id version] (str/split dep-str #":" 3)]
(when (and group-id artifact-id version)
(r/map->Dependency {:project :gradle
:type :java
:file file-path
:name (str group-id "/" artifact-id)
:version version}))))

(defn extract-deps
[relative-file-path absolute-file-path]
(try
(let [repos (get-repositories absolute-file-path)
deps (filter-deps-from-gradle-dependencies absolute-file-path)
deps (keep #(convert-grandle-dependency relative-file-path %) deps)
deps (map #(assoc % :repositories repos) deps)]
deps)
(catch Exception ex
(log/error (.getMessage ex))
nil)))

(defn load-deps
([] (load-deps "."))
([dir]
(let [file (io/file dir project-file)]
(when (.exists file)
(extract-deps (u.dep/relative-path file)
(.getAbsolutePath file))))))
54 changes: 54 additions & 0 deletions test/antq/dep/gradle_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(ns antq.dep.gradle-test
(:require
[antq.dep.gradle :as sut]
[antq.record :as r]
[clojure.java.io :as io]
[clojure.test :as t]))

(def ^:private file-path
"path/to/build.gradle")

(def ^:private expected-repos
{"MavenRepo" {:url "https://repo.maven.apache.org/maven2/"}
"clojars" {:url "https://repo.clojars.org"}})

(defn- java-dependency
[m]
(r/map->Dependency (merge {:project :gradle
:type :java
:file file-path
:repositories expected-repos}
m)))

(def ^:private defined-deps
[(java-dependency {:name "org.ajoberstar/jovial" :version "0.3.0"})
(java-dependency {:name "org.clojure/tools.namespace" :version "1.0.0"})
(java-dependency {:name "org.clojure/clojure" :version "1.10.0"})])

(t/deftest extract-deps-test
(let [deps (sut/extract-deps
file-path
(.getPath (io/resource "dep/build.gradle")))
defined-deps (set defined-deps)
actual-deps (set deps)]
;; NOTE: Gradle on local additionally detects `nrepl/nrepl`
;; And also, gradle on GitHub Actions additionally detects `org.clojure/java.classpath`
;; So we check only dependencies which is explicitly defined in buld.gradle.
(t/is (every? #(contains? actual-deps %) defined-deps))))

(t/deftest extract-deps-without-repositories-test
(let [deps (sut/extract-deps
file-path
(.getPath (io/resource "dep/build_no_repo.gradle")))
defined-deps (->> defined-deps
(map #(assoc % :repositories nil))
(set))
actual-deps (set deps)]
(t/is (every? #(contains? actual-deps %) defined-deps))))

(t/deftest extract-deps-command-error-test
(with-redefs [sut/gradle-command "__non-existing-command__"]
(let [deps (sut/extract-deps
file-path
(.getPath (io/resource "dep/build.gradle")))]
(t/is (nil? deps)))))
25 changes: 25 additions & 0 deletions test/resources/dep/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id 'java-library'
}

repositories {
mavenCentral()
maven {
name = 'clojars'
url = 'https://repo.clojars.org'
}
}

dependencies {
implementation 'org.clojure:clojure:1.10.0'

testRuntimeOnly 'org.ajoberstar:jovial:0.3.0'

testImplementation 'org.clojure:tools.namespace:1.0.0'
}

task antq_list_repositories {
doLast {
project.repositories.each { println "ANTQ;" + it.name + ";" + it.url }
}
}
23 changes: 23 additions & 0 deletions test/resources/dep/build_no_repo.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
id 'dev.clojurephant.clojure' version '0.6.0'
id 'maven-publish'
}

group = 'net.clojars.myname'
version = '0.1.0-SNAPSHOT'

repositories {
mavenCentral()
maven {
name = 'clojars'
url = 'https://repo.clojars.org'
}
}

dependencies {
implementation 'org.clojure:clojure:1.10.0'

testRuntimeOnly 'org.ajoberstar:jovial:0.3.0'

devImplementation 'org.clojure:tools.namespace:1.0.0'
}

0 comments on commit ecf3ecd

Please sign in to comment.