spacepaste

  1.  
  2. import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
  3. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  4. import net.minecraftforge.gradle.user.IReobfuscator
  5. import net.minecraftforge.gradle.user.ReobfMappingType
  6. import net.minecraftforge.gradle.user.ReobfTaskFactory
  7. import net.minecraftforge.gradle.user.patcherUser.forge.ForgeExtension
  8. import net.minecraftforge.gradle.user.patcherUser.forge.ForgePlugin
  9. import nl.javadude.gradle.plugins.license.LicenseExtension
  10. import nl.javadude.gradle.plugins.license.LicensePlugin
  11. import org.ajoberstar.grgit.Grgit
  12. import org.gradle.api.internal.HasConvention
  13. import org.spongepowered.asm.gradle.plugins.MixinExtension
  14. import org.spongepowered.asm.gradle.plugins.MixinGradlePlugin
  15. import java.util.regex.Pattern
  16. // Gradle repositories and dependencies
  17. buildscript {
  18. repositories {
  19. mavenCentral()
  20. jcenter()
  21. maven {
  22. setUrl("http://files.minecraftforge.net/maven")
  23. }
  24. maven {
  25. setUrl("http://repo.spongepowered.org/maven")
  26. }
  27. maven {
  28. setUrl("https://plugins.gradle.org/m2/")
  29. }
  30. }
  31. }
  32. plugins {
  33. base
  34. java
  35. maven
  36. id("net.minecraftforge.gradle.forge").version("2.3-SNAPSHOT")
  37. id("org.spongepowered.mixin").version("0.5-SNAPSHOT")
  38. id("com.github.johnrengelman.shadow").version("2.0.4")
  39. id("com.github.hierynomus.license").version("0.14.0")
  40. id("org.ajoberstar.grgit").version("3.0.0-beta.1")
  41. }
  42. // TODO: Reduce duplication of buildscript code between CC projects?
  43. group = "io.github.opencubicchunks"
  44. version = getModVersion()
  45. val theForgeVersion: String by project
  46. val versionSuffix: String by project
  47. val versionMinorFreeze: String by project
  48. val theMappingsVersion: String by project
  49. val licenseYear: String by project
  50. val projectName: String by project
  51. val malisisCoreVersion: String by project
  52. val malisisCoreMinVersion: String by project
  53. val sourceSets = the<JavaPluginConvention>().sourceSets
  54. val mainSourceSet = sourceSets["main"]
  55. val minecraft = the<ForgeExtension>()
  56. base {
  57. archivesBaseName = "CubicWorldGen"
  58. }
  59. java {
  60. sourceCompatibility = JavaVersion.VERSION_1_8
  61. }
  62. minecraft {
  63. version = theForgeVersion
  64. runDir = "run"
  65. mappings = theMappingsVersion
  66. isUseDepAts = true
  67. replace("@@MALISIS_VERSION@@", malisisCoreMinVersion)
  68. replaceIn("io/github/opencubicchunks/cubicchunks/cubicgen/CustomCubicMod.java")
  69. }
  70. license {
  71. val ext = (this as HasConvention).convention.extraProperties
  72. ext["project"] = projectName
  73. ext["year"] = licenseYear
  74. exclude("**/*.info")
  75. exclude("**/package-info.java")
  76. exclude("**/*.json")
  77. exclude("**/*.xml")
  78. exclude("assets/*")
  79. header = file("HEADER.txt")
  80. ignoreFailures = false
  81. strictCheck = true
  82. mapping(mapOf("java" to "SLASHSTAR_STYLE"))
  83. }
  84. mixin {
  85. add(sourceSets["main"], "cubicgen.refmap.json")
  86. }
  87. repositories {
  88. mavenCentral()
  89. maven { setUrl("https://oss.sonatype.org/content/repositories/public/") }
  90. // Note: sponge repository needs to be the second one because flow-noise is both in sponge and sonatype repository
  91. // but sponge has older one, and we need the newer one from sonatype
  92. // currently gradle seems to resolve dependencies from repositories in the order they are defined here
  93. maven { setUrl("http://repo.spongepowered.org/maven") }
  94. }
  95. val deobfCompile by configurations
  96. val compile by configurations
  97. val testCompile by configurations
  98. val forgeGradleGradleStart by configurations
  99. val forgeGradleMcDeps by configurations
  100. val shade by configurations.creating
  101. compile.extendsFrom(shade)
  102. // needed for tests to work
  103. testCompile.extendsFrom(forgeGradleGradleStart)
  104. testCompile.extendsFrom(forgeGradleMcDeps)
  105. dependencies {
  106. deobfCompile("net.malisis:malisiscore:$malisisCoreVersion") {
  107. isTransitive = false
  108. }
  109. shade("com.flowpowered:flow-noise:1.0.1-SNAPSHOT")
  110. deobfCompile("io.github.opencubicchunks:cubicchunks-api:1.12.2-0.0-SNAPSHOT")
  111. deobfCompile("io.github.opencubicchunks:cubicchunks:1.12.2-0.0-SNAPSHOT")
  112. // provided by cubicchunks implementation
  113. shade("org.spongepowered:mixin:0.7.10-SNAPSHOT") {
  114. isTransitive = false
  115. }
  116. testCompile("junit:junit:4.11")
  117. testCompile("org.hamcrest:hamcrest-junit:2.0.0.0")
  118. testCompile("it.ozimov:java7-hamcrest-matchers:0.7.0")
  119. testCompile("org.mockito:mockito-core:2.1.0-RC.2")
  120. testCompile("org.spongepowered:launchwrappertestsuite:1.0-SNAPSHOT")
  121. }
  122. fun Jar.setupManifest() {
  123. manifest {
  124. attributes["FMLAT"] = "cubicchunks_cubicgen_at.cfg"
  125. attributes["FMLCorePluginContainsFMLMod"] = "true"
  126. attributes["FMLCorePlugin"] = "io.github.opencubicchunks.cubicchunks.cubicgen.asm.CubicGenCoreMod"
  127. attributes["TweakClass"] = "org.spongepowered.asm.launch.MixinTweaker"
  128. attributes["TweakOrder"] = "0"
  129. attributes["ForceLoadAsMod"] = "true"
  130. attributes["Maven-Version"] = "${project.group}:${project.base.archivesBaseName}:${project.version}:core"
  131. }
  132. }
  133. tasks {
  134. val shadeJar by tasks.creating(ShadowJar::class) {
  135. from(sourceSets["main"].output)
  136. configurations = listOf(shade)
  137. setupManifest()
  138. classifier = "all"
  139. }
  140. configure<NamedDomainObjectContainer<IReobfuscator>> {
  141. create("shadeJar").apply {
  142. mappingType = ReobfMappingType.SEARGE
  143. }
  144. }
  145. tasks["assemble"].dependsOn("reobfShadeJar")
  146. "test"(Test::class) {
  147. systemProperty("lwts.tweaker", "io.github.opencubicchunks.cubicchunks.tweaker.MixinTweakerServer")
  148. jvmArgs("-Dmixin.debug.verbose=true", //verbose mixin output for easier debugging of mixins
  149. "-Dmixin.checks.interfaces=true", //check if all interface methods are overriden in mixin
  150. "-Dmixin.env.remapRefMap=true")
  151. testLogging {
  152. showStandardStreams = true
  153. }
  154. }
  155. "processResources"(ProcessResources::class) {
  156. // this will ensure that this task is redone when the versions change.
  157. inputs.property("version", project.version.toString())
  158. inputs.property("mcversion", minecraft.version)
  159. // replace stuff in mcmod.info, nothing else
  160. from(mainSourceSet.resources.srcDirs) {
  161. include("mcmod.info")
  162. // replace version and mcversion
  163. expand(mapOf("version" to project.version.toString(), "mcversion" to minecraft.version))
  164. }
  165. // copy everything else, thats not the mcmod.info
  166. from(mainSourceSet.resources.srcDirs) {
  167. exclude("mcmod.info")
  168. }
  169. }
  170. /*
  171. // based on:
  172. // https://github.com/Ordinastie/MalisisCore/blob/30d8efcfd047ac9e9bc75dfb76642bd5977f0305/build.gradle#L204-L256
  173. // https://github.com/gradle/kotlin-dsl/blob/201534f53d93660c273e09f768557220d33810a9/samples/maven-plugin/build.gradle.kts#L10-L44
  174. val uploadArchives by tasks.creating(Upload::class) {
  175. repositories {
  176. withConvention(MavenRepositoryHandlerConvention::class) {
  177. mavenDeployer {
  178. // Sign Maven POM
  179. //beforeDeployment {
  180. // signing.signPom(this)
  181. //}
  182. val username = if (project.hasProperty("sonatypeUsername")) project.properties["sonatypeUsername"] else System.getenv("sonatypeUsername")
  183. val password = if (project.hasProperty("sonatypePassword")) project.properties["sonatypePassword"] else System.getenv("sonatypePassword")
  184. withGroovyBuilder {
  185. "snapshotRepository"("url" to "https://oss.sonatype.org/content/repositories/snapshots") {
  186. "authentication"("userName" to username, "password" to password)
  187. }
  188. "repository"("url" to "https://oss.sonatype.org/service/local/staging/deploy/maven2") {
  189. "authentication"("userName" to username, "password" to password)
  190. }
  191. }
  192. // Maven POM generation
  193. pom.project {
  194. withGroovyBuilder {
  195. "name"(projectName)
  196. "artifactId"(base.archivesBaseName.toLowerCase())
  197. "packaging"("jar")
  198. "url"("https://github.com/OpenCubicChunks/CubicChunks")
  199. "description"("Unlimited world height mod for Minecraft")
  200. "scm" {
  201. "connection"("scm:git:git://github.com/OpenCubicChunks/CubicChunks.git")
  202. "developerConnection"("scm:git:ssh://git@github.com:OpenCubicChunks/CubicChunks.git")
  203. "url"("https://github.com/OpenCubicChunks/RegionLib")
  204. }
  205. "licenses" {
  206. "license" {
  207. "name"("The MIT License")
  208. "url"("http://www.tldrlegal.com/license/mit-license")
  209. "distribution"("repo")
  210. }
  211. }
  212. "developers" {
  213. "developer" {
  214. "id"("Barteks2x")
  215. "name"("Barteks2x")
  216. }
  217. // TODO: add more developers
  218. }
  219. "issueManagement" {
  220. "system"("github")
  221. "url"("https://github.com/OpenCubicChunks/CubicChunks/issues")
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }*/
  229. }
  230. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  231. // !!!!!!!!!!!VERSIONS!!!!!!!!!!!!!
  232. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  233. fun getMcVersion(): String {
  234. return theForgeVersion.split("-")[0]
  235. }
  236. //returns version string according to this: http://mcforge.readthedocs.org/en/latest/conventions/versioning/
  237. //format: MCVERSION-MAJORMOD.MAJORAPI.MINOR.PATCH(-final/rcX/betaX)
  238. //rcX and betaX are not implemented yet
  239. fun getModVersion(): Any {
  240. return object {
  241. val v: String by lazy {
  242. try {
  243. val git = Grgit.open()
  244. val describe = org.ajoberstar.grgit.operation.DescribeOp(git.repository).call()
  245. val branch = getGitBranch(git)
  246. val snapshotSuffix = if (project.hasProperty("doRelease")) "" else "-SNAPSHOT"
  247. getModVersion(describe, branch) + snapshotSuffix;
  248. } catch (ex: RuntimeException) {
  249. logger.error("Unknown error when accessing git repository! Are you sure the git repository exists?", ex)
  250. String.format("%s-%s.%s.%s%s%s", getMcVersion(), "9999", "9999", "9999", "", "NOVERSION")
  251. }
  252. }
  253. override fun toString() = v
  254. }
  255. }
  256. fun getGitBranch(git: org.ajoberstar.grgit.Grgit): String {
  257. var branch: String = git.branch.current.name
  258. if (branch.equals("HEAD")) {
  259. branch = when {
  260. System.getenv("TRAVIS_BRANCH")?.isEmpty() == false -> // travis
  261. System.getenv("TRAVIS_BRANCH")
  262. System.getenv("GIT_BRANCH")?.isEmpty() == false -> // jenkins
  263. System.getenv("GIT_BRANCH")
  264. System.getenv("BRANCH_NAME")?.isEmpty() == false -> // ??? another jenkins alternative?
  265. System.getenv("BRANCH_NAME")
  266. else -> throw RuntimeException("Found HEAD branch! This is most likely caused by detached head state! Will assume unknown version!")
  267. }
  268. }
  269. if (branch.startsWith("origin/")) {
  270. branch = branch.substring("origin/".length)
  271. }
  272. return branch
  273. }
  274. fun getModVersion(describe: String, branch: String): String {
  275. if (branch.startsWith("MC_")) {
  276. val branchMcVersion = branch.substring("MC_".length)
  277. if (branchMcVersion != getMcVersion()) {
  278. logger.warn("Branch version different than project MC version! MC version: " +
  279. getMcVersion() + ", branch: " + branch + ", branch version: " + branchMcVersion)
  280. }
  281. }
  282. //branches "master" and "MC_something" are not appended to version sreing, everything else is
  283. //only builds from "master" and "MC_version" branches will actually use the correct versioning
  284. //but it allows to distinguish between builds from different branches even if version number is the same
  285. val branchSuffix = if (branch == "master" || branch.startsWith("MC_")) "" else ("-" + branch.replace("[^a-zA-Z0-9.-]", "_"))
  286. val baseVersionRegex = "v[0-9]+\\.[0-9]+"
  287. val unknownVersion = String.format("%s-UNKNOWN_VERSION%s%s", getMcVersion(), versionSuffix, branchSuffix)
  288. if (!describe.contains('-')) {
  289. //is it the "vX.Y" format?
  290. if (describe.matches(Regex(baseVersionRegex))) {
  291. return String.format("%s-%s.0.0%s%s", getMcVersion(), describe, versionSuffix, branchSuffix)
  292. }
  293. logger.error("Git describe information: \"$describe\" in unknown/incorrect format")
  294. return unknownVersion
  295. }
  296. //Describe format: vX.Y-build-hash
  297. val parts = describe.split("-")
  298. if (!parts[0].matches(Regex(baseVersionRegex))) {
  299. logger.error("Git describe information: \"$describe\" in unknown/incorrect format")
  300. return unknownVersion
  301. }
  302. if (!parts[1].matches(Regex("[0-9]+"))) {
  303. logger.error("Git describe information: \"$describe\" in unknown/incorrect format")
  304. return unknownVersion
  305. }
  306. val mcVersion = getMcVersion()
  307. val modAndApiVersion = parts[0].substring(1)
  308. //next we have commit-since-tag
  309. val commitSinceTag = Integer.parseInt(parts[1])
  310. val minorFreeze = if (versionMinorFreeze.isEmpty()) -1 else Integer.parseInt(versionMinorFreeze as String)
  311. val minor = if (minorFreeze < 0) commitSinceTag else minorFreeze
  312. val patch = if (minorFreeze < 0) 0 else (commitSinceTag - minorFreeze)
  313. return String.format("%s-%s.%d.%d%s%s", mcVersion, modAndApiVersion, minor, patch, versionSuffix, branchSuffix)
  314. }
  315. fun extractForgeMinorVersion(): String {
  316. // version format: MC_VERSION-MAJOR.MINOR.?.BUILD
  317. return theForgeVersion.split(Regex("-")).getOrNull(1)?.split(Regex("\\."))?.getOrNull(1)
  318. ?: throw RuntimeException("Invalid forge version format: $theForgeVersion")
  319. }
  320.