diff --git a/compiler/src/fast_build/ark_compiler/common/ob_config_resolver.ts b/compiler/src/fast_build/ark_compiler/common/ob_config_resolver.ts index b01c71e06681dfb5e58b57392ba65347eb34e5c2..aee592a481158ad6b9eefefbe738ede3fdd290db 100644 --- a/compiler/src/fast_build/ark_compiler/common/ob_config_resolver.ts +++ b/compiler/src/fast_build/ark_compiler/common/ob_config_resolver.ts @@ -56,12 +56,20 @@ import { stopEvent, writeArkguardObfuscatedSourceCode } from '../../../ark_utils'; -import { OBFUSCATION_TOOL, red, yellow } from './ark_define'; +import { + OBFUSCATION_TOOL, + red, + yellow, + EXTNAME_D_ETS, + EXTNAME_D_TS, + EXTNAME_PROTO_BIN, + EXTNAME_JS, + EXTNAME_TS, + } from './ark_define'; import { logger } from '../../../compile_info'; import { MergedConfig } from '../common/ob_config_resolver'; import { ModuleSourceFile } from '../module/module_source_file'; import { readProjectAndLibsSource } from './process_ark_config'; -import { CommonLogger } from '../logger'; import { MemoryMonitor } from '../../meomry_monitor/rollup-plugin-memory-monitor'; import { MemoryDefine } from '../../meomry_monitor/memory_define'; import { ESMODULE } from '../../../pre_define'; @@ -469,11 +477,62 @@ export async function handlePostObfuscationTasks( arkObfuscator.fileContentManager.writeFileNamesMap(); } + // enable filename obfuscation will remove redundant files + if (projectConfig.obfuscationMergedObConfig?.options?.enableFileNameObfuscation) { + const deletedFilesSet: Set | undefined = arkObfuscator?.filePathManager?.getDeletedSourceFilePaths(); + removeRedundantFiles(sourceProjectConfig, deletedFilesSet, projectConfig); + } + printTimeSumInfo('All files obfuscation:'); printTimeSumData(); endFilesEvent(EventList.ALL_FILES_OBFUSCATION); } +/** + * Remove redundant files + */ +export function removeRedundantFiles( + sourceProjectConfig: Object, + deletedFilesSet: Set | undefined, + projectConfig: Object, +): void { + const obfuscationCacheDir: string | undefined = sourceProjectConfig.obfuscationOptions.obfuscationCacheDir; + if (obfuscationCacheDir && deletedFilesSet && obfuscationCacheDir.length > 0 && deletedFilesSet.size > 0) { + const isByteHarOrHsp: boolean = projectConfig.compileShared || (projectConfig.compileHar && projectConfig.byteCodeHar); + const arkguardNameCache: string = fs.readFileSync(path.join(obfuscationCacheDir, 'nameCache.json'), 'utf8'); + const nameCacheCollection: Object = JSON.parse(arkguardNameCache); + const projectRootPath: string = projectConfig.projectRootPath.replace(/\\/g, '/'); + const productPath: string = path.resolve(projectConfig.aceModuleBuild, '../etsFortgz'); + const cachePath: string = projectConfig.cachePath; + deletedFilesSet.forEach((deletedFile) => { + let deletedFilePath: string = deletedFile.replace(projectRootPath, '').substring(1); + let obfName: string | undefined = nameCacheCollection[deletedFilePath]?.obfName; + if (!obfName) { + return; + } + let obfNameWithoutExt: string = obfName.substring(0, obfName.lastIndexOf(path.extname(obfName))); + let obfCacheFilePath: string = path.join(cachePath, obfNameWithoutExt); + removeFile(obfCacheFilePath, EXTNAME_D_ETS); + removeFile(obfCacheFilePath, EXTNAME_D_TS); + removeFile(obfCacheFilePath, EXTNAME_JS); + removeFile(obfCacheFilePath, EXTNAME_TS); + removeFile(obfCacheFilePath, EXTNAME_PROTO_BIN); + if (isByteHarOrHsp) { + let obfProductFilePath: string = path.join(productPath, obfNameWithoutExt); + removeFile(obfProductFilePath, EXTNAME_D_ETS); + removeFile(obfProductFilePath, EXTNAME_D_TS); + } + }); + } +} + +function removeFile(fileWithoutExt: string, ext: string): void { + let filePath = toUnixPath(fileWithoutExt + ext); + if (fs.existsSync(filePath)) { + fs.rmSync(filePath); + } +} + /** * Write obfuscation caches if needed */ diff --git a/compiler/src/fast_build/ark_compiler/utils.ts b/compiler/src/fast_build/ark_compiler/utils.ts index c4c94a77dd5c682088b0543fbf31982862de5b4f..562c1acae67eafd054e790fe55b56a7df41d7ea1 100644 --- a/compiler/src/fast_build/ark_compiler/utils.ts +++ b/compiler/src/fast_build/ark_compiler/utils.ts @@ -93,7 +93,7 @@ export function changeFileExtension(file: string, targetExt: string, originExt = return fileWithoutExt + targetExt; } -function removeCacheFile(cacheFilePath: string, ext: string): void { +export function removeCacheFile(cacheFilePath: string, ext: string): void { let filePath = toUnixPath(changeFileExtension(cacheFilePath, ext)); if (fs.existsSync(filePath)) { fs.rmSync(filePath); diff --git a/compiler/test/ark_compiler_ut/common/ob_config_resolver.test.ts b/compiler/test/ark_compiler_ut/common/ob_config_resolver.test.ts index a8045dafac6cc0dbe1e37efd4d2759d91232f141..ad88e252227c859d33b7b9704461e9acaa4cf7f7 100644 --- a/compiler/test/ark_compiler_ut/common/ob_config_resolver.test.ts +++ b/compiler/test/ark_compiler_ut/common/ob_config_resolver.test.ts @@ -38,7 +38,8 @@ import { getUpdatedFiles, obfuscationPreprocess, reObfuscate, - printObfLogger + printObfLogger, + removeRedundantFiles } from '../../../lib/fast_build/ark_compiler/common/ob_config_resolver'; import { OBFUSCATION_RULE_PATH, @@ -1056,5 +1057,57 @@ mocha.describe('test obfuscate config resolver api', function () { expect(writeArkguardObfuscatedSourceCodeStub.calledTwice).to.be.true; }); }); + + mocha.describe('10-6: removeRedundantFiles', function () { + let etsTestDir: string = 'test/ark_compiler_ut/testdata/testRemoveFiles/ets'; + let etsFortgzTestDir: string = 'test/ark_compiler_ut/testdata/testRemoveFiles/etsFortgz'; + let obfFilePath: string = 'test/ark_compiler_ut/testdata/testRemoveFiles/etsFortgz/a.d.ets'; + const sourceProjectConfig = { + obfuscationOptions: { + obfuscationCacheDir: 'test/ark_compiler_ut/testdata/testRemoveFiles/' + } + } + const projectConfig = { + projectRootPath: '', + cachePath: '', + aceModuleBuild: 'test/ark_compiler_ut/testdata/testRemoveFiles/ets', + compileShared: true + } + mocha.before(function () { + if (!fs.existsSync(etsTestDir)) { + fs.mkdirSync(etsTestDir, { recursive: true }); + } + if (!fs.existsSync(etsFortgzTestDir)) { + fs.mkdirSync(etsFortgzTestDir, { recursive: true }); + } + if (!fs.existsSync(obfFilePath)) { + fs.writeFileSync(obfFilePath, 'test'); + } + }); + + mocha.after(function () { + if (fs.existsSync(obfFilePath)) { + fs.unlinkSync(obfFilePath); + } + if (fs.existsSync(etsFortgzTestDir)) { + fs.rmdirSync(etsFortgzTestDir, { recursive: true }); + } + if (fs.existsSync(etsTestDir)) { + fs.rmdirSync(etsTestDir, { recursive: true }); + } + }); + + mocha.it('10-6-1: should not be remove when file is not obfuscated', async function () { + let deletedFilesSet: Set = new Set(['/test1.ets']); + await removeRedundantFiles(sourceProjectConfig, deletedFilesSet, projectConfig); + expect(fs.existsSync(obfFilePath)).to.be.true; + }); + + mocha.it('10-6-2: should be remove when file is obfuscated', async function () { + let deletedFilesSet: Set = new Set(['/test.ets']); + await removeRedundantFiles(sourceProjectConfig, deletedFilesSet, projectConfig); + expect(fs.existsSync(obfFilePath)).to.be.false; + }); + }); }); }); \ No newline at end of file diff --git a/compiler/test/ark_compiler_ut/testdata/testRemoveFiles/nameCache.json b/compiler/test/ark_compiler_ut/testdata/testRemoveFiles/nameCache.json new file mode 100644 index 0000000000000000000000000000000000000000..1cdad14d856377aac3deee7a506a1c558ac0fe0d --- /dev/null +++ b/compiler/test/ark_compiler_ut/testdata/testRemoveFiles/nameCache.json @@ -0,0 +1,5 @@ +{ + "test.ets": { + "obfName": "a.ets" + } +} \ No newline at end of file