From ddcc818e828b290459d3ddffe9102fc312139823 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Wed, 3 May 2023 14:21:01 -0400 Subject: [PATCH] fix: check for android and JDK (#6554) --- cli/src/common.ts | 26 ++++++++++++++++++++++ cli/src/tasks/migrate.ts | 47 +++++++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/cli/src/common.ts b/cli/src/common.ts index 5e68ec393..d9d5651ec 100644 --- a/cli/src/common.ts +++ b/cli/src/common.ts @@ -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; @@ -533,3 +534,28 @@ export function resolvePlatform( return null; } + +export async function checkJDKMajorVersion(): Promise { + 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; + } +} diff --git a/cli/src/tasks/migrate.ts b/cli/src/tasks/migrate.ts index 6f06b6493..bffd9eb0e 100644 --- a/cli/src/tasks/migrate.ts +++ b/cli/src/tasks/migrate.ts @@ -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'; @@ -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( @@ -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; @@ -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`); + } } }