Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Don't downgrade gradle version on migrate #7385

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 39 additions & 53 deletions cli/src/tasks/migrate.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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<void> => {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}:`,
Expand Down