aboutsummaryrefslogtreecommitdiff
path: root/publish/build.gradle
blob: 1a05d54efeb6d7ad9a113bef8a2f8ababc8dc976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
plugins {
	id "com.github.breadmoirai.github-release" version "2.2.12"
	id "com.matthewprenger.cursegradle" version "1.4.0"
	id "com.modrinth.minotaur" version "1.2.1"
}

import com.modrinth.minotaur.TaskModrinthUpload
import groovy.json.JsonSlurper
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

def getCommitVersion(){
	try {
		def commitHashProc = "python3 ${projectDir}/get_version.py".execute()
		commitHashProc.waitFor()
		if(commitHashProc.exitValue() == 0){
			def commitHash = commitHashProc.text.trim()
			
			return commitHash
		} else {
			println commitHashProc.err.text
			throw new Exception("get_version.py exited with non-zero return value")
		}
	} catch(Exception e){
		println "Failed to run get_version.py: " + e.getMessage()
	}
	return "UNKNOWN" // fallback
}

def buildVersion = getCommitVersion()
def changeLogFile = new File("${projectDir}/changelog.md")
def changeLog = changeLogFile.exists() ? changeLogFile.getText('UTF-8') : ""
ext.gameVersionsMap = new JsonSlurper().parseText(file("gameVersions.json").getText('UTF-8'))
ext.gameVersions = gameVersionsMap.keySet()

def githubOK = project.hasProperty("githubToken")
def curseOK = project.hasProperty("curseToken") && project.hasProperty("gameVersion")
def modrinthOK = project.hasProperty("modrinthToken") && project.hasProperty("gameVersion")

def debugPublish = project.hasProperty("debugPublish") && project.debugPublish.toBoolean()

def useVPrefixForTag = project.hasProperty("useVPrefixForTag") && project.useVPrefixForTag.toBoolean()

task build {}
task assemble {}

if(githubOK){
	githubRelease {
		token project.githubToken // This is your personal access token with Repo permissions
							 // You get this from your user settings > developer settings > Personal Access Tokens
		owner project.githubOwner // default is the last part of your group. Eg group: "com.github.breadmoirai" => owner: "breadmoirai"
		repo project.githubRepo // by default this is set to your project name
		tagName ((useVPrefixForTag ? "v" : "") + "${buildVersion}") // by default this is set to "v${project.version}"
		targetCommitish "master" // by default this is set to "master"
		releaseName "${project.releaseName} ${buildVersion}"
		body changeLog // by default this is empty
		draft false // by default this is false
		prerelease false // by default this is false
		releaseAssets getFiles() // this points to which files you want to upload as assets with your release

		overwrite false // by default false; if set to true, will delete an existing release with the same tag and name
		dryRun debugPublish // by default false; you can use this to see what actions would be taken without making a release
		apiEndpoint "https://api.github.com" // should only change for github enterprise users
		client new OkHttpClient.Builder()
			.writeTimeout(5, TimeUnit.MINUTES)
			.readTimeout(5, TimeUnit.MINUTES)
			.build() // Added because I kept getting SocketTimeoutExceptions
	}
} else {
	println("Not configuring GitHub publish because project arguments are missing.")
}

if(project.hasProperty("gameVersion")){
	def gameVersion = project.gameVersion
	def baseVersion = toBaseVersion(gameVersion)
	def files = getFiles(baseVersion)
	def shortest = null
	files.each {
		if(shortest == null || it.name.length() < shortest.name.length()){
			shortest = it
		}
	}
	def additionalFiles = files - shortest	

	if(curseOK) {
		curseforge {
			apiKey = project.curseToken
			project {
				id = project.curseID
				changelogType = 'markdown'
				changelog = changeLog
				releaseType = 'release'
				addGameVersion gameVersion
				addGameVersion "Forge"

				mainArtifact(file(shortest)) {
					displayName = "${releaseName} ${buildVersion} for Minecraft ${gameVersion}"
				}
				additionalFiles.each { addArtifact(it) }
			}
			options {
				debug = debugPublish
				javaIntegration = false
				forgeGradleIntegration = false
				javaVersionAutoDetect = false
			}
		}
	} else {
		println("Not configuring CurseForge publish because project arguments are missing.")
	}
	
	if(modrinthOK) {
		task publishModrinth (type: TaskModrinthUpload) {
			token = project.modrinthToken
			projectId = project.modrinthID
			versionNumber = "$gameVersion-$buildVersion"
			versionName = "$gameVersion-$buildVersion" // the doc says this defaults to the versionNumber, but it defaulted to the string "undefined" for me so i'm setting it
			uploadFile = shortest
			addGameVersion(gameVersion)
			additionalFiles.each { addFile(it) }
			addLoader('forge')
			changelog = changeLog
			detectLoaders = false
		}
	} else {
		println("Not configuring Modrinth publish because project arguments are missing.")
	}
}

def toBaseVersion(ver){
	return String.join(".", ver.tokenize(".").subList(0, 2))
}

def getVersionProjectPath(ver){
	if(project.versionedProjectDirectories.toBoolean()){
		return "${projectDir}/../${ver}"
	} else {
		return "${projectDir}/.."
	}
}

def getFiles(ver) {
	def files = []
	new File("${getVersionProjectPath(ver)}/build/libs").eachFile(groovy.io.FileType.FILES, {
		if(!(it.name.endsWith("-sources.jar") || it.name.endsWith("-dev.jar"))){
			files << it
		}
	})
	return files
}

def getFiles() {
	def files = []
	project.gameVersions.collect({toBaseVersion(it)}).each {
		files += getFiles(it)
	}
	return files
}

// for debug
task listFiles {
	doLast {
		project.gameVersions.collect({toBaseVersion(it)}).each {
			println("$it: ${getFiles(it)}")
		}
	}
}

def getBuildArgumentSets(ver) {
	def projectConfig = new Properties()
	projectConfig.load(file("../gradle.properties").newReader())
	return projectConfig.enable_mixin ? ["", "-Pnomixin"] : [""]
}

project.gameVersions.each { ver ->
	def baseVer = toBaseVersion(ver)
	def dir = getVersionProjectPath(baseVer)
	task ("cleanBuild-$baseVer", type: Exec) {
		workingDir dir
		commandLine 'sh', '-c', "rm -f build/libs/* && " + String.join(" && ", getBuildArgumentSets(ver).collect({"./gradlew build ${it}"}))
	}
}

task cleanBuildAll(dependsOn: project.gameVersions.collect({"cleanBuild-${toBaseVersion(it)}"})) {

}