2
0
Fork 0
mirror of https://github.com/ethauvin/rife2-hello.git synced 2025-04-25 07:27:12 -07:00

Aligned with Gradle plugin source state

This commit is contained in:
Geert Bevin 2023-03-05 13:19:23 -05:00
parent 794f614e57
commit 7f82944093
4 changed files with 132 additions and 20 deletions

View file

@ -36,33 +36,65 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
/**
* Gradle task to pre-compile RIFE2 templates
*/
@CacheableTask
public abstract class PrecompileTemplates extends DefaultTask {
@Classpath
public abstract ConfigurableFileCollection getClasspath();
/**
* The directories where template files can be found.
*
* @return the directories with template files
*/
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public abstract ConfigurableFileCollection getTemplatesDirectories();
/**
* The template types to pre-compile.
*
* @return a list of template types
*/
@Input
public abstract ListProperty<TemplateType> getTypes();
/**
* The encoding to use when reading the template files.
* Defaults to {@code UTF-8}.
*
* @return the encoding of the template files
*/
@Input
@Optional
public abstract Property<String> getEncoding();
/**
* Indicates whether the pre-compilation should be verbose or not.
*
* @return {@code true} when the pre-compilation should be verbose; or
* {@code false} otherwise
*/
@Input
@Optional
public abstract Property<Boolean> getVerbose();
/**
* Provides the directory into which pre-compiled template class files should be stored.
*
* @return the output directory for the template pre-compilation
*/
@OutputDirectory
public abstract DirectoryProperty getOutputDirectory();
@Classpath
public abstract ConfigurableFileCollection getClasspath();
@Inject
protected abstract ExecOperations getExecOperations();
/**
* Perform the template pre-compilation
*/
@TaskAction
public void precompileTemplates() {
for (var type : getTypes().get()) {

View file

@ -19,14 +19,45 @@ import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
/**
* The Gradle RIFE2 extension
*/
public abstract class Rife2Extension {
/**
* The RIFE2 version that should be used by the project.
*
* @return the RIFE2 version as a string
*/
public abstract Property<String> getVersion();
/**
* Indicates whether the project should be launched with the RIFE2 agent or not.
*
* @return {@code true} when the project should be launched with the RIFE2 agent;
* {@code false} otherwise
*/
public abstract Property<Boolean> getUseAgent();
/**
* Specifies the main Java class to use when building the uber jar.
*
* @return the fully qualified name of the main class to use when launching the uber jar.
*/
public abstract Property<String> getUberMainClass();
/**
* Specifies the template types that should be precompiled.
* By default, none are precompiled.
*
* @return a list of template types to precompile
*/
public abstract ListProperty<TemplateType> getPrecompiledTemplateTypes();
/**
* Specifies the directories where the template files can be found.
* By default, this includes {@code "src/main/resources/templates"}.
*
* @return the collection of directories to look for template files
*/
public abstract ConfigurableFileCollection getTemplateDirectories();
}

View file

@ -45,11 +45,11 @@ import java.util.Locale;
import java.util.stream.Collectors;
public class Rife2Plugin implements Plugin<Project> {
public static final List<String> DEFAULT_TEMPLATES_DIRS = List.of("src/main/resources/templates");
public static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
public static final String RIFE2_GROUP = "rife2";
public static final String WEBAPP_SRCDIR = "src/main/webapp";
public static final String PRECOMPILE_TEMPLATES_TASK_NAME = "precompileTemplates";
static final List<String> DEFAULT_TEMPLATES_DIRS = List.of("src/main/resources/templates");
static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
static final String RIFE2_GROUP = "rife2";
static final String WEBAPP_SRCDIR = "src/main/webapp";
static final String PRECOMPILE_TEMPLATES_TASK_NAME = "precompileTemplates";
@Override
public void apply(Project project) {
@ -121,10 +121,18 @@ public class Rife2Plugin implements Plugin<Project> {
Rife2Extension rife2Extension) {
tasks.named("jar", Jar.class, jar -> {
jar.from(precompileTemplatesTask);
// This isn't great because it needs to be partially hardcoded, in order to avoid the templates
// declared in `src/main/resources/templates` to be included in the jar file.
rife2Extension.getPrecompiledTemplateTypes().get().forEach(templateType ->
jar.exclude("/templates/**." + templateType.identifier().toLowerCase()));
excludeTemplateSourcesInClassPath(jar, precompileTemplatesTask, rife2Extension);
});
}
private static void excludeTemplateSourcesInClassPath(Jar jar, TaskProvider<PrecompileTemplates> precompileTemplatesTask, Rife2Extension rife2Extension) {
// This isn't great because it needs to be partially hardcoded, in order to avoid the templates
// declared in `src/main/resources/templates` to be included in the jar file.
precompileTemplatesTask.get().getTemplatesDirectories().forEach(dir -> {
if (dir.getAbsolutePath().contains("/src/main/resources/")) {
rife2Extension.getPrecompiledTemplateTypes().get().forEach(templateType ->
jar.exclude("/" + dir.getName() + "/**." + templateType.identifier().toLowerCase()));
}
});
}
@ -138,7 +146,7 @@ public class Rife2Plugin implements Plugin<Project> {
conf.setCanBeResolved(false);
});
rife2DevelopmentOnly.getDependencies().addAllLater(templateDirectories.getElements().map(locations ->
locations.stream().map(fs -> dependencies.create(project.files(fs))).collect(Collectors.toList()))
locations.stream().map(fs -> dependencies.create(project.files(fs))).collect(Collectors.toList()))
);
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
}
@ -163,11 +171,7 @@ public class Rife2Plugin implements Plugin<Project> {
.filter(f -> f.getAsFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
.map(project::zipTree)
.toList()));
// This isn't great because it needs to be hardcoded, in order to avoid the templates
// declared in `src/main/resources/templates` to be included in the jar file.
// which means that if for whatever reason the user also uses the same directory for
// something else, it will be excluded from the jar file.
jar.exclude("templates");
excludeTemplateSourcesInClassPath(jar, precompileTemplatesTask, rife2Extension);
plugins.withId("application", unused -> jar.manifest(manifest ->
manifest.getAttributes().put("Main-Class", rife2Extension.getUberMainClass().get()))
);
@ -246,4 +250,4 @@ public class Rife2Plugin implements Plugin<Project> {
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(DEFAULT_GENERATED_RIFE2_CLASSES_DIR));
});
}
}
}

View file

@ -1,24 +1,69 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.uwyn.rife2.gradle;
import java.io.Serial;
import java.io.Serializable;
/**
* Allows template types to be specified for pre-compilation.
*/
public class TemplateType implements Serializable {
@Serial private static final long serialVersionUID = -2736320275307140837L;
/**
* The {@code html} template type.
*/
public static TemplateType HTML = new TemplateType("html");
/**
* The {@code json} template type.
*/
public static TemplateType JSON = new TemplateType("json");
/**
* The {@code svg} template type.
*/
public static TemplateType SVG = new TemplateType("svg");
/**
* The {@code xml} template type.
*/
public static TemplateType XML = new TemplateType("xml");
/**
* The {@code txt} template type.
*/
public static TemplateType TXT = new TemplateType("txt");
/**
* The {@code sql} template type.
*/
public static TemplateType SQL = new TemplateType("sql");
private final String identifier_;
/**
* Creates a new template type instance.
*
* @param identifier the identifier of this template type
*/
public TemplateType(String identifier) {
identifier_ = identifier;
}
/**
* Retrieves the identifier for this template type
* @return the template type identifier as a string
*/
public String identifier() {
return identifier_;
}