|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +// Function to read Android SDK version from build.gradle |
| 5 | +function getAndroidSdkVersion() { |
| 6 | + try { |
| 7 | + const gradlePath = path.join(__dirname, "../../android/build.gradle"); |
| 8 | + const gradleContent = fs.readFileSync(gradlePath, "utf8"); |
| 9 | + |
| 10 | + // Extract all SDK versions from ext block |
| 11 | + const minSdkMatch = gradleContent.match(/minSdkVersion\s*=\s*(\d+)/); |
| 12 | + const compileSdkMatch = gradleContent.match( |
| 13 | + /compileSdkVersion\s*=\s*(\d+)/ |
| 14 | + ); |
| 15 | + const targetSdkMatch = gradleContent.match(/targetSdkVersion\s*=\s*(\d+)/); |
| 16 | + |
| 17 | + const versions = { |
| 18 | + minSdk: minSdkMatch ? minSdkMatch[1] : null, |
| 19 | + compileSdk: compileSdkMatch ? compileSdkMatch[1] : null, |
| 20 | + targetSdk: targetSdkMatch ? targetSdkMatch[1] : null, |
| 21 | + }; |
| 22 | + |
| 23 | + if (minSdkMatch) console.log(`Found minSdkVersion: ${minSdkMatch[1]}`); |
| 24 | + if (compileSdkMatch) |
| 25 | + console.log(`Found compileSdkVersion: ${compileSdkMatch[1]}`); |
| 26 | + if (targetSdkMatch) |
| 27 | + console.log(`Found targetSdkVersion: ${targetSdkMatch[1]}`); |
| 28 | + |
| 29 | + return versions; |
| 30 | + } catch (error) { |
| 31 | + console.error("Error reading Android SDK version:", error); |
| 32 | + return null; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Function to read Gradle version from build.gradle |
| 37 | +function getGradleVersion() { |
| 38 | + try { |
| 39 | + const gradlePath = path.join(__dirname, "../../android/build.gradle"); |
| 40 | + const gradleContent = fs.readFileSync(gradlePath, "utf8"); |
| 41 | + |
| 42 | + // Extract Gradle version |
| 43 | + const gradleMatch = gradleContent.match( |
| 44 | + /classpath\s+['"]com\.android\.tools\.build:gradle:([\d.]+)['"]/ |
| 45 | + ); |
| 46 | + if (gradleMatch) { |
| 47 | + console.log(`Found Gradle version: ${gradleMatch[1]}`); |
| 48 | + return gradleMatch[1]; |
| 49 | + } |
| 50 | + |
| 51 | + console.log("Could not find Gradle version in build.gradle"); |
| 52 | + return null; |
| 53 | + } catch (error) { |
| 54 | + console.error("Error reading Gradle version:", error); |
| 55 | + return null; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Function to read iOS minimum SDK version from Podfile |
| 60 | +function getIosMinSdkVersion() { |
| 61 | + try { |
| 62 | + const podfilePath = path.join(__dirname, "../../ios/Podfile"); |
| 63 | + const podfileContent = fs.readFileSync(podfilePath, "utf8"); |
| 64 | + |
| 65 | + // Extract deployment_target value |
| 66 | + const deploymentTargetMatch = podfileContent.match( |
| 67 | + /deployment_target\s*=\s*['"]([\d.]+)['"]/ |
| 68 | + ); |
| 69 | + if (deploymentTargetMatch) { |
| 70 | + console.log(`Found iOS deployment target: ${deploymentTargetMatch[1]}`); |
| 71 | + return deploymentTargetMatch[1]; |
| 72 | + } |
| 73 | + |
| 74 | + console.log("Could not find iOS deployment target in Podfile"); |
| 75 | + return null; |
| 76 | + } catch (error) { |
| 77 | + console.error("Error reading iOS SDK version:", error); |
| 78 | + return null; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Function to update versions.json with new SDK versions |
| 83 | +function updateVersionsJson() { |
| 84 | + try { |
| 85 | + // Read mendix_version.json to get the latest version |
| 86 | + const mendixVersionsPath = path.join( |
| 87 | + __dirname, |
| 88 | + "../../mendix_version.json" |
| 89 | + ); |
| 90 | + const mendixVersionsContent = fs.readFileSync(mendixVersionsPath, "utf8"); |
| 91 | + const mendixVersions = JSON.parse(mendixVersionsContent); |
| 92 | + |
| 93 | + // Get the latest version (first key in the object) |
| 94 | + const latestMendixVersion = Object.keys(mendixVersions)[0]; |
| 95 | + const latestNativeTemplateVersion = mendixVersions[latestMendixVersion].min; |
| 96 | + |
| 97 | + // Read current native_template_versions.json |
| 98 | + const nativeTemplateVersionsPath = path.join( |
| 99 | + __dirname, |
| 100 | + "native_template_versions.json" |
| 101 | + ); |
| 102 | + const nativeTemplateVersionsContent = fs.readFileSync( |
| 103 | + nativeTemplateVersionsPath, |
| 104 | + "utf8" |
| 105 | + ); |
| 106 | + const nativeTemplateVersions = JSON.parse(nativeTemplateVersionsContent); |
| 107 | + |
| 108 | + // Get current SDK versions |
| 109 | + const androidSdk = getAndroidSdkVersion(); |
| 110 | + const gradle = getGradleVersion(); |
| 111 | + const iosMinSdk = getIosMinSdkVersion(); |
| 112 | + |
| 113 | + // Check if the version already exists in native_template_versions.json |
| 114 | + if (!nativeTemplateVersions[latestMendixVersion]) { |
| 115 | + // Create new object with the latest version at the top |
| 116 | + const updatedVersions = { |
| 117 | + [latestMendixVersion]: { |
| 118 | + max: mendixVersions[latestMendixVersion].max, |
| 119 | + min: latestNativeTemplateVersion, |
| 120 | + androidSdk: { |
| 121 | + min: androidSdk?.minSdk || null, |
| 122 | + compile: androidSdk?.compileSdk || null, |
| 123 | + target: androidSdk?.targetSdk || null, |
| 124 | + }, |
| 125 | + gradle: gradle, |
| 126 | + iosMinSdk: iosMinSdk, |
| 127 | + }, |
| 128 | + ...nativeTemplateVersions, |
| 129 | + }; |
| 130 | + |
| 131 | + // Write updated versions back to file |
| 132 | + fs.writeFileSync( |
| 133 | + nativeTemplateVersionsPath, |
| 134 | + JSON.stringify(updatedVersions, null, 4) |
| 135 | + ); |
| 136 | + } else { |
| 137 | + console.log( |
| 138 | + `Version ${latestMendixVersion} already exists in native_template_versions.json` |
| 139 | + ); |
| 140 | + } |
| 141 | + } catch (error) { |
| 142 | + console.error("Error updating versions:", error); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Export functions for use in other scripts |
| 147 | +module.exports = { |
| 148 | + getAndroidSdkVersion, |
| 149 | + getGradleVersion, |
| 150 | + getIosMinSdkVersion, |
| 151 | + updateVersionsJson, |
| 152 | +}; |
0 commit comments