forked from phillip055/ktorio.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.kt
executable file
·148 lines (123 loc) · 5.03 KB
/
migrate.kt
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
#!/usr/bin/env kscript
/**
* This is a script I use as a sandbox to perform several migration tasks
* that are tedious and error-prone to do by hand.
*
* * https://github.com/holgerbrandl/kscript
*
* * kscript --idea migrate.kt
*
*/
@file:MavenRepository("ktor", "https://kotlin.bintray.com/ktor")
@file:DependsOn("io.ktor:ktor-server-netty:0.9.4")
@file:DependsOn("io.ktor:ktor-client-core:0.9.4")
@file:DependsOn("io.ktor:ktor-client-apache:0.9.4")
@file:DependsOn("io.ktor:ktor-websockets:0.9.4")
@file:DependsOn("com.fasterxml.jackson.core:jackson-databind:2.9.4")
@file:DependsOn("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.0")
@file:EntryPoint("Migrate")
import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.dataformat.yaml.*
import kotlinx.coroutines.experimental.*
import java.io.*
object Migrate {
@JvmStatic
fun main(args: Array<String>) {
runBlocking {
MigrateFeatures.migrateFeatures()
}
}
}
object MigrateFeatures {
val base = File("${System.getenv("HOME")}/projects/ktorio.github.io")
val yaml = ObjectMapper(YAMLFactory().apply {
this.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
this.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
})
suspend fun migrateFeatures() {
migrateFolder(base["features"]!!, base["servers/features"]!!)
}
suspend fun migrateFolder(fromFolder: File, toFolder: File) {
if (!fromFolder.exists()) error("$fromFolder do not exists")
val fromRelative = fromFolder.relativeTo(base)
val toRelative = toFolder.relativeTo(base)
println("Processing... $fromRelative -> $toRelative")
for (baseName in fromFolder.list()) {
val relativePath = "/$fromRelative/$baseName"
val defaultOldPermalink = relativePath.replace(".md", ".html")
val file = fromFolder[baseName]
if (file.isDirectory) {
migrateFolder(fromFolder[baseName], toFolder[baseName])
continue
}
if (!file.exists()) {
continue
}
if (!baseName.endsWith(".md") && !baseName.endsWith(".html")) {
println(" * $relativePath")
fromFolder[baseName].copyTo(toFolder[baseName], overwrite = true)
continue
}
println(" - $relativePath")
//println("----------")
//println(defaultPermalink)
val lines = file!!.readLines()
val info = parseFrontMatter(lines)
// Only do stuff it has frontmatter
if (info != null) {
val oldPermalink = info["permalink"]?.toString() ?: defaultOldPermalink
val permalinkBasename = oldPermalink.substringAfterLast('/')
val newPermalink = "/$toRelative/$permalinkBasename"
val newCategory = toRelative.path.substringBefore('/')
info["category"] = newCategory
if (newPermalink != oldPermalink) {
info["redirect_from"] = (((info["redirect_from"] as? List<String>?) ?: listOf()) + listOf(
oldPermalink
)).distinct()
}
if (info["permalink"] != null) {
info["permalink"] = newPermalink
}
if (info["children"] != null) {
info["children"] = "/$toRelative/$permalinkBasename".replace(".html", "") + "/"
}
val newLines = replaceFrontMatter(lines, info)
val newContent = newLines.joinToString("\n")
toFolder[baseName].ensureFolders().writeText(newContent)
//println(newContent)
//file.writeText(newContent)
}
}
}
fun replaceFrontMatter(lines: List<String>, info: Any): List<String> =
listOf("---") + yaml.writeValueAsString(info).trimEnd().lines() + listOf("---") + stripFrontMatter(lines)
fun stripFrontMatter(lines: List<String>): List<String> {
if (lines.firstOrNull() == "---") {
for (n in 1 until lines.size) {
if (lines[n] == "---") {
return lines.drop(n + 1)
}
}
}
return lines
}
fun parseFrontMatter(lines: List<String>): LinkedHashMap<String, Any?>? {
if (lines.firstOrNull() == "---") {
for (n in 1 until lines.size) {
if (lines[n] == "---") {
val frontMatterContent = lines.subList(1, n).joinToString("\n")
val value = yaml.readValue(frontMatterContent, LinkedHashMap::class.java)
return value as LinkedHashMap<String, Any?>
}
}
}
return null
}
}
operator fun File.get(name: String): File = when {
!name.contains('/') -> File(this, name)
else -> File(this, name.substringBefore('/'))[name.substringAfter('/', "")]
}
fun File.ensureFolders(): File = this.apply {
this.parentFile.mkdirs()
}