Commit 5c808238 authored by Him188's avatar Him188 Committed by GitHub

Add mirai-doc publishing workflow (#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
parent b06bf6ab
......@@ -105,10 +105,9 @@ subprojects {
runCatching {
upload.GitHub.upload(
file,
"https://api.github.com/repos/mamoe/mirai-repo/contents/shadow/${project.name}/$filename",
project,
"mirai-repo",
"shadow/"
"shadow/${project.name}/$filename"
)
}.exceptionOrNull()?.let {
System.err.println("GitHub Upload failed")
......@@ -138,27 +137,45 @@ subprojects {
val dokkaGitHubUpload by tasks.creating {
group = "mirai"
dependsOn(tasks.getByName("dokkaMarkdown"))
dependsOn(tasks.getByName("dokkaGfm"))
doFirst {
val baseDir = file("./build/dokka-markdown")
val baseDir = file("./build/dokka-gfm/${project.name}")
timeout.set(Duration.ofHours(6))
file("build/dokka-markdown/").walk()
file("build/dokka-gfm/").walk()
.filter { it.isFile }
.map { old ->
if (old.name == "index.md") File(old.parentFile, "README.md").also { new -> old.renameTo(new) }
else old
}
.forEach { file ->
if (file.endsWith(".md")) {
file.writeText(
file.readText().replace("index.md", "README.md", ignoreCase = true)
.replace(Regex("""```\n([\s\S]*?)```""")) {
"\n" + """
```kotlin
$it
```
""".trimIndent()
})
} /* else if (file.name == "README.md") {
file.writeText(file.readText().replace(Regex("""(\n\n\|\s)""")) {
"\n\n" + """"
|||
|:----------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
""".trimIndent()
})
}*/
val filename = file.toRelativeString(baseDir)
println("Uploading file $filename")
runCatching {
upload.GitHub.upload(
file,
"https://api.github.com/repos/mamoe/mirai-doc/contents/${project.name}/$filename",
project,
"mirai-doc",
""
"${project.name}/${project.version}/$filename"
)
}.exceptionOrNull()?.let {
System.err.println("GitHub Upload failed")
......
......@@ -150,7 +150,7 @@ object CuiCloud {
@OptIn(ExperimentalContracts::class)
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
internal inline fun <R> retryCatching(n: Int, onFailure: () -> Unit = {}, block: () -> R): Result<R> {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
......@@ -165,6 +165,7 @@ internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
} catch (e: Throwable) {
}
exception = e
onFailure()
}
}
return Result.failure(exception!!)
......
......@@ -18,6 +18,7 @@ import io.ktor.client.engine.cio.CIO
import io.ktor.client.features.HttpTimeout
import io.ktor.client.request.put
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.gradle.api.Project
......@@ -70,12 +71,20 @@ object GitHub {
)
}
fun upload(file: File, url: String, project: Project, repo: String, baseFilePath: String) = runBlocking {
fun upload(file: File, project: Project, repo: String, targetFilePath: String) = runBlocking {
val token = getGithubToken(project)
println("token.length=${token.length}")
retryCatching(1000) {
val url = "https://api.github.com/repos/mamoe/$repo/contents/$targetFilePath"
retryCatching(100, onFailure = { delay(30_000) }) { // 403 forbidden?
Http.put<String>("$url?access_token=$token") {
val sha = getGithubSha(repo, "$baseFilePath${project.name}/${file.name}", "master", project)
val sha = retryCatching(3, onFailure = { delay(30_000) }) {
getGithubSha(
repo,
targetFilePath,
"master",
project
)
}.getOrNull()
println("sha=$sha")
val content = String(Base64.getEncoder().encode(file.readBytes()))
body = """
......@@ -88,6 +97,7 @@ object GitHub {
}.let {
println("Upload response: $it")
}
delay(1000)
}.getOrThrow()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment