Skip to content

Commit

Permalink
Merge pull request #491 from pblanchardie/maven-indent
Browse files Browse the repository at this point in the history
harmonization of pom.xml indentation and eol management
  • Loading branch information
pascalgrimaud authored Jan 6, 2022
2 parents d0135e7 + 5eca4f3 commit 88703ad
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class FileUtils {

public static final String REGEXP_PREFIX_MULTILINE = "(?m)";
public static final String REGEXP_PREFIX_DOTALL = "(?s)";
public static final String DOT_STAR_REGEX = ".*";
public static final String REGEXP_DOT_STAR = ".*";
public static final String REGEXP_SPACE_STAR = "[ \t]*";

private FileUtils() {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.jhipster.lite.generator.buildtool.maven.domain;

import static tech.jhipster.lite.common.domain.WordUtils.LF;
import static tech.jhipster.lite.common.domain.WordUtils.indent;

import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -66,37 +65,36 @@ public static String getParentHeader(Parent parent) {
}

public static String getParentHeader(Parent parent, int indentation) {
StringBuilder result = new StringBuilder()
.append(PARENT_BEGIN)
.append(LF)
.append(indent(2, indentation))
String begin = PARENT_BEGIN + LF;

String content = new StringBuilder()
.append(GROUP_ID_BEGIN)
.append(parent.getGroupId())
.append(GROUP_ID_END)
.append(LF)
.append(indent(2, indentation))
.append(ARTIFACT_ID_BEGIN)
.append(parent.getArtifactId())
.append(ARTIFACT_ID_END)
.append(LF);
return result.toString();
.append(LF)
.toString()
.indent(indentation);

return begin + content;
}

public static String getParent(Parent parent, int indentation) {
StringBuilder result = new StringBuilder()
.append(getParentHeader(parent, indentation))
.append(indent(2, indentation))
String header = getParentHeader(parent, indentation);

String additionalContent = new StringBuilder()
.append(VERSION_BEGIN)
.append(parent.getVersion())
.append(VERSION_END)
.append(LF)
.append(indent(2, indentation))
.append("<relativePath />")
.append(LF)
.append(indent(1, indentation))
.append(PARENT_END);
.toString()
.indent(indentation);

return result.toString();
return header + additionalContent + PARENT_END;
}

public static String getDependency(Dependency dependency, int indentation) {
Expand Down Expand Up @@ -177,72 +175,35 @@ public static String getExclusions(List<Dependency> exclusions, int indentation)
return begin + body + EXCLUSIONS_END;
}

public static String getPlugin(Plugin plugin) {
return getPlugin(plugin, WordUtils.DEFAULT_INDENTATION, 3);
}

public static String getPlugin(Plugin plugin, int indentation) {
return getPlugin(plugin, indentation, 3);
}

public static String getPluginHeader(Plugin plugin) {
return getPluginHeader(plugin, WordUtils.DEFAULT_INDENTATION, 3);
}

public static String getPluginHeader(Plugin plugin, int indentation) {
return getPluginHeader(plugin, indentation, 3);
}
String begin = PLUGIN_BEGIN + LF;

public static String getPluginManagement(Plugin plugin) {
return getPlugin(plugin, WordUtils.DEFAULT_INDENTATION, 4);
}

public static String getPluginManagement(Plugin plugin, int indentation) {
return getPlugin(plugin, indentation, 4);
}

public static String getPluginManagementHeader(Plugin plugin) {
return getPluginHeader(plugin, WordUtils.DEFAULT_INDENTATION, 4);
}

public static String getPluginManagementHeader(Plugin plugin, int indentation) {
return getPluginHeader(plugin, indentation, 4);
}

public static String getPluginHeader(Plugin plugin, int indentation, int initialIndentation) {
StringBuilder result = new StringBuilder()
.append(PLUGIN_BEGIN)
.append(LF)
.append(indent(initialIndentation + 1, indentation))
String content = new StringBuilder()
.append(GROUP_ID_BEGIN)
.append(plugin.getGroupId())
.append(GROUP_ID_END)
.append(LF)
.append(indent(initialIndentation + 1, indentation))
.append(ARTIFACT_ID_BEGIN)
.append(plugin.getArtifactId())
.append(ARTIFACT_ID_END)
.append(LF);
return result.toString();
.toString()
.indent(indentation);

return begin + content;
}

public static String getPlugin(Plugin plugin, int indentation, int initialIndentation) {
StringBuilder result = new StringBuilder().append(getPluginHeader(plugin, indentation, initialIndentation));
public static String getPlugin(Plugin plugin, int indentation) {
String header = getPluginHeader(plugin, indentation);

plugin
.getVersion()
.ifPresent(version ->
result.append(indent(initialIndentation + 1, indentation)).append(VERSION_BEGIN).append(version).append(VERSION_END).append(LF)
);
StringBuilder additionalBodyBuilder = new StringBuilder();

//replace '\n' to make the multi-line additionalConfiguration platform specific
plugin
.getAdditionalElements()
.ifPresent(additionalElements -> result.append(plugin.getAdditionalElements().get().indent((initialIndentation + 1) * indentation)));
plugin.getVersion().ifPresent(version -> additionalBodyBuilder.append(VERSION_BEGIN).append(version).append(VERSION_END).append(LF));

result.append(indent(initialIndentation, indentation)).append(PLUGIN_END);
plugin.getAdditionalElements().ifPresent(additionalBodyBuilder::append);

String additionalBody = additionalBodyBuilder.toString().indent(indentation);

return result.toString();
return header + additionalBody + PLUGIN_END;
}

public static String getProperty(String key, String version) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.jhipster.lite.generator.buildtool.maven.domain;

import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.common.domain.FileUtils.*;
import static tech.jhipster.lite.common.domain.WordUtils.*;
import static tech.jhipster.lite.generator.buildtool.maven.domain.Maven.*;
import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML;
Expand Down Expand Up @@ -31,12 +31,12 @@ public void addParent(Project project, Parent parent) {

int indent = (Integer) project.getConfig(PRETTIER_DEFAULT_INDENT).orElse(DEFAULT_INDENTATION);

String parentNode = Maven.getParent(parent, indent).replace(LF, project.getEndOfLine());
String parentHeaderNode = Maven.getParentHeader(parent, indent).replace(LF, project.getEndOfLine());
String parentHeaderRegexp = FileUtils.REGEXP_PREFIX_MULTILINE + parentHeaderNode;
String parentHeaderNode = Maven.getParentHeader(parent, indent).indent(indent);
String parentHeaderRegexp = (REGEXP_PREFIX_MULTILINE + parentHeaderNode).replace(LF, project.getEndOfLine());

if (!projectRepository.containsRegexp(project, "", POM_XML, parentHeaderRegexp)) {
projectRepository.replaceText(project, "", POM_XML, NEEDLE_PARENT, parentNode);
String newParentNode = Maven.getParent(parent, indent).indent(indent).replace(LF, project.getEndOfLine());
projectRepository.replaceText(project, "", POM_XML, REGEXP_SPACE_STAR + NEEDLE_PARENT + project.getEndOfLine(), newParentNode);
}
}

Expand Down Expand Up @@ -68,15 +68,13 @@ private void addDependency(Project project, Dependency dependency, List<Dependen
int level = NEEDLE_DEPENDENCY_MANAGEMENT.equals(needle) ? 3 : 2;

String dependencyNode = Maven.getDependencyHeader(dependency, indent).indent(2 * indent);
String dependencyRegexp = (FileUtils.REGEXP_PREFIX_MULTILINE + dependencyNode).replace(LF, project.getEndOfLine());
String dependencyRegexp = (REGEXP_PREFIX_MULTILINE + dependencyNode).replace(LF, project.getEndOfLine());

if (!projectRepository.containsRegexp(project, "", POM_XML, dependencyRegexp)) {
project.addDefaultConfig(PRETTIER_DEFAULT_INDENT);

String newDependencyNode = Maven.getDependency(dependency, indent, exclusions).indent(level * indent);
String dependencyWithNeedle = (newDependencyNode + indent(level, indent) + needle).replace(LF, project.getEndOfLine());

projectRepository.replaceText(project, "", POM_XML, "[ \t]*" + needle, dependencyWithNeedle);
projectRepository.replaceText(project, "", POM_XML, REGEXP_SPACE_STAR + needle, dependencyWithNeedle);
}
}

Expand All @@ -97,44 +95,46 @@ public void deleteDependency(Project project, Dependency dependency) {

@Override
public void addPlugin(Project project, Plugin plugin) {
project.addDefaultConfig(PRETTIER_DEFAULT_INDENT);

int indent = (Integer) project.getConfig(PRETTIER_DEFAULT_INDENT).orElse(DEFAULT_INDENTATION);

String pluginNodeNode = Maven.getPluginHeader(plugin, indent);

//Checking for plugin declaration between <plugin> section and needle (not </plugin> as it can conflict with <plugin> section in <pluginManagement>)
String pluginRegexp =
FileUtils.REGEXP_PREFIX_DOTALL + PLUGIN_BEGIN + FileUtils.DOT_STAR_REGEX + pluginNodeNode + FileUtils.DOT_STAR_REGEX + NEEDLE_PLUGIN;

if (!projectRepository.containsRegexp(project, "", POM_XML, pluginRegexp)) {
String pluginWithNeedle = Maven.getPlugin(plugin, indent) + project.getEndOfLine() + indent(3, indent) + NEEDLE_PLUGIN;

projectRepository.replaceText(project, "", POM_XML, NEEDLE_PLUGIN, pluginWithNeedle);
}
addPlugin(project, plugin, NEEDLE_PLUGIN);
}

@Override
public void addPluginManagement(Project project, Plugin plugin) {
addPlugin(project, plugin, NEEDLE_PLUGIN_MANAGEMENT);
}

private void addPlugin(Project project, Plugin plugin, String needle) {
project.addDefaultConfig(PRETTIER_DEFAULT_INDENT);

int indent = (Integer) project.getConfig(PRETTIER_DEFAULT_INDENT).orElse(DEFAULT_INDENTATION);

String pluginNodeNode = Maven.getPluginManagementHeader(plugin, indent);
//Checking for plugin declaration in <pluginManagement> section
String pluginRegexp =
FileUtils.REGEXP_PREFIX_DOTALL +
PLUGIN_MANAGEMENT_BEGIN +
FileUtils.DOT_STAR_REGEX +
pluginNodeNode +
FileUtils.DOT_STAR_REGEX +
PLUGIN_MANAGEMENT_END;
int level = NEEDLE_PLUGIN_MANAGEMENT.equals(needle) ? 4 : 3;

String pluginNode = Maven.getPluginHeader(plugin, indent).indent(level * indent);

String pluginRegexp;
if (NEEDLE_PLUGIN_MANAGEMENT.equals(needle)) {
//Checking for plugin declaration in <pluginManagement> section
pluginRegexp =
(
FileUtils.REGEXP_PREFIX_DOTALL +
PLUGIN_MANAGEMENT_BEGIN +
FileUtils.REGEXP_DOT_STAR +
pluginNode +
FileUtils.REGEXP_DOT_STAR +
PLUGIN_MANAGEMENT_END
);
} else {
//Checking for plugin declaration between <plugin> section and needle (not </plugin> as it can conflict with <plugin> section in <pluginManagement>)
pluginRegexp =
FileUtils.REGEXP_PREFIX_DOTALL + PLUGIN_BEGIN + FileUtils.REGEXP_DOT_STAR + pluginNode + FileUtils.REGEXP_DOT_STAR + NEEDLE_PLUGIN;
}

if (!projectRepository.containsRegexp(project, "", POM_XML, pluginRegexp)) {
String pluginWithNeedle =
Maven.getPluginManagement(plugin, indent) + project.getEndOfLine() + indent(4, indent) + NEEDLE_PLUGIN_MANAGEMENT;
if (!projectRepository.containsRegexp(project, "", POM_XML, pluginRegexp.replace(LF, project.getEndOfLine()))) {
String newPluginNode = Maven.getPlugin(plugin, indent).indent(level * indent);
String pluginWithNeedle = (newPluginNode + indent(level, indent) + needle).replace(LF, project.getEndOfLine());

projectRepository.replaceText(project, "", POM_XML, NEEDLE_PLUGIN_MANAGEMENT, pluginWithNeedle);
projectRepository.replaceText(project, "", POM_XML, REGEXP_SPACE_STAR + needle, pluginWithNeedle);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static tech.jhipster.lite.TestUtils.*;
import static tech.jhipster.lite.common.domain.WordUtils.DEFAULT_INDENTATION;
import static tech.jhipster.lite.generator.buildtool.maven.application.MavenAssertFiles.*;
import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML;

Expand Down Expand Up @@ -61,7 +62,7 @@ void shouldAddParentOnlyOneTime() throws Exception {
mavenApplicationService.addParent(project, parent);
mavenApplicationService.addParent(project, parent);

assertFileContentManyTimes(project, POM_XML, Maven.getParentHeader(parent), 1);
assertFileContentManyTimes(project, POM_XML, Maven.getParentHeader(parent).indent(DEFAULT_INDENTATION), 1);
}

@Test
Expand Down Expand Up @@ -175,7 +176,12 @@ void shouldAddDependencyOnlyOneTime() throws Exception {
mavenApplicationService.addDependency(project, dependency);
mavenApplicationService.addDependency(project, dependency);

assertFileContentManyTimes(project, POM_XML, Maven.getDependencyHeader(dependency, 2).indent(4), 1);
assertFileContentManyTimes(
project,
POM_XML,
Maven.getDependencyHeader(dependency, DEFAULT_INDENTATION).indent(2 * DEFAULT_INDENTATION),
1
);
}

@Test
Expand Down Expand Up @@ -381,7 +387,7 @@ void shouldAddPluginOnlyOneTime() throws Exception {
mavenApplicationService.addPlugin(project, plugin);
mavenApplicationService.addPlugin(project, plugin);

assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin), 1);
assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(3 * DEFAULT_INDENTATION), 1);
}

@Test
Expand All @@ -392,7 +398,7 @@ void shouldAddPluginManagementOnlyOneTime() throws Exception {
mavenApplicationService.addPluginManagement(project, plugin);
mavenApplicationService.addPluginManagement(project, plugin);

assertFileContentManyTimes(project, POM_XML, Maven.getPluginManagementHeader(plugin), 1);
assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(4 * DEFAULT_INDENTATION), 1);
}

@Test
Expand All @@ -403,8 +409,8 @@ void shouldAddPluginManagementWithExistingPlugin() throws Exception {
mavenApplicationService.addPlugin(project, plugin);
mavenApplicationService.addPluginManagement(project, plugin);

assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin), 1);
assertFileContentManyTimes(project, POM_XML, Maven.getPluginManagementHeader(plugin), 1);
assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(3 * DEFAULT_INDENTATION), 1);
assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(4 * DEFAULT_INDENTATION), 1);
}

@Test
Expand Down
Loading

0 comments on commit 88703ad

Please sign in to comment.