diff --git a/src/funcCoreTools/getBrewPackageName.ts b/src/funcCoreTools/getBrewPackageName.ts index 7a640a07a..21dee7190 100644 --- a/src/funcCoreTools/getBrewPackageName.ts +++ b/src/funcCoreTools/getBrewPackageName.ts @@ -5,7 +5,37 @@ import { funcPackageName } from '../constants'; import { FuncVersion, getMajorVersion } from '../FuncVersion'; +import { cpUtils } from '../utils/cpUtils'; export function getBrewPackageName(version: FuncVersion): string { return `${funcPackageName}@${getMajorVersion(version)}`; } + +export async function tryGetInstalledBrewPackageName(version: FuncVersion): Promise { + const brewPackageName: string = getBrewPackageName(version); + if (await isBrewPackageInstalled(brewPackageName)) { + return brewPackageName; + } else { + let oldPackageName: string | undefined; + if (version === FuncVersion.v2) { + oldPackageName = funcPackageName; + } else if (version === FuncVersion.v3) { + oldPackageName = funcPackageName + '-v3-preview'; + } + + if (oldPackageName && await isBrewPackageInstalled(oldPackageName)) { + return oldPackageName; + } else { + return undefined; + } + } +} + +async function isBrewPackageInstalled(packageName: string): Promise { + try { + await cpUtils.executeCommand(undefined, undefined, 'brew', 'ls', packageName); + return true; + } catch (error) { + return false; + } +} diff --git a/src/funcCoreTools/getFuncPackageManagers.ts b/src/funcCoreTools/getFuncPackageManagers.ts index 560e5265b..e8207a329 100644 --- a/src/funcCoreTools/getFuncPackageManagers.ts +++ b/src/funcCoreTools/getFuncPackageManagers.ts @@ -6,7 +6,7 @@ import { funcPackageName, PackageManager } from '../constants'; import { FuncVersion } from '../FuncVersion'; import { cpUtils } from '../utils/cpUtils'; -import { getBrewPackageName } from './getBrewPackageName'; +import { tryGetInstalledBrewPackageName } from './getBrewPackageName'; export async function getFuncPackageManagers(isFuncInstalled: boolean): Promise { const result: PackageManager[] = []; @@ -35,14 +35,18 @@ export async function getFuncPackageManagers(isFuncInstalled: boolean): Promise< async function hasBrew(isFuncInstalled: boolean): Promise { for (const version of Object.values(FuncVersion)) { if (version !== FuncVersion.v1) { - const brewPackageName: string = getBrewPackageName(version); - try { - isFuncInstalled ? - await cpUtils.executeCommand(undefined, undefined, 'brew', 'ls', brewPackageName) : + if (isFuncInstalled) { + const packageName: string | undefined = await tryGetInstalledBrewPackageName(version); + if (packageName) { + return true; + } + } else { + try { await cpUtils.executeCommand(undefined, undefined, 'brew', '--version'); - return true; - } catch (error) { - // an error indicates no brew + return true; + } catch (error) { + // an error indicates no brew + } } } } diff --git a/src/funcCoreTools/uninstallFuncCoreTools.ts b/src/funcCoreTools/uninstallFuncCoreTools.ts index bdd1d53c0..bf9eab39a 100644 --- a/src/funcCoreTools/uninstallFuncCoreTools.ts +++ b/src/funcCoreTools/uninstallFuncCoreTools.ts @@ -10,7 +10,7 @@ import { FuncVersion } from '../FuncVersion'; import { localize } from '../localize'; import { cpUtils } from '../utils/cpUtils'; import { nonNullValue } from '../utils/nonNull'; -import { getBrewPackageName } from './getBrewPackageName'; +import { tryGetInstalledBrewPackageName } from './getBrewPackageName'; import { getFuncPackageManagers } from './getFuncPackageManagers'; import { tryGetLocalFuncVersion } from './tryGetLocalFuncVersion'; @@ -35,7 +35,7 @@ export async function uninstallFuncCoreTools(_context: IActionContext, packageMa break; case PackageManager.brew: const version: FuncVersion = nonNullValue(await tryGetLocalFuncVersion(), 'localFuncVersion'); - const brewPackageName: string = getBrewPackageName(version); + const brewPackageName: string = nonNullValue(await tryGetInstalledBrewPackageName(version), 'brewPackageName'); await cpUtils.executeCommand(ext.outputChannel, undefined, 'brew', 'uninstall', brewPackageName); break; default: diff --git a/src/funcCoreTools/updateFuncCoreTools.ts b/src/funcCoreTools/updateFuncCoreTools.ts index a57eed5fe..2aaa08045 100644 --- a/src/funcCoreTools/updateFuncCoreTools.ts +++ b/src/funcCoreTools/updateFuncCoreTools.ts @@ -8,7 +8,8 @@ import { ext } from '../extensionVariables'; import { FuncVersion } from '../FuncVersion'; import { localize } from '../localize'; import { cpUtils } from '../utils/cpUtils'; -import { getBrewPackageName } from './getBrewPackageName'; +import { nonNullValue } from '../utils/nonNull'; +import { getBrewPackageName, tryGetInstalledBrewPackageName } from './getBrewPackageName'; import { getNpmDistTag, INpmDistTag } from './getNpmDistTag'; export async function updateFuncCoreTools(packageManager: PackageManager, version: FuncVersion): Promise { @@ -20,10 +21,16 @@ export async function updateFuncCoreTools(packageManager: PackageManager, versio break; case PackageManager.brew: const brewPackageName: string = getBrewPackageName(version); - await cpUtils.executeCommand(ext.outputChannel, undefined, 'brew', 'upgrade', brewPackageName); + const installedBrewPackageName: string = nonNullValue(await tryGetInstalledBrewPackageName(version), 'brewPackageName'); + if (brewPackageName !== installedBrewPackageName) { + // Uninstall deprecated tag and install latest tag + await cpUtils.executeCommand(ext.outputChannel, undefined, 'brew', 'uninstall', installedBrewPackageName); + await cpUtils.executeCommand(ext.outputChannel, undefined, 'brew', 'install', brewPackageName); + } else { + await cpUtils.executeCommand(ext.outputChannel, undefined, 'brew', 'upgrade', brewPackageName); + } break; default: throw new RangeError(localize('invalidPackageManager', 'Invalid package manager "{0}".', packageManager)); - break; } } diff --git a/src/funcCoreTools/validateFuncCoreToolsIsLatest.ts b/src/funcCoreTools/validateFuncCoreToolsIsLatest.ts index e7308bf50..7267339f2 100644 --- a/src/funcCoreTools/validateFuncCoreToolsIsLatest.ts +++ b/src/funcCoreTools/validateFuncCoreToolsIsLatest.ts @@ -38,6 +38,7 @@ export async function validateFuncCoreToolsIsLatest(): Promise { return; } else if (packageManagers.length === 1) { packageManager = packageManagers[0]; + context.telemetry.properties.packageManager = packageManager; } else { context.telemetry.properties.multiFunc = 'true'; if (showMultiCoreToolsWarning) {