From c7154955a96a875dd2fcfdd824815eb16784e623 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 5 Apr 2024 16:07:00 +0200 Subject: [PATCH 1/2] fix(cli): Don't downgrade gradle version on migrate --- cli/src/tasks/migrate.ts | 92 +++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 53 deletions(-) diff --git a/cli/src/tasks/migrate.ts b/cli/src/tasks/migrate.ts index dc4071444a..dbe2f21db4 100644 --- a/cli/src/tasks/migrate.ts +++ b/cli/src/tasks/migrate.ts @@ -1,6 +1,7 @@ import { writeFileSync, readFileSync, existsSync } from '@ionic/utils-fs'; import { join } from 'path'; import rimraf from 'rimraf'; +import { coerce, gt, gte } from 'semver'; import { getAndroidPlugins } from '../android/common'; import c from '../colors'; @@ -187,6 +188,36 @@ export async function migrateCommand( allDependencies['@capacitor/android'] && existsSync(config.android.platformDirAbs) ) { + const gradleWrapperVersion = getGradleWrapperVersion( + join( + config.android.platformDirAbs, + 'gradle', + 'wrapper', + 'gradle-wrapper.properties', + ), + ); + + if (!installFailed && gt(gradleVersion, gradleWrapperVersion)) { + try { + await runTask(`Upgrading gradle wrapper files`, () => { + return updateGradleWrapperFiles(config.android.platformDirAbs); + }); + } catch (e: any) { + if (e.includes('EACCES')) { + logger.error( + `gradlew file does not have executable permissions. This can happen if the Android platform was added on a Windows machine. Please run ${c.input( + `chmod +x ./${config.android.platformDir}/gradlew`, + )} and ${c.input( + `cd ${config.android.platformDir} && ./gradlew wrapper --distribution-type all --gradle-version ${gradleVersion} --warning-mode all`, + )} to update the files manually`, + ); + } else { + logger.error(`gradle wrapper files were not updated ${e}`); + } + } + } else { + logger.warn('Skipped upgrading gradle wrapper files'); + } await runTask(`Migrating build.gradle file.`, () => { return updateBuildGradle( join(config.android.platformDirAbs, 'build.gradle'), @@ -229,21 +260,6 @@ export async function migrateCommand( }, ); - // Update gradle-wrapper.properties - await runTask( - `Migrating gradle-wrapper.properties by updating gradle version to ${gradleVersion}.`, - () => { - return updateGradleWrapper( - join( - config.android.platformDirAbs, - 'gradle', - 'wrapper', - 'gradle-wrapper.properties', - ), - ); - }, - ); - // Variables gradle await runTask(`Migrating variables.gradle file.`, () => { return (async (): Promise => { @@ -344,33 +360,6 @@ export async function migrateCommand( logger.warn('Skipped Running cap sync.'); } - if ( - allDependencies['@capacitor/android'] && - existsSync(config.android.platformDirAbs) - ) { - if (!installFailed) { - try { - await runTask(`Upgrading gradle wrapper files`, () => { - return updateGradleWrapperFiles(config.android.platformDirAbs); - }); - } catch (e: any) { - if (e.includes('EACCES')) { - logger.error( - `gradlew file does not have executable permissions. This can happen if the Android platform was added on a Windows machine. Please run ${c.input( - `chmod +x ./${config.android.platformDir}/gradlew`, - )} and ${c.input( - `cd ${config.android.platformDir} && ./gradlew wrapper --distribution-type all --gradle-version ${gradleVersion} --warning-mode all`, - )} to update the files manually`, - ); - } else { - logger.error(`gradle wrapper files were not updated`); - } - } - } else { - logger.warn('Skipped upgrading gradle wrapper files'); - } - } - // Write all breaking changes await runTask(`Writing breaking changes.`, () => { return writeBreakingChanges(); @@ -542,19 +531,17 @@ function readFile(filename: string): string | undefined { } } -async function updateGradleWrapper(filename: string) { +function getGradleWrapperVersion(filename: string): string { const txt = readFile(filename); if (!txt) { - return; + return '0.0.0'; } - const replaced = setAllStringIn( - txt, - 'distributionUrl=', - '\n', - // eslint-disable-next-line no-useless-escape - `https\\://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip`, + const version = txt.substring( + txt.indexOf('gradle-') + 7, + txt.indexOf('-all.zip'), ); - writeFileSync(filename, replaced, 'utf-8'); + const semverVersion = coerce(version)?.version; + return semverVersion ? semverVersion : '0.0.0'; } async function updateGradleWrapperFiles(platformDir: string) { @@ -666,11 +653,10 @@ async function updateBuildGradle( for (const dep of Object.keys(neededDeps)) { if (replaced.includes(`classpath '${dep}`)) { - const semver = await import('semver'); const firstIndex = replaced.indexOf(dep) + dep.length + 1; const existingVersion = '' + replaced.substring(firstIndex, replaced.indexOf("'", firstIndex)); - if (semver.gte(neededDeps[dep], existingVersion)) { + if (gte(neededDeps[dep], existingVersion)) { replaced = setAllStringIn( replaced, `classpath '${dep}:`, From 5a73807a03f4778e95c6c79940be14b31ef4b06b Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 5 Apr 2024 19:15:18 +0200 Subject: [PATCH 2/2] remove error message --- cli/src/tasks/migrate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/tasks/migrate.ts b/cli/src/tasks/migrate.ts index dbe2f21db4..b9f15d0e82 100644 --- a/cli/src/tasks/migrate.ts +++ b/cli/src/tasks/migrate.ts @@ -212,7 +212,7 @@ export async function migrateCommand( )} to update the files manually`, ); } else { - logger.error(`gradle wrapper files were not updated ${e}`); + logger.error(`gradle wrapper files were not updated`); } } } else {