diff --git a/compiler/src/interop/main.js b/compiler/src/interop/main.js index 38e8a5075d37e9382cc8e328002e56f2efb237a3..c76d06e6092707d88a4fea3cb1c089d05a6b2dfa 100644 --- a/compiler/src/interop/main.js +++ b/compiler/src/interop/main.js @@ -44,7 +44,7 @@ const { } = require('log4js'); const { - entryFileLanguageInfo, + setEntryFileLanguage, isMixCompile, processAbilityPagesFullPath, transformAbilityPages @@ -512,11 +512,11 @@ function readAbilityEntrance(moduleJson) { if (moduleSrcEntry) { abilityPages.push(moduleSrcEntry); abilityPagesFullPath.add(getAbilityFullPath(projectConfig.projectPath, moduleSrcEntry)); - entryFileLanguageInfo.set(moduleSrcEntry, isStatic); + setEntryFileLanguage(moduleSrcEntry, isStatic); } else if (moduleSrcEntrance) { abilityPages.push(moduleSrcEntrance); abilityPagesFullPath.add(getAbilityFullPath(projectConfig.projectPath, moduleSrcEntrance)); - entryFileLanguageInfo.set(moduleSrcEntrance, isStatic); + setEntryFileLanguage(moduleSrcEntrance, isStatic); } if (moduleJson.module.abilities && moduleJson.module.abilities.length > 0) { setEntrance(moduleJson.module.abilities, abilityPages); @@ -535,11 +535,11 @@ function setEntrance(abilityConfig, abilityPages) { const isStatic = ability.arkTSMode === ARKTS_MODE.STATIC; if (ability.srcEntry) { abilityPages.push(ability.srcEntry); - entryFileLanguageInfo.set(ability.srcEntry, isStatic); + setEntryFileLanguage(ability.srcEntry, isStatic); abilityPagesFullPath.add(getAbilityFullPath(projectConfig.projectPath, ability.srcEntry)); } else if (ability.srcEntrance) { abilityPages.push(ability.srcEntrance); - entryFileLanguageInfo.set(ability.srcEntrance, isStatic); + setEntryFileLanguage(ability.srcEntrance, isStatic); abilityPagesFullPath.add(getAbilityFullPath(projectConfig.projectPath, ability.srcEntrance)); } }); diff --git a/compiler/src/interop/src/fast_build/ark_compiler/interop/interop_manager.ts b/compiler/src/interop/src/fast_build/ark_compiler/interop/interop_manager.ts index 42c47e2e34c870b046fbe7a6a9e67178b1b38ca5..4ec3efcc8d9dee4c0057eb1c10c37528a0fe2a38 100644 --- a/compiler/src/interop/src/fast_build/ark_compiler/interop/interop_manager.ts +++ b/compiler/src/interop/src/fast_build/ark_compiler/interop/interop_manager.ts @@ -57,10 +57,14 @@ import { } from './pre_define'; import { readFirstLineSync } from './utils'; -export let entryFileLanguageInfo = new Map(); +let entryFileLanguageInfo = new Map(); export let workerFile = null; export let mixCompile = undefined; +export function setEntryFileLanguage(filePath: string, language: string): void { + entryFileLanguageInfo.set(filePath, language); +} + export class FileManager { private static instance: FileManager | undefined = undefined; @@ -72,7 +76,7 @@ export class FileManager { static mixCompile: boolean = false; static glueCodeFileInfos: Map = new Map(); static isInteropSDKEnabled: boolean = false; - static sharedObj: Object | undefined = undefined; + interopConfig: InteropConfig | undefined = undefined; private constructor() { } @@ -113,14 +117,18 @@ export class FileManager { return FileManager.instance; } - public static setRollUpObj(shared: Object): void { - FileManager.sharedObj = shared; - } - public static setMixCompile(mixCompile: boolean): void { FileManager.mixCompile = mixCompile; } + public setInteropConfig(interopConfig: InteropConfig): void { + this.interopConfig = interopConfig; + } + + public getInteropConfig(): InteropConfig { + return this.interopConfig; + } + private static initLanguageVersionFromDependentModuleMap( dependentModuleMap: Map ): void { @@ -313,11 +321,7 @@ export class FileManager { } private static logError(error: LogData): void { - if (FileManager.sharedObj) { - CommonLogger.getInstance(FileManager.sharedObj).printErrorAndExit(error); - } else { - console.error(error.toString()); - } + console.error(error.toString()); } private static matchSDKPath(path: string): { @@ -380,22 +384,22 @@ export class FileManager { } } -export function initFileManagerInRollup(share: Object): void { - if (!share.projectConfig.mixCompile) { +export function initFileManagerInRollup(InteropConfig: InteropConfig): void { + if (!isMixCompile()) { return; } FileManager.mixCompile = true; - const sdkInfo = collectSDKInfo(share); + const sdkInfo = collectSDKInfo(InteropConfig); FileManager.init( - share.projectConfig.dependentModuleMap, - share.projectConfig.aliasPaths, + InteropConfig.projectConfig.dependentModuleMap, + InteropConfig.projectConfig.aliasPaths, sdkInfo.dynamicSDKPath, sdkInfo.staticSDKInteropDecl, sdkInfo.staticSDKGlueCodePath ); - FileManager.setRollUpObj(share); + FileManager.getInstance().setInteropConfig(InteropConfig); } export function collectSDKInfo(share: Object): { @@ -498,7 +502,7 @@ export function processAbilityPagesFullPath(abilityPagesFullPath: Set): export function transformAbilityPages(abilityPath: string): boolean { - const entryBridgeCodePath = process.env.entryBridgeCodePath; + const entryBridgeCodePath = getBrdigeCodeRootPath(abilityPath, FileManager.getInstance().getInteropConfig()); if (!entryBridgeCodePath) { const errInfo = LogDataFactory.newInstance( ErrorCode.ETS2BUNDLE_INTERNAL_MISSING_BRIDGECODE_PATH_INFO, @@ -521,9 +525,12 @@ export function transformAbilityPages(abilityPath: string): boolean { return false; } -function transformModuleNameToRelativePath(moduleName): string { +export function transformModuleNameToRelativePath(filePath: string): string { let defaultSourceRoot = 'src/main'; - const normalizedModuleName = moduleName.replace(/\\/g, '/'); + if (FileManager.getInstance().getInteropConfig()?.projectConfig?.isOhosTest) { + defaultSourceRoot = 'src/ohosTest'; + } + const normalizedModuleName = filePath.replace(/\\/g, '/'); const normalizedRoot = defaultSourceRoot.replace(/\\/g, '/'); const rootIndex = normalizedModuleName.indexOf(`/${normalizedRoot}/`); @@ -532,15 +539,16 @@ function transformModuleNameToRelativePath(moduleName): string { ErrorCode.ETS2BUNDLE_INTERNAL_WRONG_MODULE_NAME_FROM_ACEMODULEJSON, ArkTSInternalErrorDescription, `defaultSourceRoot '${defaultSourceRoot}' not found ` + - `when process moduleName '${moduleName}'` + `when process moduleName '${filePath}'` ); throw Error(errInfo.toString()); } - const relativePath = normalizedModuleName.slice(rootIndex + normalizedRoot.length + 1); + const relativePath = normalizedModuleName.slice(rootIndex + normalizedRoot.length + 1).replace(/^\/+/, ''); return './' + relativePath; } + export function getApiPathForInterop(apiDirs: string[], languageVersion: string): void { if (languageVersion !== ARKTS_1_2) { return; @@ -592,6 +600,8 @@ export function rebuildEntryObj(projectConfig: Object, interopConfig: InteropCon * As the entry for mix compile,so mixCompile status will be set true */ export function initConfigForInterop(interopConfig: InteropConfig): Object { + initFileManagerInRollup(interopConfig); + function getEntryObj(): void { loadEntryObj(projectConfig); initBuildInfo(); @@ -642,4 +652,5 @@ export function destroyInterop(): void { FileManager.cleanFileManagerObject(); entryFileLanguageInfo.clear(); mixCompile = false; -} \ No newline at end of file +} + diff --git a/compiler/src/interop/src/fast_build/ark_compiler/interop/type.ts b/compiler/src/interop/src/fast_build/ark_compiler/interop/type.ts index c4eae6d787d01e7849e84bc34bb2f28f084fa346..cb4b3a29ea850cd50e2b8e6bab823639be24949f 100644 --- a/compiler/src/interop/src/fast_build/ark_compiler/interop/type.ts +++ b/compiler/src/interop/src/fast_build/ark_compiler/interop/type.ts @@ -120,4 +120,5 @@ export interface InteropInfo { export interface InteropConfig { interopModuleInfo: Map; + projectConfig: Object; } diff --git a/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts b/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts index 519808aae9d354f5186c8c0f740a4caa9a28e80b..dd986076f062c32502d48d8a75abc6d1c147bb83 100644 --- a/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts +++ b/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts @@ -76,6 +76,7 @@ export function etsChecker() { const recordInfo = MemoryMonitor.recordStage(MemoryDefine.ROLLUP_PLUGIN_BUILD_START); const hookEventFactory: CompileEvent = getHookEventFactory(this.share, 'etsChecker', 'buildStart'); const eventServiceChecker = createAndStartEvent(hookEventFactory, 'serviceChecker'); + // remove after hvigor use another api initFileManagerInRollup(this.share); if (process.env.watchMode === 'true' && process.env.triggerTsWatch === 'true') { tsWatchEmitter = new EventEmitter(); diff --git a/compiler/test/ark_compiler_ut/interop/interop_manager.test.ts b/compiler/test/ark_compiler_ut/interop/interop_manager.test.ts index 3891f4512b6d7d206c4ce57cc74b9b0ad09ea733..5f3569ad7629b1266c3ca160ea5c19c310048c83 100644 --- a/compiler/test/ark_compiler_ut/interop/interop_manager.test.ts +++ b/compiler/test/ark_compiler_ut/interop/interop_manager.test.ts @@ -24,11 +24,13 @@ import { getBrdigeCodeRootPath, isMixCompile, initConfigForInterop, - destroyInterop + destroyInterop, + transformModuleNameToRelativePath } from '../../../lib/fast_build/ark_compiler/interop/interop_manager'; import { ARKTS_1_1, ARKTS_1_2, ARKTS_HYBRID } from '../../../lib/fast_build/ark_compiler/interop/pre_define'; import { sdkConfigs } from '../../../main'; import { toUnixPath } from '../../../lib/utils'; +import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; export interface ArkTSEvolutionModule { language: string; @@ -357,19 +359,64 @@ mocha.describe('test getBrdigeCodeRootPath api', function () { }); }); -mocha.describe('test mixCompile',function(){ - mocha.it('1-1 test mixCompile is false',function(){ +mocha.describe('test mixCompile', function () { + mocha.it('1-1 test mixCompile is false', function () { expect(isMixCompile()).to.be.false; }) - mocha.it('1-2 test mixCompile is true',function(){ - initConfigForInterop(); + mocha.it('1-2 test mixCompile is true', function () { + this.rollup = new RollUpPluginMock(); + this.rollup.build(); + this.rollup.share.projectConfig.mixCompile = true; + initConfigForInterop(this.rollup.share); expect(isMixCompile()).to.be.true; }) - mocha.it('1-3 test mixCompile is false when destroy interop',function(){ - initConfigForInterop(); + mocha.it('1-3 test mixCompile is false when destroy interop', function () { + this.rollup = new RollUpPluginMock(); + this.rollup.build(); + this.rollup.share.projectConfig.mixCompile = true; + initConfigForInterop(this.rollup.share); destroyInterop() expect(isMixCompile()).to.be.false; }) }) + +mocha.describe('test transformModuleNameToRelativePath api', function () { + mocha.before(function () { + this.rollup = new RollUpPluginMock(); + this.rollup.build(); + this.rollup.share.projectConfig.mixCompile = true; + initConfigForInterop(this.rollup.share); + }); + + mocha.it('1-1 test transformModuleNameToRelativePath when compile common module', function () { + this.rollup.share.projectConfig.isOhosTest = false; + + const moduleName = '/a/b/c/src/main/ets/module/file.ets'; + const result = transformModuleNameToRelativePath(moduleName); + + expect(result).to.equal('./ets/module/file.ets'); + }); + + mocha.it('1-2 test transformModuleNameToRelativePath when compile ohostest', function () { + this.rollup.share.projectConfig.isOhosTest = true; + initConfigForInterop(this.rollup.share); + const moduleName = '/a/b/c/src/ohosTest/ets/module/testfile.ets'; + const result = transformModuleNameToRelativePath(moduleName); + + expect(result).to.equal('./ets/module/testfile.ets'); + }); + + mocha.it('1-3 test transformModuleNameToRelativePath throws when sourceRoot not in path', function () { + this.rollup.share.projectConfig.isOhosTest = false; + + const invalidModuleName = '/a/b/c/invalidroot/ets/module/file.ets'; + + expect(() => transformModuleNameToRelativePath(invalidModuleName)).to.throw(Error); + }); + + mocha.after(() => { + delete this.rollup; + }); +})