Skip to content

Commit 8c72a0c

Browse files
authored
Use .NET's newer -getProperty capability (#335)
1 parent 14c3e6a commit 8c72a0c

File tree

7 files changed

+130
-176
lines changed

7 files changed

+130
-176
lines changed

resources/netCore/GetBlazorManifestLocations.targets

Lines changed: 0 additions & 19 deletions
This file was deleted.

resources/netCore/GetProjectProperties.targets

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/debugging/netSdk/NetSdkDebugHelper.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,29 @@ export class NetSdkDebugHelper extends NetCoreDebugHelper {
111111
const ridOS = await normalizeOsToRidOs();
112112
const ridArchitecture = await normalizeArchitectureToRidArchitecture();
113113
const additionalProperties = composeArgs(
114-
withNamedArg('/p:ContainerRuntimeIdentifier', `"${ridOS}-${ridArchitecture}"`, { assignValue: true }), // We have to pre-quote the file paths because we cannot simultaneously use `assignValue` and `shouldQuote`
114+
withNamedArg('-p:ContainerRuntimeIdentifier', `"${ridOS}-${ridArchitecture}"`, { assignValue: true }), // We have to pre-quote the RID because we cannot simultaneously use `assignValue` and `shouldQuote`
115115
)();
116116
const resolvedAppProject = resolveVariables(debugConfiguration.netCore?.appProject, folder);
117117

118-
const projectInfo = await getNetCoreProjectInfo('GetProjectProperties', resolvedAppProject, additionalProperties);
118+
const projectInfo = await getNetCoreProjectInfo(resolvedAppProject, additionalProperties);
119119

120-
if (projectInfo.length < 6 || !projectInfo[5]) {
120+
if (!projectInfo.enableSdkContainerSupport) {
121121
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."));
122122
}
123123

124-
const projectProperties: NetSdkProjectProperties = {
125-
assemblyName: projectInfo[0],
126-
targetFramework: projectInfo[1],
127-
appOutput: projectInfo[2],
128-
containerWorkingDirectory: projectInfo[3],
129-
isSdkContainerSupportEnabled: projectInfo[4] === 'true',
130-
imageName: projectInfo[5],
124+
return {
125+
assemblyName: projectInfo.assemblyName,
126+
targetFramework: projectInfo.targetFrameworks[0],
127+
appOutput: projectInfo.assemblyRelativeOutputPath,
128+
containerWorkingDirectory: projectInfo.assemblyContainerPath,
129+
isSdkContainerSupportEnabled: projectInfo.enableSdkContainerSupport,
130+
imageName: projectInfo.imageName,
131131
};
132-
133-
return projectProperties;
134132
}
135133

136134
private async normalizeAppOutput(unnormalizedContainerWorkingDirectory: string, isSdkContainerSupportEnabled: boolean): Promise<string> {
137135
if (isSdkContainerSupportEnabled) {
138-
return await getDockerOSType() === 'windows' // fourth is output path
136+
return await getDockerOSType() === 'windows'
139137
? path.win32.normalize(unnormalizedContainerWorkingDirectory)
140138
: path.posix.normalize(unnormalizedContainerWorkingDirectory);
141139
} else {

src/debugging/netcore/NetCoreDebugHelper.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,13 @@ export class NetCoreDebugHelper implements DebugHelper {
204204
}
205205

206206
protected async getProjectProperties(debugConfiguration: DockerDebugConfiguration): Promise<NetCoreProjectProperties> {
207-
const projectInfo = await getNetCoreProjectInfo('GetProjectProperties', debugConfiguration.netCore?.appProject);
207+
const projectInfo = await getNetCoreProjectInfo(debugConfiguration.netCore?.appProject);
208208

209-
if (projectInfo.length < 3) {
210-
throw new Error(l10n.t('Unable to determine assembly output path.'));
211-
}
212-
213-
// First line is assembly name, second is target framework, third+ are output path(s)
214-
const projectProperties: NetCoreProjectProperties = {
215-
assemblyName: projectInfo[0],
216-
targetFramework: projectInfo[1],
217-
appOutput: projectInfo[2]
209+
return {
210+
assemblyName: projectInfo.assemblyName,
211+
targetFramework: projectInfo.targetFrameworks[0],
212+
appOutput: projectInfo.assemblyRelativeOutputPath,
218213
};
219-
220-
return projectProperties;
221214
}
222215

223216
private async acquireDebuggers(platformOS: PlatformOS): Promise<void> {

src/scaffolding/wizard/netCore/NetCoreGatherInformationStep.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@ export class NetCoreGatherInformationStep extends GatherInformationStep<NetCoreS
2828
public async prompt(wizardContext: NetCoreScaffoldingWizardContext): Promise<void> {
2929
await this.ensureNetCoreBuildTasks(wizardContext);
3030

31-
const projectInfo = await getNetCoreProjectInfo('GetProjectProperties', wizardContext.artifact);
32-
33-
if (projectInfo.length < 2) {
34-
throw new Error(vscode.l10n.t('Unable to determine project info for \'{0}\'', wizardContext.artifact));
35-
}
31+
const projectInfo = await getNetCoreProjectInfo(wizardContext.artifact);
3632

3733
if (!wizardContext.netCoreAssemblyName) {
38-
wizardContext.netCoreAssemblyName = projectInfo[0]; // Line 1 is the assembly name including ".dll"
34+
wizardContext.netCoreAssemblyName = projectInfo.assemblyName;
3935
}
4036

4137
if (!wizardContext.netCoreRuntimeBaseImage || !wizardContext.netCoreSdkBaseImage) {
42-
this.targetFramework = projectInfo[1]; // Line 2 is the <TargetFramework> value, or first item from <TargetFrameworks>
38+
this.targetFramework = projectInfo.targetFrameworks[0];
4339

4440
const regexMatch = /net(coreapp)?([\d.]+)/i.exec(this.targetFramework);
4541

src/tasks/netcore/updateBlazorManifest.ts

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,21 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as fse from 'fs-extra';
7-
import * as path from 'path';
8-
import { l10n } from 'vscode';
9-
import * as xml2js from 'xml2js';
10-
import { getNetCoreProjectInfo } from '../../utils/netCoreUtils';
7+
import * as vscode from 'vscode';
8+
import { getBlazorManifestInfo } from '../../utils/netCoreUtils';
119
import { pathNormalize } from '../../utils/pathNormalize';
1210
import { PlatformOS } from '../../utils/platform';
1311
import { DockerContainerVolume } from '../DockerRunTaskDefinitionBase';
1412
import { DockerRunTaskDefinition } from "../DockerRunTaskProvider";
1513
import { DockerRunTaskContext } from "../TaskHelper";
1614

17-
interface ContentRootAttributes {
18-
BasePath: string;
19-
Path: string;
20-
}
21-
22-
interface ContentRoot {
23-
$: ContentRootAttributes;
24-
}
25-
26-
interface StaticWebAssets {
27-
ContentRoot: ContentRoot[];
28-
}
29-
30-
interface XmlManifest {
31-
StaticWebAssets: StaticWebAssets;
32-
}
33-
3415
interface JsonManifest {
3516
ContentRoots: string[];
3617
}
3718

3819
export async function updateBlazorManifest(context: DockerRunTaskContext, runDefinition: DockerRunTaskDefinition): Promise<void> {
39-
const contents = await getNetCoreProjectInfo('GetBlazorManifestLocations', runDefinition.netCore.appProject);
40-
41-
if (contents.length < 2) {
42-
throw new Error(l10n.t('Unable to determine Blazor manifest locations from output file.'));
43-
}
44-
45-
await transformBlazorManifest(context, contents[0].trim(), contents[1].trim(), runDefinition.dockerRun.volumes, runDefinition.dockerRun.os);
20+
const blazorInfo = await getBlazorManifestInfo(runDefinition.netCore.appProject);
21+
await transformBlazorManifest(context, blazorInfo.inputManifestPath, blazorInfo.outputManifestPath, runDefinition.dockerRun.volumes, runDefinition.dockerRun.os);
4622
}
4723

4824
async function transformBlazorManifest(context: DockerRunTaskContext, inputManifest: string, outputManifest: string, volumes: DockerContainerVolume[], os: PlatformOS): Promise<void> {
@@ -58,20 +34,16 @@ async function transformBlazorManifest(context: DockerRunTaskContext, inputManif
5834

5935
os = os || 'Linux';
6036

61-
context.terminal.writeOutputLine(l10n.t('Attempting to containerize Blazor static web assets manifest...'));
37+
context.terminal.writeOutputLine(vscode.l10n.t('Attempting to containerize Blazor static web assets manifest...'));
6238

63-
if (path.extname(inputManifest) === '.json') {
64-
await transformJsonBlazorManifest(inputManifest, outputManifest, volumes, os);
65-
} else {
66-
await transformXmlBlazorManifest(inputManifest, outputManifest, volumes, os);
67-
}
39+
await transformJsonBlazorManifest(inputManifest, outputManifest, volumes, os);
6840
}
6941

7042
async function transformJsonBlazorManifest(inputManifest: string, outputManifest: string, volumes: DockerContainerVolume[], os: PlatformOS): Promise<void> {
7143
const manifest = <JsonManifest>await fse.readJson(inputManifest);
7244

7345
if (!manifest?.ContentRoots) {
74-
throw new Error(l10n.t('Failed to parse Blazor static web assets manifest.'));
46+
throw new Error(vscode.l10n.t('Failed to parse Blazor static web assets manifest.'));
7547
}
7648

7749
if (!Array.isArray(manifest.ContentRoots)) {
@@ -87,33 +59,6 @@ async function transformJsonBlazorManifest(inputManifest: string, outputManifest
8759
await fse.utimes(outputManifest, 0, 0);
8860
}
8961

90-
async function transformXmlBlazorManifest(inputManifest: string, outputManifest: string, volumes: DockerContainerVolume[], os: PlatformOS): Promise<void> {
91-
const contents = (await fse.readFile(inputManifest)).toString();
92-
const manifest = <XmlManifest>await xml2js.parseStringPromise(contents);
93-
94-
if (!manifest?.StaticWebAssets) {
95-
throw new Error(l10n.t('Failed to parse Blazor static web assets manifest.'));
96-
}
97-
98-
if (!Array.isArray(manifest.StaticWebAssets.ContentRoot)) {
99-
return;
100-
}
101-
102-
for (const contentRoot of manifest.StaticWebAssets.ContentRoot) {
103-
if (contentRoot && contentRoot.$) {
104-
contentRoot.$.Path = tryContainerizePath(contentRoot.$.Path, volumes, os);
105-
}
106-
}
107-
108-
const outputContents = (new xml2js.Builder()).buildObject(manifest);
109-
110-
// Write out a new manifest
111-
await fse.writeFile(outputManifest, outputContents);
112-
113-
// Set the mtime to 1970 so that next time .NET builds, it will overwrite the output file
114-
await fse.utimes(outputManifest, 0, 0);
115-
}
116-
11762
function tryContainerizePath(oldPath: string, volumes: DockerContainerVolume[], os: PlatformOS): string {
11863
const matchingVolume: DockerContainerVolume = volumes.find(v => oldPath.toLowerCase().startsWith(v.localPath.toLowerCase()));
11964

0 commit comments

Comments
 (0)