|
7 | 7 | */ |
8 | 8 | const nodefs = require("node:fs"); |
9 | 9 | const path = require("node:path"); |
| 10 | +const tty = require("node:tty"); |
10 | 11 | const { |
11 | 12 | findNearest, |
12 | 13 | getPackageVersion, |
@@ -42,6 +43,72 @@ const cliPlatformIOSVersion = (() => { |
42 | 43 | }; |
43 | 44 | })(); |
44 | 45 |
|
| 46 | +/** |
| 47 | + * Configures Gradle wrapper as necessary before the Android app is built. |
| 48 | + * @param {string} sourceDir |
| 49 | + */ |
| 50 | +function configureGradleWrapper(sourceDir, fs = nodefs) { |
| 51 | + const androidCommands = ["build-android", "run-android"]; |
| 52 | + if ( |
| 53 | + process.env["RNTA_CONFIGURE_GRADLE_WRAPPER"] === "0" || |
| 54 | + !process.argv.some((arg) => androidCommands.includes(arg)) |
| 55 | + ) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + const version = toVersionNumber( |
| 61 | + getPackageVersion("react-native", sourceDir, fs) |
| 62 | + ); |
| 63 | + |
| 64 | + const gradleWrapperProperties = path.join( |
| 65 | + sourceDir, |
| 66 | + "gradle", |
| 67 | + "wrapper", |
| 68 | + "gradle-wrapper.properties" |
| 69 | + ); |
| 70 | + const props = readTextFile(gradleWrapperProperties); |
| 71 | + const re = /gradle-([.0-9]*?)-.*?\.zip/; |
| 72 | + const m = props.match(re); |
| 73 | + if (!m) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const gradleVersion = (() => { |
| 78 | + const gradleVersion = toVersionNumber(m[1]); |
| 79 | + if (version === 0 || version >= v(0, 74, 0)) { |
| 80 | + if (gradleVersion < v(8, 6, 0)) { |
| 81 | + return "8.6"; |
| 82 | + } |
| 83 | + } else if (version >= v(0, 73, 0)) { |
| 84 | + if (gradleVersion < v(8, 3, 0)) { |
| 85 | + return "8.3"; |
| 86 | + } |
| 87 | + } else if (version >= v(0, 72, 0)) { |
| 88 | + if (gradleVersion < v(8, 1, 1)) { |
| 89 | + return "8.1.1"; |
| 90 | + } |
| 91 | + } else if (gradleVersion < v(7, 5, 1) || gradleVersion >= v(8, 0, 0)) { |
| 92 | + return "7.6.4"; |
| 93 | + } |
| 94 | + return undefined; |
| 95 | + })(); |
| 96 | + |
| 97 | + if (gradleVersion) { |
| 98 | + const tag = tty.WriteStream.prototype.hasColors() |
| 99 | + ? "\u001B[33m\u001B[1mwarn\u001B[22m\u001B[39m" |
| 100 | + : "warn"; |
| 101 | + console.warn(tag, `Setting Gradle version ${gradleVersion}`); |
| 102 | + fs.writeFileSync( |
| 103 | + gradleWrapperProperties, |
| 104 | + props.replace(re, `gradle-${gradleVersion}-bin.zip`) |
| 105 | + ); |
| 106 | + } |
| 107 | + } catch (_) { |
| 108 | + // ignore |
| 109 | + } |
| 110 | +} |
| 111 | + |
45 | 112 | /** |
46 | 113 | * @param {string} sourceDir |
47 | 114 | * @returns {string} |
@@ -109,6 +176,7 @@ function configureProjects({ android, ios, windows }, fs = nodefs) { |
109 | 176 | path.resolve(projectRoot, android.sourceDir) |
110 | 177 | ), |
111 | 178 | }; |
| 179 | + configureGradleWrapper(android.sourceDir, fs); |
112 | 180 | } |
113 | 181 |
|
114 | 182 | if (ios) { |
|
0 commit comments