Skip to content

Commit

Permalink
Merge pull request #127 from nebula-plugins/refactor-netflixossstrate…
Browse files Browse the repository at this point in the history
…gies

Refactor netflixossstrategies
  • Loading branch information
rpalcolea committed Nov 7, 2018
2 parents 5a468b1 + 87d3fed commit 18db4e6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
Expand Up @@ -545,7 +545,7 @@ class ReleasePluginIntegrationSpec extends GitVersioningIntegrationSpec {
def 'devSnapshot works if default is changed'() {
buildFile << '''\
release {
defaultVersionStrategy = nebula.plugin.release.NetflixOssStrategies.SNAPSHOT
defaultVersionStrategy = nebula.plugin.release.NetflixOssStrategies.SNAPSHOT(project)
}
'''.stripIndent()
git.add(patterns: ['build.gradle'] as Set)
Expand Down
87 changes: 52 additions & 35 deletions src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy
Expand Up @@ -15,6 +15,7 @@
*/
package nebula.plugin.release

import nebula.plugin.release.ReleaseExtension
import nebula.plugin.release.git.opinion.Strategies
import nebula.plugin.release.git.semver.ChangeScope
import nebula.plugin.release.git.semver.PartialSemVerStrategy
Expand All @@ -27,47 +28,43 @@ import java.util.regex.Pattern
import static nebula.plugin.release.git.semver.StrategyUtil.*

class NetflixOssStrategies {
static final PartialSemVerStrategy TRAVIS_BRANCH_MAJOR_X = fromTravisPropertyPattern(~/^(\d+)\.x$/)
static final PartialSemVerStrategy TRAVIS_BRANCH_MAJOR_MINOR_X = fromTravisPropertyPattern(~/^(\d+)\.(\d+)\.x$/)
static final PartialSemVerStrategy NEAREST_HIGHER_ANY = nearestHigherAny()
private static final scopes = one(Strategies.Normal.USE_SCOPE_PROP,
TRAVIS_BRANCH_MAJOR_X, TRAVIS_BRANCH_MAJOR_MINOR_X,
Strategies.Normal.ENFORCE_GITFLOW_BRANCH_MAJOR_X, Strategies.Normal.ENFORCE_BRANCH_MAJOR_X,
Strategies.Normal.ENFORCE_GITFLOW_BRANCH_MAJOR_MINOR_X, Strategies.Normal.ENFORCE_BRANCH_MAJOR_MINOR_X,
NEAREST_HIGHER_ANY, Strategies.Normal.useScope(ChangeScope.MINOR))

static final SemVerStrategy SNAPSHOT = Strategies.SNAPSHOT.copyWith(normalStrategy: scopes)
static final SemVerStrategy DEVELOPMENT = Strategies.DEVELOPMENT.copyWith(
normalStrategy: scopes,
buildMetadataStrategy: BuildMetadata.DEVELOPMENT_METADATA_STRATEGY)
static final SemVerStrategy PRE_RELEASE = Strategies.PRE_RELEASE.copyWith(normalStrategy: scopes)
static final SemVerStrategy FINAL = Strategies.FINAL.copyWith(normalStrategy: scopes)

static final class BuildMetadata {
static ReleaseExtension nebulaReleaseExtension
private static final String TRAVIS_BRANCH_PROP = 'release.travisBranch'

static final PartialSemVerStrategy DEVELOPMENT_METADATA_STRATEGY = { state ->
boolean needsBranchMetadata = true
nebulaReleaseExtension.releaseBranchPatterns.each {
if (state.currentBranch.name =~ it) {
needsBranchMetadata = false
}
}
String shortenedBranch = (state.currentBranch.name =~ nebulaReleaseExtension.shortenedBranchPattern)[0][1]
shortenedBranch = shortenedBranch.replaceAll(/[_\/-]/, '.')
def metadata = needsBranchMetadata ? "${shortenedBranch}.${state.currentHead.abbreviatedId}" : state.currentHead.abbreviatedId
state.copyWith(inferredBuildMetadata: metadata)
}
static SemVerStrategy SNAPSHOT(Project project) {
Strategies.SNAPSHOT.copyWith(normalStrategy: getScopes(project))
}

static SemVerStrategy DEVELOPMENT(Project project) {
Strategies.DEVELOPMENT.copyWith(
normalStrategy: getScopes(project),
buildMetadataStrategy: BuildMetadata.DEVELOPMENT_METADATA_STRATEGY)
}

static Project project
static SemVerStrategy PRE_RELEASE(Project project) {
Strategies.PRE_RELEASE.copyWith(normalStrategy: getScopes(project))
}

static final String TRAVIS_BRANCH_PROP = 'release.travisBranch'
static SemVerStrategy FINAL(Project project) {
Strategies.FINAL.copyWith(normalStrategy: getScopes(project))
}

private static getScopes(Project project) {
Object travisReleaseBranch = (project.hasProperty(TRAVIS_BRANCH_PROP)) ? project.property(TRAVIS_BRANCH_PROP) : null
final PartialSemVerStrategy TRAVIS_BRANCH_MAJOR_X = fromTravisPropertyPattern(travisReleaseBranch, ~/^(\d+)\.x$/)
final PartialSemVerStrategy TRAVIS_BRANCH_MAJOR_MINOR_X = fromTravisPropertyPattern(travisReleaseBranch, ~/^(\d+)\.(\d+)\.x$/)
final PartialSemVerStrategy NEAREST_HIGHER_ANY = nearestHigherAny()
one(Strategies.Normal.USE_SCOPE_PROP,
TRAVIS_BRANCH_MAJOR_X, TRAVIS_BRANCH_MAJOR_MINOR_X,
Strategies.Normal.ENFORCE_GITFLOW_BRANCH_MAJOR_X, Strategies.Normal.ENFORCE_BRANCH_MAJOR_X,
Strategies.Normal.ENFORCE_GITFLOW_BRANCH_MAJOR_MINOR_X, Strategies.Normal.ENFORCE_BRANCH_MAJOR_MINOR_X,
NEAREST_HIGHER_ANY, Strategies.Normal.useScope(ChangeScope.MINOR))
}

static PartialSemVerStrategy fromTravisPropertyPattern(Pattern pattern) {
private static PartialSemVerStrategy fromTravisPropertyPattern(Object travisReleaseBranch, Pattern pattern) {
return closure { state ->
if (project.hasProperty(TRAVIS_BRANCH_PROP)) {
def branch = project.property(TRAVIS_BRANCH_PROP)
if (travisReleaseBranch) {
def branch = travisReleaseBranch
def m = branch =~ pattern
if (m) {
def major = m.groupCount() >= 1 ? parseIntOrZero(m[0][1]) : -1
Expand Down Expand Up @@ -110,7 +107,7 @@ class NetflixOssStrategies {
* component as {@code 1.2.3}.
* </p>
*/
static PartialSemVerStrategy nearestHigherAny() {
static private PartialSemVerStrategy nearestHigherAny() {
return closure { state ->
def nearest = state.nearestVersion
if (nearest.any.lessThanOrEqualTo(nearest.normal)) {
Expand All @@ -120,4 +117,24 @@ class NetflixOssStrategies {
}
}
}



static final class BuildMetadata {
static ReleaseExtension nebulaReleaseExtension

static final PartialSemVerStrategy DEVELOPMENT_METADATA_STRATEGY = { state ->
boolean needsBranchMetadata = true
nebulaReleaseExtension.releaseBranchPatterns.each {
if (state.currentBranch.name =~ it) {
needsBranchMetadata = false
}
}
String shortenedBranch = (state.currentBranch.name =~ nebulaReleaseExtension.shortenedBranchPattern)[0][1]
shortenedBranch = shortenedBranch.replaceAll(/[_\/-]/, '.')
def metadata = needsBranchMetadata ? "${shortenedBranch}.${state.currentHead.abbreviatedId}" : state.currentHead.abbreviatedId
state.copyWith(inferredBuildMetadata: metadata)
}
}

}
11 changes: 5 additions & 6 deletions src/main/groovy/nebula/plugin/release/ReleasePlugin.groovy
Expand Up @@ -57,7 +57,6 @@ class ReleasePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
this.project = project
NetflixOssStrategies.project = project

def gitRoot = project.hasProperty('git.root') ? project.property('git.root') : project.rootProject.projectDir

Expand All @@ -77,11 +76,11 @@ class ReleasePlugin implements Plugin<Project> {
versionStrategy new OverrideStrategies.NoCommitStrategy()
versionStrategy new OverrideStrategies.ReleaseLastTagStrategy(project)
versionStrategy new OverrideStrategies.GradlePropertyStrategy(project)
versionStrategy NetflixOssStrategies.SNAPSHOT
versionStrategy NetflixOssStrategies.DEVELOPMENT
versionStrategy NetflixOssStrategies.PRE_RELEASE
versionStrategy NetflixOssStrategies.FINAL
defaultVersionStrategy = NetflixOssStrategies.DEVELOPMENT
versionStrategy NetflixOssStrategies.SNAPSHOT(project)
versionStrategy NetflixOssStrategies.DEVELOPMENT(project)
versionStrategy NetflixOssStrategies.PRE_RELEASE(project)
versionStrategy NetflixOssStrategies.FINAL(project)
defaultVersionStrategy = NetflixOssStrategies.DEVELOPMENT(project)
}

releaseExtension.with {
Expand Down

0 comments on commit 18db4e6

Please sign in to comment.