Skip to content

Commit

Permalink
Merge pull request #2323 from DamnClin/uniformise-line-break
Browse files Browse the repository at this point in the history
Uniformise line breaks in modules
  • Loading branch information
pascalgrimaud authored Jun 30, 2022
2 parents ea65fef + 11aef00 commit 3175c41
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

public class JHipsterModule {

public static final String LINE_BREAK = "\n";

private final JHipsterProjectFolder projectFolder;
private final Collection<JHipsterModuleFile> files;
private final JHipsterModuleMandatoryReplacements mandatoryReplacements;
Expand Down Expand Up @@ -267,7 +269,7 @@ public JHipsterModuleBuilder documentation(DocumentationTitle title, JHipsterSou
String target = "documentation/" + title.filename() + source.extension();
files().add(source, to(target));

String markdownLink = "- [" + title.get() + "](" + target + ") \n" + JHIPSTER_DOCUMENTATION_NEEDLE;
String markdownLink = "- [" + title.get() + "](" + target + ") " + LINE_BREAK + JHIPSTER_DOCUMENTATION_NEEDLE;
optionalReplacements().in("README.md").add(text(JHIPSTER_DOCUMENTATION_NEEDLE), markdownLink);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

abstract class JHipsterModuleReplacements {

public static final String LF = "\n";
private final Collection<FileReplacer> replacers;

protected JHipsterModuleReplacements(JHipsterModuleReplacementsBuilder<?, ?> builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tech.jhipster.lite.module.domain.replacement;

import static tech.jhipster.lite.module.domain.replacement.JHipsterModuleReplacements.*;

import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule;

public record JustLineAfter(ElementMatcher element) implements PositionalMatcher {
public JustLineAfter {
Expand All @@ -12,6 +11,7 @@ public record JustLineAfter(ElementMatcher element) implements PositionalMatcher
@Override
public String buildReplacement(String value) {
Assert.notBlank("value", value);
return element().searchMatcher().concat(LF).concat(value);

return element().searchMatcher().concat(JHipsterModule.LINE_BREAK).concat(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tech.jhipster.lite.module.domain.replacement;

import static tech.jhipster.lite.module.domain.replacement.JHipsterModuleReplacements.*;

import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule;

public record JustLineBefore(ElementMatcher element) implements PositionalMatcher {
public JustLineBefore {
Expand All @@ -12,6 +11,7 @@ public record JustLineBefore(ElementMatcher element) implements PositionalMatche
@Override
public String buildReplacement(String value) {
Assert.notBlank("value", value);
return value.concat(LF).concat(element().searchMatcher());

return value.concat(JHipsterModule.LINE_BREAK).concat(element().searchMatcher());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -27,9 +29,8 @@
class FileSystemPackageJsonHandler {

private static final String QUOTE = "\"";
private static final String BREAK = "\n";
private static final String LINE_END = ",";
private static final String LINE_SEPARATOR = LINE_END + BREAK;
private static final String LINE_SEPARATOR = LINE_END + LINE_BREAK;

private final NpmVersions npmVersions;

Expand Down Expand Up @@ -70,7 +71,7 @@ private Path getPackageJsonFile(JHipsterProjectFolder projectFolder) {
}

private String cleanupLineBreaks(Indentation indentation, String content) {
return content.replaceAll(",(\\s*|[\r\n]*)}", BREAK + indentation.spaces() + "}");
return content.replaceAll(",(\\s*|[\r\n]*)}", LINE_BREAK + indentation.spaces() + "}");
}

private String replaceScripts(Indentation indentation, Scripts scripts, String content) {
Expand Down Expand Up @@ -171,7 +172,7 @@ public String handle() {

private String appendEntries(Matcher blocMatcher) {
return blocMatcher.replaceFirst(match -> {
StringBuilder result = new StringBuilder().append(match.group(1)).append(BREAK);
StringBuilder result = new StringBuilder().append(match.group(1)).append(LINE_BREAK);

result.append(entriesBloc(indentation, entries));

Expand All @@ -188,9 +189,9 @@ private String appendNewBlock(String result) {
.append(blocName)
.append(QUOTE)
.append(": {")
.append(BREAK)
.append(LINE_BREAK)
.append(entriesBloc(indentation, entries))
.append(BREAK)
.append(LINE_BREAK)
.append(indentation.spaces())
.append("}")
.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static org.joox.JOOX.*;
import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -30,7 +31,7 @@
class MavenCommandHandler {

private static final Pattern SPACES_ONLY_LINE = Pattern.compile("^\\s+$", Pattern.MULTILINE);
private static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
private static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + LINE_BREAK;
private static final String COMMAND = "command";
private static final String GROUP_ID = "groupId";
private static final String ARTIFACT_ID = "artifactId";
Expand All @@ -47,8 +48,6 @@ class MavenCommandHandler {
VERSION,
ARTIFACT_ID,
};
private static final String BREAK = "\n";

private final Indentation indentation;
private final Path pomPath;
private final Match document;
Expand Down Expand Up @@ -84,13 +83,13 @@ public void handle(SetJavaDependencyVersion command) {
}

private void appendProperties(SetJavaDependencyVersion command) {
Match properties = $("properties").append(BREAK).append(indentation.spaces());
Match properties = $("properties").append(LINE_BREAK).append(indentation.spaces());

appendPropertyLine(properties, command);

findFirst(PROPERTIES_ANCHOR).after(properties);

document.find("project properties").before(BREAK).before(BREAK).before(indentation.spaces());
document.find("project properties").before(LINE_BREAK).before(LINE_BREAK).before(indentation.spaces());
}

private void appendPropertyLine(Match properties, SetJavaDependencyVersion command) {
Expand All @@ -102,7 +101,7 @@ private void appendPropertyLine(Match properties, SetJavaDependencyVersion comma
properties
.append(indentation.spaces())
.append($(command.property(), command.dependencyVersion()))
.append(BREAK)
.append(LINE_BREAK)
.append(indentation.spaces());
}
}
Expand Down Expand Up @@ -135,15 +134,15 @@ public void handle(AddJavaDependency command) {

private void appendDependencies(AddJavaDependency command) {
Match dependencies = $("dependencies")
.append(BREAK)
.append(LINE_BREAK)
.append(indentation.times(2))
.append(dependencyNode(command))
.append(BREAK)
.append(LINE_BREAK)
.append(indentation.spaces());

findFirst(DEPENDENCIES_ANCHOR).after(dependencies);

document.find("project dependencies").before(BREAK).before(BREAK).before(indentation.spaces());
document.find("project dependencies").before(LINE_BREAK).before(LINE_BREAK).before(indentation.spaces());
}

private void appendDependency(AddJavaDependency command, Match dependencies) {
Expand Down Expand Up @@ -175,7 +174,7 @@ private Consumer<Match> appendBeforeFirstTestDependency(AddJavaDependency comman
.stream()
.filter(dependencyMatch(command.dependencyId()))
.findFirst()
.ifPresent(node -> node.after(indentation.times(2)).after(BREAK).after(BREAK));
.ifPresent(node -> node.after(indentation.times(2)).after(LINE_BREAK).after(LINE_BREAK));
};
}

Expand All @@ -193,7 +192,12 @@ private Predicate<Match> testDependency() {
}

private void appendDependencyInLastPosition(AddJavaDependency command, Match dependencies) {
dependencies.append(BREAK).append(indentation.times(2)).append(dependencyNode(command)).append(BREAK).append(indentation.spaces());
dependencies
.append(LINE_BREAK)
.append(indentation.times(2))
.append(dependencyNode(command))
.append(LINE_BREAK)
.append(indentation.spaces());
}

private Match dependencyNode(AddJavaDependency command) {
Expand All @@ -203,36 +207,36 @@ private Match dependencyNode(AddJavaDependency command) {
appendScope(command, dependency);
appendOptional(command, dependency);

dependency.append(BREAK).append(indentation.times(2));
dependency.append(LINE_BREAK).append(indentation.times(2));

return dependency;
}

private Match buildDependencyNode(AddJavaDependency command) {
return $("dependency")
.append(BREAK)
.append(LINE_BREAK)
.append(indentation.times(3))
.append($(GROUP_ID, command.groupId().get()))
.append(BREAK)
.append(LINE_BREAK)
.append(indentation.times(3))
.append($(ARTIFACT_ID, command.artifactId().get()));
}

private void appendVersion(AddJavaDependency command, Match dependency) {
command
.version()
.ifPresent(version -> dependency.append(BREAK).append(indentation.times(3)).append($(VERSION, version.mavenVariable())));
.ifPresent(version -> dependency.append(LINE_BREAK).append(indentation.times(3)).append($(VERSION, version.mavenVariable())));
}

private void appendScope(AddJavaDependency command, Match dependency) {
if (command.scope() != JavaDependencyScope.COMPILE) {
dependency.append(BREAK).append(indentation.times(3)).append($("scope", Enums.map(command.scope(), MavenScope.class).key()));
dependency.append(LINE_BREAK).append(indentation.times(3)).append($("scope", Enums.map(command.scope(), MavenScope.class).key()));
}
}

private void appendOptional(AddJavaDependency command, Match dependency) {
if (command.optional()) {
dependency.append(BREAK).append(indentation.times(3)).append($("optional", "true"));
dependency.append(LINE_BREAK).append(indentation.times(3)).append($("optional", "true"));
}
}

Expand All @@ -242,7 +246,7 @@ private void writePom() {
writer.write(HEADER);

for (Element e : document) {
String element = JOOX.$(e).toString().replace("\r\n", "\n");
String element = JOOX.$(e).toString().replace("\r\n", LINE_BREAK);

element = SPACES_ONLY_LINE.matcher(element).replaceAll("");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -13,7 +15,6 @@

class PropertiesFileSpringPropertiesHandler {

private static final String BREAK = "\n";
private static final String EQUAL = "=";
private static final String COLLECTION_SEPARATOR = ",";

Expand Down Expand Up @@ -49,7 +50,7 @@ private String buildProperties(PropertyKey key, PropertyValue value) throws IOEx
int propertyIndex = currentProperties.indexOf(propertyId(key));
if (propertyIndex != -1) {
String start = currentProperties.substring(0, propertyIndex);
String end = currentProperties.substring(currentProperties.indexOf(BREAK, propertyIndex));
String end = currentProperties.substring(currentProperties.indexOf(LINE_BREAK, propertyIndex));

return start + propertyLine(key, value) + end;
}
Expand All @@ -65,7 +66,7 @@ private String readOrInitProperties() throws IOException {
return "";
}

return Files.readString(file) + BREAK;
return Files.readString(file) + LINE_BREAK;
}

private String propertyLine(PropertyKey key, PropertyValue value) {
Expand Down

0 comments on commit 3175c41

Please sign in to comment.