@@ -13,9 +13,9 @@ import fs from '@react-native-windows/fs';
1313import path from 'path' ;
1414import chalk from 'chalk' ;
1515import { performance } from 'perf_hooks' ;
16- import { XMLSerializer } from '@xmldom/xmldom' ;
16+
1717import { Ora } from 'ora' ;
18- const formatter = require ( 'xml-formatter' ) ;
18+
1919
2020import 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 ;
0 commit comments