Skip to content

Commit

Permalink
Display licenses on jar page [#415]
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias committed Jan 17, 2016
1 parent 97af985 commit b7947ca
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
16 changes: 16 additions & 0 deletions dev-resources/test-maven/test-maven.pom
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
<artifactId>test</artifactId>
<version>0.0.4</version>
<name>blarg</name>
<licenses>
<license>
<name>Some License</name>
<url>http://example.com/license</url>
<distribution>repo</distribution>
</license>
<license>
<name>Some Other License</name>
<url>http://example.com/license2</url>
</license>
</licenses>
<scm>
<connection>scm:git:git://example.com/example/example.git</connection>
<tag>abcde</tag>
<url>http://example.com/example/example</url>
</scm>
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
Expand Down
52 changes: 29 additions & 23 deletions src/clojars/maven.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
(:import org.apache.maven.model.io.xpp3.MavenXpp3Reader
org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader
java.io.IOException
(org.apache.maven.model Scm Model)))
(org.apache.maven.model Scm Model License)))

(defn without-nil-values
"Prunes a map of pairs that have nil values."
[m]
Expand All @@ -16,6 +17,21 @@
(conj m entry)))
(empty m) m))

(defn scm-to-map [^Scm scm]
(when scm
(without-nil-values
{:connection (.getConnection scm)
:developer-connection (.getDeveloperConnection scm)
:tag (.getTag scm)
:url (.getUrl scm)})))

(defn license-to-seq [^License license]
(without-nil-values
{:name (.getName license)
:url (.getUrl license)
:distribution (.getDistribution license)
:comments (.getComments license)}))

(defn model-to-map [^Model model]
(without-nil-values
{:name (or (.getArtifactId model)
Expand All @@ -27,23 +43,15 @@
:description (.getDescription model)
:homepage (.getUrl model)
:url (.getUrl model)
:licenses (.getLicenses model)
:scm (.getScm model)
:authors (vec (map #(.getName %) (.getContributors model)))
:dependencies (vec (map
(fn [d] {:group_name (.getGroupId d)
:jar_name (.getArtifactId d)
:version (.getVersion d)
:scope (or (.getScope d) "compile")})
(.getDependencies model)))}))

(defn scm-to-map [^Scm scm]
(when scm
(without-nil-values
{:connection (.getConnection scm)
:developer-connection (.getDeveloperConnection scm)
:tag (.getTag scm)
:url (.getUrl scm)})))
:licenses (mapv license-to-seq (.getLicenses model))
:scm (scm-to-map (.getScm model))
:authors (mapv #(.getName %) (.getContributors model))
:dependencies (mapv
(fn [d] {:group_name (.getGroupId d)
:jar_name (.getArtifactId d)
:version (.getVersion d)
:scope (or (.getScope d) "compile")})
(.getDependencies model))}))

(defn read-pom
"Reads a pom file returning a maven Model object."
Expand Down Expand Up @@ -87,17 +95,15 @@
nil)))

(defn github-info [pom-map]
(let [scm (:scm pom-map)
url (and scm (.getUrl scm))
(let [url (get-in pom-map [:scm :url])
github-re #"^https?://github.com/([^/]+/[^/]+)"
user-repo (->> (str url) (re-find github-re) second)]
user-repo))

(defn commit-url [pom-map]
(let [scm (:scm pom-map)
url (and scm (.getUrl scm))
(let [{:keys [url tag]} (:scm pom-map)
base-url (re-find #"https?://github.com/[^/]+/[^/]+" (str url))]
(if (and base-url (.getTag scm)) (str base-url "/commit/" (.getTag scm)))))
(if (and base-url tag) (str base-url "/commit/" tag))))

(defn parse-int [^String s]
(when s
Expand Down
4 changes: 1 addition & 3 deletions src/clojars/tools/generate_feed.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
:when (and (not (re-matches #".*/\..*" (str f)))
(.endsWith (.getName f) ".pom"))
:let [pom (try
(-> (maven/pom-to-map f)
(update :scm maven/scm-to-map)
maven/without-nil-values)
(maven/pom-to-map f)
(catch Exception e (.printStackTrace e)))]
:when pom]
pom))
Expand Down
6 changes: 6 additions & 0 deletions src/clojars/web/jar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@
[:li.homepage
[:h4 "Homepage"]
(safe-link-to homepage homepage)])
(when-let [licenses (seq (:licenses pom-map))]
[:li.license
[:h4 "License"]
[:ul#licenses
(for [{:keys [name url]} licenses]
[:li (safe-link-to url name)])]])
[:li
[:h4 "Version Badge"]
[:p
Expand Down
17 changes: 12 additions & 5 deletions test/clojars/test/unit/maven.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
(is (= "fake" (:group m)))
(is (= "child" (:name m)))))

(deftest directory-for-handles-normal-group-name
(is (= (io/file (config :repo) "fake" "test" "1.0.0")
(directory-for {:group_name "fake"
:jar_name "test"
:version "1.0.0"}))))
(deftest pom-to-map-parses-scm
(let [{:keys [tag url]} (:scm (pom-to-map (.toString (io/resource "test-maven/test-maven.pom"))))]
(is (= "abcde" tag))
(is (= "http://example.com/example/example" url))))

(deftest pom-to-map-parses-licenses
(let [[l1 l2] (:licenses (pom-to-map (.toString (io/resource "test-maven/test-maven.pom"))))]
(is (= "Some License" (:name l1)))
(is (= "http://example.com/license" (:url l1)))

(is (= "Some Other License" (:name l2)))
(is (= "http://example.com/license2" (:url l2)))))

(deftest directory-for-handles-group-names-with-dots
(is (= (io/file (config :repo) "com" "novemberain" "monger" "1.2.0-alpha1")
Expand Down

0 comments on commit b7947ca

Please sign in to comment.