Skip to content

Commit 307d7e8

Browse files
committed
Add some msg on debug (-X) mode. Add new option: gitCmd
1 parent 8fb9d1c commit 307d7e8

9 files changed

Lines changed: 141 additions & 47 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ add these repos to your `settings.xml` or your project `pom.xml`. Example:
6969
<th>Default</th>
7070
<th>Description</th>
7171
</tr>
72+
<tr>
73+
<td>gitCmd</td>
74+
<td>git</td>
75+
<td>Name for 'git' executable to use. You may specify an absolute pathname or a command in you "PATH".</td>
76+
</tr>
7277
<tr>
7378
<td>tstampFormat</td>
7479
<td>yyyyMMddHHmmss</td>

TODO.org

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*** DONE write basic assertion (verify.sh) to validate Mojo's output
88
** DONE Clean up Mojo's output on execution.
99
** SOMEDAY Improve error handling? (need more test cases?)
10+
*** DONE Don't depend on bash
11+
*** DONE On debug (-X) print forked commands to stdout
12+
*** DONE On debug (-X) pring lines as they are processed by git-describe-log-lines
1013
** TODO Write example pom.xml/project (may be same as integration test?)
1114
*** add filtering of propeties file
1215
*** add field on jar MANIFEST
@@ -31,7 +34,7 @@
3134
* TODO Generalize protocol for potential non-git implementations
3235
** see https://github.com/Raynes/bultitude
3336
* SOMEDAY Learn more about Plexus and how Maven uses it
34-
* TODO Propose a defmojo macro for clojure-maven project?
37+
* Propose a defmojo macro for clojure-maven project?
3538
** DONE Implement
3639
(defmojo
3740
"This is an example Maven Plugin written in Clojure"

src/it/setting-git-cmd/git.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
git "$@"

src/it/setting-git-cmd/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.code54.mojo</groupId>
7+
<artifactId>buildversion-plugin-example</artifactId>
8+
<version>9.9.99</version>
9+
10+
<name>Build version project example</name>
11+
<organization>
12+
<name>Code54</name>
13+
<url>http://code54.com</url>
14+
</organization>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>com.code54.mojo</groupId>
20+
<artifactId>buildversion-plugin</artifactId>
21+
<version>1.0.1-SNAPSHOT</version>
22+
<executions>
23+
<execution>
24+
<goals><goal>set-properties</goal></goals>
25+
<configuration>
26+
<gitCmd>./git.sh</gitCmd>
27+
</configuration>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
32+
</plugins>
33+
</build>
34+
35+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
// make git.sh wrapper executable.
3+
// Need to do this because maven-invoker-plugin's cloneProjectsTo doesn't seem to preserve
4+
// permissions
5+
println "*** Ensure git.sh is executable"
6+
def p = "chmod +x target/it/setting-git-cmd/git.sh".execute()
7+
p.waitFor()
8+
9+
return true
10+
11+
12+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// looking for usage of "git.sh", because we specified that as the git command to use
3+
4+
def build_log = new File("target/it/setting-git-cmd/build.log").text
5+
6+
return build_log.contains("git.sh")
7+

src/main/clojure/buildversion_plugin/git.clj

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
(:import java.util.Date java.text.SimpleDateFormat)
44
(:use ;; [clojure.tools.trace :only [dotrace deftrace]]
55
[clojure.java.io :only [reader]]
6-
[clojure.string :only [trim-newline replace-first split]] )
6+
[clojure.string :only [trim-newline replace-first split join]] )
77
(:require
88
[conch.core :as sh] ))
99

10+
11+
(def ^:dynamic *debug-fn* #(println %))
12+
(def ^:dynamic *git-cmd* "git")
13+
14+
1015
(defn run-git
1116
([args] (run-git "." args))
12-
([project-dir args]
13-
(apply sh/proc `("git" ~@args :dir ~project-dir))))
17+
([project-dir args]
18+
(*debug-fn* (str "Running cmd: " *git-cmd* " " (join " " args)))
19+
(apply sh/proc `(~*git-cmd* ~@args :dir ~project-dir))))
1420

1521
(defn run-git-wait
1622
([args] (run-git-wait "." args))
@@ -28,12 +34,13 @@
2834
"Given a seq of \"git log\" output lines, return map with :git-tag (most recent tag)
2935
and :git-tag-delta (number of commits to reach it)"
3036
(loop [i 0, lines log-lines-seq]
31-
(let [[_ hash tag] (re-find #"^(\w+) .*tag: (v[^\)\,]+).*" (first lines))]
37+
(let [line (first lines)
38+
[_ hash tag] (re-find #"^(\w+) .*tag: (v[^\)\,]+).*" line)]
39+
(*debug-fn* (str "Processing git-log line: " line))
3240
(if (and (not tag) (next lines))
3341
(recur (inc i) (next lines))
3442
[tag i]))))
3543

36-
3744
(defn git-describe-first-parent [dir]
3845
"Return map with :git-tag (most recent tag on current branch (always following \"first-parent\" on merges))
3946
and :git-tag-delta (number of commits -couting on first-parent paths only- from :git-tag to HEAD) "
@@ -44,37 +51,40 @@ and :git-tag-delta (number of commits -couting on first-parent paths only- from
4451
(sh/destroy p)
4552
{:git-tag tag, :git-tag-delta delta}))
4653

47-
(defn infer-project-version [dir config-map]
54+
(defn infer-project-version [dir {:keys [tstamp-format git-cmd debug-fn]
55+
:or {tstamp-format "yyyyMMddHHmmss"
56+
git-cmd "git"
57+
debug-fn #(println %)}}]
4858
"Infer the current project version from tags on the source-control system"
4959

50-
(let [tstamp-format (:tstamp-format config-map)
51-
commit-tstamp (-> (run-git-wait dir ["log" "-n" "1" "--format=%ct"])
52-
trim-newline
53-
(Long/parseLong)
54-
(* 1000)
55-
Date.)
56-
format-str (or tstamp-format "yyyyMMddHHmmss")
57-
commit-tstamp-str (.format (SimpleDateFormat. format-str)
58-
commit-tstamp)
60+
(binding [*debug-fn* debug-fn, *git-cmd* git-cmd]
61+
(let [commit-tstamp (-> (run-git-wait dir ["log" "-n" "1" "--format=%ct"])
62+
trim-newline
63+
(Long/parseLong)
64+
(* 1000)
65+
Date.)
66+
format-str (or tstamp-format "yyyyMMddHHmmss")
67+
commit-tstamp-str (.format (SimpleDateFormat. format-str)
68+
commit-tstamp)
5969

60-
[short-hash long-hash] (split
61-
(run-git-wait dir [ "log" "-n" "1" "--format=%h %H" ])
62-
#" " )
70+
[short-hash long-hash] (split
71+
(run-git-wait dir [ "log" "-n" "1" "--format=%h %H" ])
72+
#" " )
6373

64-
versioning-properties {:build-tag "N/A"
65-
:build-version "N/A"
66-
:build-tag-delta "0"
67-
:build-tstamp commit-tstamp-str
68-
:build-commit long-hash
69-
:build-commit-abbrev short-hash }
70-
71-
{:keys [git-tag git-tag-delta] } (git-describe-first-parent dir)]
74+
versioning-properties {:build-tag "N/A"
75+
:build-version "N/A"
76+
:build-tag-delta "0"
77+
:build-tstamp commit-tstamp-str
78+
:build-commit long-hash
79+
:build-commit-abbrev short-hash }
80+
81+
{:keys [git-tag git-tag-delta] } (git-describe-first-parent dir)]
7282

73-
(if (nil? git-tag)
74-
versioning-properties
75-
76-
(let [maven-artifact-version ((re-find #"v(.*)" git-tag) 1)]
77-
(merge versioning-properties
78-
{:build-tag maven-artifact-version
79-
:build-version (str (replace-first git-tag #"^v" "") "-" git-tag-delta "-" short-hash)
80-
:build-tag-delta (str git-tag-delta) })))))
83+
(if (nil? git-tag)
84+
versioning-properties
85+
86+
(let [maven-artifact-version ((re-find #"v(.*)" git-tag) 1)]
87+
(merge versioning-properties
88+
{:build-tag maven-artifact-version
89+
:build-version (str (replace-first git-tag #"^v" "") "-" git-tag-delta "-" short-hash)
90+
:build-tag-delta (str git-tag-delta) }))))))

src/main/clojure/buildversion_plugin/mojo.clj

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,36 @@
1919
{:goal "set-properties" :phase "initialize" }
2020

2121
;; Mojo parameters
22-
23-
[project {:expression "${project}" :required true :readonly true}
24-
25-
tstamp-format {:alias "tstampFormat" :default "yyyyMMddHHmmss"
22+
[project {:expression "${project}"
23+
:required true
24+
:readonly true}
25+
tstamp-format {:alias "tstampFormat"
26+
:default "yyyyMMddHHmmss"
2627
:typename "java.lang.String"}
27-
28-
custom-script {:alias "customProperties" :typename "java.lang.String"} ]
29-
28+
custom-script {:alias "customProperties"
29+
:typename "java.lang.String"}
30+
git-cmd {:alias "gitCmd"
31+
:default "git"
32+
:typename "java.lang.String"} ]
3033

3134
;; Goal execution
32-
(let [git-versions (git/infer-project-version "." {:tstamp-format tstamp-format} )
35+
(let [log-debug #(.debug log/*plexus-log* (str "[buildversion-plugin] " %))
36+
git-versions (git/infer-project-version "."
37+
{:tstamp-format tstamp-format
38+
:git-cmd (or git-cmd "git")
39+
:debug-fn log-debug } )
3340
custom-versions (if custom-script
3441
(eval-custom-script git-versions custom-script)
3542
{})
3643
versions-map (merge git-versions custom-versions)
3744
props (.getProperties project)
3845
]
3946

40-
(log/debug (str "buildversion-plugin - Setting properties: "))
47+
(log-debug "Setting properties: ")
4148
(doseq [[prop value] versions-map]
42-
(log/debug (str (name prop) ": " value))
49+
(log-debug (str (name prop) ": " value))
4350
(.put props (name prop) value))))
4451

4552

46-
47-
4853
;; injecting project version does not working well :-(
4954
;; (.setVersion project (:maven-artifact-version versions-map))

src/test/clojure/buildversion_plugin/test/git.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@
7474
(is (re-seq #"git version [\d\.]+"
7575
(git/run-git-wait sample-project-dir ["--version"]))))
7676

77+
(deftest test-run-git-invalid-path
78+
(binding [git/*git-cmd* "/invalid/path/to/git"]
79+
(is (thrown-with-msg? Exception #"/invalid/path/to/git"
80+
(git/run-git-wait sample-project-dir ["--version"])))))
81+
82+
(deftest test-run-git-invalid-arguments
83+
(is (thrown-with-msg? RuntimeException #"Git command failed"
84+
(git/run-git-wait sample-project-dir ["--invalid-argument-here"]))))
85+
7786
(deftest test-infer-project-versions
7887

7988
(assert-for-commit "First tagged commit"
@@ -149,6 +158,12 @@
149158
"bbccdde Dummy commit2"
150159
"ccddeef Dymmy commit3"] nil 2))
151160

161+
(deftest test-git-describe-first-parent--unexpected-output
162+
163+
(let [lines ["unexpected" "output" "from" "git"]]
164+
(git/git-describe-log-lines lines)))
165+
166+
152167

153168
(deftest test-tstamp-format-option
154169
(let [actual-versions (git/infer-project-version sample-project-dir

0 commit comments

Comments
 (0)