Skip to content

Commit

Permalink
build: Generate templates with correct license headers from the start
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Aug 15, 2021
1 parent 379366c commit c0a8a02
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build-logic/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,6 +109,7 @@ void generate() throws IOException {
final Path outputDirectory = this.getOutputDir().get().getAsFile().toPath();
Files.createDirectories(outputDirectory);
final Set<Map<String, Object>> variants = this.getBaseSet().get().prepareDataForGeneration();
final @Nullable String header = Properties.finalized(this.getBaseSet().get().getHeader()).getOrNull();

final Set<String> seenOutputs = new HashSet<>();
Files.walkFileTree(sourceDirectory, new FileVisitor<Path>() {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +71,7 @@ public abstract class TemplateSet implements Named {
private final ConfigurableFileCollection dataFiles;
private final MapProperty<String, Object> properties;
private final NamedDomainObjectContainer<Variant> variants;
private final Property<String> header;
private final String name;

@Inject
Expand All @@ -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
Expand Down Expand Up @@ -108,6 +113,19 @@ public MapProperty<String, Object> getProperties() {
return this.properties;
}

/**
* A literal header to insert at the top of generated source files.
*
* <p>This property is optional.</p>
*
* @return the header
*/
@Input
@Optional
public Property<String> getHeader() {
return this.header;
}

// variant

@Nested
Expand Down
79 changes: 48 additions & 31 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.spongepowered.gradle.math.templates.GenerateTemplates
import java.io.StringWriter
import java.util.stream.Collectors

plugins {
jacoco
Expand All @@ -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 {
Expand Down Expand Up @@ -57,8 +98,14 @@ sourceSets {
}
}
}
configureEach {
templates.templateSets.configureEach {
header.set(licenseText)
}
}
}


dependencies {
val errorproneVersion: String by project
val junitVersion: String by project
Expand All @@ -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 -- //
Expand Down

0 comments on commit c0a8a02

Please sign in to comment.