Skip to content

Commit 5c80823

Browse files
authored
Add mirai-doc publishing workflow (mamoe#346)
* Update doc on push * Optimize kotlin src block * Generate by version * Fix table optimizing * Slowdown uploading * Fix index.md to README.md * Publish docs on release created
1 parent b06bf6a commit 5c80823

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

build.gradle.kts

+24-7
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ subprojects {
105105
runCatching {
106106
upload.GitHub.upload(
107107
file,
108-
"https://api.github.com/repos/mamoe/mirai-repo/contents/shadow/${project.name}/$filename",
109108
project,
110109
"mirai-repo",
111-
"shadow/"
110+
"shadow/${project.name}/$filename"
112111
)
113112
}.exceptionOrNull()?.let {
114113
System.err.println("GitHub Upload failed")
@@ -138,27 +137,45 @@ subprojects {
138137
val dokkaGitHubUpload by tasks.creating {
139138
group = "mirai"
140139

141-
dependsOn(tasks.getByName("dokkaMarkdown"))
140+
dependsOn(tasks.getByName("dokkaGfm"))
142141
doFirst {
143-
val baseDir = file("./build/dokka-markdown")
142+
val baseDir = file("./build/dokka-gfm/${project.name}")
144143

145144
timeout.set(Duration.ofHours(6))
146-
file("build/dokka-markdown/").walk()
145+
file("build/dokka-gfm/").walk()
147146
.filter { it.isFile }
148147
.map { old ->
149148
if (old.name == "index.md") File(old.parentFile, "README.md").also { new -> old.renameTo(new) }
150149
else old
151150
}
152151
.forEach { file ->
152+
if (file.endsWith(".md")) {
153+
file.writeText(
154+
file.readText().replace("index.md", "README.md", ignoreCase = true)
155+
.replace(Regex("""```\n([\s\S]*?)```""")) {
156+
"\n" + """
157+
```kotlin
158+
$it
159+
```
160+
""".trimIndent()
161+
})
162+
} /* else if (file.name == "README.md") {
163+
file.writeText(file.readText().replace(Regex("""(\n\n\|\s)""")) {
164+
"\n\n" + """"
165+
|||
166+
|:----------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
167+
|
168+
""".trimIndent()
169+
})
170+
}*/
153171
val filename = file.toRelativeString(baseDir)
154172
println("Uploading file $filename")
155173
runCatching {
156174
upload.GitHub.upload(
157175
file,
158-
"https://api.github.com/repos/mamoe/mirai-doc/contents/${project.name}/$filename",
159176
project,
160177
"mirai-doc",
161-
""
178+
"${project.name}/${project.version}/$filename"
162179
)
163180
}.exceptionOrNull()?.let {
164181
System.err.println("GitHub Upload failed")

buildSrc/src/main/kotlin/upload/CuiCloud.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ object CuiCloud {
150150
@OptIn(ExperimentalContracts::class)
151151
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
152152
@kotlin.internal.InlineOnly
153-
internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
153+
internal inline fun <R> retryCatching(n: Int, onFailure: () -> Unit = {}, block: () -> R): Result<R> {
154154
contract {
155155
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
156156
}
@@ -165,6 +165,7 @@ internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
165165
} catch (e: Throwable) {
166166
}
167167
exception = e
168+
onFailure()
168169
}
169170
}
170171
return Result.failure(exception!!)

buildSrc/src/main/kotlin/upload/GitHub.kt

+13-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import io.ktor.client.engine.cio.CIO
1818
import io.ktor.client.features.HttpTimeout
1919
import io.ktor.client.request.put
2020
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.delay
2122
import kotlinx.coroutines.runBlocking
2223
import kotlinx.coroutines.withContext
2324
import org.gradle.api.Project
@@ -70,12 +71,20 @@ object GitHub {
7071
)
7172
}
7273

73-
fun upload(file: File, url: String, project: Project, repo: String, baseFilePath: String) = runBlocking {
74+
fun upload(file: File, project: Project, repo: String, targetFilePath: String) = runBlocking {
7475
val token = getGithubToken(project)
7576
println("token.length=${token.length}")
76-
retryCatching(1000) {
77+
val url = "https://api.github.com/repos/mamoe/$repo/contents/$targetFilePath"
78+
retryCatching(100, onFailure = { delay(30_000) }) { // 403 forbidden?
7779
Http.put<String>("$url?access_token=$token") {
78-
val sha = getGithubSha(repo, "$baseFilePath${project.name}/${file.name}", "master", project)
80+
val sha = retryCatching(3, onFailure = { delay(30_000) }) {
81+
getGithubSha(
82+
repo,
83+
targetFilePath,
84+
"master",
85+
project
86+
)
87+
}.getOrNull()
7988
println("sha=$sha")
8089
val content = String(Base64.getEncoder().encode(file.readBytes()))
8190
body = """
@@ -88,6 +97,7 @@ object GitHub {
8897
}.let {
8998
println("Upload response: $it")
9099
}
100+
delay(1000)
91101
}.getOrThrow()
92102
}
93103

0 commit comments

Comments
 (0)