Skip to content

Commit a790659

Browse files
committed
maven wrapper support
1 parent b9f4880 commit a790659

File tree

7 files changed

+89
-60
lines changed

7 files changed

+89
-60
lines changed

lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.diffplug.spotless;
1717

1818
import static java.nio.charset.StandardCharsets.UTF_8;
19+
import static java.util.Objects.requireNonNull;
1920

2021
import java.io.File;
2122
import java.io.FileWriter;
@@ -30,6 +31,9 @@
3031
*/
3132
public abstract class GitPrePushHookInstaller {
3233

34+
private static final String HOOK_HEADLINE = "##### SPOTLESS HOOK START #####";
35+
private static final String HOOK_FOOTER = "##### SPOTLESS HOOK END #####";
36+
3337
/**
3438
* Logger for recording informational and error messages during the installation process.
3539
*/
@@ -47,8 +51,8 @@ public abstract class GitPrePushHookInstaller {
4751
* @param root The root directory of the Git repository.
4852
*/
4953
public GitPrePushHookInstaller(GitPreHookLogger logger, File root) {
50-
this.logger = logger;
51-
this.root = root;
54+
this.logger = requireNonNull(logger, "logger can not be null");
55+
this.root = requireNonNull(root, "root file can not be null");
5256
}
5357

5458
/**
@@ -73,10 +77,6 @@ public void install() throws Exception {
7377
return;
7478
}
7579

76-
if (!isExecutorInstalled()) {
77-
return;
78-
}
79-
8080
var hookContent = "";
8181
final var gitHookFile = root.toPath().resolve(".git/hooks/pre-push").toFile();
8282
if (!gitHookFile.exists()) {
@@ -100,7 +100,7 @@ public void install() throws Exception {
100100
}
101101

102102
if (isGitHookInstalled(gitHookFile)) {
103-
logger.info("Skipping, git pre-push hook already installed %s", gitHookFile.getAbsolutePath());
103+
logger.warn("Skipping, git pre-push hook already installed %s", gitHookFile.getAbsolutePath());
104104
return;
105105
}
106106

@@ -110,13 +110,6 @@ public void install() throws Exception {
110110
logger.info("Git pre-push hook installed successfully to the file %s", gitHookFile.getAbsolutePath());
111111
}
112112

113-
/**
114-
* Checks if the required executor for performing the desired pre-push actions is installed.
115-
*
116-
* @return {@code true} if the executor is installed, {@code false} otherwise.
117-
*/
118-
protected abstract boolean isExecutorInstalled();
119-
120113
/**
121114
* Provides the content of the hook that should be inserted into the pre-push script.
122115
*
@@ -142,7 +135,7 @@ private boolean isGitInstalled() {
142135
*/
143136
private boolean isGitHookInstalled(File gitHookFile) throws Exception {
144137
final var hook = Files.readString(gitHookFile.toPath(), UTF_8);
145-
return hook.contains("##### SPOTLESS HOOK START #####");
138+
return hook.contains(HOOK_HEADLINE);
146139
}
147140

148141
/**
@@ -169,21 +162,21 @@ private void writeFile(File file, String content) throws IOException {
169162
*/
170163
protected String preHookTemplate(String executor, String commandCheck, String commandApply) {
171164
var spotlessHook = "\n";
172-
spotlessHook += "\n##### SPOTLESS HOOK START #####";
165+
spotlessHook += "\n" + HOOK_HEADLINE;
173166
spotlessHook += "\nSPOTLESS_EXECUTOR=" + executor;
174167
spotlessHook += "\nif ! $SPOTLESS_EXECUTOR " + commandCheck + " ; then";
175168
spotlessHook += "\n echo 1>&2 \"spotless found problems, running " + commandApply + "; commit the result and re-push\"";
176169
spotlessHook += "\n $SPOTLESS_EXECUTOR " + commandApply;
177170
spotlessHook += "\n exit 1";
178171
spotlessHook += "\nfi";
179-
spotlessHook += "\n##### SPOTLESS HOOK END #####";
172+
spotlessHook += "\n" + HOOK_FOOTER;
180173
spotlessHook += "\n\n";
181174
return spotlessHook;
182175
}
183176

184177
public interface GitPreHookLogger {
185178
void info(String format, Object... arguments);
186-
179+
void warn(String format, Object... arguments);
187180
void error(String format, Object... arguments);
188181
}
189182
}

lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerGradle.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,21 @@ public GitPrePushHookInstallerGradle(GitPreHookLogger logger, File root) {
3333
this.gradlew = root.toPath().resolve("gradlew").toFile();
3434
}
3535

36+
3637
/**
37-
* Checks if the Gradle wrapper (`gradlew`) is present in the root directory.
38-
* This ensures that the executor used for formatting (`spotlessCheck` and `spotlessApply`) is available.
39-
*
40-
* @return {@code true} if the Gradle wrapper is found, {@code false} otherwise.
41-
* An error is logged if the wrapper is not found.
38+
* {@inheritDoc}
4239
*/
4340
@Override
44-
protected boolean isExecutorInstalled() {
41+
protected String preHookContent() {
42+
return preHookTemplate(executor(), "spotlessCheck", "spotlessApply");
43+
}
44+
45+
private String executor() {
4546
if (gradlew.exists()) {
46-
return true;
47+
return gradlew.getAbsolutePath();
4748
}
4849

49-
logger.error("Failed to find gradlew in root directory");
50-
return false;
51-
}
52-
53-
@Override
54-
protected String preHookContent() {
55-
return preHookTemplate(gradlew.getAbsolutePath(), "spotlessCheck", "spotlessApply");
50+
logger.info("Gradle wrapper is not installed, using global gradle");
51+
return "gradle";
5652
}
5753
}

lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,27 @@
2323
*/
2424
public class GitPrePushHookInstallerMaven extends GitPrePushHookInstaller {
2525

26+
private final File mvnw;
27+
2628
public GitPrePushHookInstallerMaven(GitPreHookLogger logger, File root) {
2729
super(logger, root);
30+
this.mvnw = root.toPath().resolve("mvnw").toFile();
2831
}
2932

3033
/**
31-
* Confirms that Maven is installed and available for use.
32-
*
33-
* <p>This method assumes that if this code is running, then Maven is already properly installed and configured,
34-
* so it always returns {@code true}.
35-
*
36-
* @return {@code true}, indicating that Maven is available.
34+
* {@inheritDoc}
3735
*/
3836
@Override
39-
protected boolean isExecutorInstalled() {
40-
return true;
37+
protected String preHookContent() {
38+
return preHookTemplate(executor(), "spotless:check", "spotless:apply");
4139
}
4240

43-
@Override
44-
protected String preHookContent() {
45-
return preHookTemplate("mvn", "spotless:check", "spotless:apply");
41+
private String executor() {
42+
if (mvnw.exists()) {
43+
return mvnw.getAbsolutePath();
44+
}
45+
46+
logger.info("Maven wrapper is not installed, using global maven");
47+
return "mvn";
4648
}
4749
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTask.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public void info(String format, Object... arguments) {
4949
getLogger().lifecycle(String.format(format, arguments));
5050
}
5151

52+
@Override
53+
public void warn(String format, Object... arguments) {
54+
getLogger().warn(String.format(format, arguments));
55+
}
56+
5257
@Override
5358
public void error(String format, Object... arguments) {
5459
getLogger().error(String.format(format, arguments));

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTaskTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class SpotlessInstallPrePushHookTaskTest extends GradleIntegrationHarness {
2424
@Test
2525
public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws Exception {
2626
// given
27-
final var gradlew = setFile("gradlew").toContent("");
2827
setFile(".git/config").toContent("");
2928
newFile(".git/hooks").mkdirs();
3029
setFile("build.gradle").toLines(
@@ -46,7 +45,7 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
4645
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
4746

4847
final var content = getTestResource("git_pre_hook/pre-push.created")
49-
.replace("${executor}", gradlew.getAbsolutePath())
48+
.replace("${executor}", "gradle")
5049
.replace("${checkCommand}", "spotlessCheck")
5150
.replace("${applyCommand}", "spotlessApply");
5251
assertFile(".git/hooks/pre-push").hasContent(content);
@@ -55,7 +54,6 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
5554
@Test
5655
public void should_append_to_existing_pre_hook_file_when_hook_file_exists() throws Exception {
5756
// given
58-
final var gradlew = setFile("gradlew").toContent("");
5957
setFile(".git/config").toContent("");
6058
setFile("build.gradle").toLines(
6159
"plugins {",
@@ -76,7 +74,7 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
7674
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
7775

7876
final var content = getTestResource("git_pre_hook/pre-push.existing-added")
79-
.replace("${executor}", gradlew.getAbsolutePath())
77+
.replace("${executor}", "gradle")
8078
.replace("${checkCommand}", "spotlessCheck")
8179
.replace("${applyCommand}", "spotlessApply");
8280
assertFile(".git/hooks/pre-push").hasContent(content);

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public void info(String format, Object... arguments) {
6262
getLog().info(String.format(format, arguments));
6363
}
6464

65+
@Override
66+
public void warn(String format, Object... arguments) {
67+
getLog().warn(String.format(format, arguments));
68+
}
69+
6570
@Override
6671
public void error(String format, Object... arguments) {
6772
getLog().error(String.format(format, arguments));

testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public void info(String format, Object... arguments) {
3232
logs.add(String.format(format, arguments));
3333
}
3434

35+
@Override
36+
public void warn(String format, Object... arguments) {
37+
logs.add(String.format(format, arguments));
38+
}
39+
3540
@Override
3641
public void error(String format, Object... arguments) {
3742
logs.add(String.format(format, arguments));
@@ -54,7 +59,7 @@ public void should_not_create_pre_hook_file_when_git_is_not_installed() throws E
5459
}
5560

5661
@Test
57-
public void should_not_create_pre_hook_file_when_gradlew_is_not_installed() throws Exception {
62+
public void should_use_global_gradle_when_gradlew_is_not_installed() throws Exception {
5863
// given
5964
final var gradle = new GitPrePushHookInstallerGradle(logger, rootFolder());
6065
setFile(".git/config").toContent("");
@@ -63,10 +68,14 @@ public void should_not_create_pre_hook_file_when_gradlew_is_not_installed() thro
6368
gradle.install();
6469

6570
// then
66-
assertThat(logs).hasSize(2);
71+
assertThat(logs).hasSize(4);
6772
assertThat(logs).element(0).isEqualTo("Installing git pre-push hook");
68-
assertThat(logs).element(1).isEqualTo("Failed to find gradlew in root directory");
69-
assertThat(newFile(".git/hooks/pre-push")).doesNotExist();
73+
assertThat(logs).element(1).isEqualTo("Git pre-push hook not found, creating it");
74+
assertThat(logs).element(2).isEqualTo("Gradle wrapper is not installed, using global gradle");
75+
assertThat(logs).element(3).isEqualTo("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push").getAbsolutePath());
76+
77+
final var content = gradleHookContent("git_pre_hook/pre-push.created", false);
78+
assertFile(".git/hooks/pre-push").hasContent(content);
7079
}
7180

7281
@Test
@@ -92,7 +101,7 @@ public void should_not_create_pre_hook_file_when_hook_already_installed() throws
92101
public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws Exception {
93102
// given
94103
final var gradle = new GitPrePushHookInstallerGradle(logger, rootFolder());
95-
final var gradlew = setFile("gradlew").toContent("");
104+
setFile("gradlew").toContent("");
96105
setFile(".git/config").toContent("");
97106

98107
// when
@@ -104,15 +113,15 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
104113
assertThat(logs).element(1).isEqualTo("Git pre-push hook not found, creating it");
105114
assertThat(logs).element(2).isEqualTo("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push").getAbsolutePath());
106115

107-
final var content = gradleHookContent("git_pre_hook/pre-push.created");
116+
final var content = gradleHookContent("git_pre_hook/pre-push.created", true);
108117
assertFile(".git/hooks/pre-push").hasContent(content);
109118
}
110119

111120
@Test
112121
public void should_append_to_existing_pre_hook_file_when_hook_file_exists() throws Exception {
113122
// given
114123
final var gradle = new GitPrePushHookInstallerGradle(logger, rootFolder());
115-
final var gradlew = setFile("gradlew").toContent("");
124+
setFile("gradlew").toContent("");
116125
setFile(".git/config").toContent("");
117126
setFile(".git/hooks/pre-push").toResource("git_pre_hook/pre-push.existing");
118127

@@ -124,14 +133,15 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
124133
assertThat(logs).element(0).isEqualTo("Installing git pre-push hook");
125134
assertThat(logs).element(1).isEqualTo("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push").getAbsolutePath());
126135

127-
final var content = gradleHookContent("git_pre_hook/pre-push.existing-added");
136+
final var content = gradleHookContent("git_pre_hook/pre-push.existing-added", true);
128137
assertFile(".git/hooks/pre-push").hasContent(content);
129138
}
130139

131140
@Test
132141
public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists() throws Exception {
133142
// given
134143
final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder());
144+
setFile("mvnw").toContent("");
135145
setFile(".git/config").toContent("");
136146

137147
// when
@@ -143,20 +153,40 @@ public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists
143153
assertThat(logs).element(1).isEqualTo("Git pre-push hook not found, creating it");
144154
assertThat(logs).element(2).isEqualTo("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push").getAbsolutePath());
145155

146-
final var content = mavenHookContent("git_pre_hook/pre-push.created");
156+
final var content = mavenHookContent("git_pre_hook/pre-push.created", true);
157+
assertFile(".git/hooks/pre-push").hasContent(content);
158+
}
159+
160+
@Test
161+
public void should_use_global_maven_when_maven_wrapper_is_not_installed() throws Exception {
162+
// given
163+
final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder());
164+
setFile(".git/config").toContent("");
165+
166+
// when
167+
gradle.install();
168+
169+
// then
170+
assertThat(logs).hasSize(4);
171+
assertThat(logs).element(0).isEqualTo("Installing git pre-push hook");
172+
assertThat(logs).element(1).isEqualTo("Git pre-push hook not found, creating it");
173+
assertThat(logs).element(2).isEqualTo("Maven wrapper is not installed, using global maven");
174+
assertThat(logs).element(3).isEqualTo("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push").getAbsolutePath());
175+
176+
final var content = mavenHookContent("git_pre_hook/pre-push.created", false);
147177
assertFile(".git/hooks/pre-push").hasContent(content);
148178
}
149179

150-
private String gradleHookContent(String resourcePath) {
180+
private String gradleHookContent(String resourcePath, boolean isWrapper) {
151181
return getTestResource(resourcePath)
152-
.replace("${executor}", setFile("gradlew").toContent("").getAbsolutePath())
182+
.replace("${executor}", isWrapper ? newFile("gradlew").getAbsolutePath() : "gradle")
153183
.replace("${checkCommand}", "spotlessCheck")
154184
.replace("${applyCommand}", "spotlessApply");
155185
}
156186

157-
private String mavenHookContent(String resourcePath) {
187+
private String mavenHookContent(String resourcePath, boolean isWrapper) {
158188
return getTestResource(resourcePath)
159-
.replace("${executor}", "mvn")
189+
.replace("${executor}", isWrapper ? newFile("mvnw").getAbsolutePath() : "mvn")
160190
.replace("${checkCommand}", "spotless:check")
161191
.replace("${applyCommand}", "spotless:apply");
162192
}

0 commit comments

Comments
 (0)