idlize

Форк
0
/
common.ts 
89 строк · 2.9 Кб
1
/*
2
 * Copyright (c) 2024 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import * as fs from "fs"
17
import * as path from "path"
18
import { IDLEntry } from "../idl"
19
import * as webidl2 from "webidl2"
20
import { toIDLNode } from "./deserialize";
21
import { zip } from "../util";
22

23
export function fromIDL(
24
    inputDir: string,
25
    inputFile: string | undefined,
26
    outputDir: string,
27
    extension: string,
28
    verbose: boolean,
29
    transform: (name: string, content: string) => string
30
): void {
31
    inputDir = path.resolve(inputDir)
32
    const files: string[] =
33
        inputFile
34
            ? [path.join(inputDir, inputFile)]
35
            : fs.readdirSync(inputDir)
36
                .map((elem: string) => path.join(inputDir, elem))
37

38
    const results: string[] =
39
        files
40
            .map((file: string) => transform(file, fs.readFileSync(file).toString()))
41

42
    zip(files, results)
43
        .forEach(([fileName, output]: [string, string]) => {
44
            fs.mkdirSync(outputDir, { recursive : true })
45
            const outFile = path.join(
46
                outputDir,
47
                path.basename(fileName).replace(".idl", extension)
48
            )
49
            if (verbose) console.log(output)
50
            fs.writeFileSync(outFile, licence.concat(output))
51
        })
52
}
53

54
export function scanIDL(
55
    inputDir: string,
56
    inputFile: string | undefined,
57
): IDLEntry[][] {
58
    inputDir = path.resolve(inputDir)
59
    const files: string[] =
60
        inputFile
61
            ? [path.join(inputDir, inputFile)]
62
            : fs.readdirSync(inputDir)
63
                .map((elem: string) => path.join(inputDir, elem))
64

65
    return files
66
        .map((file: string) => {
67
            let content = fs.readFileSync(file).toString()
68
            let parsed = webidl2.parse(content)
69
            return parsed.map(it => toIDLNode(file, it))
70
        })
71
}
72

73
export const licence =
74
`/*
75
 * Copyright (c) 2024 Huawei Device Co., Ltd.
76
 * Licensed under the Apache License, Version 2.0 (the "License");
77
 * you may not use this file except in compliance with the License.
78
 * You may obtain a copy of the License at
79
 *
80
 * http://www.apache.org/licenses/LICENSE-2.0
81
 *
82
 * Unless required by applicable law or agreed to in writing, software
83
 * distributed under the License is distributed on an "AS IS" BASIS,
84
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
85
 * See the License for the specific language governing permissions and
86
 * limitations under the License.
87
 */
88

89
`
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.