From a8fcfec92d6afcf56d535482079b1ea74eea322c Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Tue, 16 Aug 2022 00:56:31 +0800 Subject: [PATCH] build: new gradle build scripts [skip ci] --- build.gradle | 111 +++------------------------------------ gradle.properties | 1 + gradle/artifacts.gradle | 43 +++++++++++++++ gradle/publishing.gradle | 33 ++++++++++++ gradle/signing.gradle | 6 +++ gradle/tasks.gradle | 17 ++++++ gradle/utils.gradle | 17 ++++++ gradle/version.gradle | 27 ++++++++++ 8 files changed, 151 insertions(+), 104 deletions(-) create mode 100644 gradle/artifacts.gradle create mode 100644 gradle/publishing.gradle create mode 100644 gradle/signing.gradle create mode 100644 gradle/tasks.gradle create mode 100644 gradle/utils.gradle create mode 100644 gradle/version.gradle diff --git a/build.gradle b/build.gradle index 0cd6609e..ef54f2d7 100644 --- a/build.gradle +++ b/build.gradle @@ -13,13 +13,14 @@ buildscript { apply plugin: 'net.minecraftforge.gradle' // Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. apply plugin: 'eclipse' -apply plugin: 'signing' apply plugin: 'maven-publish' apply plugin: 'org.spongepowered.mixin' + group = 'org.auioc.mcmod' archivesBaseName = "arnicalib" -version = "${minecraft_version}-${arnicalib_version}" +project.ext.modVersion= "${arnicalib_version}" +apply from: 'gradle/version.gradle' java.toolchain.languageVersion = JavaLanguageVersion.of(17) @@ -70,107 +71,9 @@ mixin { add sourceSets.main, 'arnicalib.mixin-refmap.json' } -ext { - manifest_attributes = [ - "Specification-Title": 'arnicalib', - "Specification-Vendor": 'AUIOC', - "Specification-Version": '1', - "Implementation-Title": project.name, - "Implementation-Version": "${arnicalib_version}", - "Implementation-Vendor" :'AUIOC', - "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), - "TweakClass": 'org.spongepowered.asm.launch.MixinTweaker', - "TweakOrder": 0, - "MixinConfigs": 'arnicalib.mixin.json', - "ArnicaLib-Version": "${archivesBaseName}-${version}-${getVar('CI_VERSION')}" - ] -} - -jar { - manifest {attributes(manifest_attributes)} -} -jar.finalizedBy('reobfJar') +apply from: 'gradle/tasks.gradle' -task deobfJar(type: Jar) { - from sourceSets.main.output - classifier = 'deobf' - manifest {attributes(manifest_attributes)} -} -task sourcesJar(type: Jar, dependsOn: classes) { - from sourceSets.main.allSource - classifier = 'sources' - manifest {attributes(manifest_attributes)} -} -task forgelibJar(type: Jar) { - from sourceSets.main.output - from sourceSets.main.allJava - classifier = 'forgelib' - manifest {attributes(manifest_attributes)} -} -artifacts { - archives sourcesJar - archives deobfJar - archives forgelibJar -} +apply from: 'gradle/artifacts.gradle' +apply from: 'gradle/publishing.gradle' +apply from: 'gradle/signing.gradle' -tasks.build.dependsOn('sourcesJar', 'deobfJar', 'forgelibJar') - -publishing { - publications { - mavenJava(MavenPublication) { - groupId project.group - artifactId project.archivesBaseName - version project.version - artifact jar - artifact deobfJar { - classifier 'deobf' - } - artifact sourcesJar { - classifier 'sources' - } - artifact forgelibJar { - classifier 'forgelib' - } - } - } - repositories { - maven { - name = 'GitHubPackages' - url = uri('https://maven.pkg.github.com/auioc/arnicalib-mcmod') - credentials { - username = getVar('GITHUB_ACTOR') - password = getVar('GITHUB_TOKEN') - } - } - maven { - name = 'McmodsRepo' - url "file://${project.projectDir}/mcmodsrepo" - } - } -} - -signing { - useInMemoryPgpKeys(findProperty('signingKey'), findProperty('signingPassword')) - sign publishing.publications -} - - -task genUpdateJson() { - doFirst { - def j = [ - 'homepage': 'https://github.com/auioc/arnicalib-mcmod/releases', - 'promos': [ - "${minecraft_version}-latest": "${arnicalib_version}", - "${minecraft_version}-recommended": "${arnicalib_version}", - ] - ] - println(groovy.json.JsonOutput.toJson(j)) - File file = file("tmp/${minecraft_version}.json") - file.write(groovy.json.JsonOutput.toJson(j)) - } -} - - -def getVar(String var_name) { - return System.getenv(var_name) -} diff --git a/gradle.properties b/gradle.properties index 4e9c6397..75a28a9d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,5 @@ mappings_version=1.18.2 mixin_version=0.8.5 +github_repo=auioc/arnicalib-mcmod arnicalib_version=5.3.8 diff --git a/gradle/artifacts.gradle b/gradle/artifacts.gradle new file mode 100644 index 00000000..5056a66e --- /dev/null +++ b/gradle/artifacts.gradle @@ -0,0 +1,43 @@ +def jarManifest = [ + "Specification-Title": 'arnicalib', + "Specification-Vendor": 'AUIOC', + "Specification-Version": '1', + "Implementation-Title": project.name, + "Implementation-Version": "${arnicalib_version}", + "Implementation-Vendor" :'AUIOC', + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), + "TweakClass": 'org.spongepowered.asm.launch.MixinTweaker', + "TweakOrder": 0, + "MixinConfigs": 'arnicalib.mixin.json', + "ArnicaLib-Version": "${project.archivesBaseName}-${project.fullVersion}" + ] + +jar { + manifest {attributes(jarManifest)} +} +jar.finalizedBy('reobfJar') + +task deobfJar(type: Jar) { + from sourceSets.main.output + classifier = 'deobf' + manifest {attributes(jarManifest)} +} +task sourcesJar(type: Jar, dependsOn: classes) { + from sourceSets.main.allSource + classifier = 'sources' + manifest {attributes(jarManifest)} +} +task forgelibJar(type: Jar) { + from sourceSets.main.output + from sourceSets.main.allJava + classifier = 'forgelib' + manifest {attributes(jarManifest)} +} + +artifacts { + archives sourcesJar + archives deobfJar + archives forgelibJar +} + +tasks.build.dependsOn('sourcesJar', 'deobfJar', 'forgelibJar') diff --git a/gradle/publishing.gradle b/gradle/publishing.gradle new file mode 100644 index 00000000..eac7de3d --- /dev/null +++ b/gradle/publishing.gradle @@ -0,0 +1,33 @@ +publishing { + publications { + mavenJava(MavenPublication) { + groupId project.group + artifactId project.archivesBaseName + version project.version + artifact jar + artifact deobfJar { + classifier 'deobf' + } + artifact sourcesJar { + classifier 'sources' + } + artifact forgelibJar { + classifier 'forgelib' + } + } + } + repositories { + maven { + name = 'GitHubPackages' + url = uri("https://maven.pkg.github.com/${github_repo}") + credentials { + username = System.getenv('GITHUB_ACTOR') + password = System.getenv('GITHUB_TOKEN') + } + } + maven { + name = 'McmodsRepo' + url "file://${project.projectDir}/mcmodsrepo" + } + } +} diff --git a/gradle/signing.gradle b/gradle/signing.gradle new file mode 100644 index 00000000..13728b58 --- /dev/null +++ b/gradle/signing.gradle @@ -0,0 +1,6 @@ +apply plugin: 'signing' + +signing { + useInMemoryPgpKeys(findProperty('signingKey'), findProperty('signingPassword')) + sign publishing.publications +} diff --git a/gradle/tasks.gradle b/gradle/tasks.gradle new file mode 100644 index 00000000..6c32285a --- /dev/null +++ b/gradle/tasks.gradle @@ -0,0 +1,17 @@ +task genUpdateJson() { + doFirst { + def j = [ + 'homepage': "https://github.com/${github_repo}/releases", + 'promos': [ + "${minecraft_version}-latest": "${project.modVersion}", + "${minecraft_version}-recommended": "${project.modVersion}", + ] + ] + File file = file("tmp/${minecraft_version}.json") + file.write(groovy.json.JsonOutput.toJson(j)) + } +} + +task outputProjectFullName() { + file("tmp/fullname.txt").write("${project.archivesBaseName}-${project.fullVersion}") +} diff --git a/gradle/utils.gradle b/gradle/utils.gradle new file mode 100644 index 00000000..29fde4e1 --- /dev/null +++ b/gradle/utils.gradle @@ -0,0 +1,17 @@ +def getExecOutput(commands) { + def out = new ByteArrayOutputStream() + exec { + commandLine commands + standardOutput out + } + return out.toString().trim(); +} + +def getEnvAsBoolean(name) { + return System.getenv(name) && System.getenv(name).toBoolean() +} + +ext { + getExecOutput = this.&getExecOutput + getEnvAsBoolean = this.&getEnvAsBoolean +} diff --git a/gradle/version.gradle b/gradle/version.gradle new file mode 100644 index 00000000..9b5f27a6 --- /dev/null +++ b/gradle/version.gradle @@ -0,0 +1,27 @@ +apply from: "$rootDir/gradle/utils.gradle" + +def getBuildVersion() { + if (getEnvAsBoolean('CI')) { + if (getEnvAsBoolean('GITHUB_ACTIONS')) { + def dev = getEnvAsBoolean('IS_RELEASE') ? '' : 'dev-' + def sha = System.getenv('GITHUB_SHA').substring(0, 7) + def buildNumber = System.getenv('GITHUB_RUN_NUMBER') + + return "${dev}rev.${sha}-build.${buildNumber}" + } else { + return System.getenv('CI_VERSION') + } + } else { + def sha = getExecOutput(['git', 'rev-parse', '--verify', '--short', 'HEAD']) + def dirty = getExecOutput(['git', 'status', '--short']).isEmpty() ? '' : '-dirty' + return "dev-rev.${sha}-build.0${dirty}" + } +} + + +project.ext.baseVersion = "${minecraft_version}-${project.modVersion}" +project.ext.fullVersion = "${baseVersion}-${getBuildVersion()}" +project.version = getEnvAsBoolean('IS_RELEASE') ? baseVersion : fullVersion + +logger.lifecycle("Project version: ${project.version}") +logger.lifecycle("Full version: ${fullVersion}")