-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-types.gradle.kts
More file actions
225 lines (207 loc) · 9.59 KB
/
Copy pathnode-types.gradle.kts
File metadata and controls
225 lines (207 loc) · 9.59 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.gradle.api.tasks.Exec
import java.io.File
private val autoJs6NodeTypeDeclarationModules = listOf(
"toast",
"app",
"dialogs",
"clipboard",
"device",
"shell",
"engines",
"accessibility",
"media_projection",
"image",
"images",
"ocr",
"storage",
"storages",
"database",
"sqlite",
"console",
"timers",
"files",
"base64",
"colors",
"formatter",
"converter",
"s13n",
"mime",
"nanoid",
"util",
"opencc",
"pinyin",
"pinyin4j",
"rhino",
"notifications",
"sensors",
"fetch",
"axios",
"websocket",
"work_manager",
"plugins",
"media_store",
)
private fun nodeTypesDisplayPath(file: File): String =
runCatching { file.relativeTo(rootProject.projectDir).invariantSeparatorsPath }
.getOrElse { file.absolutePath }
private fun nodeTypesReadJsonObject(file: File): Map<*, *> =
JsonSlurper().parse(file) as? Map<*, *>
?: throw GradleException("Expected JSON object in ${file.absolutePath}")
private fun nodeTypesStringList(value: Any?): List<String> =
(value as? List<*>).orEmpty().map(Any?::toString)
val verifyAutoJs6NodeTypeDeclarations = tasks.register("verifyAutoJs6NodeTypeDeclarations") {
group = "verification"
description = "Verifies plugin-owned AutoJs6 Node TypeScript declarations and the TypeScript smoke sample."
val examplesRoot = rootProject.projectDir.resolve("sample/nodejs")
val typesRoot = rootProject.projectDir.resolve("docs/nodejs/types/autojs6-node")
val sampleRoot = examplesRoot.resolve("typescript-smoke")
val examplesManifestFile = examplesRoot.resolve("examples.json")
val jsonReport = layout.buildDirectory.file("reports/nodejs/typescript-declarations.json")
val markdownReport = layout.buildDirectory.file("reports/nodejs/typescript-declarations.md")
inputs.dir(typesRoot)
inputs.dir(sampleRoot)
inputs.file(examplesManifestFile)
outputs.file(jsonReport)
outputs.file(markdownReport)
doLast {
val indexFile = typesRoot.resolve("index.d.ts")
val commonFile = typesRoot.resolve("common.d.ts")
val profileCapabilitiesFile = typesRoot.resolve("profile_capabilities.d.ts")
val sampleTsConfig = sampleRoot.resolve("tsconfig.json")
val sampleSource = sampleRoot.resolve("src/main.ts")
val requiredFiles = listOf(indexFile, commonFile, profileCapabilitiesFile, sampleTsConfig, sampleSource)
val missingFiles = requiredFiles.filterNot(File::isFile).map(::nodeTypesDisplayPath)
if (missingFiles.isNotEmpty()) {
throw GradleException("Missing AutoJs6 Node TypeScript required file(s): ${missingFiles.joinToString()}")
}
val indexText = indexFile.readText()
val profileCapabilitiesText = profileCapabilitiesFile.readText()
val sampleText = sampleSource.readText()
val tsConfigText = sampleTsConfig.readText()
val rows = autoJs6NodeTypeDeclarationModules.map { moduleName ->
val declaration = typesRoot.resolve("$moduleName.d.ts")
if (!declaration.isFile) {
throw GradleException("Missing TypeScript declaration for Node bridge module '$moduleName'.")
}
val declarationText = declaration.readText()
if (!declarationText.contains("""declare module "$moduleName"""")) {
throw GradleException("${nodeTypesDisplayPath(declaration)} must declare module \"$moduleName\".")
}
if (!indexText.contains("$moduleName.d.ts")) {
throw GradleException("index.d.ts must reference $moduleName.d.ts.")
}
if (!sampleText.contains("""require("$moduleName")""")) {
throw GradleException("typescript-smoke sample must import '$moduleName'.")
}
linkedMapOf(
"module" to moduleName,
"declaration" to nodeTypesDisplayPath(declaration),
"bytes" to declaration.length(),
)
}
if (!tsConfigText.contains("../../../docs/nodejs/types/autojs6-node/**/*.d.ts")) {
throw GradleException("typescript-smoke tsconfig.json must include the plugin-owned AutoJs6 Node declarations.")
}
if (!indexText.contains("profile_capabilities.d.ts")) {
throw GradleException("index.d.ts must reference profile_capabilities.d.ts.")
}
if (!profileCapabilitiesText.contains("ProfileModuleCatalog") ||
!profileCapabilitiesText.contains("ProfileModuleModeFor") ||
!profileCapabilitiesText.contains("desktop_compat_opt_in") ||
!profileCapabilitiesText.contains("NodeProfileV12CapabilityCatalog") ||
!profileCapabilitiesText.contains("NodeProfileV12DeclarationMatrix")
) {
throw GradleException("profile_capabilities.d.ts must declare the profile-aware module catalog and conditional helpers.")
}
if (!sampleText.contains("""ProfileModuleModeFor<"java", "safe_default">""") ||
!sampleText.contains("""ProfileModuleModeFor<"worker_threads", "desktop_compat_opt_in">""") ||
!sampleText.contains("ProfileDeclarationMatrix") ||
!sampleText.contains("""NodeProfileV12ModeFor<"ui.overlay", "pro_compat_opt_in">""") ||
!sampleText.contains("NodeProfileV12DeclarationMatrix")
) {
throw GradleException("typescript-smoke sample must cover Safe/Pro/Desktop profile declaration differences.")
}
val examplesManifest = nodeTypesReadJsonObject(examplesManifestFile)
val examples = (examplesManifest["examples"] as? List<*>).orEmpty().map {
it as? Map<*, *> ?: throw GradleException("Each Node example manifest entry must be an object.")
}
val typeSmoke = examples.singleOrNull { it["name"] == "typescript-smoke" }
?: throw GradleException("${nodeTypesDisplayPath(examplesManifestFile)} must include the typescript-smoke example.")
if ("typescript" !in nodeTypesStringList(typeSmoke["tags"])) {
throw GradleException("typescript-smoke example must carry the 'typescript' tag.")
}
val report = linkedMapOf<String, Any?>(
"schema" to "autojs6-node-types-report-v1",
"owner" to "AutoJs6-Plugin-NodeJs-Runtime",
"typesRoot" to nodeTypesDisplayPath(typesRoot),
"sample" to nodeTypesDisplayPath(sampleRoot),
"examplesManifest" to nodeTypesDisplayPath(examplesManifestFile),
"moduleCount" to rows.size,
"modules" to rows,
)
jsonReport.get().asFile.apply {
parentFile.mkdirs()
writeText(JsonOutput.prettyPrint(JsonOutput.toJson(report)) + "\n")
}
markdownReport.get().asFile.apply {
parentFile.mkdirs()
writeText(
buildString {
appendLine("# AutoJs6 Node TypeScript Declaration Validation")
appendLine()
appendLine("| Module | Declaration | Bytes |")
appendLine("| --- | --- | ---: |")
rows.forEach { row ->
appendLine("| ${row["module"]} | ${row["declaration"]} | ${row["bytes"]} |")
}
},
)
}
logger.lifecycle("Verified ${rows.size} plugin-owned AutoJs6 Node TypeScript declaration modules.")
}
}
val generateNodeTypescriptSmokeTsconfig = tasks.register("generateNodeTypescriptSmokeTsconfig") {
group = "verification"
description = "Generates a tsconfig for the plugin-owned Node TypeScript smoke sample."
val sampleRoot = rootProject.projectDir.resolve("sample/nodejs/typescript-smoke")
val typesRoot = rootProject.projectDir.resolve("docs/nodejs/types/autojs6-node")
val generatedTsconfig = layout.buildDirectory.file("generated/nodejs/typescript-smoke/tsconfig.json")
inputs.file(sampleRoot.resolve("tsconfig.json"))
inputs.dir(sampleRoot.resolve("src"))
inputs.dir(typesRoot)
outputs.file(generatedTsconfig)
doLast {
val sourceInclude = sampleRoot.resolve("src").absolutePath.replace('\\', '/') + "/**/*.ts"
val typesInclude = typesRoot.absolutePath.replace('\\', '/') + "/**/*.d.ts"
val tsconfig = linkedMapOf<String, Any?>(
"compilerOptions" to linkedMapOf(
"target" to "ES2020",
"module" to "CommonJS",
"moduleResolution" to "node",
"strict" to true,
"noEmit" to true,
"skipLibCheck" to false,
"lib" to listOf("ES2020"),
"types" to emptyList<String>(),
),
"include" to listOf(sourceInclude, typesInclude),
)
generatedTsconfig.get().asFile.apply {
parentFile.mkdirs()
writeText(JsonOutput.prettyPrint(JsonOutput.toJson(tsconfig)) + "\n")
}
}
}
tasks.register<Exec>("typeCheckNodeTypescriptSmoke") {
group = "verification"
description = "Runs tsc against the plugin-owned Node TypeScript smoke sample and declarations."
dependsOn(verifyAutoJs6NodeTypeDeclarations, generateNodeTypescriptSmokeTsconfig)
val tsconfig = layout.buildDirectory.file("generated/nodejs/typescript-smoke/tsconfig.json")
if (System.getProperty("os.name").startsWith("Windows", ignoreCase = true)) {
commandLine("cmd", "/c", "tsc", "-p", tsconfig.get().asFile.absolutePath, "--pretty", "false")
} else {
commandLine("tsc", "-p", tsconfig.get().asFile.absolutePath, "--pretty", "false")
}
}