@@ -4,10 +4,18 @@ namespace LibYear.Core.FileTypes;
44
55public abstract class XmlProjectFile : IProjectFile
66{
7+ private enum Whitespace
8+ {
9+ Tabs ,
10+ FourSpaces ,
11+ TwoSpaces
12+ }
13+
714 public string FileName { get ; }
815 public IDictionary < string , PackageVersion ? > Packages { get ; }
916
1017 private readonly XDocument _xmlContents ;
18+ private readonly Whitespace _whitespace ;
1119 private readonly string _elementName ;
1220 private readonly string [ ] _packageAttributeNames ;
1321 private readonly string _versionAttributeName ;
@@ -21,13 +29,20 @@ protected XmlProjectFile(string filename, string contents, string elementName, s
2129 _versionAttributeName = versionAttributeName ;
2230
2331 _xmlContents = XDocument . Parse ( contents ) ;
32+ _whitespace = DetermineWhitespace ( contents ) ;
33+
2434 Packages = _xmlContents . Descendants ( elementName )
2535 . ToDictionary (
2636 d => packageAttributeNames . Select ( p => d . Attribute ( p ) ? . Value ?? d . Element ( p ) ? . Value ) . FirstOrDefault ( v => v != null ) ! ,
2737 d => ParseCurrentVersion ( d , versionAttributeName )
2838 ) ;
2939 }
3040
41+ private static Whitespace DetermineWhitespace ( string contents )
42+ => contents . Contains ( "\n \t " ) ? Whitespace . Tabs
43+ : contents . Contains ( "\n <" ) ? Whitespace . TwoSpaces
44+ : Whitespace . FourSpaces ;
45+
3146 public string Update ( IReadOnlyCollection < Result > results )
3247 {
3348 foreach ( var result in results . Where ( r => r . Latest != null ) )
@@ -36,7 +51,13 @@ public string Update(IReadOnlyCollection<Result> results)
3651 UpdateElement ( element , result . Latest ! . Version . ToString ( ) ) ;
3752 }
3853
39- return _xmlContents . ToString ( ) ;
54+ var xml = _xmlContents . ToString ( ) ;
55+ return _whitespace switch
56+ {
57+ Whitespace . Tabs => xml . Replace ( " " , "\t " ) ,
58+ Whitespace . FourSpaces => xml . Replace ( " " , " " ) ,
59+ _ => xml
60+ } ;
4061 }
4162
4263 private PackageVersion ? ParseCurrentVersion ( XElement element , string versionAttributeName )
@@ -71,7 +92,7 @@ private void UpdateElement(XElement element, string latestVersion)
7192 private XElement [ ] GetMatchingElements ( Result result )
7293 => _xmlContents . Descendants ( _elementName )
7394 . Where ( d => _packageAttributeNames . Any ( attributeName => ( d . Attribute ( attributeName ) ? . Value ?? d . Element ( attributeName ) ? . Value ) == result . Name
74- && ( d . Attribute ( _versionAttributeName ) ? . Value ?? d . Element ( _versionAttributeName ) ? . Value ) == result . Installed ? . Version . ToString ( )
95+ && ( d . Attribute ( _versionAttributeName ) ? . Value ?? d . Element ( _versionAttributeName ) ? . Value ) == result . Installed ? . Version . ToString ( )
7596 )
7697 )
7798 . ToArray ( ) ;
0 commit comments