forked from MovingBlocks/Terasology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooky.kt
202 lines (160 loc) · 5.5 KB
/
gooky.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.logistics.gooky
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.file
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.MergeCommand
import org.terasology.module.ModuleMetadata
import org.terasology.module.ModuleMetadataJsonAdapter
import org.terasology.naming.Version
import java.io.File
const val MODULE_INFO_NAME = "module.txt"
const val RELEASE_BRANCH = "master"
const val UNSTABLE_BRANCH = "develop"
class ShowVersion: CliktCommand(help = """
Print the name and version of a module.
""") {
val dir by argument(name="directory", help="the directory containing the module source").file(mustExist = true, canBeFile = false)
override fun run() {
val metadata = loadModuleMetadata(dir)
echo("${metadata.id}: " +
"Name: \"${metadata.displayName}\" Version: ${metadata.version}")
}
}
class GitStatus: CliktCommand(help = """
Print something from some git commit.
(Proof of concept for git interface.)
""") {
val dir by argument(name="directory", help="the directory containing the module source").file(mustExist = true, canBeFile = false)
override fun run() {
Git.open(dir).use {git ->
val log = git.log().call()
val latest = log.first()
val who = latest.authorIdent.name
val datetime = latest.authorIdent.`when`
echo("[${datetime}] ${who}: ${latest.shortMessage}")
}
}
}
class Release: CliktCommand(help = """
Commit a stable release and increment version.
git checkout develop
git commit -m "remove -SNAPSHOT"
git checkout master
git merge develop
git checkout develop
git merge master
git commit -m "bump version"
""") {
val dir by argument(name="directory", help="the directory containing the module source").file(mustExist = true, canBeFile = false)
val push by option("--push", "-p", help="push the release commits").flag()
override fun run() {
Git.open(dir).use { git ->
with(git.checkout()) {
setName(UNSTABLE_BRANCH)
call() // not loving this as a "high-level" API
}
val releaseVersion = unSnapshot(dir)
with(git.add()) {
isUpdate = true
addFilepattern(MODULE_INFO_NAME)
call()
}
with(git.commit()) {
message = "version ${releaseVersion}"
call()
}
with(git.checkout()) {
setName(RELEASE_BRANCH)
call()
}
with(git.merge()) {
setFastForward(MergeCommand.FastForwardMode.FF_ONLY)
include(git.repository.resolve(UNSTABLE_BRANCH))
call()
}
val releaseTag = "v${releaseVersion}"
with(git.tag()) {
isAnnotated = true
name = releaseTag
message = "version ${releaseVersion}"
call()
}
with(git.checkout()) {
setName(UNSTABLE_BRANCH)
call()
}
val unstableVersion = bumpAndSnapshot(dir)
with(git.add()) {
isUpdate = true
addFilepattern(MODULE_INFO_NAME)
call()
}
with(git.commit()) {
message = "bump version ${unstableVersion}"
call()
}
if (push) {
with(git.push()) {
add(UNSTABLE_BRANCH)
add(RELEASE_BRANCH)
add(releaseTag)
call()
}
}
}
}
}
class Gooky: CliktCommand() {
init {
subcommands(Release(), ShowVersion(), GitStatus())
}
override fun run() = Unit
}
fun main(args: Array<String>) {
Gooky().main(args)
}
fun loadModuleMetadata(directory: File) : ModuleMetadata {
val moduleFile = directory.resolve(MODULE_INFO_NAME)
return moduleFile.reader().use {
ModuleMetadataJsonAdapter().read(it)!!
}
}
fun saveModuleMetadata(directory: File, metadata: ModuleMetadata) {
val moduleFile = directory.resolve(MODULE_INFO_NAME)
moduleFile.writer().use {
ModuleMetadataJsonAdapter().write(metadata, it)
}
}
fun unSnapshot(directory: File): Version {
val metadata = loadModuleMetadata(directory)
val currentVersion = metadata.version
if (!currentVersion.isSnapshot) {
throw RuntimeException("not currently a snapshot!")
}
val newVersion = Version(
currentVersion.major,
currentVersion.minor,
currentVersion.patch,
false
)
metadata.version = newVersion
saveModuleMetadata(directory, metadata)
return newVersion
}
fun bumpAndSnapshot(directory: File): Version {
val metadata = loadModuleMetadata(directory)
val currentVersion = metadata.version
if (currentVersion.isSnapshot) {
throw RuntimeException("already a snapshot!")
}
val newVersion = currentVersion.nextPatchVersion.snapshot
metadata.version = newVersion
saveModuleMetadata(directory, metadata)
return newVersion
}