Skip to content

Commit

Permalink
Merge pull request #2532 from DamnClin/fix-regex-replacements
Browse files Browse the repository at this point in the history
Fix regex replacements
  • Loading branch information
pascalgrimaud authored Jul 13, 2022
2 parents 5ee82fa + d574b4a commit cd4527e
Show file tree
Hide file tree
Showing 33 changed files with 592 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static JHipsterModuleBuilder clientModuleBuilder(JHipsterModuleProperties
return moduleBuilder(properties)
.optionalReplacements()
.in("package.json")
.add(justLineBefore(text(CACHE_NEEDLE)), jestSonar(properties.indentation()))
.add(lineBeforeText(CACHE_NEEDLE), jestSonar(properties.indentation()))
.and()
.and();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public JHipsterModule buildPiniaModule(JHipsterModuleProperties properties) {
.and()
.mandatoryReplacements()
.in("src/main/webapp/app/main.ts")
.add(justLineBefore(text(IMPORT_NEEDLE)), piniaImports())
.add(justLineBefore(text(PROVIDER_NEEDLE)), piniaProvider())
.add(lineBeforeText(IMPORT_NEEDLE), piniaImports())
.add(lineBeforeText(PROVIDER_NEEDLE), piniaProvider())
.and()
.and()
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package tech.jhipster.lite.generator.server.javatool.arch.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.from;
import static tech.jhipster.lite.module.domain.JHipsterModule.javaDependency;
import static tech.jhipster.lite.module.domain.JHipsterModule.justLineBefore;
import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder;
import static tech.jhipster.lite.module.domain.JHipsterModule.text;
import static tech.jhipster.lite.module.domain.JHipsterModule.to;
import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcTestJava;
import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -43,7 +37,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.and()
.optionalReplacements()
.in("src/test/resources/logback.xml")
.add(justLineBefore(text("<!-- jhipster-needle-logback-add-log -->")), "<logger name=\"com.tngtech.archunit\" level=\"WARN\" />")
.add(lineBeforeText("<!-- jhipster-needle-logback-add-log -->"), "<logger name=\"com.tngtech.archunit\" level=\"WARN\" />")
.and()
.and()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.TextMatcher;
import tech.jhipster.lite.module.domain.replacement.TextReplacer;

public class OAuth2ModuleFactory {

Expand Down Expand Up @@ -150,7 +150,7 @@ private void appendSpringProperties(JHipsterModuleBuilder builder) {

private void appendIntegrationTestAnnotationUpdates(JHipsterModuleBuilder builder, JHipsterModuleProperties properties) {
String baseClass = properties.projectBaseName().capitalized() + "App.class";
TextMatcher importNeedle = text(SPRING_BOOT_IMPORT);
TextReplacer importNeedle = text(SPRING_BOOT_IMPORT);

String integrationtTestFile = "src/test/java/" + properties.basePackage().path() + "/IntegrationTest.java";

Expand Down
40 changes: 15 additions & 25 deletions src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModuleContext.JHipsterModuleContextBuilder;
Expand Down Expand Up @@ -42,17 +43,14 @@
import tech.jhipster.lite.module.domain.postaction.JHipsterModulePostActions.JHipsterModulePostActionsBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
import tech.jhipster.lite.module.domain.replacement.ElementMatcher;
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleMandatoryReplacements;
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleMandatoryReplacements.JHipsterModuleMandatoryReplacementsBuilder;
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleOptionalReplacements;
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleOptionalReplacements.JHipsterModuleOptionalReplacementsBuilder;
import tech.jhipster.lite.module.domain.replacement.JustAfter;
import tech.jhipster.lite.module.domain.replacement.JustBefore;
import tech.jhipster.lite.module.domain.replacement.JustLineAfter;
import tech.jhipster.lite.module.domain.replacement.JustLineBefore;
import tech.jhipster.lite.module.domain.replacement.RegexMatcher;
import tech.jhipster.lite.module.domain.replacement.TextMatcher;
import tech.jhipster.lite.module.domain.replacement.RegexNeedleBeforeReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexReplacer;
import tech.jhipster.lite.module.domain.replacement.TextNeedleBeforeReplacer;
import tech.jhipster.lite.module.domain.replacement.TextReplacer;

public class JHipsterModule {

Expand Down Expand Up @@ -161,28 +159,20 @@ public static VersionSlug versionSlug(String versionSlug) {
return new VersionSlug(versionSlug);
}

public static TextMatcher text(String text) {
return new TextMatcher(text);
public static TextReplacer text(String text) {
return new TextReplacer(text);
}

public static RegexMatcher regex(String regex) {
return new RegexMatcher(regex);
public static RegexReplacer regex(String regex) {
return new RegexReplacer(regex);
}

public static JustBefore justBefore(ElementMatcher matcher) {
return new JustBefore(matcher);
public static TextNeedleBeforeReplacer lineBeforeText(String needle) {
return new TextNeedleBeforeReplacer(needle);
}

public static JustAfter justAfter(ElementMatcher matcher) {
return new JustAfter(matcher);
}

public static JustLineBefore justLineBefore(ElementMatcher matcher) {
return new JustLineBefore(matcher);
}

public static JustLineAfter justLineAfter(ElementMatcher matcher) {
return new JustLineAfter(matcher);
public static RegexNeedleBeforeReplacer lineBeforeRegex(String regex) {
return new RegexNeedleBeforeReplacer(Pattern.compile(regex, Pattern.MULTILINE));
}

public static PropertyKey propertyKey(String key) {
Expand Down Expand Up @@ -299,15 +289,15 @@ public JHipsterModuleBuilder documentation(DocumentationTitle title, JHipsterSou
files().add(source, to(target));

String markdownLink = "- [" + title.get() + "](" + target + ")";
optionalReplacements().in(README).add(justLineBefore(text(JHIPSTER_DOCUMENTATION_NEEDLE)), markdownLink);
optionalReplacements().in(README).add(lineBeforeText(JHIPSTER_DOCUMENTATION_NEEDLE), markdownLink);

return this;
}

public JHipsterModuleBuilder readmeSection(String section) {
Assert.notBlank("section", section);

optionalReplacements().in(README).add(justLineBefore(text(JHIPSTER_README_SECTION_NEEDLE)), section);
optionalReplacements().in(README).add(lineBeforeText(JHIPSTER_README_SECTION_NEEDLE), section);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.jhipster.lite.module.domain.replacement;

public interface ContentReplacement {
String file();

String apply(String content);

void handleError(Throwable e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.function.BiFunction;

public interface ElementMatcher {
public interface ElementReplacer {
boolean notMatchIn(String content);

BiFunction<String, String, String> replacer();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package tech.jhipster.lite.module.domain.replacement;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;

public class JHipsterModuleMandatoryReplacements extends JHipsterModuleReplacements {

Expand Down Expand Up @@ -44,41 +39,37 @@ private JHipsterModuleFileMandatoryReplacementsBuilder(JHipsterModuleMandatoryRe
}

@Override
protected FileReplacer buildReplacer(String file, ElementMatcher toReplace, String replacement) {
protected ContentReplacement buildReplacer(String file, ElementReplacer toReplace, String replacement) {
return new MandatoryFileReplacer(file, new MandatoryReplacer(toReplace, replacement));
}
}

private static record MandatoryFileReplacer(String file, MandatoryReplacer replacement) implements FileReplacer {
private static record MandatoryFileReplacer(String file, MandatoryReplacer replacement) implements ContentReplacement {
public MandatoryFileReplacer {
Assert.notNull("file", file);
Assert.notNull("replacement", replacement);
}

@Override
public void apply(JHipsterProjectFolder folder) {
Path filePath = folder.filePath(file());

try {
String content = Files.readString(filePath);
String updatedContent = replacement().apply(filePath, content);
public String apply(String content) {
return replacement().apply(content);
}

Files.writeString(filePath, updatedContent, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
throw new ReplacementErrorException(filePath, e);
}
@Override
public void handleError(Throwable e) {
throw new MandatoryReplacementException(e);
}
}

private static record MandatoryReplacer(ElementMatcher currentValue, String updatedValue) {
private static record MandatoryReplacer(ElementReplacer currentValue, String updatedValue) {
public MandatoryReplacer {
Assert.notNull("currentValue", currentValue);
Assert.notNull("updatedValue", updatedValue);
}

public String apply(Path file, String content) {
public String apply(String content) {
if (currentValue().notMatchIn(content)) {
throw new UnknownCurrentValueException(file, content);
throw new UnknownCurrentValueException(currentValue().searchMatcher(), content);
}

return currentValue().replacer().apply(content, updatedValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package tech.jhipster.lite.module.domain.replacement;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;

public class JHipsterModuleOptionalReplacements extends JHipsterModuleReplacements {

Expand Down Expand Up @@ -46,35 +41,31 @@ private JHipsterModuleFileOptionalReplacementsBuilder(JHipsterModuleOptionalRepl
}

@Override
protected FileReplacer buildReplacer(String file, ElementMatcher toReplace, String replacement) {
protected ContentReplacement buildReplacer(String file, ElementReplacer toReplace, String replacement) {
return new OptionalFileReplacer(file, new OptionalReplacer(toReplace, replacement));
}
}

private static record OptionalFileReplacer(String file, OptionalReplacer replacement) implements FileReplacer {
private static final Logger log = LoggerFactory.getLogger(FileReplacer.class);
private static record OptionalFileReplacer(String file, OptionalReplacer replacement) implements ContentReplacement {
private static final Logger log = LoggerFactory.getLogger(OptionalFileReplacer.class);

public OptionalFileReplacer {
Assert.notNull("file", file);
Assert.notNull("replacement", replacement);
}

@Override
public void apply(JHipsterProjectFolder folder) {
Path filePath = folder.filePath(file());

try {
String content = Files.readString(filePath);
String updatedContent = replacement().apply(content);
public String apply(String content) {
return replacement().apply(content);
}

Files.writeString(filePath, updatedContent, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
log.debug("Can't replace content, no replacement done {}", e.getMessage(), e);
}
@Override
public void handleError(Throwable e) {
log.debug("Can't apply optional replacement: {}", e.getMessage());
}
}

private static record OptionalReplacer(ElementMatcher currentValue, String updatedValue) {
private static record OptionalReplacer(ElementReplacer currentValue, String updatedValue) {
public OptionalReplacer {
Assert.notNull("currentValue", currentValue);
Assert.notNull("updatedValue", updatedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

import java.util.ArrayList;
import java.util.Collection;
import tech.jhipster.lite.common.domain.JHipsterCollections;
import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;

abstract class JHipsterModuleReplacements {
public abstract class JHipsterModuleReplacements {

private final Collection<FileReplacer> replacers;
private final Collection<ContentReplacement> replacements;

protected JHipsterModuleReplacements(JHipsterModuleReplacementsBuilder<?, ?> builder) {
Assert.notNull("builder", builder);
Assert.notNull("builder", builder.replacers);
Assert.notNull("replacements", builder.replacements);

replacers = builder.replacers;
replacements = JHipsterCollections.immutable(builder.replacements);
}

public void apply(JHipsterProjectFolder folder) {
Assert.notNull("folder", folder);

replacers.forEach(replacement -> replacement.apply(folder));
public Collection<ContentReplacement> replacements() {
return replacements;
}

public abstract static class JHipsterModuleReplacementsBuilder<
Replacements extends JHipsterModuleReplacements, FileReplacementsBuilder extends JHipsterModuleFileReplacementsBuilder<?, ?>
> {

private final JHipsterModuleBuilder module;
private final Collection<FileReplacer> replacers = new ArrayList<>();
private final Collection<ContentReplacement> replacements = new ArrayList<>();

protected JHipsterModuleReplacementsBuilder(JHipsterModuleBuilder module) {
Assert.notNull("module", module);
Expand All @@ -40,10 +38,10 @@ public JHipsterModuleBuilder and() {
return module;
}

void add(FileReplacer fileReplacer) {
void add(ContentReplacement fileReplacer) {
Assert.notNull("fileReplacer", fileReplacer);

replacers.add(fileReplacer);
replacements.add(fileReplacer);
}

public abstract FileReplacementsBuilder in(String file);
Expand All @@ -67,22 +65,14 @@ protected JHipsterModuleFileReplacementsBuilder(ReplacementsBuilder replacements
this.file = file;
}

public Builder add(ElementMatcher elementToReplace, String replacement) {
public Builder add(ElementReplacer elementToReplace, String replacement) {
Assert.notNull("elementToReplace", elementToReplace);

replacements.add(buildReplacer(file, elementToReplace, replacement));

return self();
}

public Builder add(PositionalMatcher positional, String replacement) {
Assert.notNull("PositionalMatcher", positional);

replacements.add(buildReplacer(file, positional.element(), positional.buildReplacement(replacement)));

return self();
}

@SuppressWarnings("unchecked")
private Builder self() {
return (Builder) this;
Expand All @@ -92,6 +82,6 @@ public ReplacementsBuilder and() {
return replacements;
}

protected abstract FileReplacer buildReplacer(String file, ElementMatcher toReplace, String replacement);
protected abstract ContentReplacement buildReplacer(String file, ElementReplacer toReplace, String replacement);
}
}
Loading

0 comments on commit cd4527e

Please sign in to comment.