1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const path_1 = __importDefault(require("path"));
|
7 | const fs_extra_1 = __importDefault(require("fs-extra"));
|
8 | const glob_1 = require("glob");
|
9 | function getPackage(config) {
|
10 | if (config.android && config.android.package) {
|
11 | return config.android.package;
|
12 | }
|
13 | return null;
|
14 | }
|
15 | exports.getPackage = getPackage;
|
16 | function getPackageRoot(projectRoot) {
|
17 | return path_1.default.join(projectRoot, 'android', 'app', 'src', 'main', 'java');
|
18 | }
|
19 | function getCurrentPackageName(projectRoot) {
|
20 | let packageRoot = getPackageRoot(projectRoot);
|
21 | let mainApplicationPath = glob_1.sync(path_1.default.join(packageRoot, '**', 'MainApplication.java'))[0];
|
22 | let packagePath = path_1.default.dirname(mainApplicationPath);
|
23 | let packagePathParts = packagePath
|
24 | .replace(packageRoot, '')
|
25 | .split(path_1.default.sep)
|
26 | .filter(Boolean);
|
27 | return packagePathParts.join('.');
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | function renamePackageOnDisk(config, projectRoot) {
|
33 | const newPackageName = getPackage(config);
|
34 | if (newPackageName === null) {
|
35 | return;
|
36 | }
|
37 | const currentPackageName = getCurrentPackageName(projectRoot);
|
38 | if (currentPackageName === newPackageName) {
|
39 | return;
|
40 | }
|
41 |
|
42 | let packageRoot = getPackageRoot(projectRoot);
|
43 | let currentPackagePath = path_1.default.join(packageRoot, ...currentPackageName.split('.'));
|
44 | let newPackagePath = path_1.default.join(packageRoot, ...newPackageName.split('.'));
|
45 |
|
46 | fs_extra_1.default.mkdirpSync(newPackagePath);
|
47 |
|
48 | glob_1.sync(path_1.default.join(currentPackagePath, '**', '*')).forEach(filepath => {
|
49 | let relativePath = filepath.replace(currentPackagePath, '');
|
50 | if (fs_extra_1.default.lstatSync(filepath).isFile()) {
|
51 | fs_extra_1.default.moveSync(filepath, path_1.default.join(newPackagePath, relativePath));
|
52 | }
|
53 | else {
|
54 | fs_extra_1.default.mkdirpSync(filepath);
|
55 | }
|
56 | });
|
57 |
|
58 |
|
59 | let oldPathParts = currentPackageName.split('.');
|
60 | while (oldPathParts.length) {
|
61 | let pathToCheck = path_1.default.join(packageRoot, ...oldPathParts);
|
62 | try {
|
63 | let files = fs_extra_1.default.readdirSync(pathToCheck);
|
64 | if (files.length === 0) {
|
65 | fs_extra_1.default.rmdirSync(pathToCheck);
|
66 | }
|
67 | }
|
68 | catch (_) {
|
69 | }
|
70 | finally {
|
71 | oldPathParts.pop();
|
72 | }
|
73 | }
|
74 | const filesToUpdate = [
|
75 | ...glob_1.sync(path_1.default.join(newPackagePath, '**', '*')),
|
76 | path_1.default.join(projectRoot, 'android', 'app', 'BUCK'),
|
77 | ];
|
78 |
|
79 | filesToUpdate.forEach((filepath) => {
|
80 | try {
|
81 | if (fs_extra_1.default.lstatSync(filepath).isFile()) {
|
82 | let contents = fs_extra_1.default.readFileSync(filepath).toString();
|
83 | contents = contents.replace(new RegExp(currentPackageName, 'g'), newPackageName);
|
84 | fs_extra_1.default.writeFileSync(filepath, contents);
|
85 | }
|
86 | }
|
87 | catch (_) { }
|
88 | });
|
89 | }
|
90 | exports.renamePackageOnDisk = renamePackageOnDisk;
|
91 | function setPackageInBuildGradle(config, buildGradle) {
|
92 | let packageName = getPackage(config);
|
93 | if (packageName === null) {
|
94 | return buildGradle;
|
95 | }
|
96 | let pattern = new RegExp(`applicationId ['"].*['"]`);
|
97 | return buildGradle.replace(pattern, `applicationId '${packageName}'`);
|
98 | }
|
99 | exports.setPackageInBuildGradle = setPackageInBuildGradle;
|
100 | async function setPackageInAndroidManifest(config, manifestDocument) {
|
101 | let packageName = getPackage(config);
|
102 | manifestDocument['manifest']['$']['package'] = packageName;
|
103 | return manifestDocument;
|
104 | }
|
105 | exports.setPackageInAndroidManifest = setPackageInAndroidManifest;
|
106 |
|
\ | No newline at end of file |