diff --git a/.gitignore b/.gitignore index 2c811ef..82ba019 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ pom.xml *.class .lein-deps-sum .lein-failures -.lein-plugins \ No newline at end of file +.lein-plugins +.idea \ No newline at end of file diff --git a/README.md b/README.md index 25350a5..b55ae05 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,12 @@ current branch. ## Usage To be able to access the plugin from any project, put -`[lein-sha-version "0.1.1"]` into the `:plugins` vector of your `:sha`, or other +`[coreagile/lein-sha-version "0.1.2"]` into the `:plugins` vector of your `:sha`, or other non-standard, profile. Invoke `lein` with `lein with-profile sha` to use the sha version number for the project, e.g. `lein with-profile sha deploy clojars` to deploy the sha based version to clojars. -Put `[lein-sha-version "0.1.1"]` into the `:plugins` vector of your `:dev` +Put `[coreagile/lein-sha-version "0.1.2"]` into the `:plugins` vector of your `:dev` profile in `project.clj` if you always want to use a sha version with a specific project. @@ -22,6 +22,14 @@ To control the length of the generated SHA, you can set the `:length` key under :sha {:length 8} ``` +To add the generated SHA to your artifacts' `MANIFEST.MF`, you can set the +`:manifest-header` to your project or profile. The SHA will be written to that +header in your jar and/or uberjar. + +```clj +:sha {:manifest-header "Implementation-Version"} +``` + ## License Copyright © 2012 Hugo Duncan diff --git a/project.clj b/project.clj index 32b5f44..77515be 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject lein-sha-version "0.1.2-SNAPSHOT" +(defproject coreagile/lein-sha-version "0.1.2" :description "A Leiningen plugin to set the project version based on the git SHA." :url "https://github.com/pallet/lein-sha-version" :license {:name "Eclipse Public License" diff --git a/src/lein_sha_version/plugin.clj b/src/lein_sha_version/plugin.clj index 51052e4..6ce3997 100644 --- a/src/lein_sha_version/plugin.clj +++ b/src/lein_sha_version/plugin.clj @@ -1,24 +1,38 @@ (ns lein-sha-version.plugin (:use - [clojure.java.io :only [file]] - [leiningen.core.main :only [debug]]) + [clojure.java.io :only [file]] + [leiningen.core.main :only [warn debug]]) (:import - org.eclipse.jgit.storage.file.FileRepositoryBuilder - [org.eclipse.jgit.lib ObjectId Repository])) + org.eclipse.jgit.storage.file.FileRepositoryBuilder + [org.eclipse.jgit.lib ObjectId Repository])) (defn git-sha [{:keys [root version sha]}] (debug "Finding SHA for" root) - (let [^Repository repository (.. (FileRepositoryBuilder.) - (readEnvironment) - (findGitDir (file root)) - (build)) - ^ObjectId head (.resolve repository "HEAD")] - (if head - (let [abbr (.abbreviate head (:length sha 7))] - (debug "Found SHA" (.name head) "using" (.name abbr)) - (.name abbr)) + (try + (let [^Repository repository (.. (FileRepositoryBuilder.) + (readEnvironment) + (findGitDir (file root)) + (build)) + ^ObjectId head (.resolve repository "HEAD")] + (if head + (let [abbr (.abbreviate head (:length sha 7))] + (debug "Found SHA" (.name head) "using" (.name abbr)) + (.name abbr)) + version)) + (catch Throwable e + (warn (format "Unable to get SHA: %s" (.getMessage e))) version))) +(defn update-manifest [git-sha {manifest :manifest + {:keys [manifest-header]} :sha}] + (if manifest-header + (merge {manifest-header git-sha} manifest) + manifest)) + (defn middleware [project] - (assoc project :version (git-sha project))) + (let [version (git-sha project) + manifest (update-manifest version project)] + (-> project + (assoc :version version) + (assoc :manifest manifest))))