From 223c0516e3d1348a2c2db103d368244b626a42fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Thu, 19 Sep 2024 10:10:10 +0200 Subject: [PATCH] feat(generator): add helper methods in ReplacementCondition to simplify writing replacements --- .../jwt/domain/AngularJwtModuleFactory.java | 6 ++--- .../domain/AngularOauth2ModuleFactory.java | 18 +++++++-------- .../domain/SpringBootCoreModuleFactory.java | 7 +++--- .../ThymeleafTemplateModuleFactory.java | 6 ++--- .../lite/module/domain/JHipsterModule.java | 10 ++++++++- .../domain/JHipsterModuleShortcuts.java | 12 +++------- .../replacement/ReplacementCondition.java | 10 +++++++++ .../gradle/GradleCommandHandler.java | 22 ++++++------------- 8 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/security/jwt/domain/AngularJwtModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/angular/security/jwt/domain/AngularJwtModuleFactory.java index 2d59d6b1376..169f93ec091 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/security/jwt/domain/AngularJwtModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/security/jwt/domain/AngularJwtModuleFactory.java @@ -1,6 +1,7 @@ package tech.jhipster.lite.generator.client.angular.security.jwt.domain; import static tech.jhipster.lite.module.domain.JHipsterModule.*; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex; import java.util.regex.Pattern; import tech.jhipster.lite.module.domain.JHipsterModule; @@ -8,15 +9,14 @@ import tech.jhipster.lite.module.domain.file.JHipsterSource; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; import tech.jhipster.lite.module.domain.replacement.ElementReplacer; -import tech.jhipster.lite.module.domain.replacement.RegexReplacer; import tech.jhipster.lite.module.domain.replacement.TextNeedleBeforeReplacer; import tech.jhipster.lite.shared.error.domain.Assert; public class AngularJwtModuleFactory { private static final Pattern PROVIDE_HTTP_CLIENT_PATTERN = Pattern.compile("provideHttpClient\\(\\),"); - private static final ElementReplacer EXISTING_PROVIDE_HTTP_CLIENT_NEEDLE = new RegexReplacer( - (contentBeforeReplacement, replacement) -> PROVIDE_HTTP_CLIENT_PATTERN.matcher(contentBeforeReplacement).find(), + private static final ElementReplacer EXISTING_PROVIDE_HTTP_CLIENT_NEEDLE = regex( + notMatchingRegex(PROVIDE_HTTP_CLIENT_PATTERN), PROVIDE_HTTP_CLIENT_PATTERN ); diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java index bdb1f695a98..d1fdd830bd1 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java @@ -2,6 +2,7 @@ import static tech.jhipster.lite.module.domain.JHipsterModule.*; import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.ANGULAR; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex; import java.util.regex.Pattern; import tech.jhipster.lite.module.domain.Indentation; @@ -10,14 +11,13 @@ import tech.jhipster.lite.module.domain.file.JHipsterSource; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; import tech.jhipster.lite.module.domain.replacement.ElementReplacer; -import tech.jhipster.lite.module.domain.replacement.RegexReplacer; import tech.jhipster.lite.shared.error.domain.Assert; public class AngularOauth2ModuleFactory { private static final Pattern PROVIDE_HTTP_CLIENT = Pattern.compile("provideHttpClient\\(\\),"); - private static final ElementReplacer EXISTING_PROVIDE_HTTP_CLIENT_NEEDLE = new RegexReplacer( - (contentBeforeReplacement, replacement) -> PROVIDE_HTTP_CLIENT.matcher(contentBeforeReplacement).find(), + private static final ElementReplacer EXISTING_PROVIDE_HTTP_CLIENT_NEEDLE = regex( + notMatchingRegex(PROVIDE_HTTP_CLIENT), PROVIDE_HTTP_CLIENT ); @@ -32,22 +32,22 @@ public class AngularOauth2ModuleFactory { """; private static final Pattern EMPTY_ALLOWED_COMMON_DEPENDENCIES_PATTERN = Pattern.compile("(\"allowedCommonJsDependencies\": *\\[\\s*)]"); - private static final ElementReplacer EMPTY_ALLOWED_COMMON_DEPENDENCIES_NEEDLE = new RegexReplacer( - (contentBeforeReplacement, replacement) -> EMPTY_ALLOWED_COMMON_DEPENDENCIES_PATTERN.matcher(contentBeforeReplacement).find(), + private static final ElementReplacer EMPTY_ALLOWED_COMMON_DEPENDENCIES_NEEDLE = regex( + notMatchingRegex(EMPTY_ALLOWED_COMMON_DEPENDENCIES_PATTERN), EMPTY_ALLOWED_COMMON_DEPENDENCIES_PATTERN ); private static final Pattern FILLED_ALLOWED_COMMON_DEPENDENCIES_PATTERN = Pattern.compile( "(\"allowedCommonJsDependencies\": *\\[[^]]+)]" ); - private static final ElementReplacer FILLED_ALLOWED_COMMON_DEPENDENCIES_NEEDLE = new RegexReplacer( - (contentBeforeReplacement, replacement) -> FILLED_ALLOWED_COMMON_DEPENDENCIES_PATTERN.matcher(contentBeforeReplacement).find(), + private static final ElementReplacer FILLED_ALLOWED_COMMON_DEPENDENCIES_NEEDLE = regex( + notMatchingRegex(FILLED_ALLOWED_COMMON_DEPENDENCIES_PATTERN), FILLED_ALLOWED_COMMON_DEPENDENCIES_PATTERN ); private static final Pattern FILLED_STANDALONE_PATTERN = Pattern.compile("(imports: *\\[[^]]+)]"); - private static final ElementReplacer FILLED_STANDALONE_NEEDLE = new RegexReplacer( - (contentBeforeReplacement, replacement) -> FILLED_STANDALONE_PATTERN.matcher(contentBeforeReplacement).find(), + private static final ElementReplacer FILLED_STANDALONE_NEEDLE = regex( + notMatchingRegex(FILLED_STANDALONE_PATTERN), FILLED_STANDALONE_PATTERN ); diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java index 4a6d1e1ef47..338f33763b1 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/core/domain/SpringBootCoreModuleFactory.java @@ -1,6 +1,7 @@ package tech.jhipster.lite.generator.server.springboot.core.domain; import static tech.jhipster.lite.module.domain.JHipsterModule.*; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notContaining; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.file.JHipsterDestination; @@ -8,9 +9,7 @@ import tech.jhipster.lite.module.domain.gradleplugin.GradleMainBuildPlugin; import tech.jhipster.lite.module.domain.javabuild.GroupId; import tech.jhipster.lite.module.domain.javabuild.VersionSlug; -import tech.jhipster.lite.module.domain.javadependency.JavaDependency; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyType; +import tech.jhipster.lite.module.domain.javadependency.*; import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; import tech.jhipster.lite.module.domain.replacement.TextNeedleBeforeReplacer; @@ -29,7 +28,7 @@ public class SpringBootCoreModuleFactory { private static final VersionSlug SPRING_BOOT_VERSION_SLUG = versionSlug("spring-boot"); private static final TextNeedleBeforeReplacer DEFAULT_GOAL_REPLACER = new TextNeedleBeforeReplacer( - (contentBeforeReplacement, replacement) -> !contentBeforeReplacement.contains(""), + notContaining(""), "" ); diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/thymeleaf/template/domain/ThymeleafTemplateModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/thymeleaf/template/domain/ThymeleafTemplateModuleFactory.java index cc40e203578..0b71139237d 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/thymeleaf/template/domain/ThymeleafTemplateModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/thymeleaf/template/domain/ThymeleafTemplateModuleFactory.java @@ -2,6 +2,7 @@ import static tech.jhipster.lite.module.domain.JHipsterModule.*; import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.COMMON; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex; import java.util.regex.Pattern; import tech.jhipster.lite.module.domain.JHipsterModule; @@ -10,7 +11,6 @@ import tech.jhipster.lite.module.domain.file.JHipsterSource; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; import tech.jhipster.lite.module.domain.replacement.ElementReplacer; -import tech.jhipster.lite.module.domain.replacement.RegexReplacer; import tech.jhipster.lite.shared.error.domain.Assert; public class ThymeleafTemplateModuleFactory { @@ -38,8 +38,8 @@ public class ThymeleafTemplateModuleFactory { private static final Pattern WELCOME_THYMELEAF_MESSAGE_PATTERN = Pattern.compile( "
Welcome to your Spring Boot with Thymeleaf project!
" ); - private static final ElementReplacer EXISTING_WELCOME_THYMELEAF_MESSAGE_NEEDLE = new RegexReplacer( - (contentBeforeReplacement, replacement) -> WELCOME_THYMELEAF_MESSAGE_PATTERN.matcher(contentBeforeReplacement).find(), + private static final ElementReplacer EXISTING_WELCOME_THYMELEAF_MESSAGE_NEEDLE = regex( + notMatchingRegex(WELCOME_THYMELEAF_MESSAGE_PATTERN), WELCOME_THYMELEAF_MESSAGE_PATTERN ); diff --git a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java index 7aec98a6e1c..868add69bfb 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java +++ b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java @@ -253,7 +253,15 @@ public static TextReplacer text(String text) { } public static RegexReplacer regex(String regex) { - return new RegexReplacer(notContainingReplacement(), regex); + return regex(notContainingReplacement(), regex); + } + + public static RegexReplacer regex(ReplacementCondition condition, String regex) { + return new RegexReplacer(condition, regex); + } + + public static RegexReplacer regex(ReplacementCondition condition, Pattern pattern) { + return new RegexReplacer(condition, pattern); } public static FileStartReplacer fileStart() { diff --git a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModuleShortcuts.java b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModuleShortcuts.java index 6fc7f3416ba..6b99bb3d231 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModuleShortcuts.java +++ b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModuleShortcuts.java @@ -1,6 +1,7 @@ package tech.jhipster.lite.module.domain; import static tech.jhipster.lite.module.domain.JHipsterModule.*; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex; import java.util.regex.Pattern; import tech.jhipster.lite.module.domain.file.JHipsterSource; @@ -24,15 +25,8 @@ final class JHipsterModuleShortcuts { private static final Pattern MODULE_EXPORT = Pattern.compile("module.exports = \\{"); private static final Pattern DEFAULT_ES_LINT = Pattern.compile("\\s*'\\*': \\[], //default configuration, replace with your own"); - private static final ElementReplacer EXISTING_ESLINT_CONFIGURATION = new RegexReplacer( - (contentBeforeReplacement, replacement) -> MODULE_EXPORT.matcher(contentBeforeReplacement).find(), - MODULE_EXPORT - ); - - private static final ElementReplacer DEFAULT_ES_LINT_CONFIGURATION = new RegexReplacer( - (contentBeforeReplacement, replacement) -> DEFAULT_ES_LINT.matcher(contentBeforeReplacement).find(), - DEFAULT_ES_LINT - ); + private static final ElementReplacer EXISTING_ESLINT_CONFIGURATION = regex(notMatchingRegex(MODULE_EXPORT), MODULE_EXPORT); + private static final ElementReplacer DEFAULT_ES_LINT_CONFIGURATION = regex(notMatchingRegex(DEFAULT_ES_LINT), DEFAULT_ES_LINT); private final JHipsterModuleBuilder builder; diff --git a/src/main/java/tech/jhipster/lite/module/domain/replacement/ReplacementCondition.java b/src/main/java/tech/jhipster/lite/module/domain/replacement/ReplacementCondition.java index e8827523fa4..b73f2d104d3 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/replacement/ReplacementCondition.java +++ b/src/main/java/tech/jhipster/lite/module/domain/replacement/ReplacementCondition.java @@ -1,5 +1,7 @@ package tech.jhipster.lite.module.domain.replacement; +import java.util.regex.Pattern; + public interface ReplacementCondition { boolean needReplacement(String contentBeforeReplacement, String replacement); @@ -10,4 +12,12 @@ static ReplacementCondition always() { static ReplacementCondition notContainingReplacement() { return (contentBeforeReplacement, replacement) -> !contentBeforeReplacement.contains(replacement); } + + static ReplacementCondition notContaining(String text) { + return (contentBeforeReplacement, replacement) -> !contentBeforeReplacement.contains(text); + } + + static ReplacementCondition notMatchingRegex(Pattern pattern) { + return (contentBeforeReplacement, replacement) -> pattern.matcher(contentBeforeReplacement).find(); + } } diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java index 01c1acbe863..a4872425f37 100644 --- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java +++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java @@ -1,23 +1,21 @@ package tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle; import static tech.jhipster.lite.module.domain.JHipsterModule.*; -import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.*; -import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.*; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always; +import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex; +import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.libraryAlias; +import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.pluginAlias; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; -import tech.jhipster.lite.module.domain.Indentation; -import tech.jhipster.lite.module.domain.JHipsterModuleContext; -import tech.jhipster.lite.module.domain.JHipsterProjectFilePath; +import tech.jhipster.lite.module.domain.*; import tech.jhipster.lite.module.domain.buildproperties.BuildProperty; import tech.jhipster.lite.module.domain.buildproperties.PropertyKey; import tech.jhipster.lite.module.domain.file.*; @@ -334,13 +332,7 @@ private MandatoryReplacer existingPropertyReplacer(BuildProperty property) { "val %s by extra\\(\"(.*?)\"\\)".formatted(toCamelCasedKotlinVariable(property.key())), Pattern.MULTILINE ); - return new MandatoryReplacer( - new RegexReplacer( - (contentBeforeReplacement, replacement) -> propertyLinePattern.matcher(contentBeforeReplacement).find(), - propertyLinePattern - ), - convertToKotlinFormat(property) - ); + return new MandatoryReplacer(regex(notMatchingRegex(propertyLinePattern), propertyLinePattern), convertToKotlinFormat(property)); } private static MandatoryReplacer addNewPropertyReplacer(BuildProperty property) {