Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit.

  • Loading branch information...
commit 7a6b35d0993ece67ca8e1a80a50ea0b45e6739b7 0 parents
Phil Hagelberg authored
7 .gitignore
... ... @@ -0,0 +1,7 @@
  1 +pom.xml
  2 +*jar
  3 +/lib/
  4 +/classes/
  5 +.lein-deps-sum
  6 +/.lein-plugins/checksum
  7 +/.lein-failures
16 README.md
Source Rendered
... ... @@ -0,0 +1,16 @@
  1 +# lein-precate
  2 +
  3 +Suggest a new project.clj that's compatible with Leiningen 2.
  4 +
  5 +## Usage
  6 +
  7 +This plugin is designed to be run under Leiningen 1.x:
  8 +
  9 + $ lein plugin install lein-precate 0.1.0-SNAPSHOT
  10 + $ lein precate
  11 +
  12 +## License
  13 +
  14 +Copyright © 2012 Phil Hagelberg
  15 +
  16 +Distributed under the Eclipse Public License, the same as Clojure.
4 project.clj
... ... @@ -0,0 +1,4 @@
  1 +(defproject lein-precate "0.1.0-SNAPSHOT"
  2 + :description "Suggest a new project.clj that's compatible with Leiningen 2."
  3 + :eval-in-leiningen true
  4 + :profiles {:dev {:resources-path ["test-resources"]}})
109 src/leiningen/precate.clj
... ... @@ -0,0 +1,109 @@
  1 +(ns leiningen.precate
  2 + (:require [clojure.pprint :as pprint]
  3 + [clojure.java.io :as io]
  4 + [leiningen.core :as lein]))
  5 +
  6 +(defn defaults [project]
  7 + (let [default-file (io/file (:root project) "default-project.clj")]
  8 + (try
  9 + (spit default-file "(defproject dummy \"1.0\")")
  10 + (lein/read-project (str default-file))
  11 + (finally (.delete default-file)))))
  12 +
  13 +(defn- tidy-key [default-project [k v]]
  14 + (or (= (default-project k) v)
  15 + ;; = on regexes is broken. ಠ_ಠ
  16 + (and (#{:uberjar-exclusions :jar-exclusions} k)
  17 + (= (str (default-project k) (str v))))))
  18 +
  19 +(defn tidy-project [project]
  20 + (let [default-project (defaults project)]
  21 + (into {:root (str (:root project) "/")}
  22 + (remove (partial tidy-key default-project) project))))
  23 +
  24 +(defn suggest-profiles []
  25 + ;; TODO:
  26 + ;; ~/.lein/plugins
  27 + ;; leiningen-auth
  28 + ;; user-settings
  29 + )
  30 +
  31 +(defn- plugin? [[artifact _]]
  32 + (re-find #"^lein-" (name artifact)))
  33 +
  34 +(defn dev-dependencies [project]
  35 + (if-let [dd (:dev-dependencies project)]
  36 + (let [[plugins dev] ((juxt filter remove) plugin? dd)]
  37 + (-> (dissoc project :dev-dependencies)
  38 + (update-in [:plugins] (fnil into {}) plugins)
  39 + (update-in [:profiles :dev] (fnil into {}) {})
  40 + (update-in [:profiles :dev :dependencies] (fnil into {}) dev)))
  41 + project))
  42 +
  43 +(defn special-case-dev-deps [project]
  44 + ;; TODO: handle swank-clojure -> lein-swank, etc
  45 + project)
  46 +
  47 +(defn min-lein-version [project]
  48 + (assoc project :min-lein-version "2.0.0"))
  49 +
  50 +(defn extra-classpath-dirs [project]
  51 + (if-let [ecd (:extra-classpath-dirs project)]
  52 + (-> (dissoc project :extra-classpath-dirs)
  53 + (update-in [:profiles :dev] (fnil into {}) {})
  54 + (update-in [:profiles :dev :resources-path] (fnil into []) ecd))
  55 + project))
  56 +
  57 +(defn repositories-format [project]
  58 + (update-in project [:repositories] (partial into {})))
  59 +
  60 +(defn dependencies-format [project]
  61 + (update-in project [:dependencies] (partial into {})))
  62 +
  63 +(def vec-paths [:source-path :java-source-path :test-path :resources-path])
  64 +
  65 +(defn- vec-pathize [project key]
  66 + (if-let [path (project key)]
  67 + (assoc project key (vector (.replace path (:root project) "")))
  68 + project))
  69 +
  70 +(defn paths-as-vectors [project]
  71 + (reduce vec-pathize project vec-paths))
  72 +
  73 +(defn dissoc-empty [project key]
  74 + (if (empty? (project key))
  75 + (dissoc project key)
  76 + project))
  77 +
  78 +(defn dissoc-empty-keys [project]
  79 + (-> project
  80 + (dissoc-empty :plugins)
  81 + (dissoc-empty :repositories)
  82 + (dissoc-empty :dependencies)))
  83 +
  84 +(defn suggest-project-map [project]
  85 + (-> project
  86 + tidy-project
  87 + dev-dependencies
  88 + special-case-dev-deps
  89 + min-lein-version
  90 + extra-classpath-dirs
  91 + repositories-format
  92 + dependencies-format
  93 + dissoc-empty-keys
  94 + paths-as-vectors))
  95 +
  96 +(defn project-map-to-defproject [project]
  97 + `(~'defproject ~(symbol (or (:group project)
  98 + (:name project))) ~(:version project)
  99 + ~@(apply concat (dissoc project :name :group :version :root))))
  100 +
  101 +(defn precate
  102 + "Suggest a new project.clj that's compatible with Leiningen 2."
  103 + [project]
  104 + (when (.startsWith (System/getenv "LEIN_VERSION") "2")
  105 + (println "This plugin is designed to be used with Leiningen 1.x."))
  106 + (suggest-profiles)
  107 + (pprint/with-pprint-dispatch pprint/code-dispatch ; kinda gross =\
  108 + (pprint/pprint (project-map-to-defproject (suggest-project-map project))))
  109 + (flush))
20 test/leiningen/test/precate.clj
... ... @@ -0,0 +1,20 @@
  1 +(ns leiningen.test.precate
  2 + (:require [leiningen.core :as lein]
  3 + [clojure.java.io :as io])
  4 + (:use [clojure.test]
  5 + [leiningen.precate]))
  6 +
  7 +(def p1 (lein/read-project (.getPath (io/resource "p1.clj"))))
  8 +
  9 +(deftest test-p1
  10 + (let [p1-full (suggest-project-map p1)
  11 + p1 (tidy-project p1-full)]
  12 + (is (= {:name "hi" :group "hi" :version "1.0.0"
  13 + :dependencies {'ordered "1.1.0"},
  14 + :min-lein-version "2.0.0"
  15 + :profiles {:dev {:dependencies {'robert/hooke "1.1.3"}
  16 + :resources-path ["junk"]}}
  17 + :plugins {'lein-tar "1.0.6"}}
  18 + (dissoc p1 :source-path :resources-path :root)))
  19 + (is (= ["src/clj"] (:source-path p1)))
  20 + (is (= ["whatever"] (:resources-path p1)))))
8 test/p1.clj
... ... @@ -0,0 +1,8 @@
  1 +(defproject hi "1.0.0"
  2 + :dependencies [[ordered "1.1.0"]]
  3 + :dev-dependencies [;[;swank-clojure "1.4.0"]
  4 + [lein-tar "1.0.6"]
  5 + [robert/hooke "1.1.3"]]
  6 + :resources-path "whatever"
  7 + :source-path "src/clj"
  8 + :extra-classpath-dirs ["junk"])

0 comments on commit 7a6b35d

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