Skip to content

Commit

Permalink
refactor: publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyewch committed Jul 17, 2024
1 parent eb7dd6c commit 3e2316f
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 80 deletions.
91 changes: 11 additions & 80 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,75 +1,8 @@
plugins {
`maven-publish`
signing
}

group = "io.github.ngyewch"
version = "0.1.1"

configurations {
create("binaries")
}

val binaryArtifacts: MutableList<PublishArtifact> = ArrayList()

tasks {
register("buildBinaries")

val buildInputs = arrayOf(
arrayOf("linux", "arm64", "linux-aarch_64"),
arrayOf("linux", "ppc64le", "linux-ppcle_64"),
arrayOf("linux", "s390x", "linux-s390_64"),
arrayOf("linux", "386", "linux-x86_32"),
arrayOf("linux", "amd64", "linux-x86_64"),
arrayOf("darwin", "arm64", "osx-aarch_64"),
arrayOf("darwin", "amd64", "osx-x86_64"),
arrayOf("windows", "386", "windows-x86_32"),
arrayOf("windows", "amd64", "windows-x86_64"),
)

fun createTask(goos: String, goarch: String, classifier: String) {
val taskName = "build_${goos}_${goarch}"
val outputExeFile =
layout.buildDirectory.file("protoc-gen-twirp-java-${project.version}-${classifier}.exe").get().asFile
val outputAscFile =
layout.buildDirectory.file("protoc-gen-twirp-java-${project.version}-${classifier}.exe.asc").get().asFile

binaryArtifacts.add(artifacts.add("binaries", outputExeFile) {
this.builtBy(taskName)
this.classifier = classifier
this.extension = "exe"
})
binaryArtifacts.add(artifacts.add("binaries", outputAscFile) {
this.builtBy(taskName)
this.classifier = "${classifier}.exe"
this.extension = "asc"
})

register<Exec>(taskName) {
dependsOn("createBuildDirectory")
outputs.file(outputExeFile)
outputs.file(outputAscFile)
commandLine("go", "build", "-o", outputExeFile)
environment("GOOS", goos)
environment("GOARCH", goarch)
}

named("buildBinaries") {
dependsOn(taskName)
}
}

for (buildInput in buildInputs) {
createTask(buildInput[0], buildInput[1], buildInput[2])
}

register<Exec>("createBuildDirectory") {
commandLine("mkdir", "-p", layout.buildDirectory.get().asFile.path)
}

withType<PublishToMavenRepository>().configureEach {
dependsOn("buildBinaries")
}
id("com.gradleup.nmcp") version "0.0.8"
id("io.github.ngyewch.protoc.plugin")
}

publishing {
Expand All @@ -79,9 +12,7 @@ publishing {
artifactId = project.name
version = project.version as String

for (binaryArtifact in binaryArtifacts) {
artifact(binaryArtifact)
}
from(components["protocPlugin"])

pom {
name = project.name
Expand Down Expand Up @@ -112,18 +43,18 @@ publishing {
name = "build"
url = uri(layout.buildDirectory.dir("repo"))
}
maven {
name = "central"
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
credentials {
username = project.properties["ossrh.username"] as String?
password = project.properties["ossrh.password"] as String?
}
}
}
}

signing {
useGpgCmd()
sign(publishing.publications["maven"])
}

nmcp {
publishAllPublications {
username = project.properties["mavenCentralUsername"] as String?
password = project.properties["mavenCentralPassword"] as String?
publicationType = "AUTOMATIC"
}
}
12 changes: 12 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`java-gradle-plugin`
}

gradlePlugin {
plugins {
create("protocPlugin") {
id = "io.github.ngyewch.protoc.plugin"
implementationClass = "io.github.ngyewch.protoc.plugin.ProtocPlugin"
}
}
}
Empty file added buildSrc/settings.gradle.kts
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.ngyewch.protoc.plugin;

public class BuildParameters {
private final String goos;
private final String goarch;
private final String classifier;

public BuildParameters(String goos, String goarch, String classifier) {
super();

this.goos = goos;
this.goarch = goarch;
this.classifier = classifier;
}

public String getGoos() {
return goos;
}

public String getGoarch() {
return goarch;
}

public String getClassifier() {
return classifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.github.ngyewch.protoc.plugin;

import java.io.File;
import javax.inject.Inject;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.component.AdhocComponentWithVariants;
import org.gradle.api.component.ConfigurationVariantDetails;
import org.gradle.api.component.SoftwareComponentFactory;
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository;
import org.gradle.api.tasks.Exec;
import org.gradle.api.tasks.TaskProvider;

public class ProtocPlugin implements Plugin<Project> {
private static final BuildParameters[] BUILD_PARAMETERS_LIST = {
new BuildParameters("linux", "arm64", "linux-aarch_64"),
new BuildParameters("linux", "ppc64le", "linux-ppcle_64"),
new BuildParameters("linux", "s390x", "linux-s390_64"),
new BuildParameters("linux", "386", "linux-x86_32"),
new BuildParameters("linux", "amd64", "linux-x86_64"),
new BuildParameters("darwin", "arm64", "osx-aarch_64"),
new BuildParameters("darwin", "amd64", "osx-x86_64"),
new BuildParameters("windows", "386", "windows-x86_32"),
new BuildParameters("windows", "amd64", "windows-x86_64"),
};

private final SoftwareComponentFactory softwareComponentFactory;

@Inject
public ProtocPlugin(SoftwareComponentFactory softwareComponentFactory) {
super();

this.softwareComponentFactory = softwareComponentFactory;
}

@Override
public void apply(Project project) {
final Attribute<String> typeAttribute = Attribute.of("type", String.class);
final Configuration configuration =
project
.getConfigurations()
.create("binaries", c -> c.getAttributes().attribute(typeAttribute, "default"));

final AdhocComponentWithVariants adhocComponent =
softwareComponentFactory.adhoc("protocPlugin");
project.getComponents().add(adhocComponent);
adhocComponent.addVariantsFromConfiguration(
configuration, ConfigurationVariantDetails::mapToOptional);

final File buildDirectory = project.getLayout().getBuildDirectory().get().getAsFile();
final File exeOutputDirectory = new File(buildDirectory, "exe");

final TaskProvider<Task> buildBinariesTaskProvider =
project.getTasks().register("buildBinaries");
for (final BuildParameters buildParameters : BUILD_PARAMETERS_LIST) {
final String taskName =
String.format("build_%s_%s", buildParameters.getGoos(), buildParameters.getGoarch());
final File outputExeFile =
new File(
exeOutputDirectory,
String.format(
"%s-%s-%s.exe",
project.getName(), project.getVersion(), buildParameters.getClassifier()));
final File outputAscFile =
new File(
exeOutputDirectory,
String.format(
"%s-%s-%s.exe.asc",
project.getName(), project.getVersion(), buildParameters.getClassifier()));
project
.getArtifacts()
.add(
"binaries",
outputExeFile,
artifact -> {
artifact.builtBy(taskName);
artifact.setClassifier(buildParameters.getClassifier());
artifact.setExtension("exe");
});
project
.getArtifacts()
.add(
"binaries",
outputExeFile,
artifact -> {
artifact.builtBy(taskName);
artifact.setClassifier(buildParameters.getClassifier() + ".exe");
artifact.setExtension("asc");
});

final TaskProvider<Exec> buildBinaryTaskProvider =
project
.getTasks()
.register(
taskName,
Exec.class,
task -> {
task.doFirst(
t -> {
outputExeFile.getParentFile().mkdirs();
outputAscFile.getParentFile().mkdirs();
});
task.getOutputs().file(outputExeFile);
task.commandLine("go", "build", "-o", outputExeFile);
task.environment("GOOS", buildParameters.getGoos());
task.environment("GOARCH", buildParameters.getGoarch());
});

buildBinariesTaskProvider.configure(task -> task.dependsOn(buildBinaryTaskProvider));
}

project
.getTasks()
.withType(
PublishToMavenRepository.class, task -> task.dependsOn(buildBinariesTaskProvider));
}
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group=io.github.ngyewch
version=0.1.1
Empty file added settings.gradle.kts
Empty file.

0 comments on commit 3e2316f

Please sign in to comment.