Skip to content

Commit

Permalink
add support for newly initialized repos (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
nefilim authored Dec 22, 2021
1 parent 3729ad2 commit 10e0d4d
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 33 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/pushpr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: push and pr release

on:
push:
branches:
- main
- develop
paths-ignore:
- '**.md'
pull_request:
branches:
- develop
paths-ignore:
- '**.md'

jobs:
build-and-publish:
runs-on: ubuntu-latest

steps:
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'zulu'

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Restore Gradle cache
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build and Release Main
if: ${{ github.ref_name == 'main' }}
run: |
./gradlew --no-daemon clean build -Psemver.main.scope=patch -PgitHubTokenProp=${{ github.token }} -Pgradle.publish.key=${{ secrets.GRADLE_PUBLISH_KEY }} -Pgradle.publish.secret=${{ secrets.GRADLE_PUBLISH_SECRET }} publishPlugins githubRelease
- name: Build and Release Develop
if: ${{ github.ref_name == 'develop' }}
run: |
./gradlew --no-daemon clean build -Psemver.develop.stage=beta -PgitHubTokenProp=${{ github.token }} -Pgradle.publish.key=${{ secrets.GRADLE_PUBLISH_KEY }} -Pgradle.publish.secret=${{ secrets.GRADLE_PUBLISH_SECRET }}
- name: Build and Release Branch
if: ${{ github.ref_name != 'develop' && github.ref_name != 'main' }}
run: |
./gradlew --no-daemon clean build -Psemver.develop.stage=alpha -PgitHubTokenProp=${{ github.token }} -Pgradle.publish.key=${{ secrets.GRADLE_PUBLISH_KEY }} -Pgradle.publish.secret=${{ secrets.GRADLE_PUBLISH_SECRET }}
- name: Stop Gradle daemons
run: ./gradlew --stop
13 changes: 3 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ plugins {
`maven-publish`
alias(libs.plugins.gradlePluginPublish)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.semver)
alias(libs.plugins.githubrelease)
}

/*
* Project information
*/
group = "io.github.nefilim.gradle"
description = "Modified Git Flow based semver plugin"
version = "0.0.6"

inner class ProjectInfo {
val longName = "Gradle Semver Plugin"
Expand Down Expand Up @@ -102,12 +103,4 @@ pluginBundle {
website = info.website
vcsUrl = info.website
tags = info.tags
}

//val publishPlugins = tasks.findByPath(":plugin:publishPlugins")

//tasks.publishPlugins {
//// from(docsFileJar)
// dependsOn("publishPluginJar")
// dependsOn("publishPluginJavaDocsJar")
//}
}
9 changes: 7 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[versions]
kotest = "4.6.4"
kotlin = "1.6.10"

arrow = "1.0.1"
github-release = "2.2.12"
jgit = "6.0.0.202111291000-r"
kotest = "5.0.2"
semverCore = "0.1.0-beta.9"
semverPlugin = "0.0.6"

[libraries]
kotest-junit5-jvm = { module = "io.kotest:kotest-runner-junit5-jvm", version.ref = "kotest" }
Expand All @@ -17,4 +20,6 @@ kotlin-testing = [ "kotest-junit5-jvm", "kotest-assertions-core-jvm" ]

[plugins]
gradlePluginPublish = { id = "com.gradle.plugin-publish", version = "0.18.0" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
semver = { id = "io.github.nefilim.gradle.semver-plugin", version.ref = "semverPlugin" }
githubrelease = { id = "com.github.breadmoirai.github-release", version.ref = "github-release" }
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal fun SemVerPluginContext.calculatedVersionFlow(
val commitCount = git.commitsSinceBranchPoint(branchPoint, main).bind()
git.calculateDevelopBranchVersion(main, develop, tags).map {
it.getOrElse {
project.semverMessage("unable to determine last version, using initialVersion [${config.initialVersion}]")
project.semverMessage("unable to determine last version from main branch, using initialVersion [${config.initialVersion}]")
config.initialVersion
}.copy(stageNum = commitCount)
}.bind()
Expand Down Expand Up @@ -69,9 +69,6 @@ internal fun SemVerPluginContext.calculatedVersionFlow(
}).bind()
}
}
else -> {
SemVerError.UnsupportedBranch(currentBranch.refName).left()
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/io/github/nefilim/gradle/semver/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ internal fun SemVerPluginContext.generateVersionFile() {
createNewFile()
writeText(
"""
|$version
|${config.tagPrefix}$version
|
""".trimMargin()
|$version
|${config.tagPrefix}$version
|
""".trimMargin()
)
}
}
Expand Down
47 changes: 34 additions & 13 deletions src/main/kotlin/io/github/nefilim/gradle/semver/SemVerPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,45 @@ public class SemVerPlugin: Plugin<Project> {
target.semverMessage("semver for ${target.name}: ${target.version}")
}
} else {
target.semverMessage("semver plugin can't work if the project is not a git repository")
target.semverMessage("the current directory is not part of a git repo, cannot determine project semantic version number, please initialize a git repo with main & develop branches")
}
}
}

private fun SemVerPluginContext.calculateVersionFlow(): Either<SemVerError, Version> {
val allBranches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call().toList()
val mainRefName = allBranches.first { setOf(GitRef.MainBranch.RefName, GitRef.MainBranch.RemoteOriginRefName).contains(it.name) }.name
val developRefName = allBranches.first { setOf(GitRef.DevelopBranch.RefName, GitRef.DevelopBranch.RemoteOriginRefName).contains(it.name) }.name
project.logger.lifecycle("found main: $mainRefName, develop: $developRefName")
return either.eager {
val main = git.buildBranch(mainRefName, config).bind() as GitRef.MainBranch
val develop = git.buildBranch(developRefName, config).bind() as GitRef.DevelopBranch
val current = git.buildBranch(repository.fullBranch, config).bind()
calculatedVersionFlow(
main,
develop,
current,
).bind()
val allBranches = Either.catch { git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call().toList() }.mapLeft { SemVerError.Git(it) }.bind()
val mainRefName = allBranches.firstOrNull { setOf(GitRef.MainBranch.RefName, GitRef.MainBranch.RemoteOriginRefName).contains(it.name) }?.name
val developRefName = allBranches.firstOrNull { setOf(GitRef.DevelopBranch.RefName, GitRef.DevelopBranch.RemoteOriginRefName).contains(it.name) }?.name
when {
mainRefName == null -> {
missingRequiredBranch(GitRef.MainBranch.Name)
config.initialVersion
}
developRefName == null -> {
missingRequiredBranch(GitRef.DevelopBranch.Name)
config.initialVersion
}
else -> {
project.logger.lifecycle("found main: $mainRefName, develop: $developRefName")
val main = git.buildBranch(mainRefName, config).bind() as GitRef.MainBranch
val develop = git.buildBranch(developRefName, config).bind() as GitRef.DevelopBranch
val current = git.buildBranch(repository.fullBranch, config).bind()
calculatedVersionFlow(
main,
develop,
current,
).bind()
}
}
}
}

private fun SemVerPluginContext.missingRequiredBranch(branchName: String) {
project.logger.warn("""
|could not find [$branchName] branch, defaulting to initial version: ${config.initialVersion}
| please create the following required branches:
| main
| ∟ develop
""".trimMargin())
}

0 comments on commit 10e0d4d

Please sign in to comment.