tech.jhipster.lite.module.domain.JHipsterModuleShortcuts Maven / Gradle / Ivy
package tech.jhipster.lite.module.domain;
import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;
import java.util.regex.Pattern;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.replacement.*;
import tech.jhipster.lite.shared.error.domain.Assert;
final class JHipsterModuleShortcuts {
private static final JHipsterProjectFilePath README = path("README.md");
private static final TextNeedleBeforeReplacer JHIPSTER_DOCUMENTATION_NEEDLE = lineBeforeText("\n");
private static final TextNeedleBeforeReplacer JHIPSTER_LOCAL_ENVIRONMENT_NEEDLE = lineBeforeText(
"\n"
);
private static final TextNeedleAfterReplacer JHIPSTER_PREREQUISITES = lineAfterText("\n## Prerequisites");
private static final JHipsterProjectFilePath SPRING_MAIN_LOG_FILE = path("src/main/resources/logback-spring.xml");
private static final JHipsterProjectFilePath SPRING_TEST_LOG_FILE = path("src/test/resources/logback.xml");
private static final TextNeedleBeforeReplacer JHIPSTER_LOGGER_NEEDLE = lineBeforeText("");
private static final Pattern DEFAULT_LINTSTAGED_CONFIGURATION_ENTRY = Pattern.compile("\\s*'\\*': \\[\\s*].*");
private final JHipsterModuleBuilder builder;
JHipsterModuleShortcuts(JHipsterModuleBuilder builder) {
Assert.notNull("builder", builder);
this.builder = builder;
}
void documentation(DocumentationTitle title, JHipsterSource source) {
Assert.notNull("title", title);
Assert.notNull("source", source);
String target = "documentation/" + title.filename() + source.extension();
builder.files().add(source, to(target));
String markdownLink = "- [" + title.get() + "](" + target + ")";
builder.optionalReplacements().in(README).add(JHIPSTER_DOCUMENTATION_NEEDLE, markdownLink);
}
void localEnvironment(LocalEnvironment localEnvironment) {
Assert.notNull("localEnvironment", localEnvironment);
builder.optionalReplacements().in(README).add(JHIPSTER_LOCAL_ENVIRONMENT_NEEDLE, localEnvironment.get());
}
void prerequisites(String prerequisites) {
Assert.notBlank("prerequisites", prerequisites);
builder.optionalReplacements().in(README).add(JHIPSTER_PREREQUISITES, prerequisites);
}
void springTestLogger(String name, LogLevel level) {
Assert.notBlank("name", name);
Assert.notNull("level", 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(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) {
return new StringBuilder()
.append(builder.indentation().spaces())
.append(" ")
.toString();
}
public void integrationTestExtension(String extensionClass) {
Assert.notBlank("extensionClass", extensionClass);
builder
.mandatoryReplacements()
.in(path("src/test/java").append(builder.packagePath()).append("IntegrationTest.java"))
.add(
lineBeforeText("import org.springframework.boot.test.context.SpringBootTest;"),
"import org.junit.jupiter.api.extension.ExtendWith;"
)
.add(lineBeforeText("public @interface"), "@ExtendWith(" + extensionClass + ".class)");
}
public void preCommitActions(StagedFilesFilter stagedFilesFilter, PreCommitCommands preCommitCommands) {
Assert.notNull("stagedFilesFilter", stagedFilesFilter);
Assert.notNull("preCommitCommands", preCommitCommands);
String newLintStagedConfigurationEntry =
"%s'%s': %s,".formatted(builder.properties().indentation().times(1), stagedFilesFilter, preCommitCommands);
builder
.optionalReplacements()
.in(path(".lintstagedrc.cjs"))
.add(regex(always(), DEFAULT_LINTSTAGED_CONFIGURATION_ENTRY), "")
.add(lineAfterRegex("module.exports = \\{"), newLintStagedConfigurationEntry);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy