1+ import java.util.regex.Pattern
2+ import groovy.json.JsonSlurper
3+
4+ import org.apache.http.entity.mime.MultipartEntityBuilder ;
5+ import org.apache.http.entity.mime.content.FileBody ;
6+ import org.apache.http.entity.mime.content.StringBody ;
7+ import groovyx.net.http.ContentType
8+ import groovyx.net.http.HTTPBuilder
9+ import groovyx.net.http.Method
10+
111buildscript {
212 repositories {
313 jcenter()
414 maven { url = " http://files.minecraftforge.net/maven" }
15+ maven { url " https://plugins.gradle.org/m2/" }
516 }
617 dependencies {
718 classpath ' net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
19+ classpath " org.codehaus.groovy.modules.http-builder:http-builder:0.7"
20+ classpath " org.apache.httpcomponents:httpmime:4.5.1"
821 }
922}
1023
@@ -13,11 +26,12 @@ plugins {
1326}
1427
1528apply plugin : ' net.minecraftforge.gradle.forge'
29+
1630// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
1731
18- version = " 1.5.6 "
19- group = " com.blakebr0.extendedcrafting "
20- archivesBaseName = " ExtendedCrafting-1.12.2 "
32+ group = " omnifactorydevs.extendedcrafting " // http://maven.apache.org/guides/mini/guide-naming-conventions.html
33+ version = getRewrittenVersion()
34+ archivesBaseName = project_artifact_name
2135
2236sourceCompatibility = targetCompatibility = ' 1.8' // Need this here so eclipse task generates correctly.
2337compileJava {
@@ -27,10 +41,12 @@ compileJava {
2741minecraft {
2842 version = " 1.12.2-14.23.5.2847"
2943 runDir = " run"
30- replace ' ${version}' , project. version
3144
3245 mappings = " snapshot_20171003"
3346 makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
47+
48+ replace " GRADLE:VERSION" , project. version
49+ replace " GRADLE:MODNAME" , project_fancy_name
3450}
3551
3652repositories {
@@ -71,15 +87,191 @@ processResources {
7187 }
7288}
7389
74- task copyExamples {
75- def scripts = new File (minecraft. runDir, " scripts" )
76- scripts. mkdirs()
77- scripts. eachFile {
78- file -> file. delete()
90+ /*
91+ Fetches the last tag associated with the current branch.
92+ */
93+ def getLastTag () {
94+ return run(" git describe --abbrev=0 --tags " +
95+ (System . env[" TRAVIS_TAG" ] ? run(" git rev-list --tags --skip=1 --max-count=1" ) : " " )
96+ )
97+ }
98+
99+ /*
100+ Runs a command and returns the output.
101+ */
102+ def run (command ) {
103+ def process = command. execute()
104+ def outputStream = new StringBuffer ();
105+ def errorStream = new StringBuffer ();
106+ process. waitForProcessOutput(outputStream, errorStream)
107+
108+ errorStream. toString(). with {
109+ if (it) {
110+ throw new GradleException (" Error executing ${ command} :\n > ${ it} " )
111+ }
79112 }
80- new File (" tests" ). eachFileRecurse {
81- file -> if (file. isFile()) new File (scripts, file. getName()) << file. text
113+
114+ return outputStream. toString(). trim()
115+ }
116+
117+ /*
118+ Rewrites the version.
119+ */
120+ def getRewrittenVersion () {
121+ def lastTag = getLastTag()
122+
123+ if (System . env[' TRAVIS_TAG' ]) {
124+ return System . env[' TRAVIS_TAG' ]
125+ } else {
126+ def commitNo = run " git rev-list ${ lastTag} ..HEAD --count"
127+
128+ return lastTag + " ." + commitNo
129+ }
130+ }
131+
132+ task generateChangelog {
133+ onlyIf {
134+ System . env[' TRAVIS' ]
135+ }
136+
137+ doLast {
138+ /*
139+ Create a comprehensive changelog.
140+ */
141+ def lastTag = getLastTag()
142+
143+ def changelog = (run([
144+ " git"
145+ , " log"
146+ , " --date=format:%d %b %Y"
147+ , " --pretty=%s - **%an** (%ad)"
148+ , " ${ lastTag} ..HEAD"
149+ ]. plus(
150+ /*
151+ Collect relevant directories only, them being:
152+ * ./src/main/java
153+ * ./src/main/resources
154+ */
155+ sourceSets. main. java. srcDirs
156+ .plus(sourceSets. main. resources. srcDirs)
157+ .collect { [ " --" , it ] }
158+ ). flatten()))
159+
160+ if (changelog) {
161+ changelog = " Changes since ${ lastTag} :\n ${ ("\n" + changelog).replaceAll("\n", "\n* ")} "
162+ }
163+
164+ def f = new File (" build/tmp/changelog.md" )
165+ f. write(changelog ?: " " , " UTF-8" )
82166 }
83167}
84168
85- runClient. dependsOn(tasks. copyExamples)
169+ compileJava. dependsOn generateChangelog
170+
171+ task deployCurseForge {
172+ doLast {
173+ def final CURSEFORGE_ENDPOINT = " https://minecraft.curseforge.com/"
174+
175+ /*
176+ Helper function for checking environmental variables.
177+ */
178+ def final checkVariables = { variables ->
179+ for (vari in variables) {
180+ if (! System . env[vari]) {
181+ throw new GradleException (" Environmental variable ${ vari} is unset" )
182+ }
183+ }
184+ }
185+
186+ checkVariables([
187+ ' CURSEFORGE_API_TOKEN' , ' CURSEFORGE_PROJECT_ID'
188+ ])
189+
190+ /*
191+ Helper function for fetching JSON data.
192+ */
193+ def final fetch = { url , headers = null ->
194+ def connection = new URL (url). openConnection();
195+ connection. setRequestProperty(" X-Api-Token" , System . env[" CURSEFORGE_API_TOKEN" ])
196+ if (headers != null ) {
197+ for (header in headers) {
198+ connection. setRequestProperty(headers[1 ], headers[2 ])
199+ }
200+ }
201+
202+ def code = connection. getResponseCode()
203+ if (code == 200 ) {
204+ return new JsonSlurper (). parseText(connection. getInputStream(). getText())
205+ } else {
206+ throw new GradleException (" Fetch failed with code ${ code} (${ url} )" )
207+ }
208+ }
209+
210+ /*
211+ Fetch the list of Minecraft versions from CurseForge.
212+ */
213+ def targetVersion = project. minecraft. version
214+ def version = fetch(CURSEFORGE_ENDPOINT + " api/game/versions" ). with {
215+ def v = it. find { it. name == targetVersion }
216+
217+ if (! v) {
218+ throw new GradleException (" Version ${ project.minecraft.version} not found on CurseForge" )
219+ }
220+
221+ return v
222+ }
223+
224+ /*
225+ Fill the papers for CurseForge.
226+ */
227+ def metadata = new groovy.json.JsonBuilder (). with {
228+ def versions = [ version. id ]
229+
230+ /*
231+ CurseForge is dumb, so we'll have to prepend two spaces
232+ to each newline. This is disgusting, but it works.
233+ */
234+ def log = new File (" build/tmp/changelog.md" )
235+ .getText(" UTF-8" )
236+ .replaceAll(" \n " , " \n " )
237+ .replaceAll(" \n\\ *" , " \n •" )
238+
239+ def root = it {
240+ changelog log
241+ changelogType " markdown"
242+
243+ releaseType System . env[' TRAVIS_TAG' ] ? " release" : " beta"
244+ gameVersions versions
245+
246+ // Defined in gradle.properties
247+ displayName " ${ project_fancy_name} ${ project.version} "
248+ }
249+
250+ if (project_curseforge_dependencies) {
251+ def data = new JsonSlurper (). parseText(project_curseforge_dependencies)
252+
253+ root << [relations : [ projects : data ]]
254+ }
255+
256+ return it. toString()
257+ }
258+
259+ /*
260+ Upload the artifact to CurseForge.
261+ */
262+ new HTTPBuilder (CURSEFORGE_ENDPOINT ). request(Method . POST ) { req ->
263+ requestContentType = " multipart/form-data"
264+ headers[" X-Api-Token" ] = System . env[" CURSEFORGE_API_TOKEN" ]
265+ uri. path = " api/projects/${ System.env["CURSEFORGE_PROJECT_ID"]} /upload-file"
266+
267+ req. entity = new MultipartEntityBuilder (). with {
268+ addPart(" file" , new FileBody (
269+ new File (" ${ project.jar.destinationDir} /${ project.jar.archiveName} " )
270+ ))
271+ addPart(" metadata" , new StringBody (metadata))
272+
273+ return it. build()
274+ }
275+ }
276+ }
277+ }
0 commit comments