forked from milessabin/shapeless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.scalascript
executable file
·322 lines (285 loc) · 9.92 KB
/
release.scalascript
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
if [[ -f ~/.bash_aliases ]] ; then
shopt -s expand_aliases
source ~/.bash_aliases
fi
SCALA=`type -P scala`
if [[ -z $SCALA && `type -a scala` ]]; then
SCALA=`eval echo "${BASH_ALIASES[scala]}"`
fi
SBT=`type -P sbt`
if [[ -z $SBT && `type -a sbt` ]]; then
SBT=`eval echo "${BASH_ALIASES[sbt]}"`
fi
exec $SCALA "$0" "$SBT" "$@"
!#
/*
* Copyright (c) 2011-15 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import scala.language.postfixOps
import scala.io.Source
import scala.io.StdIn._
import scala.sys.process._
import scala.util.{Failure, Success, Try}
class CrossBranchRelease(val sbt: String) {
def run: Unit = {
(for {
_ <- checkClean()
_ <- run("git checkout master")
verifyBuild <- askBuild()
_ <- verifyBranch("master", Some("2.11.7"), None, verifyBuild)
_ <- verifyBranch("master", Some("2.12.0-M2"), None, verifyBuild)
_ <- verifyBranch("scala-2.10.x", None, Some("master"), verifyBuild)
_ <- verifyBranch("scalajs-2.11.x", None, Some("master"), verifyBuild)
_ <- verifyBranch("scalajs-2.10.x", None, Some("scala-2.10.x"), verifyBuild)
_ <- verifyBranch("jdk6-canary", None, Some("master"), verifyBuild)
curVer <- readVersion()
relVer <- askVersion(true, curVer)
nextVer <- askVersion(false, curVer)
tagExists <- checkTagExists(relVer)
remote <- askRemote()
_ <- commitVersion(curVer, relVer)
_ <- tagRelease(relVer, tagExists)
_ <- mergeVersion("master", "scala-2.10.x", relVer)
_ <- mergeVersion("master", "scalajs-2.11.x", relVer)
_ <- mergeVersion("scala-2.10.x", "scalajs-2.10.x", relVer)
_ <- release("master", Some("2.11.7"))
_ <- release("master", Some("2.12.0-M2"))
_ <- release("scala-2.10.x", None)
_ <- release("scalajs-2.11.x", None)
_ <- release("scalajs-2.10.x", None)
_ <- commitVersion(relVer, nextVer)
_ <- mergeVersion("master", "scala-2.10.x", nextVer)
_ <- mergeVersion("master", "scalajs-2.11.x", nextVer)
_ <- mergeVersion("scala-2.10.x", "scalajs-2.10.x", nextVer)
_ <- mergeVersion("master", "jdk6-canary", nextVer)
_ <- pushVersion(remote, "master")
_ <- pushVersion(remote, "scala-2.10.x")
_ <- pushVersion(remote, "scalajs-2.11.x")
_ <- pushVersion(remote, "scalajs-2.10.x")
_ <- pushVersion(remote, "jdk6-canary")
_ <- pushTag(remote, relVer, tagExists)
_ <- promote
} yield {
"completed"
}) match {
case Success(msg) =>
logInfo(msg)
case Failure(e) =>
logError(e.getMessage)
}
}
val VERSION_FILE = "version.sbt"
def logInfo(msg: String): Unit = {
println(s"${Console.GREEN}$msg ${Console.RESET}")
}
def logError(msg: String): Unit = {
System.err.println(s"${Console.RED}$msg${Console.RESET}")
}
def run(cmd: String): Try[String] = Try {
println(cmd + "...")
val stderr = new StringBuilder
val stdout = new StringBuilder
val status = cmd ! ProcessLogger(stdout append _ + "\n", stderr append _ + "\n")
val res = stdout.toString()
println(res)
if(status != 0)
sys.error(stderr.toString())
res
}
def runC(cmd: String): Try[Unit] = Try {
println(cmd + "...")
val status = cmd !
if(status != 0)
sys.error(s"$cmd exited with non-zero status $status")
else ()
}
def runS(args: Seq[String]): Try[String] = Try {
println(args.mkString(" ") + "...")
val stderr = new StringBuilder
val stdout = new StringBuilder
//args.!!
val status = args ! ProcessLogger(stdout append _ + "\n", stderr append _ + "\n")
if(status != 0)
sys.error(stderr.toString())
println(stdout.toString())
stdout.toString()
}
def readVersion() = Try {
val str = Source.fromFile(VERSION_FILE).mkString
val VersionR = "version in ThisBuild := \"(.*)\"\\s?".r
str match {
case VersionR(v) => v
}
}
def checkClean(): Try[Boolean] = {
for {
r <- run("git diff")
} yield {
if (r.isEmpty) {
true
} else {
sys.error("Working copy is not clean!")
}
}
}
def checkTagExists(newVersion: String): Try[Boolean] = {
for {
r <- runS(Seq("git", "tag", "-l", s"shapeless-$newVersion"))
} yield {
if (r.contains(newVersion)) {
println(s"Tag shapeles-$newVersion already exists! Want to override it? yes/No")
val answer = readBoolean()
if (!answer)
sys.error("Aborted by user!")
true
} else {
false
}
}
}
def confirm(question: String) = Try {
println(question)
val answer = readBoolean()
if (!answer)
sys.error("Aborted by user!")
}
def askBuild(): Try[Boolean] = Try {
println(s"Do you want to build and test all branches before releasing? yes/no")
readBoolean()
}
def askVersion(when: Boolean, default: String): Try[String] = Try {
val whenStr = if(when) "for" else "after"
println(s"Do you want to use the version number '$default' $whenStr this release? yes/no")
val answer = readBoolean()
if (!answer) {
val newVersion = readLine("Please provide the new release version: ")
val versionR = "\\d\\.\\d{1,2}\\.\\d{1,2}(-.+)?"
if (newVersion == null || !newVersion.matches(versionR)) {
sys.error("Invalid version provided!")
}
newVersion
} else {
default
}
}
def askRemote(): Try[String] = {
run("git remote -v").map { remotesStr =>
val validRemotes = remotesStr.split("\n").map(_.split("\t")(0)).distinct
println(s"Which git remote repo you want to push the changes? Valid options are: " + validRemotes.mkString(", "))
var line = ""
while( {line = readLine(); !validRemotes.contains(line)} ) {
println("Invalid remote repository. Please try again. Valid options are: " + validRemotes.mkString(", "))
}
line
}
}
def commitVersion(curVersion: String, newVersion: String): Try[Any] = {
logInfo(s"Committing version $newVersion if necessary...")
def writeVersion(version: String) = Try {
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
val content = s"""version in ThisBuild := "$version"\n"""
Files.write(Paths.get(VERSION_FILE), content.getBytes(StandardCharsets.UTF_8))
}
if (curVersion != newVersion) {
for {
_ <- run("git checkout master")
_ <- writeVersion(newVersion)
_ <- run(s"git add $VERSION_FILE")
_ <- runS(Seq("git", "commit", s"--message=Updates for $newVersion."))
} yield {}
} else {
Success({})
}
}
def tagRelease(newVersion: String, tagExists: Boolean): Try[Any] = {
logInfo(s"Tagging version $newVersion...")
val stderr = new StringBuilder
for {
_ <- run("git checkout master")
_ <- if (tagExists) {
run(s"git tag --force shapeless-$newVersion")
} else {
run(s"git tag shapeless-$newVersion")
}
} yield {}
}
def mergeVersion(from: String, branch: String, newVersion: String): Try[Any] = {
logInfo(s"Merging $from into $branch...")
for {
_ <- run(s"git checkout $branch")
curVersion <- readVersion()
_ <- if (curVersion != newVersion) {
run(s"git merge --no-edit $from")
} else {
Success({})
}
} yield {}
}
def verifyBranch(branch: String, scalaVersion: Option[String], upstream: Option[String], build: Boolean): Try[Unit] = {
val versionLabel = scalaVersion.map(v => s" (++$v)").getOrElse("")
val versionSwitch = scalaVersion.map(v => s"++$v").getOrElse("")
logInfo(s"Verify $branch$versionLabel...")
for {
merged <- upstream.fold(Try(true)) { b =>
run(s"git branch --contains $b").map { res =>
res.split("\n").map(_.trim).contains(branch)
}
}
if merged
_ <- if (build)
for {
_ <- run(s"git checkout $branch")
_ <- runC(s"$sbt $versionSwitch clean compile test:compile test runAll")
} yield ()
else
Success(())
} yield {}
}
def release(branch: String, scalaVersion: Option[String]): Try[Unit] = {
val versionLabel = scalaVersion.map(v => s" (++$v)").getOrElse("")
val versionSwitch = scalaVersion.map(v => s"++$v").getOrElse("")
logInfo(s"Releasing from $branch$versionLabel...")
for {
_ <- run(s"git checkout $branch")
_ <- runC(s"$sbt $versionSwitch clean core/publish-signed")
} yield {}
}
def pushVersion(remote: String, branch: String): Try[Any] = {
logInfo(s"Pushing version udpates for $branch to $remote...")
for {
_ <- run(s"git push $remote $branch")
} yield {}
}
def pushTag(remote: String, releaseVersion: String, tagExists: Boolean): Try[Any] = {
logInfo(s"Pushing release tag to $remote...")
for {
_ <- run("git checkout master")
_ <- run(s"git push $remote shapeless-$releaseVersion").recoverWith {
case e if tagExists && e.getMessage.contains("already exists") =>
println("Tag already exists on remote. Forcing push...")
// we already asked. Doing a forced update of the tag.
run(s"git push --force $remote shapeless-$releaseVersion")
}
} yield {}
}
def promote: Try[Unit] = {
for {
_ <- runC(s"$sbt sonatypeReleaseAll")
} yield {}
}
}
new CrossBranchRelease(args(0)).run