Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reduce duplication in code for reading/writing project.clj

  • Loading branch information...
commit 198df96d46efce45be85048046831e36ace88f71 1 parent adb4afd
@purcell purcell authored Licenser committed
Showing with 21 additions and 16 deletions.
  1. +14 −6 src/leiningen/add.clj
  2. +7 −10 src/leiningen/update.clj
20 src/leiningen/add.clj
@@ -81,6 +81,16 @@ with a numbered list of choices."
(str name " (" (str-join ", " vers) ")")))))
(defn add [project artifact & args]
+(defn- project-clj-path [project]
+ (str (:root project) "/project.clj"))
+(defn read-project-clj [project]
+ (read-clj (project-clj-path project)))
+(defn write-project-clj [project forms]
+ (with-out-writer (project-clj-path project)
+ (pr forms)))
(let [dev (or (= artifact "--dev") (= artifact "-d"))
artifact (if dev (first args) artifact)
args (if dev (rest args) args)
@@ -91,10 +101,8 @@ with a numbered list of choices."
(println "Sorry; nothing on clojars that matches" artifact (if version ""))
(if (and version (not-any? (partial = version) (:versions res)))
(println "Sorry; there is no version" version "for" (artifact-name res) ". Try one of:" (str-join ", " (:versions res)))
- (let [[a v] [(artifact-name res) (if version version (latest-stable (:versions res)))]
- project-clj-path (str (:root project) "/project.clj")
- p (read-clj project-clj-path)]
+ (let [[a v] [(artifact-name res) (if version version (latest-stable (:versions res)))]]
(println "Adding:" a v)
- (with-open [o (writer project-clj-path)]
- (binding [*out* o]
- (pr (add-artifact p (if dev :dev-dependencies :dependencies) a v)))))))))
+ (write-project-clj project
+ (add-artifact (read-project-clj project)
+ (if dev :dev-dependencies :dependencies) a v)))))))
17 src/leiningen/update.clj
@@ -1,8 +1,7 @@
(ns leiningen.update
"lein update checks for newer versions of currently used dependencies and aks the user if they should be updated to the latest stable."
(:use (clojure.contrib duck-streams seq-utils str-utils)
- [leiningen.add :only [latest-stable add-artifact find-clojar good-read-line update-dependency-list prompt-for-input]]
- [ :only [read-clj]]
+ [leiningen.add :only [latest-stable add-artifact find-clojar good-read-line update-dependency-list prompt-for-input read-project-clj write-project-clj]]
[leiningen.update-repo :only [compare-versions]]))
@@ -31,17 +30,15 @@
[artifact version nil])))
(defn update [project & args]
- (let [project-clj-path (str (:root project) "/project.clj")
- maybe-add-updates
+ (let [maybe-add-updates
(fn [dep-type initial]
(reduce (fn [p [artifact version new-version]]
(if (and new-version (ask-for-update artifact version new-version))
(update-artifact p dep-type (str artifact) new-version)
- (map find-updates (dep-type project))))
- updated-project (->> (read-clj project-clj-path)
- (maybe-add-updates :dependencies)
- (maybe-add-updates :dev-dependencies))]
- (with-out-writer project-clj-path
- (pr updated-project))))
+ (map find-updates (dep-type project))))]
+ (write-project-clj project
+ (->> (read-project-clj project)
+ (maybe-add-updates :dependencies)
+ (maybe-add-updates :dev-dependencies)))))
Please sign in to comment.
Something went wrong with that request. Please try again.