diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8669620e..26d6f27b 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -8,7 +8,16 @@ jobs: deploy: runs-on: ubuntu-latest steps: + # Set up Github actions, JDK & Gradle - uses: actions/checkout@v2 + - name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: 21 + cache: gradle + distribution: temurin + - name: Grant execute permission for gradlew + run: chmod +x gradlew # Load the variables needed - name: Load gradle.properties into the environment @@ -31,16 +40,8 @@ jobs: run: | echo "MOD_VERSION=$(echo "$GITHUB_RELEASE_TAG_NAME" | sed -E 's/-[^-]+$//')" >> $GITHUB_ENV echo "RELEASE_TYPE=$(echo "$GITHUB_RELEASE_TAG_NAME" | grep -oE '(alpha|beta)' || echo "release")" >> $GITHUB_ENV - { - echo "MOD_CHANGELOG_TRIMMED_DISCORD<> $GITHUB_ENV - { - echo "MOD_CHANGELOG_TRIMMED_PUBLISH<> $GITHUB_ENV + - name: Build changelog + run: ./gradlew buildChangelog - name: Finalize variables run: | echo "MOD_VERSION_SHORT=${MOD_VERSION%%-*}" >> $GITHUB_ENV @@ -53,14 +54,6 @@ jobs: cat prepared_discord_message.json # Build the mod and publish it to ModMaven, CurseForge, and Modrinth - - name: Set up JDK 21 - uses: actions/setup-java@v3 - with: - java-version: 21 - cache: gradle - distribution: temurin - - name: Grant execute permission for gradlew - run: chmod +x gradlew - name: Build with Gradle run: ./gradlew build - name: Publish to ModMaven diff --git a/build.gradle b/build.gradle index 1dbac450..61a881b9 100644 --- a/build.gradle +++ b/build.gradle @@ -78,6 +78,62 @@ task sourceJar(type: Jar) { from "LICENSE" } +static def trimChangelog(String changelog, int maxLength, boolean discordFormat) { + if(discordFormat) { + changelog = changelog.replaceAll(/## What's Changed\n/, '') + + changelog = changelog.replaceAll(/## New Contributors/, '**New Contributors**') + } + + changelog = changelog.replaceAll(/@([a-zA-Z0-9_]+)/, '`@$1`') + + changelog = changelog.replaceAll(/https:\/\/github.com\/([^ ]+)\/pull\/([0-9]+)/, '[#\$2](https://github.com/\$1/pull/\$2)') + + changelog = changelog.replaceAll(/\*\*Full Changelog\*\*: (https:\/\/github.com\/[^ ]+\/compare\/[^\s]+)/, '[Full Changelog](\$1)') + + if(discordFormat) { + changelog = changelog.replaceAll(/\r?\n/, '\\\\n') + changelog = changelog.replaceAll(/\\\\n\\\\n/, '\\\\n') + } + + if(maxLength > 0) { + def changelogLines = changelog.split('\\\\n') + def lastLine = changelogLines[-1] + def trimmedChangelog = [] + def currentLength = lastLine.length() + + for (int i = 0; i < changelogLines.size() - 1; i++) { + def line = changelogLines[i] + if (currentLength + line.length() + 2 + 5 > maxLength) { + trimmedChangelog << '...' + break + } + trimmedChangelog << line + currentLength += line.length() + 2 + } + + trimmedChangelog << lastLine + + return trimmedChangelog.join('\\n') + } else { + return changelog + } +} + +tasks.register('buildChangelog') { + doLast { + def githubEnvFile = new File(System.getenv("GITHUB_ENV")) + + def changelog = System.getenv("MOD_CHANGELOG") + + def changelogTrimmedDiscord = trimChangelog(changelog, 1024, true) + githubEnvFile.append("MOD_CHANGELOG_TRIMMED_DISCORD=${changelogTrimmedDiscord}\n") + + def changelogTrimmedPublish = trimChangelog(changelog, 0, false) + githubEnvFile.append("MOD_CHANGELOG_TRIMMED_PUBLISH=${changelogTrimmedPublish}\n") + } +} + publishing { publications { mavenJava(MavenPublication) {