mtcute/packages/tl/scripts/process-descriptions-yaml.ts
Alina Tumanova f5976a2d74
ESM + end-to-end tests (#11)
* feat: moved tl-runtime to esm and native ArrayBuffers

* feat: migration to esm

* fix(core): web-related fixes

* test: finally, some good fucking e2e

* chore: fixed linters etc

* ci: added e2e to ci

* build(tl): fixed gen-code on node 20

* fix: codegen Uint8Array, not Buffer

never `git reset --hard` kids

* build: only do type-aware linting for `packages/*`

* build: ignore no-unresolved in ci for e2e

* fix: node 16 doesn't have subtle crypto apparently?

* fix(tests): use Uint8Array

for gods sake please can i just merge this already

* ci: don't parallel tasks in ci

because machines are utter garbage and it may just randomly break

* ci: pass secrets to e2e tests

* ci: separate cli command for ci

apparently im retarded

* fix: run codegen in e2e

im actually retarded

* ci: more fixes for e2e

* ci: debugging stuff

* ci: still debugging

* ci: hopefully fix ci???
2023-10-16 19:23:53 +03:00

141 lines
3.5 KiB
TypeScript

import { CachedDocumentation, CachedDocumentationEntry } from './documentation.js'
type MaybeOverwrite =
| string
| {
text: string
overwrite: boolean
}
interface RegexRule {
_cached?: RegExp
regex: string
flags?: string
repl: string
}
interface DescriptionsYaml {
objects: Record<
string,
{
desc?: MaybeOverwrite
arguments?: Record<string, MaybeOverwrite>
}
>
arguments: Record<string, string>
regex: RegexRule[]
}
function unwrapMaybe(what: MaybeOverwrite, has: boolean): string | undefined {
let text: string
let overwrite = false
if (typeof what === 'object') {
if (!what.text) throw new Error('Invalid overwrite object')
text = what.text
overwrite = what.overwrite
} else {
text = what
}
if (!has || overwrite) {
return text
}
return undefined
}
export function applyDescriptionsYamlFile(input: CachedDocumentation, yaml: unknown) {
const { objects: byObjects, arguments: byArguments, regex: byRegex } = yaml as DescriptionsYaml
// first create an index of all classes and methods
const objIndex: Record<string, CachedDocumentationEntry> = {}
function indexObject(obj: Record<string, CachedDocumentationEntry>, prefix: string) {
for (const name in obj) {
objIndex[prefix + name] = obj[name]!
}
}
indexObject(input.classes, 'c_')
indexObject(input.methods, 'm_')
// process byObjects
for (const name in byObjects) {
const rules = byObjects[name]!
const obj = objIndex[name]
if (!obj) continue
if (rules.desc) {
const desc = unwrapMaybe(rules.desc, Boolean(obj.comment))
if (desc) obj.comment = desc
}
if (rules.arguments) {
for (const arg in rules.arguments) {
const repl = unwrapMaybe(rules.arguments[arg]!, obj.arguments !== undefined && arg in obj.arguments)
if (repl) {
if (!obj.arguments) obj.arguments = {}
obj.arguments[arg] = repl
}
}
}
}
// process byArguments
for (const i in objIndex) {
const obj = objIndex[i]!
for (const arg in byArguments) {
if (obj.arguments && !(arg in obj.arguments)) continue
const repl = unwrapMaybe(byArguments[arg]!, Boolean(obj.arguments && arg in obj.arguments))
if (repl) {
if (!obj.arguments) obj.arguments = {}
obj.arguments[arg] = repl
}
}
}
// process byRegex
function applyRegex(str: string | undefined, rule: RegexRule) {
if (!str) return ''
if (!rule._cached) {
let flags = rule.flags || ''
if (!flags.includes('g')) flags += 'g'
rule._cached = new RegExp(rule.regex, flags)
}
return str.replace(rule._cached, rule.repl)
}
for (const i in objIndex) {
const obj = objIndex[i]!
byRegex.forEach((rule) => {
obj.comment = applyRegex(obj.comment, rule)
if (obj.arguments) {
for (const name in obj.arguments) {
obj.arguments[name] = applyRegex(obj.arguments[name], rule)
}
}
})
}
for (const i in input.unions) {
byRegex.forEach((rule) => {
input.unions[i] = applyRegex(input.unions[i], rule)
})
}
return input
}