Skip to content

Commit

Permalink
Gradle: Fix dependency added twice + indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
murdos committed Aug 3, 2023
1 parent 4f375e7 commit a305d84
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.NotImplementedException;
import com.electronwill.nightconfig.core.Config;
Expand All @@ -26,7 +29,7 @@
import tech.jhipster.lite.module.domain.replacement.ContentReplacers;
import tech.jhipster.lite.module.domain.replacement.MandatoryFileReplacer;
import tech.jhipster.lite.module.domain.replacement.MandatoryReplacer;
import tech.jhipster.lite.module.domain.replacement.TextNeedleBeforeReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexNeedleBeforeReplacer;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemReplacer;
import tech.jhipster.lite.module.infrastructure.secondary.javadependency.JavaDependenciesCommandHandler;

Expand All @@ -38,9 +41,10 @@ public class GradleCommandHandler implements JavaDependenciesCommandHandler {
private static final String LIBRARIES_TOML_KEY = "libraries";
private static final String BUILD_GRADLE_FILE = "build.gradle.kts";

private static final String GRADLE_DEPENDENCY_NEEDLE = "// jhipster-needle-gradle-add-dependency";
private static final String GRADLE_TEST_DEPENDENCY_NEEDLE = "// jhipster-needle-gradle-add-dependency-test";
private static final Pattern GRADLE_DEPENDENCY_NEEDLE = Pattern.compile("^\\s+// jhipster-needle-gradle-add-dependency$", Pattern.MULTILINE);
private static final Pattern GRADLE_TEST_DEPENDENCY_NEEDLE = Pattern.compile("^\\s+// jhipster-needle-gradle-add-dependency-test$", Pattern.MULTILINE);

private final Indentation indentation;
private final JHipsterProjectFolder projectFolder;
private final FileConfig versionsCatalog;
private final FileSystemReplacer fileReplacer = new FileSystemReplacer();
Expand All @@ -49,6 +53,7 @@ public GradleCommandHandler(Indentation indentation, JHipsterProjectFolder proje
Assert.notNull("indentation", indentation);
Assert.notNull("projectFolder", projectFolder);

this.indentation = indentation;
this.projectFolder = projectFolder;
Path tomlVersionCatalogFile = tomlVersionCatalogPath();
versionsCatalog = FileConfig.builder(tomlVersionCatalogFile)
Expand Down Expand Up @@ -98,11 +103,11 @@ private void addJavaDependencyToBuildGradle(JavaDependency dependency) {
};

MandatoryReplacer replacer = new MandatoryReplacer(
new TextNeedleBeforeReplacer(
new RegexNeedleBeforeReplacer(
(contentBeforeReplacement, newText) -> !contentBeforeReplacement.contains(newText),
gradleScope == GradleDependencyScope.TEST_IMPLEMENTATION ? GRADLE_TEST_DEPENDENCY_NEEDLE : GRADLE_DEPENDENCY_NEEDLE
),
"%s(libs.%s)".formatted(gradleScope.command(), dependencySlug(dependency).replace("-", "."))
"%s%s(libs.%s)".formatted(indentation.times(1), gradleScope.command(), dependencySlug(dependency).replace("-", "."))
);
fileReplacer.handle(projectFolder, ContentReplacers.of(new MandatoryFileReplacer(new JHipsterProjectFilePath(BUILD_GRADLE_FILE), replacer)));
}
Expand All @@ -113,7 +118,7 @@ private void addJavaDependencyToVersionCatalog(JavaDependency dependency) {
libraryConfig.set("name", dependency.id().artifactId().get());
dependency.version().ifPresent(versionSlug -> libraryConfig.set("version.ref", versionSlug.slug()));
String libraryEntryKey = dependencySlug(dependency);
versionsCatalog.set(LIBRARIES_TOML_KEY + "." + libraryEntryKey, libraryConfig);
versionsCatalog.set(List.of(LIBRARIES_TOML_KEY, libraryEntryKey), libraryConfig);
versionsCatalog.save();
}

Expand Down

0 comments on commit a305d84

Please sign in to comment.