Skip to content

Commit

Permalink
feat: support env func (#12230)
Browse files Browse the repository at this point in the history
* feat: env variable

* refactor: more

* refactor: sig

* refactor: sig

* refactor: complettion

* refactor: match [

* refactor: support env 0806

* refactor: fix some bug in 0806

* refactor: check txt file

* refactor: more

* refactor: clean

* refactor: clean

* test: ut

* refactor: clean

* test: ut

* test: ut

* refactor: minor
  • Loading branch information
yuqizhou77 authored Aug 15, 2024
1 parent ee4d1cf commit 2dea697
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 41 deletions.
31 changes: 14 additions & 17 deletions packages/fx-core/src/component/driver/teamsApp/createAppPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@ import * as path from "path";
import { Service } from "typedi";
import { getLocalizedString } from "../../../common/localizeUtils";
import { ErrorContextMW } from "../../../common/globalVars";
import {
FileNotFoundError,
InvalidActionInputError,
JSONSyntaxError,
MissingEnvironmentVariablesError,
} from "../../../error/common";
import { FileNotFoundError, InvalidActionInputError, JSONSyntaxError } from "../../../error/common";
import { DriverContext } from "../interface/commonArgs";
import { ExecutionResult, StepDriver } from "../interface/stepDriver";
import { addStartAndEndTelemetry } from "../middleware/addStartAndEndTelemetry";
import { WrapDriverContext } from "../util/wrapUtil";
import { Constants } from "./constants";
import { CreateAppPackageArgs } from "./interfaces/CreateAppPackageArgs";
import { manifestUtils } from "./utils/ManifestUtils";
import { expandEnvironmentVariable, getEnvironmentVariables } from "../../utils/common";
import { TelemetryPropertyKey } from "./utils/telemetry";
import { InvalidFileOutsideOfTheDirectotryError } from "../../../error/teamsApp";
import { getResolvedManifest, normalizePath } from "./utils/utils";
import { copilotGptManifestUtils } from "./utils/CopilotGptManifestUtils";
import { ManifestType } from "../../utils/envFunctionUtils";

export const actionName = "teamsApp/zipAppPackage";

Expand Down Expand Up @@ -190,7 +184,7 @@ export class CreateAppPackageDriver implements StepDriver {
zip,
manifest.composeExtensions[0].apiSpecificationFile,
apiSpecificationFile,
TelemetryPropertyKey.customizedOpenAPIKeys,
ManifestType.ApiSpec,
context
);
if (addFileWithVariableRes.isErr()) {
Expand Down Expand Up @@ -244,14 +238,17 @@ export class CreateAppPackageDriver implements StepDriver {
zip,
declarativeCopilots[0].file,
copilotGptManifestFile,
TelemetryPropertyKey.customizedAIPluginKeys,
ManifestType.PluginManifest,
context
);
if (addFileWithVariableRes.isErr()) {
return err(addFileWithVariableRes.error);
}

const getCopilotGptRes = await copilotGptManifestUtils.getManifest(copilotGptManifestFile);
const getCopilotGptRes = await copilotGptManifestUtils.getManifest(
copilotGptManifestFile,
context
);

if (getCopilotGptRes.isOk()) {
if (getCopilotGptRes.value.actions) {
Expand Down Expand Up @@ -304,10 +301,10 @@ export class CreateAppPackageDriver implements StepDriver {
private static async expandEnvVars(
filePath: string,
ctx: WrapDriverContext,
telemetryKey: TelemetryPropertyKey
manifestType: ManifestType
): Promise<Result<string, FxError>> {
const content = await fs.readFile(filePath, "utf8");
return getResolvedManifest(content, filePath, telemetryKey, ctx);
return getResolvedManifest(content, filePath, manifestType, ctx);
}

private validateArgs(args: CreateAppPackageArgs): Result<any, FxError> {
Expand Down Expand Up @@ -380,7 +377,7 @@ export class CreateAppPackageDriver implements StepDriver {
zip,
pluginRelativePath,
pluginFile,
TelemetryPropertyKey.customizedAIPluginKeys,
ManifestType.PluginManifest,
context
);
if (addFileWithVariableRes.isErr()) {
Expand Down Expand Up @@ -439,7 +436,7 @@ export class CreateAppPackageDriver implements StepDriver {
zip,
normalizePath(entryName, useForwardSlash),
specFile,
TelemetryPropertyKey.customizedOpenAPIKeys,
ManifestType.ApiSpec,
context
);
if (addFileWithVariableRes.isErr()) {
Expand All @@ -456,13 +453,13 @@ export class CreateAppPackageDriver implements StepDriver {
zip: AdmZip,
entryName: string,
filePath: string,
telemetryKey: TelemetryPropertyKey,
manifestType: ManifestType,
context: WrapDriverContext
): Promise<Result<undefined, FxError>> {
const expandedEnvVarResult = await CreateAppPackageDriver.expandEnvVars(
filePath,
context,
telemetryKey
manifestType
);
if (expandedEnvVarResult.isErr()) {
return err(expandedEnvVarResult.error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import path from "path";
import { pluginManifestUtils } from "./PluginManifestUtils";
import { SummaryConstant } from "../../../configManager/constant";
import { EOL } from "os";
import { ManifestType } from "../../../utils/envFunctionUtils";

export class CopilotGptManifestUtils {
public async readCopilotGptManifestFile(
Expand Down Expand Up @@ -61,10 +62,10 @@ export class CopilotGptManifestUtils {
return err(manifestRes.error);
}
// Add environment variable keys to telemetry
const resolvedManifestRes = getResolvedManifest(
const resolvedManifestRes = await getResolvedManifest(
JSON.stringify(manifestRes.value),
path,
TelemetryPropertyKey.customizedCopilotGptKeys,
ManifestType.DeclarativeCopilotManifest,
context
);

Expand All @@ -91,7 +92,7 @@ export class CopilotGptManifestUtils {
public async validateAgainstSchema(
declaraitveCopilot: IDeclarativeCopilot,
manifestPath: string,
context?: WrapDriverContext
context: WrapDriverContext
): Promise<Result<DeclarativeCopilotManifestValidationResult, FxError>> {
const manifestRes = await this.getManifest(manifestPath, context);
if (manifestRes.isErr()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import {
} from "../constants";
import { AppStudioError } from "../errors";
import { AppStudioResultFactory } from "../results";
import { TelemetryPropertyKey } from "./telemetry";
import { getResolvedManifest } from "./utils";
import { ManifestType } from "../../../utils/envFunctionUtils";

export class ManifestUtils {
async readAppManifest(projectPath: string): Promise<Result<TeamsAppManifest, FxError>> {
Expand Down Expand Up @@ -329,10 +329,10 @@ export class ManifestUtils {
const manifestTemplateString = JSON.stringify(manifest);

// Add environment variable keys to telemetry
const resolvedManifestRes = getResolvedManifest(
const resolvedManifestRes = await getResolvedManifest(
manifestTemplateString,
manifestTemplatePath,
TelemetryPropertyKey.customizedKeys,
ManifestType.TeamsManifest,
context
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import path from "path";
import { manifestUtils } from "./ManifestUtils";
import { WrapDriverContext } from "../../util/wrapUtil";
import { getResolvedManifest } from "./utils";
import { TelemetryPropertyKey } from "./telemetry";
import { AppStudioResultFactory } from "../results";
import { AppStudioError } from "../errors";
import { getDefaultString, getLocalizedString } from "../../../../common/localizeUtils";
import { PluginManifestValidationResult } from "../interfaces/ValidationResult";
import { SummaryConstant } from "../../../configManager/constant";
import { EOL } from "os";
import { ManifestType } from "../../../utils/envFunctionUtils";

export class PluginManifestUtils {
public async readPluginManifestFile(
Expand Down Expand Up @@ -62,10 +62,10 @@ export class PluginManifestUtils {
return err(manifestRes.error);
}
// Add environment variable keys to telemetry
const resolvedManifestRes = getResolvedManifest(
const resolvedManifestRes = await getResolvedManifest(
JSON.stringify(manifestRes.value),
path,
TelemetryPropertyKey.customizedAIPluginKeys,
ManifestType.PluginManifest,
context
);

Expand All @@ -79,7 +79,7 @@ export class PluginManifestUtils {
public async validateAgainstSchema(
plugin: IPlugin,
path: string,
context?: WrapDriverContext
context: WrapDriverContext
): Promise<Result<PluginManifestValidationResult, FxError>> {
const manifestRes = await this.getManifest(path, context);
if (manifestRes.isErr()) {
Expand Down
46 changes: 40 additions & 6 deletions packages/fx-core/src/component/driver/teamsApp/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WrapDriverContext } from "../../util/wrapUtil";
import { FxError, Result, err, ok } from "@microsoft/teamsfx-api";
import { MissingEnvironmentVariablesError } from "../../../../error";
import { TelemetryPropertyKey } from "./telemetry";
import { expandVariableWithFunction, ManifestType } from "../../../utils/envFunctionUtils";

export function getCustomizedKeys(prefix: string, manifest: any): string[] {
let keys: string[] = [];
Expand Down Expand Up @@ -219,20 +220,53 @@ export function normalizePath(path: string, useForwardSlash: boolean): string {
return useForwardSlash ? path.replace(/\\/g, "/") : path;
}

export function getResolvedManifest(
export async function getResolvedManifest(
content: string,
path: string,
telemetryKey: TelemetryPropertyKey,
manifestType: ManifestType,
ctx?: WrapDriverContext
): Result<string, FxError> {
): Promise<Result<string, FxError>> {
const vars = getEnvironmentVariables(content);
let telemetryKey;
switch (manifestType) {
case ManifestType.ApiSpec:
telemetryKey = TelemetryPropertyKey.customizedOpenAPIKeys;
break;
case ManifestType.PluginManifest:
telemetryKey = TelemetryPropertyKey.customizedAIPluginKeys;
break;
case ManifestType.DeclarativeCopilotManifest:
telemetryKey = TelemetryPropertyKey.customizedCopilotGptKeys;
break;
default:
telemetryKey = TelemetryPropertyKey.customizedKeys;
break;
}
ctx?.addTelemetryProperties({
[telemetryKey]: vars.join(";"),
});
const result = expandEnvironmentVariable(content);
const notExpandedVars = getEnvironmentVariables(result);

let value = content;
if (manifestType !== ManifestType.ApiSpec) {
const processedFunctionRes = await expandVariableWithFunction(
content,
ctx,
undefined,
true,
manifestType
);
if (processedFunctionRes.isErr()) {
return processedFunctionRes;
}

value = expandEnvironmentVariable(processedFunctionRes.value);
} else {
value = expandEnvironmentVariable(value);
}

const notExpandedVars = getEnvironmentVariables(value);
if (notExpandedVars.length > 0) {
return err(new MissingEnvironmentVariablesError("teamsApp", notExpandedVars.join(","), path));
}
return ok(result);
return ok(value);
}
Loading

0 comments on commit 2dea697

Please sign in to comment.