Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "RELEASE"}
com.cemerick/pomegranate {:mvn/version "RELEASE"}
s3-wagon-private/s3-wagon-private {:mvn/version "1.3.1"}
org.clojure/data.xml {:mvn/version "0.2.0-alpha5"}
org.slf4j/slf4j-nop {:mvn/version "RELEASE"}}
:aliases
Expand All @@ -14,7 +15,17 @@
"-d" "test"]}
:depstar {:extra-deps
{seancorfield/depstar {:mvn/version "1.1.117"}}}
:deploy {:main-opts ["-m" "deps-deploy.deps-deploy" "deploy"
"deps-deploy.jar" true]}
:install {:main-opts ["-m" "deps-deploy.deps-deploy" "install"
"deps-deploy.jar"]}}}

:deploy {:exec-fn deps-deploy.deps-deploy/deploy
:exec-args {:installer :remote
:sign-releases? true
:artifact "deps-deploy.jar"

:repository {"clojars" {:url "https://clojars.org/repo"
:username "your-username"
:password "CLOJARS_your-deploy-token"}}}}

:install {:exec-fn deps-deploy.deps-deploy/deploy
:exec-args {:installer :local
:artifact "deps-deploy.jar"}
}}}
90 changes: 77 additions & 13 deletions src/deps_deploy/deps_deploy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
[clojure.java.io :as io]
[clojure.data.xml :as xml]))


(aether/register-wagon-factory! "s3p" #(org.springframework.build.aws.maven.PrivateS3Wagon.))
(aether/register-wagon-factory! "s3" #(org.springframework.build.aws.maven.SimpleStorageServiceWagon.))


(def default-repo-settings {"clojars" {:url (or (System/getenv "CLOJARS_URL") "https://clojars.org/repo")
:username (System/getenv "CLOJARS_USERNAME")
:password (System/getenv "CLOJARS_PASSWORD")}})
Expand Down Expand Up @@ -75,9 +80,9 @@
(defn- artifact [{:keys [group-id artifact-id version]}]
(str group-id "/" artifact-id "-" version))

(defmulti deploy :installer)
(defmulti deploy* :installer)

(defmethod deploy :clojars [{:keys [artifact-map coordinates repository]
(defmethod deploy* :remote [{:keys [artifact-map coordinates repository]
:or {repository default-repo-settings} :as opts}]
(println "Deploying" (artifact coordinates) "to clojars as"
(-> repository vals first :username))
Expand All @@ -88,22 +93,81 @@
:coordinates (mvn-coordinates coordinates))
(println "done."))

(defmethod deploy :local [{:keys [artifact-map coordinates]}]
(defmethod deploy* :local [{:keys [artifact-map coordinates]}]
(println "Installing" (artifact coordinates) "to your local `.m2`")
(aether/install :artifact-map artifact-map
:transfer-listener :stdout
:coordinates (mvn-coordinates coordinates))
(println "done."))

(defn -main [deploy-or-install artifact & [sign-releases]]
(let [pom (slurp "pom.xml")
coordinates (coordinates-from-pom pom)
versioned-pom (spit (versioned-pom-filename coordinates) pom)]

(defn- tag-val-replacer [tag->val]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer creating fns which take the required amount of args, and then rather bind the args using partial (to which I'm, ehrm, partial) or with an anon-fn #(replace-tag {version-tag [version] group-id-tag [group-id] ...} %)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Cool. I'm quite partial to doing things that way too ;-) it was even my first instinct, which I held back on because I figured it'd be bikeshedded the other way. Happy to change.

(fn [{:keys [tag content attrs] :as el}]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attrs is not used?

(let [replacement-val (tag->val tag ::default)]
(if (= ::default replacement-val)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great use-case for cond->:

(cond-> el
   (not= ::default replacement-val) (assoc :content replacement-val))

It's almost like this is an update right?

(update el :content (fn [c] (if (= ::default replacement-val) c replacement-val)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is. Will revise both of those. Thanks for caring.

el
(assoc el :content replacement-val)))))

(defn update-pom [{:keys [pom-file
mvn/version
mvn/group-id
mvn/artifact-id] :as opts}]
(-> (or pom-file "pom.xml")
io/reader
xml/parse
(update :content
#(map (tag-val-replacer {version-tag [version]
group-id-tag [group-id]
artifact-id-tag [artifact-id]}) %))
xml/emit-str))

(defn deploy
"The main entry point into deps-deploy via tools.deps :exec-fn which
supports an opts map that can be supplied via :exec-args.
Required keys are:
:artifact A string specifying the file path relative to the current
working directory to the artifact to be deployed. This will
normally be your library packaged as a jar.
:installer Set to either :local or :remote depending on whether you
want to install into your local .m2 cache or to a remote
:repository.
:pom-file defaults to \"pom.xml\"
:sign-releases? A boolean that specifies whether releases should be
signed
"
[{:keys [sign-releases? artifact] :as opts}]
(let [updated-pom (update-pom opts)
coordinates (coordinates-from-pom updated-pom)
updated-pom-file-name (versioned-pom-filename coordinates)]
(spit updated-pom-file-name updated-pom)
(try
(deploy {:installer (cond (= "deploy" deploy-or-install) :clojars
(= "install" deploy-or-install) :local)
:artifact-map (all-artifacts sign-releases coordinates artifact)
:coordinates coordinates})
(deploy*
(assoc opts
:artifact-map (all-artifacts sign-releases? coordinates artifact)
:coordinates coordinates))
(finally
(.delete (java.io.File. (versioned-pom-filename coordinates)))))))
(.delete (java.io.File. updated-pom-file-name))))))

;; command line mode
(defn -main [deploy-or-install artifact & [sign-releases]]
(deploy {:installer (cond (= "deploy" deploy-or-install) :remote
(= "install" deploy-or-install) :local)
:sign-releases? (= "true" sign-releases)
:artifact artifact}))

(comment

(deploy {:installer :remote
:artifact "deps-deploy.jar"
:pom-file "pom.xml"
:mvn/version "999"
:repository {"clojars" {:url "https://clojars.org/repo"
:username "clojars-username"
:password "CLOJARS-token"}}})

)