Skip to content

Commit

Permalink
changed caching to stack variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyaang committed Jul 11, 2023
1 parent cefc30a commit 3ada909
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 49 deletions.
66 changes: 33 additions & 33 deletions src/debugging/netSdk/NetSdkDebugHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ import { WorkspaceFolder, commands, l10n, tasks } from "vscode";
import { ext } from "../../extensionVariables";
import { NetChooseBuildTypeContext, netContainerBuild } from "../../scaffolding/wizard/net/NetContainerBuild";
import { AllNetContainerBuildOptions, NetContainerBuildOptionsKey } from "../../scaffolding/wizard/net/NetSdkChooseBuildStep";
import { getContainerNameWithTag } from "../../tasks/TaskHelper";
import { NetSdkRunTaskDefinition, netSdkRunTaskProvider } from "../../tasks/netSdk/NetSdkRunTaskProvider";
import { normalizeArchitectureToRidArchitecture, normalizeOsToRidOs } from "../../tasks/netSdk/netSdkTaskUtils";
import { getNetCoreProjectInfo } from "../../utils/netCoreUtils";
import { getDockerOSType } from "../../utils/osUtils";
import { PlatformOS } from "../../utils/platform";
import { quickPickProjectFileItem } from "../../utils/quickPickFile";
import { unresolveWorkspaceFolder } from "../../utils/resolveVariables";
import { DockerDebugContext, DockerDebugScaffoldContext, ResolvedDebugConfiguration } from "../DebugHelper";
import { resolveVariables, unresolveWorkspaceFolder } from "../../utils/resolveVariables";
import { DockerDebugContext, DockerDebugScaffoldContext, ResolvedDebugConfiguration, inferContainerName } from "../DebugHelper";
import { DockerDebugConfiguration } from "../DockerDebugConfigurationProvider";
import { NetCoreDebugHelper, NetCoreDebugScaffoldingOptions, NetCoreProjectProperties } from "../netcore/NetCoreDebugHelper";

export interface NetSdkProjectProperties extends NetCoreProjectProperties {
containerWorkingDirectory: string;
isSdkContainerSupportEnabled: boolean;
containerName: string;
imageName: string;
}

export class NetSdkDebugHelper extends NetCoreDebugHelper {

protected projectProperties: NetSdkProjectProperties | undefined;

public override async provideDebugConfigurations(context: DockerDebugScaffoldContext, options?: NetCoreDebugScaffoldingOptions): Promise<DockerDebugConfiguration[]> {
const configurations: DockerDebugConfiguration[] = [];

Expand Down Expand Up @@ -70,13 +67,12 @@ export class NetSdkDebugHelper extends NetCoreDebugHelper {
}

public async afterResolveDebugConfiguration(context: DockerDebugContext, debugConfiguration: DockerDebugConfiguration): Promise<void> {
const projectInfo = await this.getProjectProperties(debugConfiguration);
const runDefinition: Omit<NetSdkRunTaskDefinition, "type"> = {
netCore: {
appProject: debugConfiguration?.netCore?.appProject || await this.inferProjPath(context.actionContext, context.folder),
},
dockerRun: {
containerName: projectInfo.containerName
image: context.runDefinition.dockerRun.image,
}
};

Expand All @@ -85,25 +81,23 @@ export class NetSdkDebugHelper extends NetCoreDebugHelper {
await promise;
}

protected override async inferAppOutput(debugConfiguration: DockerDebugConfiguration): Promise<string> {
const projectInfo = await this.getProjectProperties(debugConfiguration);

// fifth is whether .NET Web apps supports SDK Containers
if (projectInfo.isSdkContainerSupportEnabled) {
return await getDockerOSType() === 'windows' // fourth is output path
? path.win32.normalize(projectInfo.containerWorkingDirectory)
: path.posix.normalize(projectInfo.containerWorkingDirectory);
} else {
throw new Error(l10n.t("Your current project configuration or .NET SDK version doesn't support SDK Container build. Please choose a compatible project or update .NET SDK."));
}
}

protected override async loadExternalInfo(context: DockerDebugContext, debugConfiguration: DockerDebugConfiguration): Promise<{ configureSsl: boolean, containerName: string, platformOS: PlatformOS }> {
const associatedTask = context.runDefinition;
const projectInfo = await this.getProjectProperties(debugConfiguration);
const projectProperties = await this.getProjectProperties(debugConfiguration, context.folder);
debugConfiguration.netCore.appOutput = await this.normalizeAppOutput(projectProperties.containerWorkingDirectory, projectProperties.isSdkContainerSupportEnabled);
context.runDefinition = {
...context.runDefinition,
dockerRun: {
containerName: inferContainerName(debugConfiguration, context, projectProperties.imageName, "dev"),
image: projectProperties.imageName,
},
netCore: {
enableDebugging: true,
}
};

return {
configureSsl: !!(associatedTask?.netCore?.configureSsl),
containerName: getContainerNameWithTag(projectInfo.containerName, "dev"),
configureSsl: false,
containerName: context.runDefinition.dockerRun.containerName,
platformOS: await getDockerOSType() === "windows" ? 'Windows' : 'Linux',
};
}
Expand All @@ -112,16 +106,13 @@ export class NetSdkDebugHelper extends NetCoreDebugHelper {
return appOutput;
}

protected override async getProjectProperties(debugConfiguration: DockerDebugConfiguration): Promise<NetSdkProjectProperties> {
if (this.projectProperties) {
return this.projectProperties;
}

protected override async getProjectProperties(debugConfiguration: DockerDebugConfiguration, folder?: WorkspaceFolder): Promise<NetSdkProjectProperties> {
const ridOS = await normalizeOsToRidOs();
const ridArchitecture = await normalizeArchitectureToRidArchitecture();
const additionalProperties = `/p:ContainerRuntimeIdentifier="${ridOS}-${ridArchitecture}"`;
const resolvedAppProject = resolveVariables(debugConfiguration.netCore?.appProject, folder);

const projectInfo = await getNetCoreProjectInfo('GetProjectProperties', debugConfiguration.netCore?.appProject, additionalProperties);
const projectInfo = await getNetCoreProjectInfo('GetProjectProperties', resolvedAppProject, additionalProperties);

if (projectInfo.length < 6 || !projectInfo[5]) {
throw new Error(l10n.t("Your current project configuration or .NET SDK version doesn't support SDK Container build. Please choose a compatible project or update .NET SDK."));
Expand All @@ -133,13 +124,22 @@ export class NetSdkDebugHelper extends NetCoreDebugHelper {
appOutput: projectInfo[2],
containerWorkingDirectory: projectInfo[3],
isSdkContainerSupportEnabled: projectInfo[4] === 'true',
containerName: projectInfo[5],
imageName: projectInfo[5],
};

this.projectProperties = projectProperties;
return projectProperties;
}

private async normalizeAppOutput(unnormalizedContainerWorkingDirectory: string, isSdkContainerSupportEnabled: boolean): Promise<string> {
if (isSdkContainerSupportEnabled) {
return await getDockerOSType() === 'windows' // fourth is output path
? path.win32.normalize(unnormalizedContainerWorkingDirectory)
: path.posix.normalize(unnormalizedContainerWorkingDirectory);
} else {
throw new Error(l10n.t("Your current project configuration or .NET SDK version doesn't support SDK Container build. Please choose a compatible project or update .NET SDK."));
}
}

/**
* @returns the project path stored in NetCoreDebugScaffoldingOptions,
* otherwise prompts the user to select a .csproj file and stores the path
Expand Down
7 changes: 0 additions & 7 deletions src/debugging/netcore/NetCoreDebugHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export interface NetCoreProjectProperties {

export class NetCoreDebugHelper implements DebugHelper {

protected projectProperties: NetCoreProjectProperties | undefined;

public async provideDebugConfigurations(context: DockerDebugScaffoldContext, options?: NetCoreDebugScaffoldingOptions): Promise<DockerDebugConfiguration[]> {
options = options || {};
options.appProject = options.appProject || await NetCoreTaskHelper.inferAppProject(context); // This method internally checks the user-defined input first
Expand Down Expand Up @@ -210,10 +208,6 @@ export class NetCoreDebugHelper implements DebugHelper {
}

protected async getProjectProperties(debugConfiguration: DockerDebugConfiguration): Promise<NetCoreProjectProperties> {
if (this.projectProperties) {
return this.projectProperties;
}

const projectInfo = await getNetCoreProjectInfo('GetProjectProperties', debugConfiguration.netCore?.appProject);

if (projectInfo.length < 3) {
Expand All @@ -227,7 +221,6 @@ export class NetCoreDebugHelper implements DebugHelper {
appOutput: projectInfo[2]
};

this.projectProperties = projectProperties;
return projectProperties;
}

Expand Down
6 changes: 1 addition & 5 deletions src/tasks/TaskHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,7 @@ export function getDefaultImageName(nameHint: string, tag?: 'dev' | 'latest'): s

export function getDefaultContainerName(nameHint: string, tag?: 'dev' | 'latest'): string {
tag = tag || 'dev';
return getContainerNameWithTag(getValidImageName(nameHint), tag);
}

export function getContainerNameWithTag(name: string, tag: string): string {
return `${name}-${tag}`;
return `${getValidImageName(nameHint)}-${tag}`;
}

export async function recursiveFindTaskByType(allTasks: TaskDefinitionBase[], type: string, node: DebugConfigurationBase | TaskDefinitionBase): Promise<TaskDefinitionBase | undefined> {
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/netSdk/NetSdkRunTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NetSdkRunTaskProvider extends DockerTaskProvider {
const projectFolderPath = path.dirname(projectPath);

// use dotnet to build the image
const buildCommand = await getNetSdkBuildCommand(isProjectWebApp, task.definition.dockerRun.containerName);
const buildCommand = await getNetSdkBuildCommand(isProjectWebApp, task.definition.dockerRun.image);
await context.terminal.execAsyncInTerminal(
buildCommand,
{
Expand All @@ -41,7 +41,7 @@ export class NetSdkRunTaskProvider extends DockerTaskProvider {
);

// use docker run to run the image
const runCommand = await getNetSdkRunCommand(isProjectWebApp, task.definition.dockerRun.containerName);
const runCommand = await getNetSdkRunCommand(isProjectWebApp, task.definition.dockerRun.image);
await context.terminal.execAsyncInTerminal(
runCommand,
{
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/netSdk/netSdkTaskUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RunContainerBindMount, RunContainerCommandOptions, Shell, composeArgs,
import { getImageNameWithTag } from '../../utils/getValidImageName';
import { getDockerOSType } from "../../utils/osUtils";
import { defaultVsCodeLabels } from "../TaskDefinitionBase";
import { getContainerNameWithTag } from '../TaskHelper';
import { getDefaultContainerName } from '../TaskHelper';

/**
* Native architecture of the current machine in the RID format
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function getNetSdkRunCommand(isProjectWebApp: boolean, imageName: s
const options: RunContainerCommandOptions = {
detached: true,
publishAllPorts: true,
name: getContainerNameWithTag(imageName, NetSdkDefaultImageTag),
name: getDefaultContainerName(imageName, NetSdkDefaultImageTag),
environmentVariables: {},
removeOnExit: true,
imageRef: getImageNameWithTag(imageName, NetSdkDefaultImageTag),
Expand Down

0 comments on commit 3ada909

Please sign in to comment.