diff --git a/build-logic/build.gradle b/build-logic/build.gradle index 8389538..62cdb9b 100644 --- a/build-logic/build.gradle +++ b/build-logic/build.gradle @@ -1,7 +1,7 @@ plugins { id "java" id "java-gradle-plugin" - id "org.spongepowered.gradle.sponge.dev" version "1.1.0-SNAPSHOT" + id "org.spongepowered.gradle.sponge.dev" version "1.1.1" } repositories { diff --git a/build-logic/src/main/java/org/spongepowered/gradle/math/templates/GenerateTemplates.java b/build-logic/src/main/java/org/spongepowered/gradle/math/templates/GenerateTemplates.java index 64e7e4a..5fe005c 100644 --- a/build-logic/src/main/java/org/spongepowered/gradle/math/templates/GenerateTemplates.java +++ b/build-logic/src/main/java/org/spongepowered/gradle/math/templates/GenerateTemplates.java @@ -29,6 +29,8 @@ import com.mitchellbosecke.pebble.loader.FileLoader; import com.mitchellbosecke.pebble.loader.Loader; import com.mitchellbosecke.pebble.template.PebbleTemplate; +import net.kyori.mammoth.Properties; +import org.checkerframework.checker.nullness.qual.Nullable; import org.gradle.api.DefaultTask; import org.gradle.api.InvalidUserDataException; import org.gradle.api.file.DirectoryProperty; @@ -107,6 +109,7 @@ void generate() throws IOException { final Path outputDirectory = this.getOutputDir().get().getAsFile().toPath(); Files.createDirectories(outputDirectory); final Set> variants = this.getBaseSet().get().prepareDataForGeneration(); + final @Nullable String header = Properties.finalized(this.getBaseSet().get().getHeader()).getOrNull(); final Set seenOutputs = new HashSet<>(); Files.walkFileTree(sourceDirectory, new FileVisitor() { @@ -139,6 +142,10 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr final Path output = outputDirectory.resolve(outputFile); Files.createDirectories(output.getParent()); try (final BufferedWriter writer = Files.newBufferedWriter(output, StandardCharsets.UTF_8)) { + if (header != null) { + writer.write(header); + writer.write(System.lineSeparator()); + } template.evaluate(writer, variant); } } diff --git a/build-logic/src/main/java/org/spongepowered/gradle/math/templates/TemplateSet.java b/build-logic/src/main/java/org/spongepowered/gradle/math/templates/TemplateSet.java index 7f19922..9b05c73 100644 --- a/build-logic/src/main/java/org/spongepowered/gradle/math/templates/TemplateSet.java +++ b/build-logic/src/main/java/org/spongepowered/gradle/math/templates/TemplateSet.java @@ -34,9 +34,12 @@ import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.model.ObjectFactory; import org.gradle.api.provider.MapProperty; +import org.gradle.api.provider.Property; +import org.gradle.api.resources.TextResource; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.Nested; +import org.gradle.api.tasks.Optional; import org.snakeyaml.engine.v2.api.Load; import org.snakeyaml.engine.v2.api.LoadSettings; import org.snakeyaml.engine.v2.exceptions.YamlEngineException; @@ -68,6 +71,7 @@ public abstract class TemplateSet implements Named { private final ConfigurableFileCollection dataFiles; private final MapProperty properties; private final NamedDomainObjectContainer variants; + private final Property header; private final String name; @Inject @@ -76,6 +80,7 @@ public TemplateSet(final String name, final ObjectFactory objects) { this.dataFiles = objects.fileCollection(); this.properties = objects.mapProperty(String.class, Object.class); this.variants = objects.domainObjectContainer(Variant.class); + this.header = objects.property(String.class); } @Input @@ -108,6 +113,19 @@ public MapProperty getProperties() { return this.properties; } + /** + * A literal header to insert at the top of generated source files. + * + *

This property is optional.

+ * + * @return the header + */ + @Input + @Optional + public Property getHeader() { + return this.header; + } + // variant @Nested diff --git a/build.gradle.kts b/build.gradle.kts index b71f088..699b0c3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,5 @@ -import org.spongepowered.gradle.math.templates.GenerateTemplates +import java.io.StringWriter +import java.util.stream.Collectors plugins { jacoco @@ -20,8 +21,48 @@ repositories { } } +// Metadata + +spongeConvention { + repository("math") { + ci(true) + publishing(true) + } + + mitLicense() + licenseParameters { + val organization: String by project + val url: String by project + this["name"] = "Math" + this["organization"] = organization + this["url"] = url + } + + sharedManifest { + attributes( + "Specification-Title" to project.name, + "Specification-Vendor" to "SpongePowered - https://spongepowered.org" + ) + } +} + val floatData = file("src/templateData/float.yaml") val intData = file("src/templateData/integer.yaml") +val licenseText = objects.property(String::class) +licenseText.set(provider { + val properties = (license as ExtensionAware).extra.properties.toMutableMap() + val template = groovy.text.SimpleTemplateEngine().createTemplate(file("HEADER.txt")).make(properties) + + val writer = StringWriter() + template.writeTo(writer) + val text = writer.toString() + val lineEnding = license.lineEnding.get() + text.split(Regex("\r?\n")).stream() + .map { if (it.isEmpty()) { " *" } else { " * $it" } } + .collect(Collectors.joining(lineEnding, "/*$lineEnding", "$lineEnding */")) +}) +licenseText.finalizeValueOnRead() + sourceSets { main { templates.templateSets { @@ -57,8 +98,14 @@ sourceSets { } } } + configureEach { + templates.templateSets.configureEach { + header.set(licenseText) + } + } } + dependencies { val errorproneVersion: String by project val junitVersion: String by project @@ -82,36 +129,6 @@ tasks { options.compilerArgs.add("-Xlint:-cast") // skip cast warnings, the generated source is most likely just overly safe. } - - // Put a license header on generated source - withType(GenerateTemplates::class) { - finalizedBy(licenseFormat) - } -} - -// Metadata - -spongeConvention { - repository("math") { - ci(true) - publishing(true) - } - - mitLicense() - licenseParameters { - val organization: String by project - val url: String by project - this["name"] = "Math" - this["organization"] = organization - this["url"] = url - } - - sharedManifest { - attributes( - "Specification-Title" to project.name, - "Specification-Vendor" to "SpongePowered - https://spongepowered.org" - ) - } } // -- Publishing -- //