Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrawford-figure committed Mar 25, 2024
1 parent acffbc4 commit bcb8946
Show file tree
Hide file tree
Showing 25 changed files with 498 additions and 452 deletions.
14 changes: 6 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ dependencies {
// Leak semver library users of this plugin so that they can implement their own versionModifier strategy
// TODO: For v2, remove need for semver jar for consumers. They shouldn't need to know about this detail
listOf(
libs.swiftzer.semver
libs.swiftzer.semver,
).forEach {
api(it)
}

listOf(
gradleTestKit(),
libs.bundles.kotest
libs.bundles.kotest,
).forEach {
testImplementation(it)
}
Expand All @@ -83,7 +83,7 @@ tasks.withType<KotlinCompile>().configureEach {
freeCompilerArgs.addAll(
"-version",
"-Xjsr305=strict",
"-opt-in=kotlin.RequiresOptIn"
"-opt-in=kotlin.RequiresOptIn",
)
verbose.set(true)
}
Expand Down Expand Up @@ -127,13 +127,11 @@ githubRelease {
token(githubTokenValue)
}

ktlint {
disabledRules.set(setOf("trailing-comma-on-declaration-site", "trailing-comma-on-call-site"))
}

tasks.wrapper {
distributionType = Wrapper.DistributionType.ALL
}

logger.lifecycle("JDK toolchain version: ${java.toolchain.languageVersion.get()}")
logger.lifecycle("Kotlin version: ${extensions.findByType<org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension>()?.coreLibrariesVersion}")
logger.lifecycle(
"Kotlin version: ${extensions.findByType<org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension>()?.coreLibrariesVersion}",
)
153 changes: 79 additions & 74 deletions src/main/kotlin/com/figure/gradle/semver/SemverExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,98 +23,103 @@ import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

abstract class SemverExtension @Inject constructor(
objects: ObjectFactory,
project: Project,
providerFactory: ProviderFactory,
) {
/**
* This version invocation takes place at project build time of the project that is utilizing this plugin
* The version is not calculated until a build happens that requires `semver.version`
*/
val version: String by lazy {
providerFactory.gitCalculateSemverProvider(
gitDir = gitDir,
tagPrefix = tagPrefix,
initialVersion = initialVersion.get().toString(),
overrideVersion = overrideVersion.orNull?.toString(),
versionStrategy = versionStrategy,
versionModifier = versionModifier,
).get()
}
abstract class SemverExtension
@Inject
constructor(
objects: ObjectFactory,
project: Project,
providerFactory: ProviderFactory,
) {
/**
* This version invocation takes place at project build time of the project that is utilizing this plugin
* The version is not calculated until a build happens that requires `semver.version`
*/
val version: String by lazy {
providerFactory.gitCalculateSemverProvider(
gitDir = gitDir,
tagPrefix = tagPrefix,
initialVersion = initialVersion.get().toString(),
overrideVersion = overrideVersion.orNull?.toString(),
versionStrategy = versionStrategy,
versionModifier = versionModifier,
).get()
}

val versionTagName: String by lazy { calculateVersionTagName() }
val versionTagName: String by lazy { calculateVersionTagName() }

// TODO: For v2, see if rootRepoDirectory can be specified instead (without causing Gradle issues) where
// a semver task relies on the build task to complete. Maybe needs specified as a string instead of File to
// work? Use Git.open(rootRepoDirectory) to create the Git object then.
internal val gitDir: Property<String> =
objects.property<String>()
.convention("${project.rootProject.rootDir.path}/.git")
// TODO: For v2, see if rootRepoDirectory can be specified instead (without causing Gradle issues) where
// a semver task relies on the build task to complete. Maybe needs specified as a string instead of File to
// work? Use Git.open(rootRepoDirectory) to create the Git object then.
internal val gitDir: Property<String> =
objects.property<String>()
.convention("${project.rootProject.rootDir.path}/.git")

private val tagPrefix: Property<String> =
objects.property<String>()
.convention(VersionCalculatorConfig.DEFAULT_TAG_PREFIX)
private val tagPrefix: Property<String> =
objects.property<String>()
.convention(VersionCalculatorConfig.DEFAULT_TAG_PREFIX)

private val initialVersion: Property<SemVer> =
objects.property<SemVer>()
.convention(VersionCalculatorConfig.DEFAULT_VERSION)
private val initialVersion: Property<SemVer> =
objects.property<SemVer>()
.convention(VersionCalculatorConfig.DEFAULT_VERSION)

private val versionStrategy: ListProperty<BranchMatchingConfiguration> =
objects.listProperty<BranchMatchingConfiguration>()
.convention(null)
private val versionStrategy: ListProperty<BranchMatchingConfiguration> =
objects.listProperty<BranchMatchingConfiguration>()
.convention(null)

private val overrideVersion: Property<SemVer> =
objects.property<SemVer>()
.convention(null)
private val overrideVersion: Property<SemVer> =
objects.property<SemVer>()
.convention(null)

private val versionModifier: Property<VersionModifier> =
objects.property<VersionModifier>()
.convention { nextPatch() }
private val versionModifier: Property<VersionModifier> =
objects.property<VersionModifier>()
.convention { nextPatch() }

fun gitDir(gitDir: String) {
this.gitDir.set(gitDir)
}
fun gitDir(gitDir: String) {
this.gitDir.set(gitDir)
}

fun tagPrefix(prefix: String) {
if (overrideVersion.orNull != null) {
throw IllegalArgumentException(
"""
fun tagPrefix(prefix: String) {
if (overrideVersion.orNull != null) {
throw IllegalArgumentException(
"""
|Cannot set semver tagPrefix after override version has been set.
| The override version depends on the tagPrefix. Set the tagPrefix first.
""".trimMargin().replace("\n", "")
)
""".trimMargin().replace("\n", ""),
)
}
this.tagPrefix.set(prefix)
}
this.tagPrefix.set(prefix)
}

fun initialVersion(version: String?) {
version?.also {
this.initialVersion.set(SemVer.parse(it))
fun initialVersion(version: String?) {
version?.also {
this.initialVersion.set(SemVer.parse(it))
}
}
}

fun overrideVersion(version: String) {
this.overrideVersion.set(possiblyPrefixVersion(version, tagPrefix.get()))
}
fun overrideVersion(version: String) {
this.overrideVersion.set(possiblyPrefixVersion(version, tagPrefix.get()))
}

fun versionModifier(modifier: VersionModifier) {
this.versionModifier.set(modifier)
}
fun versionModifier(modifier: VersionModifier) {
this.versionModifier.set(modifier)
}

fun buildVersionModifier(modifier: String): VersionModifier {
return versionModifierFromString(modifier)
}
fun buildVersionModifier(modifier: String): VersionModifier {
return versionModifierFromString(modifier)
}

fun versionCalculatorStrategy(strategy: VersionCalculatorStrategy) {
this.versionStrategy.set(strategy)
}
fun versionCalculatorStrategy(strategy: VersionCalculatorStrategy) {
this.versionStrategy.set(strategy)
}

private fun calculateVersionTagName(): String {
return tagPrefix.map { prefix -> "$prefix$version" }.get()
}
private fun calculateVersionTagName(): String {
return tagPrefix.map { prefix -> "$prefix$version" }.get()
}

private fun possiblyPrefixVersion(version: String, prefix: String): SemVer {
return SemVer.parse(version.trimMargin(prefix)) // fail fast, don't let an invalid version propagate to runtime
private fun possiblyPrefixVersion(
version: String,
prefix: String,
): SemVer {
return SemVer.parse(version.trimMargin(prefix)) // fail fast, don't let an invalid version propagate to runtime
}
}
}
Loading

0 comments on commit bcb8946

Please sign in to comment.