|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | + |
| 6 | +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.'); |
| 7 | + |
| 8 | +// Get __dirname in ESM |
| 9 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | + |
| 11 | +// Constants |
| 12 | +const RELEASE_DIRECTORY = path.join(__dirname, "releases"); |
| 13 | +const APK_OUTPUT_DIRECTORY = path.join(__dirname, "android", "app", "build", "outputs", "apk", "release"); |
| 14 | +const BUILD_GRADLE_FILE_PATH = path.join(__dirname, "android", "app", "build.gradle"); |
| 15 | +const DISTRIBUTION_DIRECTORY = path.join(__dirname, "android", "app", "src", "main", "assets", "public", "nodejs", "dist"); |
| 16 | +const PACKAGE_JSON_PATH = path.join(__dirname, "package.json"); |
| 17 | +const SIGNING_PROPERTIES_PATH = path.join(__dirname, "release-signing.properties"); |
| 18 | + |
| 19 | +// Function to retrieve the application version from package.json |
| 20 | +const getApplicationVersion = async () => { |
| 21 | + const { default: { version } } = await import("file://" + PACKAGE_JSON_PATH, { assert: { type: "json" } }); |
| 22 | + return version || ""; |
| 23 | +}; |
| 24 | + |
| 25 | +// Function to read signing properties from the properties file |
| 26 | +const readSigningProperties = () => { |
| 27 | + const properties = {}; |
| 28 | + if (fs.existsSync(SIGNING_PROPERTIES_PATH)) { |
| 29 | + const content = fs.readFileSync(SIGNING_PROPERTIES_PATH, "utf-8").split("\n"); |
| 30 | + content.forEach(line => { |
| 31 | + const [key, value] = line.split("="); |
| 32 | + if (key && value) { |
| 33 | + properties[key.trim()] = value.trim(); |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + if(properties.storeFile && (properties.storeFile.startsWith(".") || !(properties.storeFile.includes("/") || properties.storeFile.includes("\\")))) { |
| 38 | + properties.storeFile = path.join(__dirname, properties.storeFile); |
| 39 | + } |
| 40 | + return properties; |
| 41 | +}; |
| 42 | + |
| 43 | +// Update build.gradle file to include specified ABI |
| 44 | +const updateBuildGradleWithABI = (abi) => { |
| 45 | + let gradleContent = fs.readFileSync(BUILD_GRADLE_FILE_PATH, "utf-8"); |
| 46 | + gradleContent = gradleContent.replace(/include \x27[a-z0-9\- ,\x27]+/g, `include '${abi}'`); |
| 47 | + fs.writeFileSync(BUILD_GRADLE_FILE_PATH, gradleContent); |
| 48 | +}; |
| 49 | + |
| 50 | +// Execute shell command with error handling |
| 51 | +const executeCommand = (command) => { |
| 52 | + try { |
| 53 | + execSync(command, { stdio: "inherit" }); |
| 54 | + } catch (error) { |
| 55 | + console.error(`Command failed: ${command}`); |
| 56 | + process.exit(error.status); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +// Main build process |
| 61 | +const buildApplication = async () => { |
| 62 | + const applicationVersion = await getApplicationVersion(); |
| 63 | + const signingProperties = readSigningProperties(); |
| 64 | + |
| 65 | + if (!applicationVersion || !/^[0-9]+(\.[0-9]+)*$/.test(applicationVersion)) { |
| 66 | + console.error(`Application version is invalid or empty: ${applicationVersion}`); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + console.log(`Application Version: ${applicationVersion}`); |
| 71 | + |
| 72 | + const signedApkPath = path.join(APK_OUTPUT_DIRECTORY, "app-release-signed.apk"); |
| 73 | + const unsignedApkPath = path.join(APK_OUTPUT_DIRECTORY, "app-release-unsigned.apk"); |
| 74 | + |
| 75 | + if (fs.existsSync(signedApkPath)) { |
| 76 | + fs.unlinkSync(signedApkPath); |
| 77 | + } |
| 78 | + |
| 79 | + if (fs.existsSync(unsignedApkPath)) { |
| 80 | + fs.unlinkSync(unsignedApkPath); |
| 81 | + } |
| 82 | + |
| 83 | + // ARM64 build process |
| 84 | + updateBuildGradleWithABI("arm64-v8a"); |
| 85 | + if (fs.existsSync(path.join(DISTRIBUTION_DIRECTORY, "premium.js"))) { |
| 86 | + fs.unlinkSync(path.join(DISTRIBUTION_DIRECTORY, "premium.js")); |
| 87 | + } |
| 88 | + |
| 89 | + const arm64PremiumFilePath = path.join(__dirname, "compiled_premium", "premium-arm64.jsc"); |
| 90 | + const destinationPremiumFilePath = path.join(DISTRIBUTION_DIRECTORY, "premium.jsc"); |
| 91 | + |
| 92 | + // Copy ARM64 premium file if it exists |
| 93 | + if (fs.existsSync(arm64PremiumFilePath)) { |
| 94 | + fs.copyFileSync(arm64PremiumFilePath, destinationPremiumFilePath); |
| 95 | + } |
| 96 | + |
| 97 | + // Build command |
| 98 | + let buildCommand = `npx cap build android`; |
| 99 | + |
| 100 | + if (signingProperties.storeFile && signingProperties.storePassword && signingProperties.keyAlias && signingProperties.keyPassword) { |
| 101 | + console.log("Signing properties found. Signing APK..."); |
| 102 | + buildCommand += ` --keystorepath ${signingProperties.storeFile} --keystorepass ${signingProperties.storePassword} --keystorealias ${signingProperties.keyAlias} --keystorealiaspass ${signingProperties.keyPassword}`; |
| 103 | + } else { |
| 104 | + console.log("Signing properties not found. Building unsigned APK..."); |
| 105 | +} |
| 106 | + |
| 107 | + buildCommand += ` --androidreleasetype APK --signing-type apksigner`; |
| 108 | + executeCommand(buildCommand); |
| 109 | + |
| 110 | + const signedApkMtime = fs.existsSync(signedApkPath) ? fs.statSync(signedApkPath).mtime : 0; |
| 111 | + const unsignedApkMtime = fs.existsSync(unsignedApkPath) ? fs.statSync(unsignedApkPath).mtime : 0; |
| 112 | + |
| 113 | + const outputApkPath = signedApkMtime > unsignedApkMtime ? signedApkPath : unsignedApkPath; |
| 114 | + fs.renameSync(outputApkPath, path.join(RELEASE_DIRECTORY, `Megacubo_${applicationVersion}_android_arm64-v8a.apk`)); |
| 115 | + |
| 116 | + // ARM build process |
| 117 | + updateBuildGradleWithABI("armeabi-v7a"); |
| 118 | + |
| 119 | + const armPremiumFilePath = path.join(__dirname, "compiled_premium", "premium-arm.jsc"); |
| 120 | + |
| 121 | + // Copy ARM premium file if it exists |
| 122 | + if (fs.existsSync(armPremiumFilePath)) { |
| 123 | + fs.copyFileSync(armPremiumFilePath, destinationPremiumFilePath); |
| 124 | + } |
| 125 | + |
| 126 | + console.log("Building ARM as last to keep project files with ARM as default instead of ARM64..."); |
| 127 | + |
| 128 | + // Resetting buildCommand for ARM build |
| 129 | + buildCommand = `npx cap build android`; |
| 130 | + |
| 131 | + if (signingProperties.storeFile && signingProperties.storePassword && signingProperties.keyAlias && signingProperties.keyPassword) { |
| 132 | + buildCommand += ` --keystorepath ${signingProperties.storeFile} --keystorepass ${signingProperties.storePassword} --keystorealias ${signingProperties.keyAlias} --keystorealiaspass ${signingProperties.keyPassword}`; |
| 133 | + } |
| 134 | + |
| 135 | + buildCommand += ` --androidreleasetype APK --signing-type apksigner`; |
| 136 | + executeCommand(buildCommand); |
| 137 | + |
| 138 | + fs.renameSync( |
| 139 | + outputApkPath, |
| 140 | + path.join(RELEASE_DIRECTORY, `Megacubo_${applicationVersion}_android_armeabi-v7a.apk`) |
| 141 | + ); |
| 142 | + |
| 143 | + console.log(`Finished: ${new Date().toLocaleString()}`); |
| 144 | +}; |
| 145 | + |
| 146 | +buildApplication().catch(error => console.error("Build failed:", error)); |
0 commit comments