Skip to content

Commit

Permalink
Fix handling of old brew package names (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Jan 23, 2020
1 parent 4eaf9d5 commit 52eef27
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
30 changes: 30 additions & 0 deletions src/funcCoreTools/getBrewPackageName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> {
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<boolean> {
try {
await cpUtils.executeCommand(undefined, undefined, 'brew', 'ls', packageName);
return true;
} catch (error) {
return false;
}
}
20 changes: 12 additions & 8 deletions src/funcCoreTools/getFuncPackageManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageManager[]> {
const result: PackageManager[] = [];
Expand Down Expand Up @@ -35,14 +35,18 @@ export async function getFuncPackageManagers(isFuncInstalled: boolean): Promise<
async function hasBrew(isFuncInstalled: boolean): Promise<boolean> {
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
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/funcCoreTools/uninstallFuncCoreTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions src/funcCoreTools/updateFuncCoreTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/funcCoreTools/validateFuncCoreToolsIsLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function validateFuncCoreToolsIsLatest(): Promise<void> {
return;
} else if (packageManagers.length === 1) {
packageManager = packageManagers[0];
context.telemetry.properties.packageManager = packageManager;
} else {
context.telemetry.properties.multiFunc = 'true';
if (showMultiCoreToolsWarning) {
Expand Down

0 comments on commit 52eef27

Please sign in to comment.