-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# This workflow will build a Java project with Gradle | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle | ||
|
||
name: Pull Request Check | ||
|
||
on: | ||
push: | ||
branches: | ||
- '**' | ||
pull_request: | ||
branches: | ||
- '**' | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: '17' | ||
- name: Cache Gradle User Files | ||
uses: actions/cache@v1 | ||
with: | ||
path: ~/.gradle | ||
key: ${{ runner.os }}-gradle-user-home | ||
- name: Cache Gradle Files | ||
uses: actions/cache@v1 | ||
with: | ||
path: ./.gradle | ||
key: ${{ runner.os }}-gradle-file | ||
- name: Grant execute permission for gradlew | ||
run: chmod +x gradlew | ||
- name: Build | ||
run: | | ||
./gradlew cleanBuild remapSpigotJar idea --no-daemon -i --stacktrace --refresh-dependencies | ||
./gradlew build collect --no-daemon -i --stacktrace | ||
- name: Upload Artifact | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: Arclight | ||
path: ./build/libs/*.jar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
buildSrc/src/main/groovy/io/izzel/arclight/gradle/tasks/UploadFilesTask.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package io.izzel.arclight.gradle.tasks | ||
|
||
import groovy.json.JsonOutput | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
import java.security.MessageDigest | ||
import java.util.function.Consumer | ||
|
||
abstract class UploadFilesTask extends DefaultTask { | ||
|
||
@Input | ||
abstract Property<String> getMcVersion() | ||
|
||
@Input | ||
abstract Property<String> getVersion() | ||
|
||
@Input | ||
abstract Property<Boolean> getSnapshot() | ||
|
||
@Input | ||
abstract Property<String> getGitHash() | ||
|
||
@Input | ||
abstract Property<String> getBranch() | ||
|
||
@TaskAction | ||
void run() { | ||
for (def file in inputs.files.asFileTree.files) { | ||
if (file.isFile()) { | ||
try { | ||
this.uploadOne(file) | ||
} catch (Exception e) { | ||
project.logger.error("Error uploading $file", e) | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static final String OBJECTS = "https://files.hypoglycemia.icu/v1/objects/%s" | ||
private static final String FILES = "https://files.hypoglycemia.icu/v1/files%s" | ||
|
||
private void uploadOne(File file) { | ||
def sha1 = sha1(file) | ||
def modloader = file.name.split('-')[1] | ||
project.logger.lifecycle("Uploading {}, sha1 {}", file.name, sha1) | ||
(new URL(OBJECTS.formatted(sha1)).openConnection() as HttpURLConnection).with { | ||
it.setRequestMethod("PUT") | ||
it.doOutput = true | ||
it.addRequestProperty("X-Lightning-Sha1", sha1) | ||
it.addRequestProperty("X-Lightning-Filename", file.name.replace(".jar", "-" + gitHash.get() + ".jar")) | ||
it.addRequestProperty("AuthToken", System.getenv().ARCLIGHT_FILES_TOKEN) | ||
it.addRequestProperty("Content-Type", "application/java-archive") | ||
it.addRequestProperty("Content-Length", Files.size(file.toPath()).toString()) | ||
it.setInstanceFollowRedirects(true) | ||
it.connect() | ||
using(it.outputStream) { | ||
Files.copy(file.toPath(), it) | ||
} | ||
if (it.responseCode != 200) { | ||
def reason = new String(it.inputStream.readAllBytes()) | ||
project.logger.error(reason) | ||
throw new Exception(reason) | ||
} | ||
} | ||
link("/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}/${modloader}", [type: 'object', value: sha1]) | ||
link("/arclight/branches/${branch.get()}/latest-snapshot", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}", cache_seconds: 3600]) | ||
if (!snapshot.get()) { | ||
link("/arclight/branches/${branch.get()}/versions-stable/${version.get()}/${modloader}", [type: 'object', value: sha1]) | ||
link("/arclight/branches/${branch.get()}/latest-stable", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-stable/${version.get()}", cache_seconds: 86400]) | ||
} | ||
} | ||
|
||
private static void link(String path, Object payload) { | ||
(new URL(FILES.formatted(path)).openConnection() as HttpURLConnection).with { | ||
it.setRequestMethod("PUT") | ||
it.doOutput = true | ||
it.addRequestProperty("Content-Type", "application/json") | ||
it.addRequestProperty("AuthToken", System.getenv().ARCLIGHT_FILES_TOKEN) | ||
it.connect() | ||
using(it.outputStream) { | ||
it.write(JsonOutput.toJson(payload).getBytes(StandardCharsets.UTF_8)) | ||
} | ||
if (it.responseCode != 200) { | ||
def reason = new String(it.inputStream.readAllBytes()) | ||
project.logger.error(reason) | ||
throw new Exception(reason) | ||
} | ||
} | ||
} | ||
|
||
static <T extends AutoCloseable> void using(T closeable, Consumer<T> consumer) { | ||
try { | ||
consumer.accept(closeable) | ||
} finally { | ||
closeable.close() | ||
} | ||
} | ||
|
||
static String sha1(File file) { | ||
MessageDigest md = MessageDigest.getInstance('SHA-1') | ||
file.eachByte 4096, { bytes, size -> | ||
md.update(bytes, 0 as byte, size) | ||
} | ||
return md.digest().collect { String.format "%02x", it }.join() | ||
} | ||
} |