Skip to content

Commit

Permalink
0.1.24 - condense get-dep-vec-map into single function
Browse files Browse the repository at this point in the history
  • Loading branch information
somecho committed Jul 4, 2023
1 parent 573d59d commit d8136da
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[deps-deploy.deps-deploy :as dd]))

(def lib 'org.clojars.some/depo)
(def version "0.1.23")
(def version "0.1.24")
(def jar-file (format "target/%s-%s.jar" (name lib) version))
(def class-dir "target/classes")
(def url "https://github.com/somecho/depo")
Expand Down
2 changes: 1 addition & 1 deletion src/depo/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
(def CONFIGURATION
{:command "depo"
:description "Manage dependencies for Clojure projects easily"
:version "0.1.23"
:version "0.1.24"
:opts [{:as "path to configuration file"
:default nil
:option "file"
Expand Down
111 changes: 50 additions & 61 deletions src/depo/readwrite.clj
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
(ns depo.readwrite
(:require [clojure.java.io :as io]
[clojure.string :as str]
[depo.parser :as dp]
[depo.resolver :as r]
[zprint.core :as zp]
[rewrite-clj.zip :as z]))

(defmulti get-all-dependency-names
"Returns all the dependencies from the config"
(fn [config-path] (last (str/split config-path #"/"))))

(defmethod get-all-dependency-names :default
[config-path]
(let [zloc (z/of-string (slurp config-path))
dep-key (if (= (last (str/split config-path #"/")) "shadow-cljs.edn") :dependencies :deps)]
(-> zloc
(z/get dep-key)
(z/string)
(read-string)
(keys)
(as-> keys (map str keys)))))

(defmethod get-all-dependency-names "project.clj"
[config-path]
(let [zloc (z/of-string (slurp config-path))]
(-> zloc
(z/find-value z/next :dependencies)
(z/next)
(z/string)
(read-string)
(as-> dep-vec (map #(str (first %)) dep-vec)))))

(defn create-identifier
[groupID artifactID dep-type]
(case dep-type
Expand Down Expand Up @@ -83,23 +57,6 @@
(map? deps) :map
(vector? deps) :vector))

(defn get-project-type
"- `config-path` - the full path to the config file as a string
Returns
- `:shadow` for shadow-cljs.edn
- `:lein` for project.clj
- `:default` for everything else
"
[config-path]
(let [config-name (-> config-path
(str/split #"/")
(last))]
(case config-name
"shadow-cljs.edn" :shadow
"project.clj" :lein
:default)))

(defn traverse-zip-map
"- `zloc` - a zipper object created by rewrite-clj
- `keys` - a vector of keys
Expand All @@ -115,27 +72,59 @@
(rest keys))
zloc)))

(defmulti get-dep-vec-map
(fn [m] (:project-type m)))

(defmethod get-dep-vec-map :default
[{:keys [zloc keys]}]
(defn get-dep-vec-map
[{:keys [zloc keys project-type]}]
(-> zloc
(traverse-zip-map keys)
(as-> zipper
(case project-type
:lein (-> (z/find-value zipper z/next (first keys))
(z/next)
(as-> zpos
(if (not-empty (rest keys))
(traverse-zip-map zpos (rest keys))
zpos)))
(traverse-zip-map zipper keys)))
(z/string)
(read-string)))

(defmethod get-dep-vec-map :lein
[{:keys [zloc keys]}]
(-> zloc
(z/find-value z/next (first keys))
(z/next)
(as-> zpos
(if (not-empty (rest keys))
(traverse-zip-map zpos (rest keys))
zpos))
(z/string)
(read-string)))
(defn get-project-type
"- `config-path` - the full path to the config file as a string
Returns
- `:shadow` for shadow-cljs.edn
- `:lein` for project.clj
- `:default` for everything else
"
[config-path]
(let [config-name (-> config-path
(str/split #"/")
(last))]
(case config-name
"shadow-cljs.edn" :shadow
"project.clj" :lein
:default)))

(defn get-all-dependency-names
[config-path]
(let [zloc (z/of-string (slurp config-path))
project-type (get-project-type config-path)
deps-key (case project-type
(or :shadow :lein) :dependencies
:deps)]
(-> zloc
(as-> zipper
(case project-type
:lein (-> zipper
(z/find-value z/next deps-key)
(z/next))
(z/get zipper deps-key)))
(z/string)
(read-string)
(as-> vec-map
(case (get-dependency-type vec-map)
:map (keys vec-map)
:vector (map #(str (first %)) vec-map)))
(println))))

(defn add-dependency
[{:keys [deps id]}]
Expand Down Expand Up @@ -269,6 +258,6 @@
(spit config-path))))
; (println))))

; (apply-operation {:config-path "test/resources/input/bb.edn"
; (apply-operation {:config-path "test/resources/input/shadow-cljs.edn"
; :id "reagent"
; :operation :add})
4 changes: 2 additions & 2 deletions test/depo/depo_e2e_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
snapshot (str SNAPSHOTS-FOLDER "/" target-folder "/" filename)]
(io/copy (io/file input) (io/file target))
(apply sh (concat ["clojure" "-M" "-m" "depo.core" "-f" target] commands))
(= (slurp snapshot) (slurp target))))
(is (= (slurp snapshot) (slurp target)))))

(doall
(for [{:keys [name commands]} tests]
Expand All @@ -58,4 +58,4 @@
#(doall
(for [f FILES]
(testing f
(is (apply apply-compare (concat [f name] commands))))))))))
(apply apply-compare (concat [f name] commands)))))))))

0 comments on commit d8136da

Please sign in to comment.