Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit d27ecee9483c244ea6414f2decb888aed96518c8 @technomancy committed Jul 3, 2012
Showing with 57 additions and 0 deletions.
  1. +10 −0 .gitignore
  2. +17 −0 README.md
  3. +6 −0 project.clj
  4. +24 −0 src/leiningen/clean_m2.clj
@@ -0,0 +1,10 @@
+/target
+/lib
+/classes
+/checkouts
+pom.xml
+*.jar
+*.class
+.lein-deps-sum
+.lein-failures
+.lein-plugins
@@ -0,0 +1,17 @@
+# lein-clean-m2
+
+A Leiningen plugin to clean the local repository of unused artifacts.
+
+This only takes the `:dependencies` and `:plugins` of a single project
+into consideration; don't use this in a user account unless it's only
+used for a single project.
+
+## Usage
+
+ $ lein clean-m2
+
+## License
+
+Copyright © 2012 Phil Hagelberg
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,6 @@
+(defproject lein-clean-m2 "0.1.0"
+ :description "Remove unused artifacts from local repository."
+ :url "https://github.com/technomancy/lein-clean-m2"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :eval-in-leiningen true)
@@ -0,0 +1,24 @@
+(ns leiningen.clean-m2
+ (:require [leiningen.core.classpath :as cp]
+ [clojure.java.io :as io]))
+
+(def local-repo (io/file (System/getProperty "user.home") ".m2" "repository"))
+
+(defn- coords [local-repo-path f]
+ (rest (re-find #"([^\/]+)/([^\/]+)/([^\/]+)/"
+ (.replace (str f) local-repo-path ""))))
+
+(defn clean-m2
+ "Wipe local repository of all artifacts not required for project.
+
+Don't use this in a user account unless it's only used for a single project."
+ [project & [dry-run?]]
+ (let [artifacts (concat (cp/resolve-dependencies :plugins project)
+ (cp/resolve-dependencies :dependencies project))
+ local-repo (:local-repo project local-repo)
+ used? (set (map (partial coords (str local-repo)) artifacts))]
+ (doseq [f (file-seq (io/file local-repo))]
+ (when (and (not (used? (coords (str local-repo) f))) (.isFile f))
+ (println "Unused:" (str f))
+ (when-not dry-run?
+ (io/delete-file f))))))

0 comments on commit d27ecee

Please sign in to comment.