16
import { error } from "node:console";
17
import * as fs from "node:fs"
18
import * as path from "node:path"
19
import { argv } from 'process'
24
for (let i = 2; i < argv.length; ++i) {
45
let archPath = 'arm64-v8a'
48
archPath = 'armeabi-v7a'
51
const libName = `libNativeBridge_ohos_${arch}.so`
52
const libSource = `native/${libName}`
55
const libsDir = `ohos-app/api_perf/entry/libs/`
56
const libTargetDir = `${libsDir}/${archPath}`
57
const libTarget = `${libTargetDir}/${libName}`
58
const codesTargetDir = 'ohos-app/api_perf/entry/src/main/ets/idlize'
59
const koalauiModulesDir = 'peer_lib/ts/@koalaui'
60
const arkoalaArkuiSrcDir = `generated/${isFull ? 'peers' : 'subset'}/koalaui/arkoala-arkui/src`
61
const peerNodePath = `${arkoalaArkuiSrcDir}/PeerNode.ts`
62
const testDtsDir = `tests/subset/ets`
63
const commonFilePath = path.join(codesTargetDir, 'common.ts')
64
const testDtsPath = path.join(codesTargetDir, 'test.d.ts')
65
let ohosSdkRoot = process.env.OHOS_SDK ?? '../koala-ui/ohos-sdk/ohos-sdk'
66
let ohosSdkVersion = process.env.OHOS_SDK_VERSION ?? 'HarmonyOS-NEXT-DP1'
67
let sysroot = `${ohosSdkRoot}/${ohosSdkVersion}/base/native/sysroot/`
68
let llvm = `${ohosSdkRoot}/${ohosSdkVersion}/base/native/llvm/`
69
let target = `${isArm64 ? 'aarch64-linux-ohos' : 'arm-linux-ohos'}`
71
if (fs.existsSync(codesTargetDir)) fs.rmSync(codesTargetDir, { recursive: true })
72
if (fs.existsSync(libTargetDir)) fs.rmSync(libTargetDir, { recursive: true })
73
if (!fs.existsSync(libsDir)) fs.mkdirSync(libsDir)
74
fs.mkdirSync(codesTargetDir)
75
if (!fs.existsSync(libTargetDir)) fs.mkdirSync(libTargetDir)
78
function resolveLibDependency() {
81
let cppSharedLibPath = `${llvm}/lib/${target}/`
82
let cppLibName = `libc++_shared.so`
83
console.log(`copy ${cppSharedLibPath}/${cppLibName} ${libTargetDir}/${cppLibName}`)
84
fs.copyFileSync(`${cppSharedLibPath}/${cppLibName}`, `${libTargetDir}/${cppLibName}`)
87
console.log(`copy ${libSource} ${libTarget}`)
88
fs.copyFileSync(libSource, libTarget)
100
const testDtsWhiteList = []
102
function getCommonFilePath() {
103
return commonFilePath
106
function getTestDtsPath() {
110
function copyTestDts(sourceDir, specificFile = undefined) {
111
let resolveTestDts = file => {
112
const sourceFile = path.join(sourceDir, specificFile ?? file)
113
if (fs.lstatSync(sourceFile).isDirectory()) {
114
copyTestDts(sourceFile)
117
console.log(`sourceFile : ${sourceFile}`)
118
if (file.endsWith('.d.ts') || testDtsWhiteList.includes(file)) {
119
console.log(`sourceFile endsWith : ${sourceFile}`)
120
let data = fs.readFileSync(sourceFile, 'utf8')
121
let expectedData = data
122
fs.appendFileSync(getTestDtsPath(), expectedData, 'utf8', (error) => {
124
console.error(`appendFile ${getTestDtsPath()} error :`, error)
127
console.log(`appendFile ${getTestDtsPath()} successfully`)
132
fs.readdirSync(sourceDir).forEach(resolveTestDts)
134
resolveTestDts(sourceDir)
138
function copyAndFixPeerFiles(sourceDir, codesTargetDir, isCommon) {
139
const importUtil = 'import util from \'@ohos.util\'\n'
140
let initString = `${importUtil}
142
export class ArkComponent {
143
protected peer?: NativePeerNode
144
setPeer(peer: NativePeerNode) {
150
fs.readFile(peerNodePath, 'utf8', (error, data) => {
152
console.error(`read ${peerNodePath} error : `, error)
155
initString += data.replace(/.*@koalaui\/common"/g, '')
156
initString = initString.replace(/.*@koalaui\/arkoala"/g, '')
157
initString = initString.replace(/.*@koalaui\/interop"/g, '')
158
initString = initString.replace(/.*@koalaui\/runtime"/g, '')
159
fs.writeFile(getCommonFilePath(), initString, 'utf8', (error) => {
161
console.error(`Init ${getCommonFilePath()} error : `, error)
164
console.log(`Init ${getCommonFilePath()} successfully`);
169
fs.readdirSync(sourceDir).forEach(file => {
170
const sourceFile = path.join(sourceDir, file);
171
if (fs.lstatSync(sourceFile).isDirectory()) {
172
copyAndFixPeerFiles(sourceFile, codesTargetDir, isCommon)
174
const targetFile = path.join(codesTargetDir, file);
175
let condition = file.endsWith('.ts')
176
if (!blackList.includes(file) && (condition || whiteList.includes(file))) {
177
fs.readFile(sourceFile, 'utf8', (error, data) => {
179
console.error(`read ${sourceFile} error : `, error)
184
expectedData = data.replace(/import.*/g, '')
185
expectedData = expectedData.replace(/new TextDecoder/g, 'new util.TextDecoder')
186
fs.appendFile(getCommonFilePath(), expectedData, 'utf8', (error) => {
188
console.error(`appendFile ${getCommonFilePath()} error :`, error)
191
console.log(`appendFile ${getCommonFilePath()} successfully`);
194
expectedData = data.replace(/@arkoala\/arkui/g, '.')
195
expectedData = expectedData.replace(/@koalaui\/arkoala/g, './common')
196
expectedData = expectedData.replace(/@koalaui\/common/g, './common')
197
expectedData = expectedData.replace(/@koalaui\/interop/g, './common')
198
expectedData = expectedData.replace(/@koalaui\/runtime/g, './common')
199
expectedData = expectedData.replace(/implements.*/g, '{')
200
if (file === "NativeModule.ts") {
201
const requireStatement = 'LOAD_NATIVE as NativeModule'
202
const requireNapiStatement = `globalThis.requireNapi("libNativeBridge_ohos_${arch}.so", true)`
203
expectedData = expectedData.replace(requireStatement, requireNapiStatement)
205
if (expectedData.includes('new TextDecoder') || expectedData.includes('new TextEncoder')) {
206
expectedData = expectedData.replace(/new TextDecoder/g, 'new util.TextDecoder')
207
expectedData = expectedData.replace(/new TextEncoder/g, 'new util.TextEncoder')
208
expectedData = importUtil + expectedData
210
fs.writeFile(targetFile, expectedData, 'utf8', (error) => {
212
console.error(`write ${targetFile} error :`, error)
215
console.log(`write ${targetFile} successfully`);
223
resolveLibDependency()
224
copyAndFixPeerFiles(koalauiModulesDir, codesTargetDir, true)
225
if (!isFull) copyTestDts(testDtsDir)
227
console.log(`copy ${isFull ? 'full' : 'subset'} peer codes to ohos project.`)
228
copyAndFixPeerFiles(arkoalaArkuiSrcDir, codesTargetDir)