Skip to content

Commit

Permalink
Merge pull request #12 from somecho/dev
Browse files Browse the repository at this point in the history
Redesign dependency operations
  • Loading branch information
somecho authored Jul 5, 2023
2 parents 29747bc + 7230dbb commit 1c19396
Show file tree
Hide file tree
Showing 21 changed files with 849 additions and 910 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.4.30")
(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
14 changes: 9 additions & 5 deletions generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
(def INPUT-FOLDER "test/resources/input/")
(def SNAPSHOTS-FOLDER "test/resources/snapshots/")
(def FILES ["project.clj" "deps.edn" "bb.edn" "shadow-cljs.edn"])
(def TARGETS ["add-reagent-1.2.0" "add-multiple"])
(def TARGETS [{:name "add-reagent-1.2.0"
:commands ["add" "reagent@1.2.0"]}
{:name "add-multiple"
:commands ["add" "reagent@1.2.0" "org.clojure/java.jdbc@0.7.12"]}])

(defn setup-directories []
(mapv #(io/make-parents (str SNAPSHOTS-FOLDER % "/FILE")) TARGETS))
(mapv #(io/make-parents (str SNAPSHOTS-FOLDER (:name %) "/FILE")) TARGETS))

(defn create-snapshot
[input-file target-folder & commands]
Expand All @@ -23,7 +26,8 @@
(println "Setting up snapshot directories")
(setup-directories)
(println "Creating snapshots")
(println "add-reagent-1.2.0 snapshots")
(mapv #(create-snapshot % "add-reagent-1.2.0" "add" "reagent@1.2.0") FILES)
(mapv #(create-snapshot % "add-multiple" "add" "reagent@1.2.0" "org.clojure/java.jdbc@0.7.12") FILES)
(doall
(for [{:keys [name commands]} TARGETS]
(do (println name "snapshots")
(mapv #(apply create-snapshot (concat [% name] commands)) FILES))))
(println "All snapshots have been created"))
23 changes: 12 additions & 11 deletions src/depo/core.clj
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
(ns ^:no-doc depo.core
(:require [cli-matic.core :refer [run-cmd]]
[depo.readwrite :as rw]
[depo.errors :as e]))
[depo.dispatch :as dd]
[depo.errors :as e]
[depo.zoperations :as zo]))

(defn add-cmd [{:keys [_arguments file]}]
(let [args _arguments
config-path (if file file (rw/get-config))]
config-path (if file file (dd/get-config))]
(if-not (empty? args)
(do (mapv #(rw/apply-operation {:config-path config-path
(do (mapv #(dd/apply-operation {:config-path config-path
:id %
:operation :add}) args)
(println "Done!"))
(println (e/err :no-args)))))

(defn remove-cmd [{:keys [_arguments file]}]
(let [args _arguments
config-path (if file file (rw/get-config))]
config-path (if file file (dd/get-config))]
(if-not (empty? args)
(do (mapv #(rw/apply-operation {:config-path config-path
(do (mapv #(dd/apply-operation {:config-path config-path
:id %
:operation :remove}) args)
(println "Done!"))
(println (e/err :no-args)))))

(defn update-cmd [{:keys [_arguments file]}]
(let [args _arguments
config-path (if file file (rw/get-config))]
config-path (if file file (dd/get-config))]
(if (empty? args)
(do (mapv #(rw/apply-operation {:config-path config-path
(do (mapv #(dd/apply-operation {:config-path config-path
:id %
:operation :update})
(rw/get-all-dependency-names config-path))
(zo/get-all-dependency-names config-path))
(println "Done!"))
(mapv #(rw/apply-operation {:config-path config-path
(mapv #(dd/apply-operation {:config-path config-path
:id %
:operation :update}) args))))

(def CONFIGURATION
{:command "depo"
:description "Manage dependencies for Clojure projects easily"
:version "0.1.23"
:version "0.4.30"
:opts [{:as "path to configuration file"
:default nil
:option "file"
Expand Down
107 changes: 107 additions & 0 deletions src/depo/dispatch.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
(ns depo.dispatch
"Functions related to the control flow of the CLI,
as well as IO operations, such as reading and writing the config."
(:require [clojure.java.io :as io]
[depo.schema :as schema]
[depo.utils :as dutils]
[depo.zoperations :as zo]
[malli.core :as m]
[rewrite-clj.zip :as z]
[zprint.core :as zp]))

(defn skip-procedure
"Takes in a `depo.schema/PROCEDURE` map and returns it,
printing `reason` on the way."
[{:keys [zloc]} & reason]
(apply println (concat reason ["Skipping."]))
zloc)

(defn ignore-pass
"Takes in a `depo.schema/PROCEDURE` map and
checks whether the operation `f` should be skipped."
[{:keys [dep-data deps-type identifier]
:as procedure} f]
(case deps-type
:map (cond
(= "RELEASE" (:mvn/version dep-data))
(skip-procedure procedure "Version has been set as release.")
(contains? dep-data :local/root)
(skip-procedure procedure identifier "is a local dependency.")
:else (f))
:vector (f)))

(defn dispatch
"Takes in a `depo.schema/PROCEDURE` map and
dispatches the appropriate operations on it.
Returns a zipper object after the operation.
Operations
- `:add`
- `:remove`
- `:update`"
[{:keys [operation dep-exists identifier]
:as procedure}]
(case operation
:add (if dep-exists
(ignore-pass procedure
#(zo/update-dependency procedure))
(zo/append-dependency procedure))
:remove (if dep-exists
(zo/remove-dependency procedure)
(skip-procedure identifier "is not a dependency."))
:update (if dep-exists
(ignore-pass procedure
#(zo/update-dependency procedure))
(skip-procedure procedure identifier "is not a dependency."))))

(defn apply-operation
"Takes in a map containing
- `:config-path` - the path to the config file
- `:id` - the CLI argument
- `:operation` - `:add`, `:update` or `:remove`
Creates a `depo.schema/PROCEDURE` map to dispatch operations.
It writes the resulting configuration into `config-path`. "
[{:keys [config-path id operation]}]
(let [config-zip (z/of-string (slurp config-path))
project-type (dutils/get-project-type config-path)
access-keys [(dutils/create-keys project-type)]
deps (zo/get-deps config-zip access-keys project-type)
{:keys [groupID artifactID]} (dutils/parse id)
deps-type (cond
(z/map? deps) :map
(z/vector? deps) :vector)
identifier (symbol (dutils/create-identifier groupID artifactID deps-type))
dependency-data (zo/get-dependency-data deps identifier)
procedure {:operation operation
:dep-exists (if dependency-data true false)
:identifier identifier
:project-type project-type
:argument id
:dep-data dependency-data
:deps-type deps-type
:zloc deps}]
(if (m/validate schema/PROCEDURE procedure)
(-> (dispatch procedure)
(z/root-string)
(zp/zprint-str {:parse-string? true
:style :indent-only})
(as-> new-conf (spit config-path new-conf)))
(println "Failed to validate procedure."))))

(defn get-config
"Looks in the current directory for the following files
- deps.edn
- project.clj
- shadow-cljs.edn
- bb.edn
Returns the a string matching the first file that it finds.
Returns nil otherwise"
[]
(cond
(.exists (io/file "deps.edn")) "deps.edn"
(.exists (io/file "project.clj")) "project.clj"
(.exists (io/file "shadow-cljs.edn")) "shadow-cljs.edn"
(.exists (io/file "bb.edn")) "bb.edn"))
Loading

0 comments on commit 1c19396

Please sign in to comment.