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
315 lines (263 loc) · 9.53 KB
/
post-process.js
File metadata and controls
315 lines (263 loc) · 9.53 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* 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);
// Cordova Android 15 generates `cdv_*` resources and version-qualified value
// directories that are required later in the build. Keep the generated tree and
// only overlay this project's custom resources on top of it.
copyDirRecursively(localResPath, resPath);
enableLegacyJni();
enableStaticContext();
patchTargetSdkVersion();
enableKeyboardWorkaround();
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 = "36";
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 enableStaticContext() {
try {
const prefix = execSync('npm prefix').toString().trim();
const mainActivityPath = path.join(
prefix,
'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
);
if (!fs.existsSync(mainActivityPath)) {
return;
}
let content = fs.readFileSync(mainActivityPath, 'utf-8');
// Skip if fully patched
if (
content.includes('WeakReference<Context>') &&
content.includes('public static Context getContext()') &&
content.includes('weakContext = new WeakReference<>(this);')
) {
return;
}
// Add missing imports
if (!content.includes('import java.lang.ref.WeakReference;')) {
content = content.replace(
/import org\.apache\.cordova\.\*;/,
match =>
match +
'\nimport android.content.Context;\nimport java.lang.ref.WeakReference;'
);
}
// Inject static field and method into class body
content = content.replace(
/public class MainActivity extends CordovaActivity\s*\{/,
match =>
match +
`\n\n private static WeakReference<Context> weakContext;\n\n` +
` public static Context getContext() {\n` +
` return weakContext != null ? weakContext.get() : null;\n` +
` }\n`
);
// Insert weakContext assignment inside onCreate
content = content.replace(
/super\.onCreate\(savedInstanceState\);/,
`super.onCreate(savedInstanceState);\n weakContext = new WeakReference<>(this);`
);
fs.writeFileSync(mainActivityPath, content, 'utf-8');
} catch (err) {
console.error('[Cordova Hook] ❌ Failed to patch MainActivity:', err.message);
}
}
function enableKeyboardWorkaround() {
try{
const prefix = execSync('npm prefix').toString().trim();
const mainActivityPath = path.join(
prefix,
'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
);
if (!fs.existsSync(mainActivityPath)) {
return;
}
let content = fs.readFileSync(mainActivityPath, 'utf-8');
// Skip if already patched
if (content.includes('SoftInputAssist')) {
return;
}
// Add import
if (!content.includes('import com.foxdebug.system.SoftInputAssist;')) {
content = content.replace(
/import java.lang.ref.WeakReference;|import org\.apache\.cordova\.\*;/,
match =>
match + '\nimport com.foxdebug.system.SoftInputAssist;'
);
}
// Declare field
if (!content.includes('private SoftInputAssist softInputAssist;')) {
content = content.replace(
/public class MainActivity extends CordovaActivity\s*\{/,
match =>
match +
`\n\n private SoftInputAssist softInputAssist;\n`
);
}
// Initialize in onCreate
content = content.replace(
/loadUrl\(launchUrl\);/,
`loadUrl(launchUrl);\n\n softInputAssist = new SoftInputAssist(this);`
);
fs.writeFileSync(mainActivityPath, content, 'utf-8');
console.log('[Cordova Hook] ✅ Enabled keyboard workaround');
} catch (err) {
console.error('[Cordova Hook] ❌ Failed to enable keyboard workaround:', 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,
relativePath,
);
});
} else {
removeConflictingResourceFiles(src, dest);
fs.copyFileSync(src, dest);
// log
const message = `copied: ${path.basename(src)}`;
console.log('\x1b[32m%s\x1b[0m', message); // green
}
}
function removeConflictingResourceFiles(src, dest) {
const parentDir = path.dirname(dest);
if (!fs.existsSync(parentDir)) {
return;
}
const resourceDirName = path.basename(parentDir);
if (!resourceDirName.startsWith('mipmap') && !resourceDirName.startsWith('drawable')) {
return;
}
const srcExt = path.extname(src);
const resourceName = path.basename(src, srcExt);
for (const existingName of fs.readdirSync(parentDir)) {
const existingPath = path.join(parentDir, existingName);
if (existingPath === dest || !fs.statSync(existingPath).isFile()) {
continue;
}
const existingExt = path.extname(existingName);
const existingResourceName = path.basename(existingName, existingExt);
if (existingResourceName !== resourceName || existingExt === srcExt) {
continue;
}
fs.rmSync(existingPath);
console.log('\x1b[31m%s\x1b[0m', `deleted conflicting resource: ${existingName}`);
}
}