Skip to content
Permalink
Browse files

0.1.0 Initial

  • Loading branch information...
tonsky committed Apr 29, 2019
0 parents commit ac8a5808f227d7c18214f3fae8bcf8e2ab26320c
Showing with 276 additions and 0 deletions.
  1. +14 −0 .gitignore
  2. +21 −0 LICENSE
  3. +58 −0 README.md
  4. +3 −0 deps.edn
  5. +5 −0 project.clj
  6. +148 −0 src/uberdeps/api.clj
  7. +27 −0 src/uberdeps/uberjar.clj
@@ -0,0 +1,14 @@
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/
*.sublime-workspace
.DS_Store
.cpcache
21 LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Nikita Prokopov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,58 @@
# Uberjar builder for deps.edn projects

Takes deps.edn and packs an uberjar out of it.

- Fast: Does not unpack intermediate jars on disk.
- Explicit: Prints dependency tree. Realize how much crap you’re packing.
- Standalone: does not depend on current classpath, does not need live Clojure environment.
- Embeddable and configurable: fine-tune your build by combining config options and calling specific steps from your code.

## Usage

Add to your bash aliases:

```sh
clj -Sdeps '{:deps {uberdeps {:mvn/version "0.1.0"}}}' -m uberdeps.uberjar
```

Or add to your `deps.edn` or `~/.clojure/deps.edn`:

```clojure
:aliases {
:uberjar {:extra-deps {uberdeps {:mvn/version "0.1.0"}}
:main-opts ["-m" "uberdeps.uberjar"]}}
}
```

Supported command-line options are:

```
--deps-file <file> Which deps.edn file to use to build classpath. Defaults to 'deps.edn'
--aliases <alias:alias:...> Colon-separated list of alias names to include from deps file. Defaults to nothing
--target <file> Jar file to ouput to. Defaults to 'target/<directory-name>.jar'
--level (debug|info|warn|error) Verbose level. Defaults to debug
```

## Programmatic API

```clojure
(require '[uberdeps.api :as uberdeps])
(let [exclusions (into uberdeps/exclusions [#"\.DS_Store" #".*\.cljs" #"cljsjs/.*"])
deps (clojure.edn/read-string (slurp "deps.edn"))]
(binding [uberdeps/exclusions exclusions
uberdeps/level :warn]
(uberdeps/package deps "target/uber.jar" {:aliases #{:uberjar}})))
```
## Changelog
### 0.1.0 - April 29, 2019
Initial version
## License
Copyright © 2019 Nikita Prokopov
Licensed under MIT (see [LICENSE](LICENSE)).
@@ -0,0 +1,3 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1-beta2"}
org.clojure/tools.deps.alpha {:mvn/version "0.6.496"}}}
@@ -0,0 +1,5 @@
(defproject uberdeps "0.1.0"
:dependencies
[[org.clojure/clojure "1.10.1-beta2"]
[org.clojure/tools.deps.alpha "0.6.496"]]
:main uberdeps.main)
@@ -0,0 +1,148 @@
(ns uberdeps.api
(:require
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.tools.deps.alpha :as deps])
(:import
[java.io File InputStream FileInputStream FileOutputStream BufferedInputStream BufferedOutputStream]
[java.util.jar JarEntry JarInputStream JarOutputStream]
[java.nio.file.attribute FileTime]))


(set! *warn-on-reflection* true)


(def ^:private ^:dynamic *seen-files)
(def ^:private ^:dynamic *seen-libs)
(def ^:private ^:dynamic context)
(def ^:private ^:dynamic indent "")


(def ^:dynamic level :debug) ; :debug :info :warn :error


; from https://github.com/seancorfield/depstar/blob/06eb9dea599840b38ec493669c89de5aa1ff2eba/src/hf/depstar/uberjar.clj
(def ^:dynamic exclusions
"Filename patterns to exclude. These are checked with re-matches and
should therefore be complete filename matches including any path."
[#"project.clj"
#"LICENSE"
#"COPYRIGHT"
#".*\.pom"
#"module-info\.class"
#"(?i)META-INF/.*\.(MF|SF|RSA|DSA)"
#"(?i)META-INF/(INDEX\.LIST|DEPENDENCIES|NOTICE|LICENSE)(\.txt)?"])


(defn- copy-stream [^InputStream in ^String path last-modified ^JarOutputStream out]
(when-not (some #(re-matches % path) exclusions)
(if-some [context' (get @*seen-files path)]
(when (#{:debug :info :warn} level)
(println (str "! Duplicate entry \"" path "\" from \"" context "\" already seen in \"" context' "\"")))
(let [entry (doto
(JarEntry. path)
(.setLastModifiedTime last-modified))]
(.putNextEntry out entry)
(io/copy in out)
(.closeEntry out)
(swap! *seen-files assoc path context)))))


(defn copy-directory [^File dir out]
(let [dir-path (.getPath dir)
dir-path' (if (str/ends-with? dir-path "/") dir-path (str dir-path "/"))]
(doseq [^File file (file-seq (io/file dir-path'))
:when (.isFile file)]
(with-open [in (io/input-stream file)]
(let [rel-path (-> (.getPath file) (subs (count dir-path')))
modified (FileTime/fromMillis (.lastModified file))]
(copy-stream in rel-path modified out))))))


(defn copy-jar [^File file out]
(with-open [in (JarInputStream. (BufferedInputStream. (FileInputStream. file)))]
(loop [entry (.getNextEntry in)]
(when (some? entry)
(when-not (.isDirectory entry)
(copy-stream in (.getName entry) (.getLastModifiedTime entry) out))
(recur (.getNextEntry in))))))


(defn package* [path out]
(let [file (io/file path)]
(cond
(not (.exists file))
:skip

(.isDirectory file)
(copy-directory file out)

(str/ends-with? path ".jar")
(copy-jar file out)

:else
(throw (ex-info (str ":( Unknown entity at classpath: " path) {:path path})))))


(defn package-lib [lib coord lib-map out]
(if (contains? @*seen-libs lib)
(when (#{:debug :info :warn} level)
(println (str "- " indent "duplicate" lib)))
(binding [context (str lib " " (or (:mvn/version coord) (:sha coord)))]
; pack library itself
(when (#{:debug} level)
(println (str (if (= "" indent) "+ " ". ") indent context)))
(doseq [path (:paths coord)]
(package* path out))
(swap! *seen-libs conj lib)
; pack dependants
(binding [indent (str indent " ")]
(doseq [[lib' coord'] lib-map
:when (some #(= lib %) (:dependents coord'))]
(package-lib lib' coord' lib-map out))))))


(defn package-libs [deps-map out]
(let [lib-map (->> (deps/resolve-deps deps-map (:args-map deps-map))
(into (sorted-map)))]
(doseq [[lib coord] lib-map
:when (nil? (:dependents coord))] ; roots
(package-lib lib coord lib-map out))))


(defn package-paths [deps-map out]
(let [paths (concat
(:paths deps-map)
(:extra-paths (:args-map deps-map)))]
(doseq [path (sort paths)]
(binding [context (str path "/**")]
(when (#{:debug} level)
(println (str "+ " context)))
(package* path out)))))


(defn- deps-map [deps {:keys [aliases]}]
(let [deps-map (merge-with merge
{:mvn/repos
{"central" {:url "https://repo1.maven.org/maven2/"}
"clojars" {:url "https://repo.clojars.org/"}}}
deps)]
(-> deps-map
(dissoc :aliases)
(assoc :args-map (deps/combine-aliases deps-map aliases)))))


(defn package
([deps target] (package deps target {}))
([deps ^String target opts]
(let [deps-map (deps-map deps opts)
t0 (System/currentTimeMillis)]
(when (#{:debug :info} level)
(println (str "[uberdeps] Packaging " target "...")))
(binding [*seen-files (atom {})
*seen-libs (atom #{})]
(with-open [out (JarOutputStream. (BufferedOutputStream. (FileOutputStream. target)))]
(package-libs deps-map out)
(package-paths deps-map out)))
(when (#{:debug :info} level)
(println (str "[uberdeps] Packaged " target " in " (- (System/currentTimeMillis) t0) " ms"))))))
@@ -0,0 +1,27 @@
(ns uberdeps.uberjar
(:require
[clojure.edn :as edn]
[uberdeps.api :as api]
[clojure.string :as str]
[clojure.java.io :as io]))


(defn -main [& {:as args}]
(let [deps-file (or (get args "--deps-file") "deps.edn")
target (or (get args "--target")
(as-> (io/file ".") %
(.getCanonicalFile %)
(.getName %)
(str "target/" % ".jar")))
aliases (-> (or (get args "--aliases") "")
(str/split #":")
(->> (remove str/blank?)
(map keyword)
(into #{})))
level (keyword (or (get args "--level") "debug"))]
(binding [api/level level]
(api/package
(edn/read-string (slurp deps-file))
target
{:aliases aliases}))
(shutdown-agents)))

0 comments on commit ac8a580

Please sign in to comment.
You can’t perform that action at this time.