Skip to content

Commit

Permalink
feat(env-checker): [part-4] add check/install logic for function plug…
Browse files Browse the repository at this point in the history
…in (#267)

* feat(env-checker): add check/install logic for function plugin

* fix: fix comments

* chore: remove useless comment

* fix: fix comments

* chore: remove useless import
  • Loading branch information
a1exwang authored Apr 14, 2021
1 parent 87d64f8 commit dc4d58b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/fx-core/src/plugins/resource/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FxResult, FunctionPluginResultFactory as ResultFactory } from "./result
import { LifeCycle } from "./enums";
import { Logger } from "./utils/logger";
import { telemetryHelper } from "./utils/telemetry-helper";
import { setFeatureFlag } from "./utils/depsChecker/checkerAdapter";

// This layer tries to provide a uniform exception handling for function plugin.
export class FunctionPlugin implements Plugin {
Expand Down Expand Up @@ -78,6 +79,7 @@ export class FunctionPlugin implements Plugin {

public async preDeploy(ctx: PluginContext): Promise<FxResult> {
Logger.setLogger(ctx.logProvider);
setFeatureFlag(ctx.answers);
await StepHelperFactory.preDeployStepHelper.start(
Object.entries(PreDeploySteps).length, ctx.dialog);
const res = await this.runWithErrorWrapper(ctx, LifeCycle.preDeploy,
Expand All @@ -89,6 +91,7 @@ export class FunctionPlugin implements Plugin {

public async deploy(ctx: PluginContext): Promise<FxResult> {
Logger.setLogger(ctx.logProvider);
setFeatureFlag(ctx.answers);
await StepHelperFactory.deployStepHelper.start(
Object.entries(DeploySteps).length, ctx.dialog);
const res = await this.runWithErrorWrapper(ctx, LifeCycle.deploy,
Expand Down
34 changes: 33 additions & 1 deletion packages/fx-core/src/plugins/resource/function/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import { FxResult, FunctionPluginResultFactory as ResultFactory } from "./result
import { Logger } from "./utils/logger";
import { PostProvisionSteps, PreDeploySteps, ProvisionSteps, StepGroup, step } from "./resources/steps";
import { functionNameQuestion, nodeVersionQuestion } from "./questions";
import { dotnetHelpLink, Messages } from "./utils/depsChecker/common";
import { DotnetChecker } from "./utils/depsChecker/dotnetChecker";
import { handleDotnetError } from "./utils/depsChecker/checkerAdapter";
import { isLinux } from "./utils/depsChecker/common";
import { DepsCheckerError } from "./utils/depsChecker/errors";

type Site = WebSiteManagementModels.Site;
type AppServicePlan = WebSiteManagementModels.AppServicePlan;
Expand Down Expand Up @@ -446,7 +451,8 @@ export class FunctionPluginImpl {
return ResultFactory.Success();
}

await FunctionDeploy.checkDotNetVersion(ctx, workingPath);
// NOTE: make sure this step is before using `dotnet` command if you refactor this code.
await this.handleDotnetChecker();

await runWithErrorCatchAndThrow(new InstallTeamsfxBindingError(), async () =>
await step(StepGroup.PreDeployStepGroup, PreDeploySteps.installTeamsfxBinding, async () =>
Expand Down Expand Up @@ -594,4 +600,30 @@ export class FunctionPluginImpl {

return undefined;
}

private async handleDotnetChecker(): Promise<void> {
await step(StepGroup.PreDeployStepGroup, PreDeploySteps.dotnetInstall, async () => {
const dotnetChecker = new DotnetChecker();
try {
if (await dotnetChecker.isInstalled()) {
return;
}
} catch (error) {
handleDotnetError(error);
return;
}

if (isLinux()) {
// TODO: handle linux installation
handleDotnetError(new DepsCheckerError(Messages.defaultErrorMessage, dotnetHelpLink));
return;
}

try {
await dotnetChecker.install();
} catch (error) {
handleDotnetError(error);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum PostProvisionSteps {
}

export enum PreDeploySteps {
dotnetInstall = "Install .NET Core SDK if needed.",
installTeamsfxBinding = "Install TeamsFX Binding.",
npmPrepare = "Install/Build js files."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import * as path from "path";
import { Logger } from "../logger";
import { DepsCheckerError } from "./errors";
import { dotnetChecker, DotnetChecker } from "./dotnetChecker";
import { ConfigMap } from "fx-api";
import { ConfigMap, PluginContext, returnUserError } from "fx-api";
import { Messages, dotnetHelpLink } from "./common";

export { cpUtils } from "./cpUtils";
export const logger = Logger;
Expand Down Expand Up @@ -74,3 +76,11 @@ export async function getDotnetForShell(): Promise<string> {
const execPath = await dotnetChecker.getDotnetExecPath();
return DotnetChecker.escapeFilePath(execPath);
}

export function handleDotnetError(error: Error): void {
if (error instanceof DepsCheckerError) {
throw returnUserError(error, "function", "DepsCheckerError", error.helpLink, error);
} else {
throw returnUserError(new Error(Messages.defaultErrorMessage), "function", "DepsCheckerError", dotnetHelpLink, error);
}
}

0 comments on commit dc4d58b

Please sign in to comment.