Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Add new entrypoint (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquimmnetto authored Dec 1, 2022
1 parent a9b8fe6 commit 824865d
Show file tree
Hide file tree
Showing 14 changed files with 463 additions and 270 deletions.
52 changes: 52 additions & 0 deletions src/main/groovy/wooga/gradle/version/IVersionScheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package wooga.gradle.version;

import wooga.gradle.version.internal.release.base.VersionStrategy;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* Version schemes available for use with this plugin.
* Each scheme consists of at least 4 strategies (development, snapshot, pre-release and final)
* plus additional strategies that can be provided in getStrategies() and fetched with strategyFor(ReleaseStage).
*
* A default strategy can also be provided.
*/
//This has to be a java class (not groovy) because groovy 2 doesn't support java 8 default interface methods.
public interface IVersionScheme { //TODO: Rename to VersionScheme when breaking change
VersionStrategy getDevelopment();
VersionStrategy getSnapshot();
VersionStrategy getPreRelease();
//is not getFinal because final is a java keyword and this would mess with groovy's property invocation.
VersionStrategy getFinalStrategy();

/**
* @return All available strategies for this scheme. Heavily recomended to include development, snapshot,
* preRelease and Final here as well, put here all non-standard strategies that you wish to provide.
*/
List<VersionStrategy> getStrategies();
VersionStrategy getDefaultStrategy();

/**
* Finds a strategy in this scheme with the given ReleaseStage
* @param stage - ReleaseStage for the desired strategy
* @return the desired VersionStrategy or null if none are found.
*/
default VersionStrategy strategyFor(ReleaseStage stage) {
return getStrategies().stream().filter( it -> it.getReleaseStage() == stage).findFirst().orElse(null);
}

/**
* Finds the matching strategy for a given stage name and returns its ReleaseStage object.
* @param stageName - Desired stage name. Will be matched against the strategies in this scheme.
* @return ReleaseStage for the first strategy with a matching stage name.
*/
default ReleaseStage findStageForStageName(String stageName) {
return Arrays.stream(ReleaseStage.values()).filter(
candidateStage -> Optional
.ofNullable(strategyFor(candidateStage))
.map(strategy -> strategy.getStages().contains(stageName))
.orElse(false)
).findFirst().orElse(null);
}
}
23 changes: 22 additions & 1 deletion src/main/groovy/wooga/gradle/version/VersionCodeScheme.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@

package wooga.gradle.version

import org.ajoberstar.grgit.Grgit
import wooga.gradle.version.internal.VersionCode

enum VersionCodeScheme {
none, semver, semverBasic, releaseCountBasic, releaseCount
none({ -> 0}),
semverBasic({
String version, int offset -> VersionCode.generateSemverVersionCode(version) + offset
}),
semver({
String version, int offset -> VersionCode.generateSemverVersionCode(version, true) + offset
}),
releaseCountBasic({
Grgit git, int offset -> VersionCode.generateBuildNumberVersionCode(git, false, offset)
}),
releaseCount({
Grgit git, int offset -> VersionCode.generateBuildNumberVersionCode(git, true, offset)
})

final Closure<Integer> generate

VersionCodeScheme(Closure generate) {
this.generate = generate
}
}
55 changes: 44 additions & 11 deletions src/main/groovy/wooga/gradle/version/VersionPluginExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,37 @@ import com.wooga.gradle.BaseSpec
import org.ajoberstar.grgit.Grgit
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import wooga.gradle.version.internal.GitStrategyPicker
import wooga.gradle.version.internal.release.base.ReleaseVersion
import wooga.gradle.version.internal.release.base.TagStrategy
import wooga.gradle.version.internal.release.base.VersionStrategy
import wooga.gradle.version.internal.release.semver.ChangeScope
import wooga.gradle.version.internal.release.semver.VersionInferenceParameters

trait VersionPluginExtension implements BaseSpec {

/**
* @return The version scheme being used by the plugin (such as SemVer2)
*/
Property<VersionScheme> getVersionScheme() {
Property<IVersionScheme> getVersionScheme() {
versionScheme
}

private final Property<VersionScheme> versionScheme = objects.property(VersionScheme)
private final Property<IVersionScheme> versionScheme = objects.property(IVersionScheme)

void versionScheme(VersionScheme value) {
void versionScheme(IVersionScheme value) {
setVersionScheme(value)
}

void versionScheme(Provider<VersionScheme> value) {
void versionScheme(Provider<IVersionScheme> value) {
setVersionScheme(value)
}

void setVersionScheme(VersionScheme value) {
void setVersionScheme(IVersionScheme value) {
versionScheme.set(value)
}

void setVersionScheme(Provider<VersionScheme> value) {
void setVersionScheme(Provider<IVersionScheme> value) {
versionScheme.set(value)
}

Expand Down Expand Up @@ -306,7 +309,7 @@ trait VersionPluginExtension implements BaseSpec {
releaseStage
}

final Provider<ReleaseStage> releaseStage = providers.provider({
final Provider<ReleaseStage> releaseStage = providers.provider { ->
if (isDevelopment.getOrElse(false)) {
return ReleaseStage.Development
} else if (isSnapshot.getOrElse(false)) {
Expand All @@ -315,10 +318,40 @@ trait VersionPluginExtension implements BaseSpec {
return ReleaseStage.Prerelease
} else if (isFinal.getOrElse(false)) {
return ReleaseStage.Final
} else {
return versionScheme.flatMap{scheme ->
stage.map {stageName -> scheme.findStageForStageName(stageName) }
}.orNull
}
ReleaseStage.Unknown
})
}.orElse(ReleaseStage.Unknown)

final ReleaseStage defaultReleaseStage
}
/**
* Infers the next version for this project based on extension information and underlying git repository tags.
* @param scheme - version scheme that this project is following
* @param stageProvider - provider with the stage desired for the next version
* @param scopeProvider - provider with the ChangeScope desired for the next version
* @return Provider<ReleaseVersion> containing the inferred version,
* @throws org.gradle.api.internal.provider.MissingValueException if there is no configured git repository in this extension.
* or empty if none of the scheme strategies can be applied to the arguments.
*/
Provider<ReleaseVersion> inferVersion(IVersionScheme scheme, Provider<String> stageProvider = this.stage,
Provider<ChangeScope> scopeProvider = this.scope) {
def strategyProvider = pickStrategy(scheme, stageProvider)
strategyProvider.map { strategy ->
def inferParams = VersionInferenceParameters.fromExtension(this).with {
it.stage = stageProvider.orNull
it.scope = scopeProvider.orNull
return it
}
return strategy.infer(inferParams)
}
}

private Provider<VersionStrategy> pickStrategy(IVersionScheme scheme, Provider<String> stageProvider) {
return git.map {
new GitStrategyPicker(it).pickStrategy(scheme, stageProvider.orNull)
}.orElse( providers.provider{
throw new IllegalStateException("A git repository must be available in order to a strategy to be selected")
})
}
}
55 changes: 53 additions & 2 deletions src/main/groovy/wooga/gradle/version/VersionScheme.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,59 @@

package wooga.gradle.version

enum VersionScheme {
semver, semver2, staticMarker, wdk
import wooga.gradle.version.internal.release.base.VersionStrategy
import wooga.gradle.version.internal.release.semver.SemVerStrategy
import wooga.gradle.version.strategies.SemverV1Strategies
import wooga.gradle.version.strategies.SemverV2Strategies
import wooga.gradle.version.strategies.StaticMarkerStrategies
import wooga.gradle.version.strategies.WdkStrategies

//TODO: Rename to VersionSchemes when breaking change
/**
* Version schemes available for use with this plugin.
* Each scheme consists of at least 4 strategies (development, snapshot, pre-release and final)
* plus additional strategies if so desired. Check IVersionScheme for more details.
*/
enum VersionScheme implements IVersionScheme {
semver(SemverV1Strategies.DEVELOPMENT,
SemverV1Strategies.SNAPSHOT,
SemverV1Strategies.PRE_RELEASE,
SemverV1Strategies.FINAL),
semver2(SemverV2Strategies.DEVELOPMENT,
SemverV2Strategies.SNAPSHOT,
SemverV2Strategies.PRE_RELEASE,
SemverV2Strategies.FINAL),
/**
* Strategies for versions generated based on a static marker. Based on Semver v2.
*/
staticMarker(StaticMarkerStrategies.DEVELOPMENT,
StaticMarkerStrategies.SNAPSHOT,
StaticMarkerStrategies.PRE_RELEASE,
StaticMarkerStrategies.FINAL, StaticMarkerStrategies.PREFLIGHT),
/**
* Schema for Paket/Nuget WDK projects. See WdkNuGetStrategies for more details.
*/
wdk(WdkStrategies.DEVELOPMENT,
WdkStrategies.SNAPSHOT,
WdkStrategies.PRE_RELEASE,
WdkStrategies.FINAL, WdkStrategies.PREFLIGHT)

final VersionStrategy development
final VersionStrategy snapshot
final VersionStrategy preRelease
final VersionStrategy finalStrategy

final List<VersionStrategy> strategies
final VersionStrategy defaultStrategy

VersionScheme(VersionStrategy development, VersionStrategy snapshot, VersionStrategy preRelease, VersionStrategy finalStrategy, SemVerStrategy... others) {
this.development = development
this.snapshot = snapshot
this.preRelease = preRelease
this.finalStrategy = finalStrategy
this.defaultStrategy = development
this.strategies = [development, snapshot, preRelease, finalStrategy, *others]
}
}


Loading

0 comments on commit 824865d

Please sign in to comment.