-
Notifications
You must be signed in to change notification settings - Fork 69
/
build.mjs
146 lines (115 loc) · 5.95 KB
/
build.mjs
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
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
console.log('This script will build the Megacubo APKs for ARM and ARM64 architectures, building PC installers is not covered yet. Remember to run \'npm run prepare\' before running this script.');
// Get __dirname in ESM
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Constants
const RELEASE_DIRECTORY = path.join(__dirname, "releases");
const APK_OUTPUT_DIRECTORY = path.join(__dirname, "android", "app", "build", "outputs", "apk", "release");
const BUILD_GRADLE_FILE_PATH = path.join(__dirname, "android", "app", "build.gradle");
const DISTRIBUTION_DIRECTORY = path.join(__dirname, "android", "app", "src", "main", "assets", "public", "nodejs", "dist");
const PACKAGE_JSON_PATH = path.join(__dirname, "package.json");
const SIGNING_PROPERTIES_PATH = path.join(__dirname, "release-signing.properties");
// Function to retrieve the application version from package.json
const getApplicationVersion = async () => {
const { default: { version } } = await import("file://" + PACKAGE_JSON_PATH, { assert: { type: "json" } });
return version || "";
};
// Function to read signing properties from the properties file
const readSigningProperties = () => {
const properties = {};
if (fs.existsSync(SIGNING_PROPERTIES_PATH)) {
const content = fs.readFileSync(SIGNING_PROPERTIES_PATH, "utf-8").split("\n");
content.forEach(line => {
const [key, value] = line.split("=");
if (key && value) {
properties[key.trim()] = value.trim();
}
});
}
if(properties.storeFile && (properties.storeFile.startsWith(".") || !(properties.storeFile.includes("/") || properties.storeFile.includes("\\")))) {
properties.storeFile = path.join(__dirname, properties.storeFile);
}
return properties;
};
// Update build.gradle file to include specified ABI
const updateBuildGradleWithABI = (abi) => {
let gradleContent = fs.readFileSync(BUILD_GRADLE_FILE_PATH, "utf-8");
gradleContent = gradleContent.replace(/include \x27[a-z0-9\- ,\x27]+/g, `include '${abi}'`);
fs.writeFileSync(BUILD_GRADLE_FILE_PATH, gradleContent);
};
// Execute shell command with error handling
const executeCommand = (command) => {
try {
execSync(command, { stdio: "inherit" });
} catch (error) {
console.error(`Command failed: ${command}`);
process.exit(error.status);
}
};
// Main build process
const buildApplication = async () => {
const applicationVersion = await getApplicationVersion();
const signingProperties = readSigningProperties();
if (!applicationVersion || !/^[0-9]+(\.[0-9]+)*$/.test(applicationVersion)) {
console.error(`Application version is invalid or empty: ${applicationVersion}`);
process.exit(1);
}
console.log(`Application Version: ${applicationVersion}`);
const signedApkPath = path.join(APK_OUTPUT_DIRECTORY, "app-release-signed.apk");
const unsignedApkPath = path.join(APK_OUTPUT_DIRECTORY, "app-release-unsigned.apk");
if (fs.existsSync(signedApkPath)) {
fs.unlinkSync(signedApkPath);
}
if (fs.existsSync(unsignedApkPath)) {
fs.unlinkSync(unsignedApkPath);
}
// ARM64 build process
updateBuildGradleWithABI("arm64-v8a");
if (fs.existsSync(path.join(DISTRIBUTION_DIRECTORY, "premium.js"))) {
fs.unlinkSync(path.join(DISTRIBUTION_DIRECTORY, "premium.js"));
}
const arm64PremiumFilePath = path.join(__dirname, "compiled_premium", "premium-arm64.jsc");
const destinationPremiumFilePath = path.join(DISTRIBUTION_DIRECTORY, "premium.jsc");
// Copy ARM64 premium file if it exists
if (fs.existsSync(arm64PremiumFilePath)) {
fs.copyFileSync(arm64PremiumFilePath, destinationPremiumFilePath);
}
// Build command
let buildCommand = `npx cap build android`;
if (signingProperties.storeFile && signingProperties.storePassword && signingProperties.keyAlias && signingProperties.keyPassword) {
console.log("Signing properties found. Signing APK...");
buildCommand += ` --keystorepath ${signingProperties.storeFile} --keystorepass ${signingProperties.storePassword} --keystorealias ${signingProperties.keyAlias} --keystorealiaspass ${signingProperties.keyPassword}`;
} else {
console.log("Signing properties not found. Building unsigned APK...");
}
buildCommand += ` --androidreleasetype APK --signing-type apksigner`;
executeCommand(buildCommand);
const signedApkMtime = fs.existsSync(signedApkPath) ? fs.statSync(signedApkPath).mtime : 0;
const unsignedApkMtime = fs.existsSync(unsignedApkPath) ? fs.statSync(unsignedApkPath).mtime : 0;
const outputApkPath = signedApkMtime > unsignedApkMtime ? signedApkPath : unsignedApkPath;
fs.renameSync(outputApkPath, path.join(RELEASE_DIRECTORY, `Megacubo_${applicationVersion}_android_arm64-v8a.apk`));
// ARM build process
updateBuildGradleWithABI("armeabi-v7a");
const armPremiumFilePath = path.join(__dirname, "compiled_premium", "premium-arm.jsc");
// Copy ARM premium file if it exists
if (fs.existsSync(armPremiumFilePath)) {
fs.copyFileSync(armPremiumFilePath, destinationPremiumFilePath);
}
console.log("Building ARM as last to keep project files with ARM as default instead of ARM64...");
// Resetting buildCommand for ARM build
buildCommand = `npx cap build android`;
if (signingProperties.storeFile && signingProperties.storePassword && signingProperties.keyAlias && signingProperties.keyPassword) {
buildCommand += ` --keystorepath ${signingProperties.storeFile} --keystorepass ${signingProperties.storePassword} --keystorealias ${signingProperties.keyAlias} --keystorealiaspass ${signingProperties.keyPassword}`;
}
buildCommand += ` --androidreleasetype APK --signing-type apksigner`;
executeCommand(buildCommand);
fs.renameSync(
outputApkPath,
path.join(RELEASE_DIRECTORY, `Megacubo_${applicationVersion}_android_armeabi-v7a.apk`)
);
console.log(`Finished: ${new Date().toLocaleString()}`);
};
buildApplication().catch(error => console.error("Build failed:", error));