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

Commit

Permalink
adds timestamp VersionCodeScheme (#72)
Browse files Browse the repository at this point in the history
* Returns the time since 1.1.2024 in seconds
* should fit in 31 bits (we need one for the sign) for about 68 years until 2092
* it’s dependency-less
* it’s monotonic (a requirement from google-side)
  • Loading branch information
pletoss authored Jul 19, 2024
1 parent e6a2014 commit c8b248b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class VersionPluginIntegrationSpec extends VersionIntegrationSpec {
"versionCodeScheme" | "releaseCount" | VersionCodeSchemes.releaseCount | PropertyLocation.property
"versionCodeScheme" | "semver" | VersionCodeSchemes.semver | PropertyLocation.property
"versionCodeScheme" | "none" | VersionCodeSchemes.none | PropertyLocation.property
"versionCodeScheme" | "timestamp" | VersionCodeSchemes.timestamp | PropertyLocation.environment
"versionCodeScheme" | "timestamp" | VersionCodeSchemes.timestamp | PropertyLocation.property
"versionCodeOffset" | 100 | _ | PropertyLocation.environment
"versionCodeOffset" | 200 | _ | PropertyLocation.property
"releaseBranchPattern" | /^m.*/ | _ | PropertyLocation.property
Expand Down Expand Up @@ -179,6 +181,10 @@ class VersionPluginIntegrationSpec extends VersionIntegrationSpec {
"versionCodeScheme" | "setVersionCodeScheme" | "releaseCount" | "String"
"versionCodeScheme" | "setVersionCodeScheme" | VersionCodeSchemes.none | "VersionCodeScheme"
"versionCodeScheme" | "setVersionCodeScheme" | VersionCodeSchemes.releaseCount | "Provider<VersionCodeScheme>"
"versionCodeScheme" | "versionCodeScheme" | "timestamp" | "String"
"versionCodeScheme" | "versionCodeScheme" | VersionCodeSchemes.timestamp | "Provider<VersionCodeScheme>"
"versionCodeScheme" | "versionCodeScheme.set" | VersionCodeSchemes.timestamp | "Provider<VersionCodeScheme>"


"versionCodeOffset" | "versionCodeOffset" | 1 | "Integer"
"versionCodeOffset" | "versionCodeOffset" | 2 | "Provider<Integer>"
Expand Down Expand Up @@ -319,4 +325,32 @@ class VersionPluginIntegrationSpec extends VersionIntegrationSpec {
extensionName = "versionBuilder"
}

// tests that timestamp versioncode scheme generates different versionCodes when called twice 2 seconds apart
def "timestamp versionCode scheme generates different versionCodes"() {
given: "a timestamp version scheme setting"
buildFile << """
versionBuilder {
versionCodeScheme = "timestamp"
}
"""

def query = new PropertyQueryTaskWriter("project.versionCode", "")
query.write(buildFile)

when: "executing the task twice with a 2 second delay"

def version1 = query.getValue(runTasksSuccessfully(query.taskName)).toInteger()
// wait 2 seconds
Thread.sleep(2000)
def version2 = query.getValue(runTasksSuccessfully(query.taskName)).toInteger()

then:
version2 > version1
// sanity check, version is bigger than the difference from 1.1.2024 until the time of this test
version1 > 17280000
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ enum VersionCodeSchemes {
semverBasic,
semver,
releaseCountBasic,
releaseCount
releaseCount,
timestamp
}
15 changes: 14 additions & 1 deletion src/main/groovy/wooga/gradle/version/internal/VersionCode.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class VersionCode {
}),
releaseCount(VersionCodeSchemes.releaseCount, {
GitVersionRepository git, int offset -> generateBuildNumberVersionCode(git, true) + offset
})
}),
timestamp(VersionCodeSchemes.timestamp, { -> generateBuildTimeVersionCode()});

private final VersionCodeSchemes external
private final Closure<Integer> generator
Expand Down Expand Up @@ -147,4 +148,16 @@ class VersionCode {
static Integer generateBuildNumberVersionCode(GitVersionRepository versionRepo, Boolean countPrerelease = true) {
return versionRepo.countReachableVersions(countPrerelease) + 1
}

/**
* Returns the time since 1.1.2024 in seconds
* should fit in 31 bits (we need one for the sign) for about 68 years until 2092
* @return
*/
static Integer generateBuildTimeVersionCode() {
def date = new Date()
def buildTime = date.getTime() - 1704063600000 // 1.1.2024
return (int)(buildTime / 1000)
}

}

0 comments on commit c8b248b

Please sign in to comment.