Skip to content

Commit

Permalink
feat(module-builder): add a new API that allows to directly add a {Ma…
Browse files Browse the repository at this point in the history
…ndatory|Optional} replacer to the replacements

This allows reusing replacers, and improves readability and maintainability of the code
  • Loading branch information
murdos committed Sep 14, 2024
1 parent bc8e287 commit e3c05a0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.MandatoryReplacer;
import tech.jhipster.lite.module.domain.replacement.TextReplacer;
import tech.jhipster.lite.shared.error.domain.Assert;

Expand Down Expand Up @@ -108,16 +109,17 @@ private Consumer<JHipsterModuleBuilder> patchTsConfig(JHipsterModuleProperties p
.mandatoryReplacements()
.in(path("tsconfig.json"))
.add(text("@tsconfig/recommended/tsconfig.json"), "@vue/tsconfig/tsconfig.dom.json")
.add(lineAfterRegex("\"compilerOptions\":"), compilerOption("sourceMap", true, properties.indentation()))
.add(lineAfterRegex("\"compilerOptions\":"), compilerOption("allowJs", true, properties.indentation()))
.add(tsConfigCompilerOption("sourceMap", true, properties.indentation()))
.add(tsConfigCompilerOption("allowJs", true, properties.indentation()))
.add(new TextReplacer(notContainingReplacement(), "\"types\": ["), "\"types\": [\"vite/client\", ")
.and()
.and();
//@formatter:on
}

private static String compilerOption(String optionName, boolean optionValue, Indentation indentation) {
return indentation.times(2) + "\"%s\": %s,".formatted(optionName, optionValue);
private static MandatoryReplacer tsConfigCompilerOption(String optionName, boolean optionValue, Indentation indentation) {
String compilerOption = indentation.times(2) + "\"%s\": %s,".formatted(optionName, optionValue);
return new MandatoryReplacer(lineAfterRegex("\"compilerOptions\":"), compilerOption);
}

private Consumer<JHipsterModuleBuilder> patchVitestConfig(JHipsterModuleProperties properties) {
Expand All @@ -128,16 +130,17 @@ private Consumer<JHipsterModuleBuilder> patchVitestConfig(JHipsterModuleProperti
.add(lineAfterRegex("from 'vitest/config';"), "import vue from '@vitejs/plugin-vue';")
.add(new TextReplacer(notContainingReplacement(), "plugins: ["), "plugins: [vue(), ")
.add(text("environment: 'node',"), "environment: 'jsdom',")
.add(lineAfterRegex("configDefaults.coverage.exclude"), vitestCoverageExclusion(properties.indentation(),"src/main/webapp/**/*.component.ts"))
.add(lineAfterRegex("configDefaults.coverage.exclude"), vitestCoverageExclusion(properties.indentation(),"src/main/webapp/app/router.ts"))
.add(lineAfterRegex("configDefaults.coverage.exclude"), vitestCoverageExclusion(properties.indentation(),"src/main/webapp/app/injections.ts"))
.add(lineAfterRegex("configDefaults.coverage.exclude"), vitestCoverageExclusion(properties.indentation(),"src/main/webapp/app/main.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/**/*.component.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/app/router.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/app/injections.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/app/main.ts"))
.and();
//@formatter:on
}

private static String vitestCoverageExclusion(Indentation indentation, String filePattern) {
return indentation.times(4) + "'" + filePattern + "',";
private static MandatoryReplacer vitestCoverageExclusion(JHipsterModuleProperties properties, String filePattern) {
Indentation indentation = properties.indentation();
return new MandatoryReplacer(lineAfterRegex("configDefaults.coverage.exclude"), indentation.times(4) + "'" + filePattern + "',");
}

public JHipsterModule buildPiniaModule(JHipsterModuleProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ void springTestLogger(String name, LogLevel level) {
Assert.notBlank("name", name);
Assert.notNull("level", level);

builder.optionalReplacements().in(SPRING_TEST_LOG_FILE).add(JHIPSTER_LOGGER_NEEDLE, logger(name, level));
builder.optionalReplacements().in(SPRING_TEST_LOG_FILE).add(logConfigurationEntry(name, level));
}

void springMainLogger(String name, LogLevel level) {
Assert.notBlank("name", name);
Assert.notNull("level", level);

builder.optionalReplacements().in(SPRING_MAIN_LOG_FILE).add(JHIPSTER_LOGGER_NEEDLE, logger(name, level));
builder.optionalReplacements().in(SPRING_MAIN_LOG_FILE).add(logConfigurationEntry(name, level));
}

private OptionalReplacer logConfigurationEntry(String name, LogLevel level) {
return new OptionalReplacer(JHIPSTER_LOGGER_NEEDLE, logger(name, level));
}

private String logger(String name, LogLevel level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ private JHipsterModuleFileMandatoryReplacementsFactoryBuilder(
super(replacements, file);
}

public JHipsterModuleFileMandatoryReplacementsFactoryBuilder add(MandatoryReplacer mandatoryReplacer) {
replacements().add(buildReplacer(file(), mandatoryReplacer.replacer(), mandatoryReplacer.updatedValue()));

return this;
}

@Override
protected ContentReplacer buildReplacer(JHipsterProjectFilePath file, ElementReplacer toReplace, String replacement) {
return new MandatoryFileReplacer(file, new MandatoryReplacer(toReplace, replacement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ private JHipsterModuleFileOptionalReplacementsFactoryBuilder(
super(replacements, file);
}

public JHipsterModuleFileOptionalReplacementsFactoryBuilder add(OptionalReplacer mandatoryReplacer) {
replacements().add(buildReplacer(file(), mandatoryReplacer.replacer(), mandatoryReplacer.updatedValue()));

return this;
}

@Override
protected ContentReplacer buildReplacer(JHipsterProjectFilePath file, ElementReplacer toReplace, String replacement) {
return new OptionalFileReplacer(file, new OptionalReplacer(toReplace, replacement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public Builder add(ElementReplacer elementToReplace, String replacement) {
return self();
}

protected ReplacementsBuilder replacements() {
return replacements;
}

protected JHipsterProjectFilePath file() {
return file;
}

@SuppressWarnings("unchecked")
private Builder self() {
return (Builder) this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.jhipster.lite.module.domain.replacement;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import tech.jhipster.lite.shared.error.domain.Assert;

public record MandatoryReplacer(ElementReplacer replacer, String updatedValue) {
Expand All @@ -9,6 +11,9 @@ public record MandatoryReplacer(ElementReplacer replacer, String updatedValue) {
}

public String apply(String content) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.modules(new JavaTimeModule());

if (replacer.dontNeedReplacement(content, updatedValue())) {
return content;
}
Expand Down

0 comments on commit e3c05a0

Please sign in to comment.