Skip to content
Browse files

improved listing - like lein-search, add description

  • Loading branch information...
1 parent 2bcba98 commit 2ef749451ae6e01c1018929126142a31f7c8b913 @kumarshantanu committed Jul 15, 2011
Showing with 193 additions and 33 deletions.
  1. +1 −1 project.clj
  2. +63 −31 src/leiningen/localrepo.clj
  3. +84 −1 src/leiningen/localrepo/internal.clj
  4. +45 −0 test/leiningen/test_localrepo.clj
View
2 project.clj
@@ -1,3 +1,3 @@
-(defproject lein-localrepo "0.2"
+(defproject lein-localrepo "0.3"
:description "Leiningen local repository plugin"
:dev-dependencies [[org.clojure/clojure "1.2.1"]])
View
94 src/leiningen/localrepo.clj
@@ -6,6 +6,7 @@
[clojure.java.io :as jio]
[clojure.string :as str]
[clojure.pprint :as ppr]
+ [clojure.xml :as xml]
[leiningen.localrepo.internal :as in])
(:import
(java.io File)
@@ -66,6 +67,17 @@
(.install installer the-file artifact local-repo)))
+(defn read-artifact-description
+ [pom-file]
+ (if (.isFile pom-file)
+ (let [raw-content (slurp pom-file)
+ xml-content (xml/parse pom-file)
+ map-content (in/xml-map xml-content)]
+ ;(with-out-str (ppr/pprint xml-content) (ppr/pprint map-content))
+ (first (:description (:project map-content))))
+ "(No description available)"))
+
+
(defn read-artifact-entries
"Read artifact entries from specified `dir` and return as a list. If
dir contains only sub-dirs then it recurses to find actual entries."
@@ -101,50 +113,70 @@
[group-id
artifact-id
version
- (.getName each)]))))))
+ (.getName each)
+ (read-artifact-description
+ (jio/file (let [[dir fne] (in/split-filepath
+ (.getAbsolutePath each))
+ [fnm ext] (in/split-filename fne)]
+ (str dir "/" fnm ".pom"))))]))))))
(defn c-list
"List artifacts in local Maven repo"
[& args]
(if (and (not (zero? (count args)))
(or (> (count args) 1)
- (not (contains? #{"-f"} (first args)))))
+ (not (contains? #{"-f" "-g"} (first args)))))
(println "Invalid argument(s):" (apply str (interpose " " args))
" ==> Allowed: [-f]")
(let [artifact-entries (read-artifact-entries
(jio/file mvn/local-repo-path))
by-group-id (group-by first artifact-entries)]
(doseq [group-id (keys by-group-id)]
- ;; print group-id header
- (println (format "[%s]" group-id))
- (let [by-artifact-id (group-by second
- (get by-group-id group-id))]
- (doseq [artifact-id (keys by-artifact-id)]
- (let [versions (distinct
- (map #(nth % 2)
- (get by-artifact-id artifact-id)))]
- (case (or (first args) :nil)
- :nil (println
- (format " %s (%s)" artifact-id
- (apply str (interpose ", "
- versions))))
- "-f" (do (println (format " %s" artifact-id))
- (doseq [each-v versions]
- (println (format " [%s]" each-v))
- (let [artifacts (get by-artifact-id
- artifact-id)]
- (doseq [each-a (filter #(= each-v
- (nth % 2))
- artifacts)]
- (println
- (format " %s"
- (last each-a)))))))
- "-d" (println
- "Detail option is not yet implemented")
- (println
- (str "Bad arg(s): "
- (apply str (interpose " " args))))))))))))
+ (if (nil? (first args))
+ ;; default - print it like lein-search
+ (let [group-artifacts (get by-group-id group-id)
+ with-pomfile (map #(let [fnm (last %)
+ ] into [] (drop-last %))
+ group-artifacts)]
+ (doseq [artifact group-artifacts]
+ (let [[gid aid ver fnm des] artifact
+ artifact-descript des]
+ (println (format "[%s/%s \"%s\"] %s"
+ gid aid ver artifact-descript)))))
+ ;; custom format
+ (do
+ (println (format "[%s]" group-id))
+ (let [by-artifact-id (group-by second
+ (get by-group-id group-id))]
+ (doseq [artifact-id (keys by-artifact-id)]
+ (let [artifacts (get by-artifact-id artifact-id)
+ description (last (first artifacts))
+ versions (distinct (map #(nth % 2) artifacts))]
+ (case (or (first args) :nil)
+ "-g" (println
+ (format " %s/%s (%s) - %s"
+ group-id artifact-id
+ (apply str (interpose ", "
+ versions))
+ (or description "")))
+ "-f" (do (println (format " %s" artifact-id))
+ (doseq [each-v versions]
+ (println (format " [%s]" each-v))
+ (let [artifacts (get by-artifact-id
+ artifact-id)]
+ (doseq [each-a (filter #(= each-v
+ (nth % 2))
+ artifacts)]
+ (println
+ (format " %s/%s - %s"
+ group-id artifact-id
+ (last (butlast each-a))))))))
+ "-d" (println
+ "Detail option is not yet implemented")
+ (println
+ (str "Bad arg(s): "
+ (apply str (interpose " " args))))))))))))))
(defn c-remove
View
85 src/leiningen/localrepo/internal.clj
@@ -1,7 +1,8 @@
(ns leiningen.localrepo.internal
"Utility functions - mostly taken/adapted from Clj-MiscUtil:
https://bitbucket.org/kumarshantanu/clj-miscutil/src"
- (:require [clojure.string :as str])
+ (:require [clojure.string :as str]
+ [clojure.pprint :as ppr])
(:import (java.io File)))
@@ -33,6 +34,12 @@
[(str/join "/" (drop-last sf)) (last sf)]))
+(defn ^String pick-dirname
+ "Given a filepath, return the directory portion from it."
+ [s]
+ (first (split-filepath s)))
+
+
(defn ^String pick-filename
"Given a filepath, return the filename portion from it."
[s]
@@ -101,3 +108,79 @@
"Same as (not (contains? haystack needle))"
[haystack needle]
(not (contains? haystack needle)))
+
+
+(defn common-keys
+ "Find the common keys in maps `m1` and `m2`."
+ [m1 m2]
+ (let [m1-ks (into #{} (keys m1))
+ m2-ks (into #{} (keys m2))]
+ (filter #(contains? m2-ks %) m1-ks)))
+
+
+(defn add-into
+ ""
+ [old-map new-map]
+ (let [com-ks (into [] (common-keys old-map new-map))
+ com-kv (zipmap com-ks
+ (map #(do [(get old-map %)
+ (get new-map %)])
+ com-ks))]
+ (merge (merge old-map new-map) com-kv)))
+
+
+(defn as-vector
+ "Convert/wrap given argument as a vector."
+ [anything]
+ (if (vector? anything) anything
+ (if (or (seq? anything) (set? anything)) (into [] anything)
+ (if (map? anything) [anything]
+ (if (nil? anything) []
+ [anything])))))
+
+
+(defn merge-incl
+ "Merge two maps inclusively. Values get wrapped inside a vector, e.g
+ user=> (merge-incl {:a 10 :b 20} {:b 30 :c 40})
+ {:a [10] :b [20 30] :c [40]}"
+ [old-map new-map]
+ (let [common-ks (into [] (common-keys old-map new-map))
+ old-keys (keys old-map)
+ new-keys (keys new-map)
+ all-keys (distinct (into old-keys new-keys))]
+ (reduce (fn [m each-k]
+ (let [old-v (get old-map each-k)
+ new-v (get new-map each-k)
+ added (into (as-vector old-v)
+ (as-vector new-v))]
+ (into m {each-k added})))
+ {} all-keys)))
+
+
+(defn xml-map
+ "Discard attributes in XML structure `e` and turn elements as keys
+ with respective values in a nested map. Values wind up in vectors.
+ Example:
+ (clojure.xml/parse (slurp \"filename.xml\"))"
+ [e]
+ (let [is-map? #(or (map? %) (instance? clojure.lang.MapEntry %))
+ tag? #(and (is-map? %)
+ (contains? % :tag))
+ not-tag? (comp not tag?)]
+ (cond
+ (tag? e) (let []
+ {(:tag e) (xml-map (:content e))})
+ (map? e) (let []
+ [e])
+ (coll? e) (cond
+ (some not-tag? e) (let [v (as-vector
+ (map xml-map e))
+ [w] v]
+ (if (and (= (count v) 1)
+ (vector? w))
+ w v))
+ :else (let [r (reduce merge-incl {}
+ (map xml-map e))]
+ r))
+ :else (let []
+ e))))
View
45 test/leiningen/test_localrepo.clj
@@ -1,2 +1,47 @@
(ns leiningen.test-localrepo
+ (:require [leiningen.localrepo.internal :as in]
+ [clojure.xml :as xml]
+ [clojure.pprint :as ppr])
(:use clojure.test))
+
+
+(def xml-test-data
+ [["<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+<project>project</project>" {:project ["project"]}]
+ ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+<project>
+ <elem>value 1</elem>
+ <elem>value 2</elem>
+</project>" {:project {:elem ["value 1" "value 2"]}}]
+ ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+<project>
+ <elem1>
+ <child>Child</child>
+ </elem1>
+</project>" {:project {:elem1 {:child ["Child"]}}}]
+ ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+<project>
+ <elem1>
+ <child1>Child 1 - instance 1</child1>
+ <child1>Child 1 - instance 2</child1>
+ <child2>Child 2</child2>
+ </elem1>
+</project>" {:project {:elem1 {:child1 ["Child 1 - instance 1"
+ "Child 1 - instance 2"]
+ :child2 ["Child 2"]}}}]])
+
+
+(deftest test-xml-parsing
+ (println "=================================================")
+ (doseq [each-data xml-test-data]
+ (let [[xml-str xml-data] each-data
+ parsed-xml (xml/parse (java.io.StringBufferInputStream.
+ xml-str))
+ parsed-data (in/xml-map parsed-xml)]
+ (is (not (string? parsed-data)))
+ (println "<<<<<<<<<<<<")
+ (println xml-str)
+ (ppr/pprint parsed-xml)
+ (ppr/pprint parsed-data)
+ (println ">>>>>>>>>>>>")
+ (is (= xml-data parsed-data)))))

0 comments on commit 2ef7494

Please sign in to comment.
Something went wrong with that request. Please try again.