Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support adding entries :dependencies or :dev-dependencies where no su…

…ch list currently exists
  • Loading branch information...
commit 0b01260c1023c60484aae3541fa31df134dce60c 1 parent 112662c
@purcell purcell authored committed
Showing with 24 additions and 13 deletions.
  1. +20 −12 src/lein_search/core.clj
  2. +4 −1 test/lein_search/core_test.clj
View
32 src/lein_search/core.clj
@@ -1,6 +1,7 @@
(ns lein-search.core
(:use [clojure.contrib.duck-streams :only (reader writer with-out-writer)])
(:use [clojure.contrib.str-utils :only (chomp str-join)])
+ (:require [clojure.zip :as zip])
(:import (java.io File PushbackReader) java.util.zip.GZIPInputStream java.net.URL))
@@ -63,19 +64,26 @@ with a numbered list of choices."
;;; Modifying defproject forms
-;; TODO: this won't add a :dev-dependencies or :dependencies if not already present
-(defn update-dependency-list
- "Modify the project's dependency list of the given type by passing it through f"
+(defn- update-dependency-list
+ "Modify the project's dependency list of the given type by passing it through f.
+
+ Adds a dependency list of that type if none currently exists."
[project dep-type f]
- (->> project
- (reduce
- (fn [[form prev] n]
- (if (= prev dep-type)
- [(cons (vec (f n)) form) nil]
- [(cons n form) n]))
- [() nil])
- first
- reverse))
+ (let [defproject (-> project zip/seq-zip zip/down)
+ dep-list-loc (if-let [marker (->> defproject
+ (iterate zip/right)
+ (take-while identity)
+ (filter #(= dep-type (zip/node %)))
+ first)]
+ (zip/next marker)
+ (-> defproject
+ zip/rightmost
+ (zip/insert-right [])
+ (zip/insert-right dep-type)
+ zip/rightmost))]
+ (-> dep-list-loc
+ (zip/replace (f (zip/node dep-list-loc)))
+ (zip/root))))
(defn add-artifact [project type artifact version]
(update-dependency-list project type
View
5 test/lein_search/core_test.clj
@@ -30,7 +30,10 @@
:dependencies 'a "1.2")))
(is (= '(defproject "whatever" :dev-dependencies [[a "1.2"]])
(add-artifact '(defproject "whatever" :dev-dependencies [])
- :dev-dependencies 'a "1.2"))))
+ :dev-dependencies 'a "1.2")))
+ (is (= '(defproject "whatever" :dev-dependencies [[a "1.2"]])
+ (add-artifact '(defproject "whatever")
+ :dev-dependencies 'a "1.2"))))
(deftest updating-artifact-versions
(is (= '(defproject "whatever" :dependencies [[a "1.2"]])
Please sign in to comment.
Something went wrong with that request. Please try again.