mirror of
https://github.com/ethauvin/rife2-hello.git
synced 2025-04-25 23:37:12 -07:00
Merge pull request #6 from melix/cc/additional-template-dirs
Add support for extra template directories
This commit is contained in:
commit
de5c974f15
7 changed files with 70 additions and 23 deletions
|
@ -6,6 +6,7 @@ public class App extends Site {
|
||||||
public void setup() {
|
public void setup() {
|
||||||
var hello = get("/hello", c -> c.print(c.template("hello")));
|
var hello = get("/hello", c -> c.print(c.template("hello")));
|
||||||
get("/", c -> c.redirect(hello));
|
get("/", c -> c.redirect(hello));
|
||||||
|
get("/world", c -> c.print(c.template("world")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
11
app/src/main/resources/templates/world.html
Normal file
11
app/src/main/resources/templates/world.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title><!--v title-->Hello<!--/v--></title>
|
||||||
|
<link rel="stylesheet" href="{{v webapp:rootUrl/}}css/style.css?{{v context:paramRandom/}}">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>Hello World from src/main/resources/templates</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -23,7 +23,7 @@ import org.gradle.api.provider.Property;
|
||||||
import org.gradle.api.tasks.CacheableTask;
|
import org.gradle.api.tasks.CacheableTask;
|
||||||
import org.gradle.api.tasks.Classpath;
|
import org.gradle.api.tasks.Classpath;
|
||||||
import org.gradle.api.tasks.Input;
|
import org.gradle.api.tasks.Input;
|
||||||
import org.gradle.api.tasks.InputDirectory;
|
import org.gradle.api.tasks.InputFiles;
|
||||||
import org.gradle.api.tasks.Optional;
|
import org.gradle.api.tasks.Optional;
|
||||||
import org.gradle.api.tasks.OutputDirectory;
|
import org.gradle.api.tasks.OutputDirectory;
|
||||||
import org.gradle.api.tasks.PathSensitive;
|
import org.gradle.api.tasks.PathSensitive;
|
||||||
|
@ -32,6 +32,7 @@ import org.gradle.api.tasks.TaskAction;
|
||||||
import org.gradle.process.ExecOperations;
|
import org.gradle.process.ExecOperations;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -41,9 +42,9 @@ public abstract class PrecompileTemplates extends DefaultTask {
|
||||||
@Classpath
|
@Classpath
|
||||||
public abstract ConfigurableFileCollection getClasspath();
|
public abstract ConfigurableFileCollection getClasspath();
|
||||||
|
|
||||||
@InputDirectory
|
@InputFiles
|
||||||
@PathSensitive(PathSensitivity.RELATIVE)
|
@PathSensitive(PathSensitivity.RELATIVE)
|
||||||
public abstract DirectoryProperty getTemplatesDirectory();
|
public abstract ConfigurableFileCollection getTemplatesDirectories();
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
public abstract ListProperty<TemplateType> getTypes();
|
public abstract ListProperty<TemplateType> getTypes();
|
||||||
|
@ -65,6 +66,8 @@ public abstract class PrecompileTemplates extends DefaultTask {
|
||||||
@TaskAction
|
@TaskAction
|
||||||
public void precompileTemplates() {
|
public void precompileTemplates() {
|
||||||
for (var type : getTypes().get()) {
|
for (var type : getTypes().get()) {
|
||||||
|
getTemplatesDirectories().getFiles().forEach(dir -> {
|
||||||
|
if (Files.exists(dir.toPath())) {
|
||||||
getExecOperations().javaexec(javaexec -> {
|
getExecOperations().javaexec(javaexec -> {
|
||||||
javaexec.setClasspath(getClasspath());
|
javaexec.setClasspath(getClasspath());
|
||||||
javaexec.getMainClass().set("rife.template.TemplateDeployer");
|
javaexec.getMainClass().set("rife.template.TemplateDeployer");
|
||||||
|
@ -78,9 +81,12 @@ public abstract class PrecompileTemplates extends DefaultTask {
|
||||||
args.add(getOutputDirectory().get().getAsFile().getPath());
|
args.add(getOutputDirectory().get().getAsFile().getPath());
|
||||||
args.add("-encoding");
|
args.add("-encoding");
|
||||||
args.add(getEncoding().orElse("UTF-8").get());
|
args.add(getEncoding().orElse("UTF-8").get());
|
||||||
args.add(getTemplatesDirectory().get().getAsFile().getPath());
|
args.add(dir.getPath());
|
||||||
javaexec.args(args);
|
javaexec.args(args);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package com.uwyn.rife2.gradle;
|
package com.uwyn.rife2.gradle;
|
||||||
|
|
||||||
import org.gradle.api.*;
|
import org.gradle.api.Plugin;
|
||||||
|
import org.gradle.api.Project;
|
||||||
import org.gradle.api.artifacts.Configuration;
|
import org.gradle.api.artifacts.Configuration;
|
||||||
import org.gradle.api.artifacts.ConfigurationContainer;
|
import org.gradle.api.artifacts.ConfigurationContainer;
|
||||||
import org.gradle.api.artifacts.dsl.DependencyHandler;
|
import org.gradle.api.artifacts.dsl.DependencyHandler;
|
||||||
|
@ -38,10 +39,11 @@ import org.gradle.api.tasks.testing.Test;
|
||||||
import org.gradle.process.CommandLineArgumentProvider;
|
import org.gradle.process.CommandLineArgumentProvider;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class Rife2Plugin implements Plugin<Project> {
|
public class Rife2Plugin implements Plugin<Project> {
|
||||||
public static final String DEFAULT_TEMPLATES_DIR = "src/main/templates";
|
public static final List<String> DEFAULT_TEMPLATES_DIRS = List.of("src/main/resources/templates", "src/main/templates");
|
||||||
public static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
|
public static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
|
||||||
public static final String RIFE2_GROUP = "rife2";
|
public static final String RIFE2_GROUP = "rife2";
|
||||||
public static final String WEBAPP_SRCDIR = "src/main/webapp";
|
public static final String WEBAPP_SRCDIR = "src/main/webapp";
|
||||||
|
@ -114,7 +116,14 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
|
|
||||||
private static void bundlePrecompiledTemplatesIntoJarFile(TaskContainer tasks,
|
private static void bundlePrecompiledTemplatesIntoJarFile(TaskContainer tasks,
|
||||||
TaskProvider<PrecompileTemplates> precompileTemplatesTask) {
|
TaskProvider<PrecompileTemplates> precompileTemplatesTask) {
|
||||||
tasks.named("jar", Jar.class, jar -> jar.from(precompileTemplatesTask));
|
tasks.named("jar", Jar.class, jar -> {
|
||||||
|
jar.from(precompileTemplatesTask);
|
||||||
|
// 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");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createRife2DevelopmentOnlyConfiguration(Project project,
|
private void createRife2DevelopmentOnlyConfiguration(Project project,
|
||||||
|
@ -125,7 +134,7 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
conf.setCanBeConsumed(false);
|
conf.setCanBeConsumed(false);
|
||||||
conf.setCanBeResolved(false);
|
conf.setCanBeResolved(false);
|
||||||
});
|
});
|
||||||
rife2DevelopmentOnly.getDependencies().add(dependencies.create(project.files(DEFAULT_TEMPLATES_DIR)));
|
DEFAULT_TEMPLATES_DIRS.stream().forEachOrdered(dir -> rife2DevelopmentOnly.getDependencies().add(dependencies.create(project.files(dir))));
|
||||||
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
|
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,6 +158,11 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
.filter(f -> f.getAsFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
|
.filter(f -> f.getAsFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
|
||||||
.map(project::zipTree)
|
.map(project::zipTree)
|
||||||
.toList()));
|
.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");
|
||||||
plugins.withId("application", unused -> jar.manifest(manifest ->
|
plugins.withId("application", unused -> jar.manifest(manifest ->
|
||||||
manifest.getAttributes().put("Main-Class", rife2Extension.getUberMainClass().get()))
|
manifest.getAttributes().put("Main-Class", rife2Extension.getUberMainClass().get()))
|
||||||
);
|
);
|
||||||
|
@ -222,7 +236,7 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
task.getVerbose().convention(true);
|
task.getVerbose().convention(true);
|
||||||
task.getClasspath().from(rife2CompilerClasspath);
|
task.getClasspath().from(rife2CompilerClasspath);
|
||||||
task.getTypes().convention(rife2Extension.getPrecompiledTemplateTypes());
|
task.getTypes().convention(rife2Extension.getPrecompiledTemplateTypes());
|
||||||
task.getTemplatesDirectory().set(project.getLayout().getProjectDirectory().dir(DEFAULT_TEMPLATES_DIR));
|
DEFAULT_TEMPLATES_DIRS.stream().forEachOrdered(dir -> task.getTemplatesDirectories().from(project.getLayout().getProjectDirectory().dir(dir)));
|
||||||
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(DEFAULT_GENERATED_RIFE2_CLASSES_DIR));
|
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(DEFAULT_GENERATED_RIFE2_CLASSES_DIR));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title><!--v title-->Hello<!--/v--></title>
|
||||||
|
<link rel="stylesheet" href="{{v webapp:rootUrl/}}css/style.css?{{v context:paramRandom/}}">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>Hello World</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -25,6 +25,9 @@ class PackagingTest extends AbstractFunctionalTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert Files.exists(fs.getPath("/rife/template/html/hello.class"))
|
assert Files.exists(fs.getPath("/rife/template/html/hello.class"))
|
||||||
|
assert Files.exists(fs.getPath("/rife/template/html/world.class"))
|
||||||
|
assert !Files.exists(fs.getPath("/templates/hello.html"))
|
||||||
|
assert !Files.exists(fs.getPath("/templates/world.html"))
|
||||||
}
|
}
|
||||||
|
|
||||||
where:
|
where:
|
||||||
|
|
|
@ -42,6 +42,7 @@ class TemplateCompilationTest extends AbstractFunctionalTest {
|
||||||
|
|
||||||
then: "template sources must be present in the classpath"
|
then: "template sources must be present in the classpath"
|
||||||
outputContains("Classpath entry: src/main/templates")
|
outputContains("Classpath entry: src/main/templates")
|
||||||
|
outputContains("Classpath entry: src/main/resources/templates")
|
||||||
}
|
}
|
||||||
|
|
||||||
def "compiles templates when running #task"() {
|
def "compiles templates when running #task"() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue