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
16import { ArkObfuscator } from '../ArkObfuscator';
17import { IOptions } from '../configs/IOptions';
18import { ReseverdSetForArkguard, readProjectPropertiesByCollectedPaths } from './ApiReader';
19import fs from 'fs';
20import path from 'path';
21
22/**
23 * Recursive retrieval of all subfile paths within a folder
24 * @param dir - Folder path to traverse
25 * @returns returns an array of all subfile paths
26 */
27function getAllFiles(inputPaths: string[]): Set<string> {
28  let results: Set<string> = new Set();
29  for (let inputPath of inputPaths) {
30    const stat = fs.statSync(inputPath);
31    if (stat.isDirectory()) {
32      getAllFilesInDirRecursively(inputPath, results);
33    } else if (stat.isFile()) {
34      results.add(inputPath);
35    }
36  }
37
38  return results;
39}
40
41function getAllFilesInDirRecursively(dir: string, results: Set<string>): void {
42  const list = fs.readdirSync(dir);
43  list.forEach((file) => {
44    const filePath = path.join(dir, file);
45    const stat = fs.statSync(filePath);
46
47    if (stat && stat.isDirectory()) {
48      getAllFilesInDirRecursively(filePath, results);
49    } else {
50      results.add(filePath);
51    }
52  });
53}
54
55/**
56 * read project reserved properties for UT
57 * @param projectPaths can be dir or file
58 * @param customProfiles
59 */
60export function readProjectProperties(projectPaths: string[], customProfiles: IOptions, arkObfuscator: ArkObfuscator): void {
61  const allPaths: Set<string> = getAllFiles(projectPaths);
62  let projectAndLibs: ReseverdSetForArkguard = readProjectPropertiesByCollectedPaths(allPaths, customProfiles, false);
63  if (customProfiles.mNameObfuscation.mRenameProperties) {
64    arkObfuscator.addReservedSetForPropertyObf(projectAndLibs);
65  }
66  if (customProfiles.mExportObfuscation) {
67    arkObfuscator.addReservedSetForDefaultObf(projectAndLibs);
68  }
69}