Skip to content

Commit

Permalink
Merge pull request #424 from grails/simplify-generated-build
Browse files Browse the repository at this point in the history
Simplify generated build
  • Loading branch information
jamesfredley authored Nov 14, 2024
2 parents 8cf161f + f30d3cb commit a5994bd
Show file tree
Hide file tree
Showing 101 changed files with 644 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CommandSpec extends Specification {
Process executeGradleCommand(String command) {
StringBuilder gradleCommand = new StringBuilder()
if (spock.util.environment.OperatingSystem.current.isWindows()) {
gradleCommand.append("gradlew.bat")
gradleCommand.append("cmd /c gradlew.bat")
} else {
gradleCommand.append("./gradlew")
}
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 @@ -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,12 +179,12 @@ public Builder scope(@NonNull Scope scope) {
}
}

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

public Builder compile() {
return scope(Scope.COMPILE);
public Builder implementation() {
return scope(Scope.IMPLEMENTATION);
}

public Builder console() {
Expand All @@ -182,21 +195,21 @@ public Builder compileOnly() {
return scope(Scope.COMPILE_ONLY);
}

public Builder runtime() {
return scope(Scope.RUNTIME);
public Builder runtimeOnly() {
return scope(Scope.RUNTIME_ONLY);
}

public Builder test() {
return scope(Scope.TEST);
public Builder testImplementation() {
return scope(Scope.TEST_IMPLEMENTATION);
}

@SuppressWarnings("unused")
public Builder testCompileOnly() {
return scope(Scope.TEST_COMPILE_ONLY);
}

public Builder testRuntime() {
return scope(Scope.TEST_RUNTIME);
public Builder testRuntimeOnly() {
return scope(Scope.TEST_RUNTIME_ONLY);
}

public Builder annotationProcessor() {
Expand All @@ -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 @@ -26,17 +26,18 @@ public class Scope {

public static final Scope ANNOTATION_PROCESSOR = new Scope(Source.MAIN, Collections.singletonList(Phase.ANNOTATION_PROCESSING));
public static final Scope BUILD = new Scope(Source.BUILD_SRC, Arrays.asList(Phase.BUILD));
public static final Scope COMPILE = new Scope(Source.MAIN, Arrays.asList(Phase.COMPILATION, Phase.RUNTIME));
public static final Scope IMPLEMENTATION = new Scope(Source.MAIN, Arrays.asList(Phase.COMPILATION, Phase.RUNTIME));
public static final Scope COMPILE_ONLY = new Scope(Source.MAIN, Collections.singletonList(Phase.COMPILATION));
public static final Scope CONSOLE = new Scope(Source.MAIN, Collections.singletonList(Phase.CONSOLE));
public static final Scope DEVELOPMENT_ONLY = new Scope(Source.MAIN, Collections.singletonList(Phase.DEVELOPMENT_ONLY));
public static final Scope RUNTIME = new Scope(Source.MAIN, Collections.singletonList(Phase.RUNTIME));
public static final Scope TEST = new Scope(Source.TEST, Arrays.asList(Phase.COMPILATION, Phase.RUNTIME));
public static final Scope RUNTIME_ONLY = new Scope(Source.MAIN, Collections.singletonList(Phase.RUNTIME));
public static final Scope TEST_IMPLEMENTATION = new Scope(Source.TEST, Arrays.asList(Phase.COMPILATION, Phase.RUNTIME));
public static final Scope TEST_ANNOTATION_PROCESSOR = new Scope(Source.TEST, Collections.singletonList(Phase.ANNOTATION_PROCESSING));
public static final Scope TEST_COMPILE_ONLY = new Scope(Source.TEST, Collections.singletonList(Phase.COMPILATION));
public static final Scope TEST_RUNTIME = new Scope(Source.TEST, Collections.singletonList(Phase.RUNTIME));
public static final Scope TEST_RUNTIME_ONLY = 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 @@ -99,13 +99,12 @@ public int hashCode() {

@NonNull
public String toSnippet() {
String snippet = gradleConfiguration.getConfigurationName();
String snippet = gradleConfiguration.getConfigurationName() + " ";
if (isPom()) {
String platformPrefix = " ";
snippet += platformPrefix + "platform";
snippet += "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,19 +60,17 @@ 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()
.groupId("com.bertramlabs.plugins")
.lookupArtifactId("asset-pipeline-grails")
.runtime());
.runtimeOnly());

final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
generatorContext.addTemplate("advancedgrails_svg", new URLTemplate("grails-app/assets/images/advancedgrails.svg", classLoader.getResource("assets/images/advancedgrails.svg")));
Expand Down
Loading

0 comments on commit a5994bd

Please sign in to comment.