Skip to content

Commit 7ce01ee

Browse files
Copilotjonthysell
andcommitted
Completely remove ensureXAMLDialect() function and its functionality
Co-authored-by: jonthysell <10852185+jonthysell@users.noreply.github.com>
1 parent 5af8740 commit 7ce01ee

File tree

2 files changed

+3
-260
lines changed

2 files changed

+3
-260
lines changed

packages/@react-native-windows/cli/src/commands/autolinkWindows/autolinkWindows.ts

Lines changed: 2 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import fs from '@react-native-windows/fs';
1313
import path from 'path';
1414
import chalk from 'chalk';
1515
import {performance} from 'perf_hooks';
16-
import {XMLSerializer} from '@xmldom/xmldom';
16+
1717
import {Ora} from 'ora';
18-
const formatter = require('xml-formatter');
18+
1919

2020
import type {
2121
Command,
@@ -114,9 +114,6 @@ export class AutoLinkWindows {
114114

115115
verboseMessage('Parsing dependencies...', verbose);
116116

117-
this.changesNecessary =
118-
(await this.ensureXAMLDialect()) || this.changesNecessary;
119-
120117
// Generating cs/cpp files for app code consumption
121118
if (projectLang === 'cs') {
122119
this.changesNecessary =
@@ -693,161 +690,6 @@ export class AutoLinkWindows {
693690
return changesNecessary;
694691
}
695692

696-
protected getExperimentalFeaturesPropsXml() {
697-
const experimentalFeaturesProps = path.join(
698-
path.dirname(this.getSolutionFile()),
699-
'ExperimentalFeatures.props',
700-
);
701-
if (fs.existsSync(experimentalFeaturesProps)) {
702-
const experimentalFeaturesContents = configUtils.readProjectFile(
703-
experimentalFeaturesProps,
704-
);
705-
return {
706-
path: experimentalFeaturesProps,
707-
content: experimentalFeaturesContents,
708-
};
709-
}
710-
return undefined;
711-
}
712-
713-
public async ensureXAMLDialect() {
714-
let changesNeeded = false;
715-
const experimentalFeatures = this.getExperimentalFeaturesPropsXml();
716-
if (experimentalFeatures) {
717-
const useWinUI3FromExperimentalFeatures =
718-
configUtils
719-
.tryFindPropertyValue(experimentalFeatures.content, 'UseWinUI3')
720-
?.toLowerCase() === 'true';
721-
// Check if WinUI2xVersion is specified in experimental features
722-
const targetWinUI2xVersion = configUtils.tryFindPropertyValue(
723-
experimentalFeatures.content,
724-
'WinUI2xVersion',
725-
);
726-
// Check if WinUI3Version is specified in experimental features
727-
const targetWinUI3xVersion = configUtils.tryFindPropertyValue(
728-
experimentalFeatures.content,
729-
'WinUI3Version',
730-
);
731-
// Use the UseWinUI3 value from ExperimentalFeatures.props
732-
changesNeeded = await this.updatePackagesConfigXAMLDialect(
733-
useWinUI3FromExperimentalFeatures,
734-
targetWinUI2xVersion,
735-
targetWinUI3xVersion,
736-
);
737-
}
738-
return changesNeeded;
739-
}
740-
741-
protected getPackagesConfigXml() {
742-
const projectFile = this.getProjectFile();
743-
const packagesConfig = path.join(
744-
path.dirname(projectFile),
745-
'packages.config',
746-
);
747-
748-
if (fs.existsSync(packagesConfig)) {
749-
return {
750-
path: packagesConfig,
751-
content: configUtils.readProjectFile(packagesConfig),
752-
};
753-
}
754-
return undefined;
755-
}
756-
757-
private async updatePackagesConfigXAMLDialect(
758-
useWinUI3: boolean,
759-
targetWinUI2xVersion: string | null,
760-
targetWinUI3xVersion: string | null,
761-
) {
762-
let changed = false;
763-
const packagesConfig = this.getPackagesConfigXml();
764-
if (packagesConfig) {
765-
// if we don't have a packages.config, then this is a C# project, in which case we use <PackageReference> and dynamically pick the right XAML package.
766-
const project = this.getWindowsConfig();
767-
768-
const winUIPropsPath = path.join(
769-
resolveRnwRoot(project),
770-
'PropertySheets/WinUI.props',
771-
);
772-
const winuiPropsContents = configUtils.readProjectFile(winUIPropsPath);
773-
774-
// Use the given WinUI2xVersion, otherwise fallback to WinUI.props
775-
const winui2xVersion =
776-
targetWinUI2xVersion ??
777-
configUtils.tryFindPropertyValue(winuiPropsContents, 'WinUI2xVersion');
778-
779-
// Use the given WinUI3Version, otherwise fallback to WinUI.props
780-
const winui3Version =
781-
targetWinUI3xVersion ??
782-
configUtils.tryFindPropertyValue(winuiPropsContents, 'WinUI3Version');
783-
784-
const dialects = [
785-
{id: 'Microsoft.WindowsAppSDK', version: winui3Version!},
786-
{id: 'Microsoft.UI.Xaml', version: winui2xVersion!},
787-
];
788-
const keepPkg = useWinUI3 ? dialects[0] : dialects[1];
789-
const removePkg = useWinUI3 ? dialects[1] : dialects[0];
790-
791-
changed = this.updatePackagesConfig(
792-
packagesConfig,
793-
[removePkg],
794-
[keepPkg],
795-
);
796-
797-
if (!this.options.check && changed) {
798-
const serializer = new XMLSerializer();
799-
const output = serializer.serializeToString(packagesConfig.content);
800-
const formattedXml = formatter(output, {indentation: ' '});
801-
await this.updateFile(packagesConfig.path, formattedXml);
802-
}
803-
}
804-
return changed;
805-
}
806-
807-
private updatePackagesConfig(
808-
packagesConfig: {path: string; content: Document},
809-
removePkgs: {id: string; version: string}[],
810-
keepPkgs: {id: string; version: string}[],
811-
) {
812-
let changed = false;
813-
const packageElements =
814-
packagesConfig.content.documentElement.getElementsByTagName('package');
815-
816-
const nodesToRemove: Element[] = [];
817-
818-
for (let i = 0; i < packageElements.length; i++) {
819-
const packageElement = packageElements.item(i)!;
820-
const idAttr = packageElement!.getAttributeNode('id');
821-
const id = idAttr!.value;
822-
const keepPkg = keepPkgs.find(pkg => pkg.id === id);
823-
if (removePkgs.find(pkg => pkg.id === id)) {
824-
nodesToRemove.push(packageElement);
825-
changed = true;
826-
} else if (keepPkg) {
827-
changed =
828-
changed || keepPkg.version !== packageElement.getAttribute('version');
829-
packageElement.setAttribute('version', keepPkg.version!);
830-
keepPkgs = keepPkgs.filter(pkg => pkg.id !== keepPkg.id);
831-
}
832-
}
833-
834-
nodesToRemove.forEach(pkg =>
835-
packagesConfig.content.documentElement.removeChild(pkg),
836-
);
837-
838-
keepPkgs.forEach(keepPkg => {
839-
const newPkg = packagesConfig.content.createElement('package');
840-
841-
Object.entries(keepPkg).forEach(([attr, value]) => {
842-
newPkg.setAttribute(attr, value as string);
843-
});
844-
newPkg.setAttribute('targetFramework', 'native');
845-
packagesConfig.content.documentElement.appendChild(newPkg);
846-
changed = true;
847-
});
848-
return changed;
849-
}
850-
851693
/** @return The CLI command to invoke autolink-windows independently */
852694
public getAutolinkWindowsCommand() {
853695
const folder = this.windowsAppConfig.folder;

packages/@react-native-windows/cli/src/e2etest/autolink.test.ts

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66

77
import path from 'path';
88
import {commanderNameToOptionName} from '@react-native-windows/telemetry';
9-
import {projectConfigWindows} from '../commands/config/projectConfig';
109
import {AutoLinkWindows} from '../commands/autolinkWindows/autolinkWindows';
1110
import {
1211
AutoLinkOptions,
1312
autolinkOptions,
1413
} from '../commands/autolinkWindows/autolinkWindowsOptions';
15-
import {DOMParser} from '@xmldom/xmldom';
16-
import {ensureCppAppProject, ensureWinUI3Project} from './projectConfig.utils';
14+
import {ensureWinUI3Project} from './projectConfig.utils';
1715

1816
test('autolink with no windows project', () => {
1917
expect(() => {
@@ -37,34 +35,7 @@ class AutoLinkTest extends AutoLinkWindows {
3735
public getWindowsProjectConfig() {
3836
return this.windowsAppConfig;
3937
}
40-
public packagesConfig = '';
41-
public experimentalFeaturesProps = '';
42-
protected getPackagesConfigXml() {
43-
return {
44-
path: 'packages.config',
45-
content: new DOMParser().parseFromString(
46-
this.packagesConfig,
47-
'application/xml',
48-
),
49-
};
50-
}
51-
protected getExperimentalFeaturesPropsXml() {
52-
return {
53-
path: 'ExperimentalFeatures.props',
54-
content: new DOMParser().parseFromString(
55-
this.experimentalFeaturesProps,
56-
'application/xml',
57-
),
58-
};
59-
}
6038
protected async updateFile(filepath: string, content: string) {
61-
if (filepath === 'packages.config') {
62-
this.packagesConfig = content;
63-
} else if (filepath === 'ExperimentalFeatures.props') {
64-
this.experimentalFeaturesProps = content;
65-
} else {
66-
throw new Error(`Unknown path: ${filepath}`);
67-
}
6839
return true;
6940
}
7041
}
@@ -250,76 +221,6 @@ test('one valid cs autolink dependency', () => {
250221
});
251222

252223

253-
254-
test('ensureXAMLDialect - useWinUI3 not in react-native.config.js, useWinUI3=true in ExperimentalFeatures.props', async () => {
255-
const folder = path.resolve('src/e2etest/projects/WithWinUI3');
256-
const rnc = require(path.join(folder, 'react-native.config.js'));
257-
258-
const config = projectConfigWindows(folder, rnc.project.windows)!;
259-
delete config.useWinUI3;
260-
const al = new AutoLinkTest(
261-
{windows: config},
262-
{},
263-
{
264-
check: false,
265-
logging: false,
266-
},
267-
);
268-
al.experimentalFeaturesProps = `<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup><UseWinUI3>true</UseWinUI3></PropertyGroup></Project>`;
269-
al.packagesConfig = `<packages><package id="SuperPkg" version="42"/></packages>`;
270-
271-
const exd = await al.ensureXAMLDialect();
272-
expect(exd).toBeTruthy();
273-
274-
const expectedExperimentalFeatures =
275-
'<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup><UseWinUI3>true</UseWinUI3></PropertyGroup></Project>';
276-
expect(al.experimentalFeaturesProps).toEqual(expectedExperimentalFeatures);
277-
278-
// example packages.config:
279-
// <packages>
280-
// <package id="SuperPkg" version="42"/>
281-
// <package id="Microsoft.WindowsAppSDK" version="1.0.0" targetFramework="native"/>
282-
// </packages>
283-
//
284-
expect(al.packagesConfig).toContain('Microsoft.WindowsAppSDK');
285-
expect(al.packagesConfig).toContain('<package id="SuperPkg" version="42"/>');
286-
expect(al.packagesConfig).not.toContain('Microsoft.UI.Xaml');
287-
});
288-
289-
test('ensureXAMLDialect - useWinUI3 not in react-native.config.js, useWinUI3=false in ExperimentalFeatures.props', async () => {
290-
const folder = path.resolve('src/e2etest/projects/WithWinUI3');
291-
const rnc = require(path.join(folder, 'react-native.config.js'));
292-
293-
const config = projectConfigWindows(folder, rnc.project.windows)!;
294-
delete config.useWinUI3;
295-
const al = new AutoLinkTest(
296-
{windows: config},
297-
{},
298-
{
299-
check: false,
300-
logging: false,
301-
},
302-
);
303-
al.experimentalFeaturesProps = `<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup><UseWinUI3>false</UseWinUI3></PropertyGroup></Project>`;
304-
al.packagesConfig = `<packages><package id="SuperPkg" version="42"/><package id="Microsoft.WindowsAppSDK"/></packages>`;
305-
306-
const exd = await al.ensureXAMLDialect();
307-
expect(exd).toBeTruthy();
308-
309-
const expectedExperimentalFeatures = `<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup><UseWinUI3>false</UseWinUI3></PropertyGroup></Project>`;
310-
expect(al.experimentalFeaturesProps).toEqual(expectedExperimentalFeatures);
311-
312-
// example packages.config:
313-
// <packages>
314-
// <package id="SuperPkg" version="42"/>
315-
// <package id="Microsoft.WindowsAppSDK" version="1.0.0" targetFramework="native"/>
316-
// </packages>
317-
//
318-
expect(al.packagesConfig).not.toContain('Microsoft.WindowsAppSDK');
319-
expect(al.packagesConfig).toContain('<package id="SuperPkg" version="42"/>');
320-
expect(al.packagesConfig).toContain('Microsoft.UI.Xaml');
321-
});
322-
323224
test('Indirect autolink dependency', () => {
324225
const autolink = new AutoLinkTest(
325226
{windows: {folder: __dirname, sourceDir: '.', solutionFile: 'foo.sln'}},

0 commit comments

Comments
 (0)