Skip to content

Commit cda14ae

Browse files
committed
Refactor the scripts
1 parent 6af9831 commit cda14ae

File tree

5 files changed

+43
-75
lines changed

5 files changed

+43
-75
lines changed

scripts/check-for-updates.main.kts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
#!/usr/bin/env kotlin
22

33
@file:JvmName("ReleaseUpdateChecker")
4-
@file:CompilerOptions("-jvm-target", "11")
4+
@file:CompilerOptions("-jvm-target", "17")
55
@file:Repository("https://repo.maven.apache.org/maven2")
6-
@file:Repository("https://jcenter.bintray.com")
7-
@file:Repository("https://jitpack.io")
86
@file:DependsOn("com.rometools:rome:2.1.0")
97

10-
// NOTE: See https://youtrack.jetbrains.com/issue/KT-42101
11-
// NOTE that currently, IntelliJ code features break when importing external scripts.
12-
@file:Import("retry.main.kts")
8+
@file:Import("retry.kts")
139

1410
import com.rometools.rome.io.SyndFeedInput
1511
import com.rometools.rome.io.XmlReader
1612
import java.net.URI
1713
import java.time.LocalDateTime
1814
import java.time.format.DateTimeFormatter
19-
import kotlin.io.path.Path
20-
import kotlin.io.path.appendText
21-
import kotlin.io.path.readText
15+
import kotlin.io.path.*
2216

2317
val datesPattern = DateTimeFormatter.ofPattern("EEE LLL dd HH:mm:ss z yyyy")
2418
val feedUrl = URI("https://developer.android.com/feeds/androidx-release-notes.xml").toURL()
25-
val reader = tryTo("initialize the feed reader") { XmlReader(feedUrl) }
26-
val file = Path("last-rss-update.txt")
19+
val reader = tryTo("initialize the feed reader") { XmlReader(feedUrl.openStream()) }
20+
val file = Path("last-rss-update.txt").takeIf { it.exists() }
2721
val feed = SyndFeedInput().build(reader)
28-
val ours = file.readText().trimEnd().parseAsLocalDateTime()
22+
val ours = file?.readText()?.trimEnd().parseAsLocalDateTime()
2923
val ourDate = ours.toLocalDate().toString()
3024
val theirs = feed.publishedDate.toString().parseAsLocalDateTime()
3125
val theirDate = theirs.toLocalDate().toString()
@@ -45,11 +39,14 @@ println("Their RSS publish date: $theirs")
4539
// To set output for a job step
4640
// see https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#setting-an-output-parameter
4741
// and https://stackoverflow.com/a/59201610
48-
val stepsOutputFile = Path(System.getenv("GITHUB_OUTPUT"))
42+
val stepsOutputFile = (System.getenv("GITHUB_OUTPUT") ?: "output.txt")
43+
.let(::Path)
44+
.also { if (it.notExists()) it.createFile() }
4945
val lineFeed: String = System.lineSeparator()
5046
stepsOutputFile.appendText("result=$freshness$lineFeed")
5147
stepsOutputFile.appendText("dateTag=$dateTag$lineFeed")
5248

53-
fun String.parseAsLocalDateTime() =
54-
runCatching { LocalDateTime.parse(this, datesPattern) }
55-
.getOrDefault(LocalDateTime.of(1, 1, 1, 1, 1))
49+
fun String?.parseAsLocalDateTime() = this
50+
?.runCatching { LocalDateTime.parse(this, datesPattern) }
51+
?.getOrNull()
52+
?: LocalDateTime.of(1, 1, 1, 1, 1)

scripts/create-new-release-notes.main.kts

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env kotlin
22

33
@file:JvmName("ReleaseNotesGenerator")
4-
@file:CompilerOptions("-jvm-target", "11")
4+
@file:CompilerOptions("-jvm-target", "17")
55
@file:Repository("https://repo.maven.apache.org/maven2")
6-
@file:Repository("https://jcenter.bintray.com")
7-
@file:Repository("https://jitpack.io")
86
@file:DependsOn("com.rometools:rome:2.1.0")
9-
@file:DependsOn("org.jsoup:jsoup:1.18.3")
7+
@file:DependsOn("org.jsoup:jsoup:1.21.1")
8+
9+
@file:Import("retry.kts")
1010

1111
import com.rometools.rome.io.SyndFeedInput
1212
import com.rometools.rome.io.XmlReader
@@ -17,16 +17,17 @@ import java.net.URI
1717
import kotlin.io.path.Path
1818
import kotlin.io.path.bufferedWriter
1919
import kotlin.io.path.writeText
20-
import kotlin.time.Duration.Companion.seconds
20+
21+
typealias LinkString = String
22+
typealias LinkDocument = Pair<LinkString, Document>
2123

2224
val resultFile = Path("release-notes.html")
23-
val waitTime = 10.seconds
2425
val feedUrl = URI("https://developer.android.com/feeds/androidx-release-notes.xml").toURL()
2526
val writer = resultFile.bufferedWriter()
2627
val reader = tryTo("initialize the feed reader") {
2728
// NOTE: Use this to test for a complicated release notes
2829
// XmlReader(File("test-feed-result.xml"))
29-
XmlReader(feedUrl)
30+
XmlReader(feedUrl.openStream())
3031
}
3132
val feed = SyndFeedInput().build(reader)
3233
val latestRelease = feed.entries.first()
@@ -40,9 +41,9 @@ Jsoup
4041
.parse(latestReleaseItems)
4142
.select("a")
4243
.asSequence()
43-
.map(::toLink)
44-
.map(::toDocument)
45-
.map(::toReleaseNote)
44+
.map(Element::toLink)
45+
.map(LinkString::toLinkDocument)
46+
.map(LinkDocument::toReleaseNote)
4647
.forEach(writer::write)
4748
.also { writer.close() }
4849
.also { reader.close() }
@@ -51,44 +52,16 @@ Jsoup
5152
val text = Jsoup.parse(resultFile).wholeText()
5253
Path("release-notes.txt").writeText(text)
5354

54-
// TODO: Duplicate; use the retry.main.kts script.
55-
// See other scripts for example usage.
56-
// NOTE that currently, IntelliJ code features break when importing external scripts.
57-
/**
58-
* Try [forAtMost] times to run the block without exception.
59-
*
60-
* We could also make subsequent runs wait
61-
* for an [exponential delay](https://en.wikipedia.org/wiki/Exponential_backoff).
62-
* See [this article](https://dzone.com/articles/understanding-retry-pattern-with-exponential-back).
63-
*
64-
* I wrote this function myself.
65-
* It is interesting that [this solution](https://stackoverflow.com/a/46890009)
66-
* proposed by Roman Elizarov is very similar to mine.
67-
*/
68-
fun <T> tryTo(
69-
description: String,
70-
forAtMost: Int = 5,
71-
block: () -> T
72-
): T {
73-
repeat(forAtMost) {
74-
runCatching(block).onSuccess { return it }
75-
println("Failed to $description.")
76-
println("Trying again in $waitTime\n")
77-
Thread.sleep(waitTime.inWholeMilliseconds)
78-
}
79-
error("All attempts to $description failed.")
80-
}
81-
82-
fun toLink(element: Element) = element.attr("href")
55+
fun Element.toLink() = this.attr("href")
8356

8457
// FIXME: Use plain Jsoup.connect()... and remove Pair creation (to)
8558
// See https://github.com/jhy/jsoup/issues/1686 for the reason.
86-
fun toDocument(link: String) = tryTo("get $link") {
87-
link to Jsoup.connect(link).get()
59+
fun LinkString.toLinkDocument() = tryTo("get $this") {
60+
this to Jsoup.connect(this).get()
8861
}
8962

90-
fun toReleaseNote(pair: Pair<String, Document>): String {
91-
val (link, document) = pair
63+
fun LinkDocument.toReleaseNote(): String {
64+
val (link, document) = this
9265
val id = link.substringAfter("#")
9366
val name = document.extractName(id)
9467
val version = document.extractVersion(id)

scripts/mouse.main.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env kotlin
2+
13
import kotlin.io.path.Path
24
import kotlin.io.path.writeText
35
import kotlin.math.roundToInt
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
#!/usr/bin/env kotlin
2-
3-
@file:JvmName("Retry")
4-
@file:CompilerOptions("-jvm-target", "11")
5-
61
import kotlin.time.Duration.Companion.seconds
72

3+
// Updates to this file are not reflected to user scripts
4+
// See https://youtrack.jetbrains.com/issue/KT-42101
5+
86
val waitTime = 10.seconds
97

108
/**
11-
* Try [forAtMost] times to run the block without exception.
9+
* Try for [maxTries] times to run the block without exception.
1210
*
1311
* We could also make subsequent runs wait
1412
* for an [exponential delay](https://en.wikipedia.org/wiki/Exponential_backoff).
@@ -18,16 +16,18 @@ val waitTime = 10.seconds
1816
* It is interesting that [this solution](https://stackoverflow.com/a/46890009)
1917
* proposed by Roman Elizarov is very similar to mine.
2018
*/
19+
@Suppress("unused")
2120
fun <T> tryTo(
2221
description: String,
23-
forAtMost: Int = 5,
22+
maxTries: Int = 5,
2423
block: () -> T
2524
): T {
26-
repeat(forAtMost) {
25+
for (i in 1..maxTries) {
2726
runCatching(block).onSuccess { return it }
2827
println("Failed to $description.")
28+
if (i == maxTries) break
2929
println("Trying again in $waitTime\n")
3030
Thread.sleep(waitTime.inWholeMilliseconds)
3131
}
32-
error("All attempts to $description failed.")
32+
error("All $maxTries attempts to $description failed.")
3333
}

scripts/set-rss-last-update.main.kts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#!/usr/bin/env kotlin
22

33
@file:JvmName("TimestampUpdater")
4-
@file:CompilerOptions("-jvm-target", "11")
4+
@file:CompilerOptions("-jvm-target", "17")
55
@file:Repository("https://repo.maven.apache.org/maven2")
6-
@file:Repository("https://jcenter.bintray.com")
7-
@file:Repository("https://jitpack.io")
86
@file:DependsOn("com.rometools:rome:2.1.0")
97

10-
// NOTE: See https://youtrack.jetbrains.com/issue/KT-42101
11-
// NOTE that currently, IntelliJ code features break when importing external scripts.
12-
@file:Import("retry.main.kts")
8+
@file:Import("retry.kts")
139

1410
import com.rometools.rome.io.SyndFeedInput
1511
import com.rometools.rome.io.XmlReader
@@ -18,7 +14,7 @@ import kotlin.io.path.Path
1814
import kotlin.io.path.writeText
1915

2016
val feedUrl = URI("https://developer.android.com/feeds/androidx-release-notes.xml").toURL()
21-
val reader = tryTo("initialize the feed reader") { XmlReader(feedUrl) }
17+
val reader = tryTo("initialize the feed reader") { XmlReader(feedUrl.openStream()) }
2218
val feed = SyndFeedInput().build(reader)
2319
val date = feed.publishedDate.toString()
2420
Path("last-rss-update.txt").writeText("$date\n")

0 commit comments

Comments
 (0)