Skip to content

Commit

Permalink
support GradleCorePlugin in a build profile
Browse files Browse the repository at this point in the history
  • Loading branch information
renanfranca committed Apr 5, 2024
1 parent 3d631e3 commit f94ec3a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
import tech.jhipster.lite.shared.error.domain.Assert;

public final class GradleCorePlugin implements GradleMainBuildPlugin {
public final class GradleCorePlugin implements GradleMainBuildPlugin, GradleProfilePlugin {

private final GradlePluginId id;
private final Optional<GradlePluginConfiguration> configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package tech.jhipster.lite.module.domain.gradleplugin;

public sealed interface GradleProfilePlugin extends GradlePlugin permits GradleCommunityProfilePlugin {}
public sealed interface GradleProfilePlugin extends GradlePlugin permits GradleCorePlugin, GradleCommunityProfilePlugin {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private Function<GradleProfilePlugin, JavaBuildCommand> toCommands(
) {
return plugin ->
switch (plugin) {
case GradleCorePlugin gradleCorePlugin -> mapCorePlugin(gradleCorePlugin, versions, buildProfile);
case GradleCommunityProfilePlugin gradleCommunityProfilePlugin -> mapCommunityProfilePlugin(
gradleCommunityProfilePlugin,
versions,
Expand All @@ -48,6 +49,17 @@ private Function<GradleProfilePlugin, JavaBuildCommand> toCommands(
};
}

private JavaBuildCommand mapCorePlugin(
GradleCorePlugin plugin,
JavaDependenciesVersions versions,
Optional<BuildProfileId> buildProfile
) {
AddGradlePluginOptionalBuilder commandBuilder = AddGradlePlugin.builder().plugin(plugin);
buildProfile.ifPresent(commandBuilder::buildProfile);
plugin.toolVersionSlug().map(versions::get).ifPresent(commandBuilder::toolVersion);
return commandBuilder.build();
}

private JavaBuildCommand mapCommunityProfilePlugin(
GradleCommunityProfilePlugin gradleCommunityProfilePlugin,
JavaDependenciesVersions versions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public static JHipsterModule fullModule() {
.plugin(mavenEnforcerPlugin())
.and()
.gradleProfilePlugins()
.plugin(gitPropertiesGradlePluginDependency())
.plugin(checkstyleGradleProfilePlugin())
.plugin(gitPropertiesGradleProfilePlugin())
.and()
.javaDependencies()
.addTestDependency(groupId("org.cassandraunit"), artifactId("cassandra-unit"), versionSlug("cassandraunit"))
Expand Down Expand Up @@ -407,7 +408,21 @@ public static GradleMainBuildPlugin checkstyleGradlePlugin() {
.build();
}

public static GradleProfilePlugin gitPropertiesGradlePluginDependency() {
public static GradleProfilePlugin checkstyleGradleProfilePlugin() {
return gradleCorePlugin()
.id("checkstyle")
.toolVersionSlug("checkstyle")
.configuration(
"""
checkstyle {
toolVersion = libs.versions.checkstyle.get()
}
"""
)
.build();
}

public static GradleProfilePlugin gitPropertiesGradleProfilePlugin() {
return gradleProfilePlugin()
.id("com.gorylenko.gradle-git-properties")
.dependency(groupId("com.gorylenko.gradle-git-properties"), artifactId("gradle-git-properties"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,19 @@ val springProfilesActive by extra("local")
"""
plugins {
java
checkstyle
id("com.gorylenko.gradle-git-properties")
// jhipster-needle-gradle-plugins
}
"""
)
.containing(
"""
checkstyle {
toolVersion = libs.versions.checkstyle.get()
}
"""
)
.containing(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
import tech.jhipster.lite.module.domain.buildproperties.PropertyValue;
import tech.jhipster.lite.module.domain.gradleplugin.GradlePlugin;
import tech.jhipster.lite.module.domain.gradleplugin.GradleProfilePlugin;
import tech.jhipster.lite.module.domain.javabuild.command.*;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
Expand Down Expand Up @@ -1029,11 +1030,11 @@ void shouldIgnoreAlreadyDeclaredPluginInBuildGradleFile() {
}

@Test
void shouldDeclareAndConfigurePluginInBuildGradleProfileFile() {
void shouldDeclareAndConfigureCommunityPluginInBuildGradleProfileFile() {
JHipsterProjectFolder projectFolder = projectFrom("src/test/resources/projects/gradle-with-local-profile");

new GradleCommandHandler(Indentation.DEFAULT, projectFolder, filesReader).handle(
AddGradlePlugin.builder().plugin(gitPropertiesGradlePluginDependency()).buildProfile(localBuildProfile()).build()
AddGradlePlugin.builder().plugin(gitPropertiesGradleProfilePlugin()).buildProfile(localBuildProfile()).build()
);

assertThat(versionCatalogContent(projectFolder)).contains(
Expand Down Expand Up @@ -1074,7 +1075,7 @@ void shouldDeclareAndConfigurePluginInBuildGradleProfileFile() {
}

@Test
void shouldDeclarePluginWithDifferentGroupIdAndPluginIdInBuildGradleProfileFile() {
void shouldDeclareCommunityPluginWithDifferentGroupIdAndPluginIdInBuildGradleProfileFile() {
JHipsterProjectFolder projectFolder = projectFrom("src/test/resources/projects/gradle-with-local-profile");

new GradleCommandHandler(Indentation.DEFAULT, projectFolder, filesReader).handle(
Expand All @@ -1099,6 +1100,33 @@ void shouldDeclarePluginWithDifferentGroupIdAndPluginIdInBuildGradleProfileFile(
"""
);
}

@Test
void shouldDeclareAndConfigureCorePluginInBuildGradleProfileFile() {
JHipsterProjectFolder projectFolder = projectFrom("src/test/resources/projects/gradle-with-local-profile");

new GradleCommandHandler(Indentation.DEFAULT, projectFolder, filesReader).handle(
AddGradlePlugin.builder().plugin(checkstyleGradleProfilePlugin()).buildProfile(localBuildProfile()).build()
);

assertThat(scriptPluginContent(projectFolder, localBuildProfile()))
.contains(
"""
plugins {
java
checkstyle
// jhipster-needle-gradle-plugins
}
"""
)
.contains(
"""
checkstyle {
toolVersion = libs.versions.checkstyle.get()
}
"""
);
}
}

@Test
Expand Down Expand Up @@ -1149,7 +1177,7 @@ private static void assertFileExists(JHipsterProjectFolder projectFolder, String
assertThat(Files.exists(profileGradlePath)).as(description, profileGradlePath.toString()).isTrue();
}

public static GradlePlugin dockerGradlePluginDependency() {
private static GradleProfilePlugin dockerGradlePluginDependency() {
return gradleProfilePlugin()
.id("com.bmuschko.docker-remote-api")
.dependency(groupId("com.bmuschko"), artifactId("gradle-docker-plugin"))
Expand Down

0 comments on commit f94ec3a

Please sign in to comment.