Skip to content

Commit

Permalink
consolidate Gradle plugin config, make Gradle buildSrc and settings f…
Browse files Browse the repository at this point in the history
…ile optional features

when buildSrc feature is selected buildscript{} will not generate in main build.gradle file
  • Loading branch information
jamesfredley committed Nov 11, 2024
1 parent 96f9c18 commit e69382a
Show file tree
Hide file tree
Showing 24 changed files with 390 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
import org.grails.forge.build.dependencies.*;
import org.grails.forge.feature.Feature;
import org.grails.forge.feature.Features;
import org.grails.forge.feature.build.gradle.GradleBuildSrc;
import org.grails.forge.feature.config.ApplicationConfiguration;
import org.grails.forge.feature.config.BootstrapConfiguration;
import org.grails.forge.feature.config.Configuration;
Expand Down Expand Up @@ -318,9 +319,19 @@ public void addBuildscriptDependency(@NonNull Dependency dependency) {
if (dependency.requiresLookup()) {
Coordinate coordinate = coordinateResolver.resolve(dependency.getArtifactId())
.orElseThrow(() -> new LookupFailedException(dependency.getArtifactId()));
this.buildscriptDependencies.add(dependency.resolved(coordinate));
addBuildscriptDependencyBasedOnFeatures(dependency.resolved(coordinate));
} else {
addBuildscriptDependencyBasedOnFeatures(dependency);
}
}

private void addBuildscriptDependencyBasedOnFeatures(@NonNull Dependency dependency) {
if (getFeature(GradleBuildSrc.class).isPresent()) {
// for buildSrc/build.gradle with initial scope
this.buildscriptDependencies.add(dependency);
} else {
// for main build.gradle with classpath scope
this.buildscriptDependencies.add(dependency.scope(Scope.CLASSPATH));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ public Dependency resolved(Coordinate coordinate) {
coordinate.isPom());
}

public Dependency scope(Scope newScope) {
return new Dependency(
newScope,
groupId,
artifactId,
version,
versionProperty,
requiresLookup,
annotationProcessorPriority,
order,
pom);
}

public boolean isAnnotationProcessorPriority() {
return annotationProcessorPriority;
}
Expand Down Expand Up @@ -166,7 +179,7 @@ public Builder scope(@NonNull Scope scope) {
}
}

public Builder buildscript() {
public Builder buildSrc() {
return scope(Scope.BUILD);
}

Expand Down Expand Up @@ -207,6 +220,10 @@ public Builder profile() {
return scope(Scope.PROFILE);
}

public Builder classpath() {
return scope(Scope.CLASSPATH);
}

public Builder annotationProcessor(boolean requiresPriority) {
this.annotationProcessorPriority = requiresPriority;
return annotationProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Scope {
public static final Scope TEST_RUNTIME = new Scope(Source.TEST, Collections.singletonList(Phase.RUNTIME));
public static final Scope OPENREWRITE = new Scope(Source.MAIN, Collections.singletonList(Phase.OPENREWRITE));
public static final Scope PROFILE = new Scope(Source.MAIN, Collections.singletonList(Phase.PROFILE));
public static final Scope CLASSPATH = new Scope(Source.BUILDSCRIPT, Collections.singletonList(Phase.BUILD));

@NonNull
private Source source;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,5 +18,6 @@
public enum Source {
MAIN,
TEST,
BUILD_SRC
BUILD_SRC,
BUILDSCRIPT
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,8 +69,18 @@ public List<GradleDependency> getDependencies() {
return dependencies;
}

@NonNull
public List<GradleDependency> getBuildSrcDependencies() {
return buildscriptDependencies.stream().filter(gradleDependency -> !gradleDependency.getConfiguration().equals(GradleConfiguration.CLASSPATH)).collect(Collectors.toList());
}

@NonNull
public List<GradleDependency> getBuildscriptDependencies() {
return buildscriptDependencies.stream().filter(gradleDependency -> gradleDependency.getConfiguration().equals(GradleConfiguration.CLASSPATH)).collect(Collectors.toList());
}

@NonNull
public List<GradleDependency> getAllBuildscriptDependencies() {
return buildscriptDependencies;
}

Expand All @@ -84,6 +94,16 @@ public List<GradlePlugin> getPluginsWithVersion() {
return plugins.stream().filter(plugin -> plugin.getVersion() != null).collect(Collectors.toList());
}

@NonNull
public List<GradlePlugin> getPluginsWithoutApply() {
return plugins.stream().filter(plugin -> !plugin.useApplyPlugin()).collect(Collectors.toList());
}

@NonNull
public List<GradlePlugin> getPluginsWithApply() {
return plugins.stream().filter(plugin -> plugin.useApplyPlugin()).collect(Collectors.toList());
}

@NonNull
public String renderExtensions() {
return renderWritableExtensions(Stream.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Optional;

public enum GradleConfiguration implements Ordered {
CLASSPATH("classpath", -2),
PROFILE("profile", -1),
BUILD("implementation", 0),
ANNOTATION_PROCESSOR("annotationProcessor", 1),
Expand Down Expand Up @@ -74,6 +75,11 @@ public static Optional<GradleConfiguration> of(@NonNull Scope scope,
return Optional.of(GradleConfiguration.BUILD);
}
break;
case BUILDSCRIPT:
if (scope.getPhases().contains(Phase.BUILD)) {
return Optional.of(GradleConfiguration.CLASSPATH);
}
break;
case MAIN:
if (scope.getPhases().contains(Phase.ANNOTATION_PROCESSING)) {
return Optional.of(GradleConfiguration.COMPILE_ONLY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -102,10 +102,10 @@ public String toSnippet() {
String snippet = gradleConfiguration.getConfigurationName();
if (isPom()) {
String platformPrefix = " ";
snippet += platformPrefix + "platform";
snippet += platformPrefix + "platform (";
}
snippet += "(\"" + getGroupId() + ':' + getArtifactId() +
(getVersion() != null ? (':' + getVersion()) : "") + "\")";
snippet += " \"" + getGroupId() + ':' + getArtifactId() +
(getVersion() != null ? (':' + getVersion()) : "") + "\"";
if (isPom()) {
snippet += ")";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@ public class GradlePlugin implements BuildPlugin {
private final boolean requiresLookup;
private final Set<String> buildImports;
private final int order;
private final boolean useApplyPlugin;

public GradlePlugin(@NonNull String id,
@Nullable String version,
Expand All @@ -46,6 +47,26 @@ public GradlePlugin(@NonNull String id,
boolean requiresLookup,
int order,
Set<String> buildImports) {
this(id,
version,
artifactId,
extension,
settingsExtension,
requiresLookup,
order,
buildImports,
false);
}

public GradlePlugin(@NonNull String id,
@Nullable String version,
@Nullable String artifactId,
@Nullable Writable extension,
@Nullable Writable settingsExtension,
boolean requiresLookup,
int order,
Set<String> buildImports,
boolean useApplyPlugin) {
this.id = id;
this.version = version;
this.artifactId = artifactId;
Expand All @@ -54,6 +75,7 @@ public GradlePlugin(@NonNull String id,
this.requiresLookup = requiresLookup;
this.order = order;
this.buildImports = buildImports;
this.useApplyPlugin = useApplyPlugin;
}

@Nullable
Expand Down Expand Up @@ -98,6 +120,10 @@ public boolean requiresLookup() {
return requiresLookup;
}

public boolean useApplyPlugin() {
return useApplyPlugin;
}

@Override
public BuildPlugin resolved(CoordinateResolver coordinateResolver) {
Coordinate coordinate = coordinateResolver.resolve(artifactId)
Expand Down Expand Up @@ -137,6 +163,7 @@ public static final class Builder {
private boolean requiresLookup;
private boolean pom = false;
private int order = 0;
private boolean useApplyPlugin = false;
private boolean template = false;
private Set<String> buildImports = new HashSet<>();

Expand Down Expand Up @@ -199,8 +226,13 @@ public GradlePlugin.Builder pom(boolean pom) {
return this;
}

public GradlePlugin.Builder useApplyPlugin(boolean useApplyPlugin) {
this.useApplyPlugin = useApplyPlugin;
return this;
}

public GradlePlugin build() {
return new GradlePlugin(id, version, artifactId, extension, settingsExtension, requiresLookup, order, buildImports);
return new GradlePlugin(id, version, artifactId, extension, settingsExtension, requiresLookup, order, buildImports, useApplyPlugin);
}

private GradlePlugin.Builder copy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,13 +60,11 @@ public String getDescription() {

@Override
public void apply(GeneratorContext generatorContext) {
generatorContext.addBuildscriptDependency(Dependency.builder()
.groupId("com.bertramlabs.plugins")
.lookupArtifactId("asset-pipeline-gradle")
.buildscript());

generatorContext.addBuildPlugin(GradlePlugin.builder()
.id("com.bertramlabs.asset-pipeline")
.extension(new RockerWritable(assetPipelineExtension.template(generatorContext.getApplicationType())))
.lookupArtifactId("asset-pipeline-gradle")
.build());

generatorContext.addDependency(Dependency.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,9 +28,7 @@
import org.grails.forge.feature.build.BuildFeature;
import org.grails.forge.feature.build.gitignore;
import org.grails.forge.feature.build.gradle.templates.buildGradle;
import org.grails.forge.feature.build.gradle.templates.buildSrcBuildGradle;
import org.grails.forge.feature.build.gradle.templates.gradleProperties;
import org.grails.forge.feature.build.gradle.templates.settingsGradle;
import org.grails.forge.options.BuildTool;
import org.grails.forge.options.Options;
import org.grails.forge.template.BinaryTemplate;
Expand Down Expand Up @@ -74,14 +72,6 @@ public void apply(GeneratorContext generatorContext) {
BuildTool buildTool = BuildTool.DEFAULT_OPTION;
GradleBuild build = dependencyResolver.create(generatorContext);

generatorContext.addTemplate("buildSrc/build", new RockerTemplate("buildSrc/" + buildTool.getBuildFileName(), buildSrcBuildGradle.template(
generatorContext.getApplicationType(),
generatorContext.getProject(),
generatorContext.getFeatures(),
build
)));


final Function<String, Coordinate> coordinateResolver = (artifactId) -> resolver.resolve(artifactId).orElseThrow(() -> new LookupFailedException(artifactId));
generatorContext.addTemplate("build", new RockerTemplate(buildTool.getBuildFileName(), buildGradle.template(
generatorContext.getApplicationType(),
Expand All @@ -94,8 +84,6 @@ public void apply(GeneratorContext generatorContext) {
configureDefaultGradleProps(generatorContext);
generatorContext.addTemplate("gitignore", new RockerTemplate(".gitignore", gitignore.template()));
generatorContext.addTemplate("projectProperties", new RockerTemplate("gradle.properties", gradleProperties.template(generatorContext.getBuildProperties().getProperties())));
String settingsFile = "settings.gradle";
generatorContext.addTemplate("gradleSettings", new RockerTemplate(settingsFile, settingsGradle.template(generatorContext.getProject(), build, coordinateResolver, generatorContext.getFeatures())));
}

private void configureDefaultGradleProps(GeneratorContext generatorContext) {
Expand Down
Loading

0 comments on commit e69382a

Please sign in to comment.