From 7531d14192d457b89045773e1057ee5172f074b3 Mon Sep 17 00:00:00 2001 From: Florian Schrofner Date: Fri, 23 Dec 2022 12:00:06 +0100 Subject: [PATCH] add gradle cache extractor --- gradle-cache-extractor/gce.clj | 69 ++++++++++++++++++++++++++++++++ gradle-cache-extractor/readme.md | 9 +++++ 2 files changed, 78 insertions(+) create mode 100755 gradle-cache-extractor/gce.clj create mode 100644 gradle-cache-extractor/readme.md diff --git a/gradle-cache-extractor/gce.clj b/gradle-cache-extractor/gce.clj new file mode 100755 index 0000000..6831b6d --- /dev/null +++ b/gradle-cache-extractor/gce.clj @@ -0,0 +1,69 @@ +#!/usr/bin/env bb + +;;based on: https://github.com/sinsongdev/gradle-cash-to-maven-repo/blob/master/gradle_cache_to_repo.py + +(require '[clojure.tools.cli :refer [parse-opts]]) + +(def cli-options + [["-s" "--src DIRECTORY" "Gradle cache directory" + :missing "Must provide a source directory"] + ["-d" "--dest DIRECTORY" "Maven repository directory" + :missing "Must provide a destination directory"] + ["-a" "--artifact ARTIFACT" "Artifact id"]]) + +(def parsed-args (parse-opts *command-line-args* cli-options)) +(def options (:options parsed-args)) + +(def src (:src options)) +(def dest (:dest options)) + +(def filter + (if-let [filter-option (:artifact options)] + (str/split filter-option #":"))) + +(def group-filter (nth filter 0 nil)) +(def artifact-filter (nth filter 1 nil)) +(def version-filter (nth filter 2 nil)) + +(defn- create-path-string [& segments] + (str/join fs/file-separator segments)) + +(defn- get-dest-dir-string [group artifact version] + (create-path-string + dest + (str/replace group #"\." fs/file-separator) + artifact + version)) + +(defn- process-version [group artifact version] + (if (or (nil? version-filter) (= version version-filter)) + (let [version-dir-string (create-path-string src group artifact version) + version-dir (fs/file version-dir-string) + hash-dirs (fs/list-dir version-dir) + dest-dir-string (get-dest-dir-string group artifact version)] + (fs/create-dirs (fs/file dest-dir-string)) + (doseq [hash-dir hash-dirs + file (fs/list-dir hash-dir)] + (fs/copy file (fs/file (create-path-string dest-dir-string (fs/file-name file))) {:replace-existing true}))))) + +(defn- process-artifact [group artifact] + (if (or (nil? artifact-filter) (= artifact artifact-filter)) + (let [artifact-dir-string (create-path-string src group artifact) + artifact-dir (fs/file artifact-dir-string)] + (doseq [version-dir (fs/list-dir artifact-dir)] + (process-version group artifact (fs/file-name version-dir)))))) + +(defn- process-group [group] + (if (or (nil? group-filter) (= group group-filter)) + (let [group-dir-string (create-path-string src group) + group-dir (fs/file group-dir-string)] + (doseq [artifact-dir (fs/list-dir group-dir)] + (process-artifact group (fs/file-name artifact-dir)))))) + +(defn- process-cache [] + (doseq [group-dir (fs/list-dir (fs/file src))] + (process-group (fs/file-name group-dir)))) + +(if-let [errors (:errors parsed-args)] + (doseq [error errors] (println (str "Error: " error))) + (process-cache)) diff --git a/gradle-cache-extractor/readme.md b/gradle-cache-extractor/readme.md new file mode 100644 index 0000000..2c473f2 --- /dev/null +++ b/gradle-cache-extractor/readme.md @@ -0,0 +1,9 @@ +# Gradle Cache Extractor +Extracts dependencies from your Gradle cache so you can provide them on your self-hosted Maven repository. +You can optionally provide a dependency with `-a` so only that dependency will be extracted. It's also possible to just specify a group id or artifact id, just leave out the parts you're not interested in. + +## Example Usage +```bash +./gce.clj -s "/home/schrofi/.gradle/caches/modules-2/files-2.1" -d "/home/schrofi/Documents/maven_repo" -a "org.bitbucket.consentmanager:android-consentmanager:1.3.3" + +./gce.clj -s "/home/schrofi/.gradle/caches/modules-2/files-2.1" -d "/home/schrofi/Documents/maven_repo" -a "org.bitbucket.consentmanager:android-consentmanager"