Skip to content

Commit

Permalink
fix: check for android and JDK (#6554)
Browse files Browse the repository at this point in the history
  • Loading branch information
markemer authored May 3, 2023
1 parent 895f86a commit ddcc818
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
26 changes: 26 additions & 0 deletions cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Config, PackageJson } from './definitions';
import { fatal } from './errors';
import { output, logger } from './log';
import { resolveNode } from './util/node';
import { runCommand } from './util/subprocess';

export type CheckFunction = () => Promise<string | null>;

Expand Down Expand Up @@ -533,3 +534,28 @@ export function resolvePlatform(

return null;
}

export async function checkJDKMajorVersion(): Promise<number> {
const string = await runCommand('java', ['--version']);
const versionRegex = RegExp(/([0-9]+)\.?([0-9]*)\.?([0-9]*)/);
const versionMatch = versionRegex.exec(string);

if (versionMatch === null) {
return -1;
}

const firstVersionNumber = parseInt(versionMatch[1]);
const secondVersionNumber = parseInt(versionMatch[2]);

if (typeof firstVersionNumber === 'number' && firstVersionNumber != 1) {
return firstVersionNumber;
} else if (
typeof secondVersionNumber === 'number' &&
firstVersionNumber == 1 &&
secondVersionNumber < 9
) {
return secondVersionNumber;
} else {
return -1;
}
}
47 changes: 29 additions & 18 deletions cli/src/tasks/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { join } from 'path';
import rimraf from 'rimraf';

import c from '../colors';
import { getCoreVersion, runTask } from '../common';
import { getCoreVersion, runTask, checkJDKMajorVersion } from '../common';
import type { Config } from '../definitions';
import { fatal } from '../errors';
import { logger, logPrompt, logSuccess } from '../log';
Expand Down Expand Up @@ -52,8 +52,8 @@ const plugins = [
'@capacitor/text-zoom',
'@capacitor/toast',
];
const coreVersion = 'next'; // TODO: Update when Capacitor 5 releases
const pluginVersion = 'next'; // TODO: Update when Capacitor 5 releases
const coreVersion = '5.0.0';
const pluginVersion = '5.0.0';
const gradleVersion = '8.0.2';

export async function migrateCommand(
Expand All @@ -72,6 +72,12 @@ export async function migrateCommand(
);
}

const jdkMajor = await checkJDKMajorVersion();

if (jdkMajor < 17) {
logger.warn('Capacitor 5 requires JDK 17 or higher. Some steps may fail.');
}

const variablesAndClasspaths:
| {
'variables': any;
Expand Down Expand Up @@ -313,21 +319,26 @@ export async function migrateCommand(
return getCommandOutput('npx', ['cap', 'sync']);
});

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`);
if (
allDependencies['@capacitor/android'] &&
existsSync(config.android.platformDirAbs)
) {
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`);
}
}
}

Expand Down

0 comments on commit ddcc818

Please sign in to comment.