Skip to content

Commit bef737e

Browse files
committed
Refactor and improve code
1 parent ee710c2 commit bef737e

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

create-new-release-notes.main.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import java.io.File
2020
import java.net.URL
2121
import java.time.Duration
2222

23+
val resultFile = File("release-notes.html")
2324
// TODO: Use Kotlin Duration when/if switched to v1.6.x or higher
2425
val waitTime = Duration.ofSeconds(10)
2526
val feedUrl = URL("https://developer.android.com/feeds/androidx-release-notes.xml")
26-
val writer = File("release-notes.html").bufferedWriter()
27+
val writer = resultFile.bufferedWriter()
2728
// NOTE: To test for a complicated release notes, use
2829
// `XmlReader(File("test-feed-result.xml"))` below
29-
// and specifically, the first entry of the feed
3030
val reader = tryToGet(
31+
// { XmlReader(File("test-feed-result.xml")) },
3132
{ XmlReader(feedUrl) },
3233
retryCount = 10,
3334
"Failed to initialize the feed reader",
@@ -43,8 +44,7 @@ fun <T> tryToGet(
4344
errorMessage: String
4445
): T {
4546
repeat(retryCount) {
46-
val result = runCatching(block)
47-
if (result.isSuccess) return result.getOrThrow()
47+
runCatching(block).onSuccess { return it }
4848
println("$failMessage; attempting again in ${waitTime.seconds} seconds")
4949
Thread.sleep(waitTime.toMillis())
5050
}
@@ -54,9 +54,9 @@ fun <T> tryToGet(
5454
reader.use { reader ->
5555
val feed = SyndFeedInput().build(reader)
5656
val latestRelease = feed.entries.first()
57-
val latestReleaseUrls = latestRelease.contents.first().value
57+
val latestReleaseItems = latestRelease.contents.first().value
5858
Jsoup
59-
.parse(latestReleaseUrls)
59+
.parse(latestReleaseItems)
6060
.select("a")
6161
.asSequence()
6262
.map(::toLink)
@@ -68,7 +68,7 @@ reader.use { reader ->
6868

6969
// Create a raw text version as well just if someone needs it
7070
val text = Jsoup
71-
.parse(File("release-notes.html"), "UTF-8")
71+
.parse(resultFile, "UTF-8")
7272
.wholeText()
7373
File("release-notes.txt").writeText(text)
7474

try-to-get.main.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.time.Duration
99
val waitTime = Duration.ofSeconds(10)
1010

1111
/**
12-
* Try for at most retryCount times to run the block without exception.
12+
* Try for at most [retryCount] times to run the block without exception.
1313
*/
1414
fun <T> tryToGet(
1515
block: () -> T,
@@ -18,8 +18,7 @@ fun <T> tryToGet(
1818
errorMessage: String
1919
): T {
2020
repeat(retryCount) {
21-
val result = runCatching(block)
22-
if (result.isSuccess) return result.getOrThrow()
21+
runCatching(block).onSuccess { return it }
2322
println("$failMessage; attempting again in ${waitTime.seconds} seconds")
2423
Thread.sleep(waitTime.toMillis())
2524
}

0 commit comments

Comments
 (0)