diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml index 8fc3faae..ef172d9d 100644 --- a/.github/workflows/release-drafter.yml +++ b/.github/workflows/release-drafter.yml @@ -24,4 +24,73 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - commitish: main \ No newline at end of file + commitish: main + + update_version: + needs: update_release_draft + permissions: + contents: write + pull-requests: write + runs-on: ubuntu-latest + timeout-minutes: 30 + + steps: + - name: Get latest draft release title + run: | + TITLE=$(gh api repos/${{ github.repository }}/releases \ + --jq '.[] | select(.draft==true) | .name' | head -n 1) + if [[ -z "$TITLE" ]]; then + echo "No draft release found." + exit 1 + fi + VERSION=$(echo "$TITLE" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') + if [[ -z "$VERSION" ]]; then + echo "No version found in version." + exit 1 + fi + echo "Extracted version: $VERSION" + echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: main + + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: zulu + java-version: 17 + + # Setup Gradle + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v4 + with: + cache-read-only: true + + - name: Update Version + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + ./gradlew replaceDraftVersion -PdraftVersion=$RELEASE_VERSION + + - name: Commit New Version + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + git config user.email "action@github.com" + git config user.name "GitHub Action" + git pull + git add src/main/kotlin/org/domaframework/doma/intellij/common/PluginUtil.kt + git add src/main/resources/logback.xml + git add src/main/resources/logback-test.xml + git add gradle.properties + if [[ -z "$(git status --porcelain)" ]]; then + echo "No changes to commit." + exit 0 + fi + git commit -am "Update Version With Release Draft - $REPLACE_VERSION" + git push origin main diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 256c24b5..cfcddc3d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: # Prepare and publish the plugin to JetBrains Marketplace repository release: name: Publish Plugin - runs-on: ubuntu-24.04 + runs-on: ubuntu-latest permissions: contents: write pull-requests: write @@ -57,4 +57,41 @@ jobs: - name: Upload Release Asset env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: gh release upload ${{ github.event.release.tag_name }} ./build/distributions/* \ No newline at end of file + run: gh release upload ${{ github.event.release.tag_name }} ./build/distributions/* + + - name: Update Version + run: | + ./gradlew replaceNewVersion -PnewVersion=${{ github.event.release.tag_name }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Commit New Version + run: | + git config user.email "action@github.com" + git config user.name "GitHub Action" + git fetch + git checkout main + git add src/main/kotlin/org/domaframework/doma/intellij/common/PluginUtil.kt + git add src/main/resources/logback.xml + git add src/main/resources/logback-test.xml + git add gradle.properties + if [[ -z "$(git status --porcelain)" ]]; then + echo "No changes to commit." + exit 0 + fi + git commit -am "Update Version With Release - $REPLACE_VERSION" + git push origin main + + create_release_draft: + permissions: + contents: write + pull-requests: write + runs-on: ubuntu-latest + timeout-minutes: 30 + + steps: + - uses: release-drafter/release-drafter@v6 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + commitish: main diff --git a/build.gradle.kts b/build.gradle.kts index ad9ed4f2..b5ef726c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -544,3 +544,88 @@ spotless { endWithNewline() } } + +val encoding: String by project + +fun replaceVersionInPluginUtil(ver: String) { + ant.withGroovyBuilder { + "replaceregexp"( + "match" to """(const val PLUGIN_VERSION = ")(\d+\.\d+\.\d+)((?:-beta)*)""", + "replace" to "\\1$ver", + "encoding" to encoding, + "flags" to "g", + ) { + "fileset"("dir" to ".") { + "include"("name" to "**/PluginUtil.kt") + } + } + } +} + +fun replaceVersionGradleProperty(ver: String) { + ant.withGroovyBuilder { + "replaceregexp"( + "match" to """(pluginVersion = )(\d+\.\d+\.\d+)((?:-beta)*)""", + "replace" to "\\1$ver", + "encoding" to encoding, + "flags" to "g", + ) { + "fileset"("dir" to ".") { + "include"("name" to "**/gradle.properties") + } + } + } +} + +fun replaceVersionInLogSetting(ver: String) { + ant.withGroovyBuilder { + "replaceregexp"( + "match" to """(org.domaframework.doma.intellij.plugin.version:-)(\d+\.\d+\.\d+)((?:-beta)*)(})""", + "replace" to "\\1$ver\\4", + "encoding" to encoding, + "flags" to "g", + ) { + "fileset"("dir" to ".") { + "include"("name" to "**/logback.xml") + "include"("name" to "**/logback-test.xml") + } + } + } +} + +fun replaceVersion(ver: String) { + checkNotNull(ver) + replaceVersionInPluginUtil(ver) + replaceVersionGradleProperty("$ver-beta") + replaceVersionInLogSetting(ver) + + val githubEnv = System.getenv("GITHUB_ENV") + val envFile = File(githubEnv) + envFile.appendText("REPLACE_VERSION=$ver\n") +} + +tasks.register("replaceNewVersion") { + doLast { + val releaseVersion = project.properties["newVersion"]?.toString() ?: "0.0.0" + val lastVersions = releaseVersion.substringAfter("v").split(".") + val major = lastVersions[0].toInt() + val minor = lastVersions[1].toInt() + val patch = lastVersions[2].toInt() + 1 + + val newVersion = "$major.$minor.$patch" + println("Release newVersion: $newVersion") + replaceVersion(newVersion) + } +} + +tasks.register("replaceDraftVersion") { + doLast { + val draftVersion = + project.properties["draftVersion"] + .toString() + .substringBefore("-beta") + + println("Release DraftVersion: $draftVersion") + replaceVersion(draftVersion) + } +} diff --git a/gradle.properties b/gradle.properties index 10c14bd8..de2f9cfe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,3 +20,5 @@ org.gradle.configuration-cache = true org.gradle.caching = true updateSinceUntilBuild = false + +encoding = UTF-8