diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..6d847a5 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,82 @@ +name: Build and Publish Release +on: + push: + tags: + - '*' +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + cache: gradle + # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. + # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md + - name: Setup Gradle + uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 + # build + - name: Build with Gradle Wrapper + run: ./gradlew assemble + env: + MVNDI_MVN_USER: ${{ secrets.MVNDI_MVN_USER }} + MVNDI_MVN_KEY: ${{ secrets.MVNDI_MVN_KEY }} + - name: Publish to Remote + run: ./gradlew publish + env: + MVNDI_MVN_USER: ${{ secrets.MVNDI_MVN_USER }} + MVNDI_MVN_KEY: ${{ secrets.MVNDI_MVN_KEY }} + + - name: Get version & release name + id: version + run: | + echo "version=$(./gradlew -q echoVersion)" >> $GITHUB_OUTPUT + echo "releaseName=$(./gradlew -q echoReleaseName)" >> $GITHUB_OUTPUT + + - name: Print version & release name + run: | + echo "Version: ${{ steps.version.outputs.version }}" + echo "Release Name: ${{ steps.version.outputs.releaseName }}" + + + - name: Create an empty CHANGELOG.md if missing + run: | + if [ ! -f CHANGELOG.md ]; then + touch CHANGELOG.md + fi + + - name: Read the first lines of CHANGELOG.md from gradle + id: changelog + run: | + { + echo "changelog<> $GITHUB_OUTPUT + + + - name: Create Release + id: createRelease + uses: ncipollo/release-action@v1.14.0 + with: + allowUpdates: true + updateOnlyUnreleased: true + artifacts: build/libs/${{ github.event.repository.name }}-${{ steps.version.outputs.version }}.jar + token: ${{ secrets.GITHUB_TOKEN }} + tag: ${{ steps.version.outputs.version }} + name: ${{ steps.version.outputs.releaseName }} + body: ${{ steps.changelog.outputs.changelog }} + # prerelease: ${{ !startsWith(github.ref, 'refs/tags/') }} #Always release for now + skipIfReleaseExists: true + + # - name: Publish to hangar & modrinth + # env: + # # Make sure you have added the repository secrets in the repository's settings + # HANGAR_API_TOKEN: ${{ secrets.HANGAR_API_TOKEN }} + # MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }} + # run: ./gradlew assemble publishPluginPublicationToHangar modrinth \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 86a6685..069df99 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,6 +43,12 @@ tasks.named("shadowJar") { archiveClassifier = null; } +publishing { + publications.create("maven") { + from(components["java"]) + } +} + tasks { assemble { dependsOn(shadowJar) @@ -78,3 +84,83 @@ java { toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) } } + +tasks.register("echoVersion") { + group = "documentation" + description = "Displays the version." + doLast { + println("${project.version}") + } +} + +tasks.register("echoReleaseName") { + group = "documentation" + description = "Displays the release name." + doLast { + println("${project.version} [${supportedMinecraftVersions}]") + } +} + +val extractChangelog = tasks.register("extractChangelog") { + group = "documentation" + description = "Extracts the changelog for the current project version from CHANGELOG.md, including the version header." + + val changelog: Property = project.objects.property(String::class) + outputs.upToDateWhen { false } + + doLast { + val version = project.version.toString() + val changelogFile = project.file("CHANGELOG.md") + + if (!changelogFile.exists()) { + println("CHANGELOG.md not found.") + changelog.set("No changelog found.") + return@doLast + } + + val lines = changelogFile.readLines() + val entries = mutableListOf() + var foundVersion = false + + for (line in lines) { + when { + // Include the version line itself + line.trim().equals("# $version", ignoreCase = true) -> { + foundVersion = true + entries.add(line) + } + // Stop collecting at the next version header + foundVersion && line.trim().startsWith("# ") -> break + // Collect lines after the version header + foundVersion -> entries.add(line) + } + } + + val result = if (entries.isEmpty()) { + "Update to $version." + } else { + entries.joinToString("\n").trim() + } + + // println("Changelog for version $version:\n$result") + changelog.set(result) + } + + // Make changelog accessible from other tasks + extensions.add(Property::class.java, "changelog", changelog) +} + +tasks.register("echoLatestVersionChangelog") { + group = "documentation" + description = "Displays the latest version change." + + dependsOn(tasks.named("extractChangelog")) + + doLast { + println((extractChangelog.get().extensions.findByType(Property::class.java) as Property).get()) + } +} + + +val versionString: String = version as String +val isRelease: Boolean = !versionString.contains("SNAPSHOT") \ No newline at end of file