|
|
|
@ -3,21 +3,29 @@ package com.jessebrault.ssg.gradle;
|
|
|
|
|
import org.gradle.api.Plugin;
|
|
|
|
|
import org.gradle.api.Project;
|
|
|
|
|
import org.gradle.api.artifacts.Configuration;
|
|
|
|
|
import org.gradle.api.artifacts.ConfigurationContainer;
|
|
|
|
|
import org.gradle.api.artifacts.Dependency;
|
|
|
|
|
import org.gradle.api.plugins.GroovyPlugin;
|
|
|
|
|
import org.gradle.api.plugins.JavaPlugin;
|
|
|
|
|
import org.gradle.api.tasks.Delete;
|
|
|
|
|
import org.gradle.api.tasks.TaskContainer;
|
|
|
|
|
import org.gradle.api.tasks.TaskProvider;
|
|
|
|
|
import org.gradle.api.plugins.JavaPluginExtension;
|
|
|
|
|
import org.gradle.api.tasks.*;
|
|
|
|
|
import org.gradle.api.tasks.compile.GroovyCompile;
|
|
|
|
|
import org.gradle.api.tasks.compile.JavaCompile;
|
|
|
|
|
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
public class SsgGradlePlugin implements Plugin<Project> {
|
|
|
|
|
|
|
|
|
|
public static final String TASK_GROUP = "ssg";
|
|
|
|
|
public static final String COMPONENTS_SOURCE_SET = "components";
|
|
|
|
|
public static final String PAGES_SOURCE_SET = "pages";
|
|
|
|
|
public static final String SSG_SOURCE_SET = "ssg";
|
|
|
|
|
|
|
|
|
|
private final ToolingModelBuilderRegistry toolingModelBuilderRegistry;
|
|
|
|
|
|
|
|
|
@ -26,34 +34,136 @@ public class SsgGradlePlugin implements Plugin<Project> {
|
|
|
|
|
this.toolingModelBuilderRegistry = toolingModelBuilderRegistry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void configureRepositories(Project project) {
|
|
|
|
|
// add tooling api repository because ssg-cli depends on it
|
|
|
|
|
project.getRepositories().maven(mavenRepository -> {
|
|
|
|
|
mavenRepository.setName("gradleToolingApiRepository");
|
|
|
|
|
mavenRepository.setUrl("https://repo.gradle.org/gradle/libs-releases");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void configureToolingModelBuilders() {
|
|
|
|
|
this.toolingModelBuilderRegistry.register(new SsgBuildModelBuilder());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void configureSsgCliDependency(Project project) {
|
|
|
|
|
final var ssgCliConfiguration = project.getConfigurations().getByName("ssgCli");
|
|
|
|
|
final Dependency ssgCliDependency = project.getDependencies().create("com.jessebrault.ssg:ssg-cli:0.4.0");
|
|
|
|
|
ssgCliConfiguration.getDependencies().add(ssgCliDependency);
|
|
|
|
|
protected Configuration createSsgApiConfiguration(Project project) {
|
|
|
|
|
final Configuration ssgApiConfiguration = project.getConfigurations().create("ssgApi");
|
|
|
|
|
ssgApiConfiguration.setCanBeConsumed(false);
|
|
|
|
|
ssgApiConfiguration.setCanBeResolved(true); // N.B.: Only a dependency container
|
|
|
|
|
ssgApiConfiguration.setVisible(false);
|
|
|
|
|
return ssgApiConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Project project) {
|
|
|
|
|
// apply at least java plugin
|
|
|
|
|
project.getPlugins().apply(JavaPlugin.class);
|
|
|
|
|
|
|
|
|
|
// add tooling api repository because ssg-cli depends on it
|
|
|
|
|
project.getRepositories().maven(mavenRepository -> {
|
|
|
|
|
mavenRepository.setName("gradle-tooling-api-repository");
|
|
|
|
|
mavenRepository.setUrl("https://repo.gradle.org/gradle/libs-releases");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.configureToolingModelBuilders();
|
|
|
|
|
|
|
|
|
|
protected Configuration createSsgCliConfiguration(Project project, Configuration ssgApiConfiguration) {
|
|
|
|
|
final Configuration ssgCliConfiguration = project.getConfigurations().create("ssgCli");
|
|
|
|
|
ssgCliConfiguration.setCanBeConsumed(false);
|
|
|
|
|
ssgCliConfiguration.setCanBeResolved(true);
|
|
|
|
|
ssgCliConfiguration.setVisible(false);
|
|
|
|
|
ssgCliConfiguration.extendsFrom(ssgApiConfiguration);
|
|
|
|
|
return ssgCliConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void createDomainSourceSet(Project project, SourceSetContainer sourceSets, String name) {
|
|
|
|
|
sourceSets.create(name, sourceSet -> {
|
|
|
|
|
// first, register the dirs
|
|
|
|
|
// java
|
|
|
|
|
sourceSet.getJava().setSrcDirs(List.of(name + File.separator + "java"));
|
|
|
|
|
// groovy
|
|
|
|
|
final var groovySourceDirectorySet = sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class);
|
|
|
|
|
groovySourceDirectorySet.setSrcDirs(List.of(name + File.separator + "groovy"));
|
|
|
|
|
// resources
|
|
|
|
|
sourceSet.getResources().setSrcDirs(List.of(name + File.separator + "resources"));
|
|
|
|
|
|
|
|
|
|
project.getTasks().withType(GroovyCompile.class).configureEach(groovyCompile -> {
|
|
|
|
|
groovyCompile.source(name + File.separator + "groovy");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void createSsgSourceSet(SourceSetContainer sourceSets) {
|
|
|
|
|
sourceSets.create(SSG_SOURCE_SET, sourceSet -> {
|
|
|
|
|
// groovy only
|
|
|
|
|
sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class).setSrcDirs(List.of(SSG_SOURCE_SET));
|
|
|
|
|
// resources live right next to build scripts, if needed
|
|
|
|
|
sourceSet.getResources().setSrcDirs(List.of(SSG_SOURCE_SET));
|
|
|
|
|
// disable java
|
|
|
|
|
sourceSet.getJava().setSrcDirs(List.of());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void configureSourceSets(Project project) {
|
|
|
|
|
final var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);
|
|
|
|
|
final var sourceSets = javaExtension.getSourceSets();
|
|
|
|
|
this.createDomainSourceSet(project, sourceSets, PAGES_SOURCE_SET);
|
|
|
|
|
this.createDomainSourceSet(project, sourceSets, COMPONENTS_SOURCE_SET);
|
|
|
|
|
this.createSsgSourceSet(sourceSets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// protected void configureCompileSsgGroovyTask(Project project) {
|
|
|
|
|
// project.getTasks().named("compileSsgGroovy", GroovyCompile.class, groovyCompile -> {
|
|
|
|
|
// final File dotSsg = project.getLayout().getProjectDirectory().dir(".ssg").getAsFile();
|
|
|
|
|
// if (!dotSsg.exists()) {
|
|
|
|
|
// //noinspection ResultOfMethodCallIgnored
|
|
|
|
|
// dotSsg.mkdirs();
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// final File compileConfigurationScript = new File(dotSsg, "compileSsgGroovy.groovy");
|
|
|
|
|
// if (!compileConfigurationScript.exists()) {
|
|
|
|
|
// try (final OutputStream outputStream = new FileOutputStream(compileConfigurationScript)) {
|
|
|
|
|
// outputStream.write("""
|
|
|
|
|
// configuration.scriptBaseClass = 'com.jessebrault.ssg.buildscript.BuildScriptBase'
|
|
|
|
|
// """.stripIndent().trim().getBytes());
|
|
|
|
|
// } catch (IOException ioException) {
|
|
|
|
|
// throw new RuntimeException(ioException);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// groovyCompile.getGroovyOptions().setConfigurationScript(compileConfigurationScript);
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
protected void configureSourceSetConfigurations(Project project, Configuration ssgApiConfiguration) {
|
|
|
|
|
final ConfigurationContainer configurations = project.getConfigurations();
|
|
|
|
|
|
|
|
|
|
final Configuration implementation = configurations.getByName("implementation");
|
|
|
|
|
implementation.extendsFrom(ssgApiConfiguration);
|
|
|
|
|
|
|
|
|
|
final Set<Configuration> subConfigurations = new HashSet<>();
|
|
|
|
|
subConfigurations.add(configurations.getByName("ssgImplementation"));
|
|
|
|
|
subConfigurations.add(configurations.getByName("pagesImplementation"));
|
|
|
|
|
subConfigurations.add(configurations.getByName("componentsImplementation"));
|
|
|
|
|
|
|
|
|
|
subConfigurations.forEach(subConfiguration -> subConfiguration.extendsFrom(implementation));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void configureSsgDependencies(
|
|
|
|
|
Project project,
|
|
|
|
|
Configuration ssgApiConfiguration,
|
|
|
|
|
Configuration ssgCliConfiguration
|
|
|
|
|
) {
|
|
|
|
|
final Dependency ssgApi = project.getDependencies().create("com.jessebrault.ssg:ssg-api:0.4.0");
|
|
|
|
|
final Dependency ssgCli = project.getDependencies().create("com.jessebrault.ssg:ssg-cli:0.4.0");
|
|
|
|
|
ssgApiConfiguration.getDependencies().add(ssgApi);
|
|
|
|
|
ssgCliConfiguration.getDependencies().add(ssgCli);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Project project) {
|
|
|
|
|
// apply the java and groovy plugins
|
|
|
|
|
project.getPlugins().apply(JavaPlugin.class);
|
|
|
|
|
project.getPlugins().apply(GroovyPlugin.class);
|
|
|
|
|
|
|
|
|
|
this.configureRepositories(project);
|
|
|
|
|
this.configureToolingModelBuilders();
|
|
|
|
|
this.configureSourceSets(project);
|
|
|
|
|
// this.configureCompileSsgGroovyTask(project);
|
|
|
|
|
|
|
|
|
|
// configurations
|
|
|
|
|
final Configuration ssgApiConfiguration = this.createSsgApiConfiguration(project);
|
|
|
|
|
final Configuration ssgCliConfiguration = this.createSsgCliConfiguration(project, ssgApiConfiguration);
|
|
|
|
|
this.configureSourceSetConfigurations(project, ssgApiConfiguration);
|
|
|
|
|
|
|
|
|
|
// Tasks
|
|
|
|
|
final TaskContainer tasks = project.getTasks();
|
|
|
|
|
|
|
|
|
|
final TaskProvider<SsgBinScriptTask> binScriptTaskTaskProvider = tasks.register(
|
|
|
|
@ -68,12 +178,13 @@ public class SsgGradlePlugin implements Plugin<Project> {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
tasks.register("cleanBin", Delete.class, deleteTask -> {
|
|
|
|
|
deleteTask.setDelete(binScriptTaskTaskProvider.map(SsgBinScriptTask::getBinDir));
|
|
|
|
|
tasks.register("cleanSsg", Delete.class, deleteTask -> {
|
|
|
|
|
deleteTask.delete(binScriptTaskTaskProvider.map(SsgBinScriptTask::getBinDir));
|
|
|
|
|
deleteTask.delete(".ssg");
|
|
|
|
|
deleteTask.setGroup(TASK_GROUP);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
project.afterEvaluate(this::configureSsgCliDependency);
|
|
|
|
|
project.afterEvaluate(p -> this.configureSsgDependencies(p, ssgApiConfiguration, ssgCliConfiguration));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|