Skip to content

Commit

Permalink
Added support for git-changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tengstrand committed Nov 26, 2017
1 parent 578f3f5 commit 803f7aa
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -15,3 +15,5 @@ pom.properties
clojure/leiningen-plugin/target
leiningen.iml
clojure/leiningen-plugin/.idea
.idea
lein-polylith.iml
11 changes: 5 additions & 6 deletions clojure/leiningen-plugin/project.clj
@@ -1,7 +1,6 @@
(defproject polylith/lein-polylith "0.1-SNAPSHOT"
:description "Polylith, a component based architecture"
:url "https://github.com/tengstrand/polylith"
:dependencies [[selmer "1.11.3"]]
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:eval-in-leiningen true)
:description "Polylith, a component based architecture"
:url "https://github.com/tengstrand/polylith"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:eval-in-leiningen true)
47 changes: 29 additions & 18 deletions clojure/leiningen-plugin/src/leiningen/polylith.clj
@@ -1,24 +1,35 @@
(ns leiningen.polylith
(:require [leiningen.polylith.cmd :as cmd]))

(defn- print-missing-root-dir []
(println "Root directory must be set in the project.clj file, e.g.:")
(println " {:polylith {:root-dir \"/Users/joakimtengstrand/projects/myproject\""))

(defn ^:no-project-needed polylith
{:help-arglists '([cmd/gen-deps
cmd/build-git
cmd/deps])
:subtasks [#'cmd/gen-deps
#'cmd/build-git
#'cmd/deps]}
{:help-arglists '([cmd/components
cmd/deps
cmd/gen-deps
cmd/git-changes
cmd/help
cmd/project-settings])
:subtasks [#'cmd/components
#'cmd/gen-deps
#'cmd/git-changes
#'cmd/help
#'cmd/deps
#'cmd/project-settings]}
([project]
(println "The Polylith architecture: https://github.com/tengstrand/polylith")
(println)
(println " lein polylith a where a is:")
(println)
(println " deps Prints all dependencies.")
(println " gen-deps Generate dependency files")
(println " build-git"))
(cmd/help))
([project subtask & args]
(case subtask
"gen-deps" (cmd/gen-deps)
"build-git" (cmd/build-git args)
"deps" (cmd/deps)
(cmd/task-not-found subtask))))
(let [settings (:polylith project)
root-dir (:root-dir settings)]
(if (nil? root-dir)
(print-missing-root-dir)
(case subtask
"components" (cmd/components root-dir)
"deps" (cmd/deps)
"gen-deps" (cmd/gen-deps)
"git-changes" (cmd/git-changes root-dir args)
"help" (cmd/help)
"settings" (cmd/project-settings settings)
(cmd/task-not-found subtask))))))
44 changes: 35 additions & 9 deletions clojure/leiningen-plugin/src/leiningen/polylith/cmd.clj
Expand Up @@ -4,26 +4,52 @@
[clojure.string :as str]
[leiningen.polylith.file :as file]))

(defn components [root-dir]
(println (file/directory-names root-dir)))

(defn help []
(println "The Polylith architecture: https://github.com/tengstrand/polylith")
(println)
(println " lein polylith x where x is:")
(println)
(println " components List all components")
(println " deps List all dependencies")
(println " gen-deps Generate dependency files")
(println " git-changes List changed components and/or systems between two Git sha1:s")
(println " settings The polylith settings in current project.clj"))

(defn deps []
(let [dependencies (core/all-dependencies)]
(p/pprint dependencies)
(flush)))

(defn build-git [[current-sha1 last-success-sha1]]
(defn git-changes [root-dir [dir current-sha1 last-success-sha1]]
(if (or (nil? current-sha1)
(nil? last-success-sha1))
(do
(println "Both current and last-success SHA1 must be set as arguments:")
(println " lein polylith build-git current-sha1 last-success-sha1")
(println "e.g.:")
(println " lein polylith build-git 1c5196cb4a0aa5f30c8ac52220614e959440e37b 8dfb454c5ed7849b52991335be1a794d591671dd"))
(core/build-git current-sha1 last-success-sha1)))
(println "Missing parameters, use the format:")
(println " lein polylith git-changes x s1 s2")
(println " x = all -> show both component and system changes")
(println " components -> show component changes")
(println " systems -> show system changes")
(println " s1 = last successful Git sha1")
(println " s2 = current Git sha1")
(println)
(println " examples:")
(println " lein polylith git-changes all 1c5196cb4a0aa5f30c8ac52220614e959440e37b 8dfb454c5ed7849b52991335be1a794d591671dd")
(println " lein polylith git-changes systems 1c5196cb4a0aa5f30c8ac52220614e959440e37b 8dfb454c5ed7849b52991335be1a794d591671dd")
(println " lein polylith git-changes components 1c5196cb4a0aa5f30c8ac52220614e959440e37b 8dfb454c5ed7849b52991335be1a794d591671dd"))
(doseq [dir (core/git-changes root-dir dir last-success-sha1 current-sha1)]
(println (str " " dir)))))

(defn task-not-found [subtask]
(println "Subtask" subtask "not found.")
(println "Please type: `lein help polylith` for help."))

(defn gen-deps []
(let [file-separator (file/file-separator)]
(doseq [dependency (core/all-dependencies)]
(core/create-dependency-file! dependency file-separator))))

(defn task-not-found [subtask]
(println "Subtask" subtask "not found.")
(println "Please type: `lein help polylith` for help."))
(defn project-settings [settings]
(println settings))
29 changes: 21 additions & 8 deletions clojure/leiningen-plugin/src/leiningen/polylith/core.clj
@@ -1,8 +1,9 @@
(ns leiningen.polylith.core
(:require [clojure.string :as str]
(:require [clojure.pprint :as p]
[clojure.java.io :as io]
[clojure.string :as str]
[leiningen.polylith.file :as file]
[clojure.pprint :as p]))
[clojure.java.shell :as shell]))

(defn str->component [name]
(symbol (str/replace name #"_" "-")))
Expand All @@ -15,9 +16,9 @@

(defn api-ns->component []
(into {}
(reduce into []
(map ns-components
(partition-by first (file/paths-in-dir "apis"))))))
(reduce into []
(map ns-components
(partition-by first (file/paths-in-dir "apis"))))))

(defn- ->imports
([imports]
Expand Down Expand Up @@ -73,7 +74,19 @@
(let [path (str "dependencies" file-separator component ".edn")]
(file/create-file path functions)))

(defn build-git [current-sha1 last-success-sha1]
(println "curr=" current-sha1 ", last-success=" last-success-sha1))
(defn matching-dir? [path dir]
(and
(str/starts-with? path (str dir "/"))
(not (= path (str dir "/parent.clj")))))

;(build-git "1c5196cb4a0aa5f30c8ac52220614e959440e37b" "8dfb454c5ed7849b52991335be1a794d591671dd")
(defn git-changes [root-dir dir last-success-sha1 current-sha1]
(let [diff (:out (shell/sh "git" "diff" "--name-only" last-success-sha1 current-sha1 :dir root-dir))
files (str/split diff #"\n")
f (condp = dir
"all" #(or (matching-dir? % "components")
(matching-dir? % "systems"))
"components" #(matching-dir? % dir)
"systems" #(matching-dir? % dir)
(fn [x] false))]
(vec (sort (set (map #(second (str/split % #"/"))
(filter f files)))))))
5 changes: 5 additions & 0 deletions clojure/leiningen-plugin/src/leiningen/polylith/file.clj
Expand Up @@ -51,3 +51,8 @@
paths (map str (filter #(.isFile %) fs))
file-paths (filter keep? paths)]
(map component-path file-paths)))

(defn directory-names [dir]
(let [files (.listFiles (clojure.java.io/file dir))]
(mapv #(last (str/split (str %) #"/"))
(filter #(.isDirectory %) files))))

0 comments on commit 803f7aa

Please sign in to comment.