forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpost-process.js
More file actions
249 lines (210 loc) · 7.15 KB
/
post-process.js
File metadata and controls
249 lines (210 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* eslint-disable no-console */
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
const buildFilePath = path.resolve(__dirname, '../build.json');
const copyToPath = path.resolve(__dirname, '../platforms/android/build.json');
const gradleFilePath = path.resolve(__dirname, '../build-extras.gradle');
const androidGradleFilePath = path.resolve(
__dirname,
'../platforms/android/app/build-extras.gradle'
);
const resPath = path.resolve(__dirname, '../platforms/android/app/src/main/res/');
const localResPath = path.resolve(__dirname, '../res/android/');
if (
!fs.existsSync(copyToPath)
&& fs.existsSync(buildFilePath)
) fs.copyFileSync(buildFilePath, copyToPath);
if (fs.existsSync(androidGradleFilePath)) fs.unlinkSync(androidGradleFilePath);
fs.copyFileSync(gradleFilePath, androidGradleFilePath);
deleteDirRecursively(resPath, [
path.join('values', 'strings.xml'),
path.join('values', 'colors.xml'),
path.join('values', 'styles.xml'),
'anim',
'xml',
]);
copyDirRecursively(localResPath, resPath);
enableLegacyJni();
replaceMainActivity();
patchTargetSdkVersion();
function getTmpDir() {
const tmpdirEnv = process.env.TMPDIR;
if (tmpdirEnv) {
try {
fs.accessSync(tmpdirEnv, fs.constants.R_OK | fs.constants.W_OK);
return tmpdirEnv;
} catch {
// TMPDIR exists but not accessible
}
}
try {
fs.accessSync("/tmp", fs.constants.R_OK | fs.constants.W_OK);
return "/tmp";
} catch {
console.log("Error: No usable temporary directory found (TMPDIR or /tmp not accessible).");
return null;
// process.exit(1);
}
}
function patchTargetSdkVersion() {
const prefix = execSync('npm prefix').toString().trim();
const gradleFile = path.join(prefix, 'platforms/android/app/build.gradle');
if (!fs.existsSync(gradleFile)) {
console.warn('[Cordova Hook] ⚠️ build.gradle not found');
return;
}
let content = fs.readFileSync(gradleFile, 'utf-8');
const sdkRegex = /targetSdkVersion\s+(cordovaConfig\.SDK_VERSION|\d+)/;
if (sdkRegex.test(content)) {
let api = "35";
const tmp = getTmpDir();
if (tmp == null) {
console.warn("---------------------------------------------------------------------------------\n\n\n\n");
console.warn(`⚠️ fdroid.bool not found`);
console.warn("⚠️ Fdroid flavour will be built");
api = "28";
console.warn("\n\n\n\n---------------------------------------------------------------------------------");
} else {
const froidFlag = path.join(getTmpDir(), 'fdroid.bool');
if (fs.existsSync(froidFlag)) {
const fdroid = fs.readFileSync(froidFlag, 'utf-8').trim();
if (fdroid == "true") {
api = "28";
}
} else {
console.warn("---------------------------------------------------------------------------------\n\n\n\n");
console.warn(`⚠️ fdroid.bool not found`);
console.warn("⚠️ Fdroid flavour will be built");
api = "28";
console.warn("\n\n\n\n---------------------------------------------------------------------------------");
//process.exit(1);
}
}
content = content.replace(sdkRegex, 'targetSdkVersion ' + api);
fs.writeFileSync(gradleFile, content, 'utf-8');
console.log('[Cordova Hook] ✅ Patched targetSdkVersion to ' + api);
} else {
console.warn('[Cordova Hook] ⚠️ targetSdkVersion not found');
}
}
function enableLegacyJni() {
const prefix = execSync('npm prefix').toString().trim();
const gradleFile = path.join(prefix, 'platforms/android/app/build.gradle');
if (!fs.existsSync(gradleFile)) return;
let content = fs.readFileSync(gradleFile, 'utf-8');
// Check for correct block to avoid duplicate insertion
if (content.includes('useLegacyPackaging = true')) return;
// Inject under android block with correct Groovy syntax
content = content.replace(/android\s*{/, match => {
return (
match +
`
packagingOptions {
jniLibs {
useLegacyPackaging = true
}
}`
);
});
fs.writeFileSync(gradleFile, content, 'utf-8');
console.log('[Cordova Hook] ✅ Enabled legacy JNI packaging');
}
function replaceMainActivity() {
try {
const prefix = execSync('npm prefix').toString().trim();
const targetPath = path.join(
prefix,
'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
);
const sourcePath = path.join(
prefix,
'MainActivity.java'
);
if (!fs.existsSync(targetPath) || !fs.existsSync(sourcePath)) {
return;
}
let content = fs.readFileSync(sourcePath, 'utf-8');
fs.writeFileSync(targetPath, content, 'utf-8');
} catch (err) {
console.error('[Cordova Hook] ❌ Failed to replace MainActivity:', err.message);
}
}
/**
* Copy directory recursively
* @param {string} src Source directory
* @param {string} dest Destination directory
* @param {string[]} skip Files to not copy
*/
function copyDirRecursively(src, dest, skip = [], currPath = '') {
const exists = fs.existsSync(src);
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();
if (!exists) {
console.log(`File ${src} does not exist`);
return;
}
if (!fs.existsSync(dest) && isDirectory) {
fs.mkdirSync(dest);
}
if (exists && isDirectory) {
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach((childItemName) => {
const relativePath = path.join(currPath, childItemName);
if (childItemName.startsWith('.')) return;
if (skip.includes(childItemName) || skip.includes(relativePath)) return;
copyDirRecursively(
path.join(src, childItemName),
path.join(dest, childItemName),
skip,
childItemName,
);
});
} else {
fs.copyFileSync(src, dest);
// log
const message = `copied: ${path.basename(src)}`;
console.log('\x1b[32m%s\x1b[0m', message); // green
}
}
/**
* Delete directory recursively
* @param {string} dir Directory to delete
* @param {string[]} except Files to not delete
*/
function deleteDirRecursively(dir, except = [], currPath = '') {
const exists = fs.existsSync(dir);
const stats = exists && fs.statSync(dir);
const isDirectory = exists && stats.isDirectory();
if (!exists) {
console.log(`File ${dir} does not exist`);
return;
}
if (exists && isDirectory) {
let deleteDir = true;
fs.readdirSync(dir).forEach((childItemName) => {
const relativePath = path.join(currPath, childItemName);
if (
childItemName.startsWith('.')
|| except.includes(childItemName)
|| except.includes(relativePath)
) {
console.log('\x1b[33m%s\x1b[0m', `skipped: ${relativePath}`); // yellow
deleteDir = false;
return;
}
deleteDirRecursively(
path.join(dir, childItemName),
except,
childItemName,
);
});
if (deleteDir) {
console.log('\x1b[31m%s\x1b[0m', `deleted: ${currPath || path.basename(dir)}`); // red
fs.rmSync(dir, { recursive: true });
}
} else {
console.log('\x1b[31m%s\x1b[0m', `deleted: ${currPath || path.basename(dir)}`); // red
fs.rmSync(dir);
}
}