Skip to content

Commit 8eca20f

Browse files
authored
Merge pull request #59 from touchlab/kpg/refactor_version_write
Version manager writes version tags
2 parents 3a3a5b3 + 0039635 commit 8eca20f

12 files changed

+90
-63
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android.useAndroidX=true
1212
org.gradle.jvmargs=-Xmx2g
1313

1414
GROUP=co.touchlab.faktory
15-
VERSION_NAME=0.1.14-devtest-SNAPSHOT
15+
VERSION_NAME=0.1.16-SNAPSHOT
1616
KOTLIN_VERSION=1.6.21
1717

1818
POM_URL=https://github.com/touchlab/KMMBridge
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package co.touchlab.faktory
2+
3+
import co.touchlab.faktory.internal.GithubCalls
4+
import co.touchlab.faktory.internal.procRunFailLog
5+
import org.gradle.api.Project
6+
7+
object GitReleaseVersionManager : GitTagBasedVersionManager() {
8+
private var versionRecorded = false
9+
10+
override fun recordVersion(project: Project, versionString: String) {
11+
if(!versionRecorded) {
12+
project.procRunFailLog("git", "push")
13+
val commitId = project.procRunFailLog("git", "rev-parse", "HEAD").first()
14+
GithubCalls.createRelease(project, project.githubRepo, versionString, commitId)
15+
versionRecorded = true
16+
}
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package co.touchlab.faktory
2+
3+
import co.touchlab.faktory.internal.procRun
4+
import co.touchlab.faktory.internal.procRunWarnLog
5+
import org.gradle.api.Project
6+
7+
abstract class GitTagBasedVersionManager: VersionManager {
8+
override fun getVersion(project: Project, versionPrefix: String): String {
9+
val tagSet = mutableSetOf<String>()
10+
// Need to make sure we have all the tags. This may need to be configurable in the future for
11+
// more complex git setups. If call fails, we'll get a warning but keep going.
12+
project.procRunWarnLog("git", "pull", "--tags")
13+
procRun("git", "tag") { line, _ ->
14+
tagSet.add(line)
15+
}
16+
return findNextVersion(versionPrefix) {
17+
tagSet.contains(it)
18+
}
19+
}
20+
}
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package co.touchlab.faktory
22

3-
import co.touchlab.faktory.co.touchlab.faktory.internal.procRun
4-
import co.touchlab.faktory.co.touchlab.faktory.internal.procRunFailLog
5-
import co.touchlab.faktory.co.touchlab.faktory.internal.procRunWarnLog
3+
import co.touchlab.faktory.internal.procRunFailLog
64
import org.gradle.api.Project
75

8-
object GitTagVersionManager : VersionManager {
9-
override fun getVersion(project: Project, versionPrefix: String): String {
10-
val tagSet = mutableSetOf<String>()
11-
// Need to make sure we have all of the tags. This may need to be configurable in the future for
12-
// more complex git setups. If call fails, we'll get a warning but keep going.
13-
project.procRunWarnLog("git", "pull", "--tags")
14-
procRun("git", "tag") { line, _ ->
15-
tagSet.add(line)
16-
}
17-
return findNextVersion(versionPrefix) {
18-
tagSet.contains(it)
19-
}
6+
object GitTagVersionManager : GitTagBasedVersionManager() {
7+
override fun recordVersion(project: Project, versionString: String) {
8+
project.procRunFailLog("git", "tag", "-a", versionString, "-m", "KMM release version $versionString")
9+
project.procRunFailLog("git", "push", "--follow-tags")
2010
}
2111
}

kmmbridge/src/main/kotlin/GithubReleaseArtifactManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package co.touchlab.faktory
22

3-
import co.touchlab.faktory.co.touchlab.faktory.internal.GithubCalls
3+
import co.touchlab.faktory.internal.GithubCalls
44
import org.gradle.api.Project
55
import java.io.File
66

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package co.touchlab.faktory
22

3-
import co.touchlab.faktory.co.touchlab.faktory.internal.GithubCalls
3+
import co.touchlab.faktory.internal.GithubCalls
4+
import co.touchlab.faktory.internal.procRunFailLog
45
import org.gradle.api.Project
56

6-
class GithubReleaseVersionManager() : VersionManager {
7-
override fun getVersion(project: Project, versionPrefix: String): String {
8-
return findNextVersion(versionPrefix) {
9-
GithubCalls.findReleaseId(project, project.githubRepo, it) != null
10-
}
7+
class GithubReleaseVersionManager() : GitTagBasedVersionManager() {
8+
override fun recordVersion(project: Project, versionString: String) {
9+
val commitId = project.procRunFailLog("git", "rev-parse", "HEAD").first()
10+
GithubCalls.createRelease(project, project.githubRepo, versionString, commitId)
1111
}
1212
}

kmmbridge/src/main/kotlin/KMMBridge.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface KmmBridgeExtension {
3939

4040
val versionPrefix: Property<String>
4141

42-
fun s3Public(
42+
fun s3PublicArtifacts(
4343
region: String,
4444
bucket: String,
4545
accessKeyId: String,
@@ -59,24 +59,34 @@ interface KmmBridgeExtension {
5959
)
6060
}
6161

62-
fun githubRelease(
62+
fun githubReleaseArtifacts(
6363
artifactRelease: String? = null
6464
) {
6565
artifactManager.set(GithubReleaseArtifactManager(artifactRelease))
66-
versionManager.set(GitTagVersionManager)
6766
}
6867

69-
fun Project.faktoryServer(faktoryReadKey: String? = null) {
68+
fun Project.faktoryServerArtifacts(faktoryReadKey: String? = null) {
7069
artifactManager.set(FaktoryServerArtifactManager(faktoryReadKey, this))
7170
}
7271

72+
fun timestampVersions() {
73+
versionManager.set(TimestampVersionManager)
74+
}
75+
76+
fun gitTagVersions() {
77+
versionManager.set(GitTagVersionManager)
78+
}
79+
80+
fun githubReleaseVersions() {
81+
versionManager.set(GitReleaseVersionManager)
82+
}
83+
7384
fun Project.spm(
7485
spmDirectory: String? = null,
75-
commitVersionStrategy: SpmDependencyManager.CommitVersionStrategy = SpmDependencyManager.CommitVersionStrategy.GitTag,
7686
packageName: String = project.name,
7787
) {
7888
val swiftPackageFolder = spmDirectory ?: projectDir.path
79-
val dependencyManager = SpmDependencyManager(swiftPackageFolder, commitVersionStrategy, packageName)
89+
val dependencyManager = SpmDependencyManager(swiftPackageFolder, packageName)
8090
dependencyManagers.set(dependencyManagers.getOrElse(emptyList()) + dependencyManager)
8191
}
8292

@@ -110,6 +120,12 @@ interface VersionManager {
110120
* Compute a final version to use for publication, based on the plugin versionPrefix
111121
*/
112122
fun getVersion(project: Project, versionPrefix: String): String
123+
124+
/**
125+
* Versions that need to write somewhere need to do it after everything else is done.
126+
* Called after dependency managers are done.
127+
*/
128+
fun recordVersion(project: Project, versionString: String)
113129
}
114130

115131
internal const val TASK_GROUP_NAME = "kmmbridge"
@@ -221,6 +237,9 @@ class KMMBridgePlugin : Plugin<Project> {
221237
val publishRemoteTask = task("kmmBridgePublish") {
222238
group = TASK_GROUP_NAME
223239
dependsOn(uploadTask)
240+
doLast {
241+
extension.versionManager.get().recordVersion(project, versionFile.readText())
242+
}
224243
}
225244

226245
for (dependencyManager in dependencyManagers) {

kmmbridge/src/main/kotlin/ManualVersionManager.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ import org.gradle.api.Project
77
*/
88
object ManualVersionManager : VersionManager {
99
override fun getVersion(project: Project, versionPrefix: String): String = versionPrefix
10+
override fun recordVersion(project: Project, versionString: String) {
11+
// Not needed
12+
}
1013
}

kmmbridge/src/main/kotlin/SpmDependencyManager.kt

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package co.touchlab.faktory
22

3-
import co.touchlab.faktory.co.touchlab.faktory.internal.GithubCalls
4-
import co.touchlab.faktory.co.touchlab.faktory.internal.procRunFailLog
3+
import co.touchlab.faktory.internal.procRunFailLog
54
import org.gradle.api.Project
65
import org.gradle.api.Task
76
import java.io.ByteArrayOutputStream
@@ -13,14 +12,8 @@ class SpmDependencyManager(
1312
* Folder where the Package.swift file lives
1413
*/
1514
private val swiftPackageFolder: String,
16-
private val commitVersionStrategy: CommitVersionStrategy,
1715
private val packageName: String
1816
) : DependencyManager {
19-
20-
enum class CommitVersionStrategy {
21-
None, GitTag, GithubRelease
22-
}
23-
2417
private val swiftPackageFilePath: String
2518
get() = "${stripEndSlash(swiftPackageFolder)}/Package.swift"
2619

@@ -31,36 +24,16 @@ class SpmDependencyManager(
3124
inputs.files(zipFile, project.urlFile, project.versionFile)
3225

3326
doLast {
34-
val originalPackageFile = project.readPackageFile()
35-
3627
val checksum = project.findSpmChecksum(zipFile)
3728
val url = project.urlFile.readText()
3829

3930
project.writePackageFile(packageName, url, checksum)
31+
4032
val version = project.versionFile.readText()
4133

42-
if (commitVersionStrategy != CommitVersionStrategy.None) {
43-
44-
project.procRunFailLog("git", "add", ".")
45-
project.procRunFailLog("git", "commit", "-m", "KMM SPM package release for $version")
46-
if (commitVersionStrategy == CommitVersionStrategy.GitTag) {
47-
project.procRunFailLog("git", "tag", "-a", version, "-m", "KMM release version $version")
48-
} else {
49-
project.procRunFailLog("git", "push")
50-
val commitId = project.procRunFailLog("git", "rev-parse", "HEAD").first()
51-
GithubCalls.createRelease(project, project.githubRepo, version, commitId)
52-
}
53-
54-
project.writePackageFile(originalPackageFile)
55-
56-
project.procRunFailLog("git", "add", ".")
57-
project.procRunFailLog("git", "commit", "-m", "KMM SPM package file revert")
58-
if (commitVersionStrategy == CommitVersionStrategy.GitTag) {
59-
project.procRunFailLog("git", "push", "--follow-tags")
60-
} else {
61-
project.procRunFailLog("git", "push")
62-
}
63-
}
34+
project.procRunFailLog("git", "add", ".")
35+
project.procRunFailLog("git", "commit", "-m", "KMM SPM package release for $version")
36+
project.procRunFailLog("git", "push")
6437
}
6538
}
6639

kmmbridge/src/main/kotlin/TimestampVersionManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ import org.gradle.api.Project
55
object TimestampVersionManager : VersionManager {
66
override fun getVersion(project: Project, versionPrefix: String): String =
77
"${versionPrefix}.${System.currentTimeMillis()}"
8+
9+
override fun recordVersion(project: Project, versionString: String) {
10+
// Not needed
11+
}
812
}

0 commit comments

Comments
 (0)