1
- using System . Xml . Linq ;
2
-
3
- namespace LibYear . Lib . FileTypes ;
4
-
5
- public abstract class XmlProjectFile : IProjectFile
6
- {
7
- private readonly XDocument _xmlContents ;
8
- private readonly string _elementName ;
9
- private readonly string [ ] _packageAttributeNames ;
10
- private readonly string _versionAttributeName ;
11
- private static readonly object _lock = new ( ) ;
12
-
13
- public string FileName { get ; }
14
- public IDictionary < string , PackageVersion ? > Packages { get ; }
15
-
16
- protected XmlProjectFile ( string filename , string elementName , string [ ] packageAttributeNames , string versionAttributeName )
17
- {
18
- FileName = filename ;
19
-
20
- _elementName = elementName ;
21
- _packageAttributeNames = packageAttributeNames ;
22
- _versionAttributeName = versionAttributeName ;
23
-
24
- lock ( _lock )
25
- {
26
- using var stream = new FileStream ( FileName , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
27
- _xmlContents = XDocument . Load ( stream ) ;
28
- }
29
-
30
- Packages = _xmlContents . Descendants ( elementName )
31
- . ToDictionary (
32
- d => packageAttributeNames . Select ( p => d . Attribute ( p ) ? . Value ?? d . Element ( p ) ? . Value ) . FirstOrDefault ( v => v != null ) ! ,
33
- d => ParseCurrentVersion ( d , versionAttributeName )
34
- ) ;
35
- }
36
-
37
- public void Update ( IEnumerable < Result > results )
38
- {
39
- lock ( _lock )
40
- {
41
- foreach ( var result in results . Where ( r => r . Latest != null ) )
42
- {
43
- foreach ( var element in GetElements ( result ) )
44
- UpdateElement ( element , result . Latest ! . Version . ToString ( ) ) ;
45
- }
46
-
47
- using var stream = new FileStream ( FileName , FileMode . Open , FileAccess . Write ) ;
48
- new StreamWriter ( stream ) . WriteAsync ( _xmlContents . ToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
49
- }
50
- }
51
-
52
- private static PackageVersion ? ParseCurrentVersion ( XElement element , string versionAttributeName )
53
- {
54
- var version = element . Attribute ( versionAttributeName ) ? . Value ?? element . Element ( versionAttributeName ) ? . Value ?? string . Empty ;
55
- return ! string . IsNullOrEmpty ( version ) ? PackageVersion . Parse ( version ) : null ;
56
- }
57
-
58
- private void UpdateElement ( XElement element , string latestVersion )
59
- {
60
- var attribute = element . Attribute ( _versionAttributeName ) ;
61
- if ( attribute != null )
62
- {
63
- attribute . Value = latestVersion ;
64
- return ;
65
- }
66
-
67
- var e = element . Element ( _versionAttributeName ) ;
68
- if ( e != null )
69
- e . Value = latestVersion ;
70
- }
71
-
72
- private IEnumerable < XElement > GetElements ( Result result )
73
- {
74
- return _xmlContents . Descendants ( _elementName )
75
- . Where ( d => _packageAttributeNames . Any ( attributeName => ( d . Attribute ( attributeName ) ? . Value ?? d . Element ( attributeName ) ? . Value ) == result . Name
76
- && ( d . Attribute ( _versionAttributeName ) ? . Value ?? d . Element ( _versionAttributeName ) ? . Value ) == result . Installed ? . Version . ToString ( )
77
- )
78
- ) ;
79
- }
1
+ using System . Xml . Linq ;
2
+
3
+ namespace LibYear . Core . FileTypes ;
4
+
5
+ public abstract class XmlProjectFile : IProjectFile
6
+ {
7
+ private readonly XDocument _xmlContents ;
8
+ private readonly string _elementName ;
9
+ private readonly string [ ] _packageAttributeNames ;
10
+ private readonly string _versionAttributeName ;
11
+ private static readonly object _lock = new ( ) ;
12
+
13
+ public string FileName { get ; }
14
+ public IDictionary < string , PackageVersion ? > Packages { get ; }
15
+
16
+ protected XmlProjectFile ( string filename , string elementName , string [ ] packageAttributeNames , string versionAttributeName )
17
+ {
18
+ FileName = filename ;
19
+
20
+ _elementName = elementName ;
21
+ _packageAttributeNames = packageAttributeNames ;
22
+ _versionAttributeName = versionAttributeName ;
23
+
24
+ lock ( _lock )
25
+ {
26
+ using var stream = new FileStream ( FileName , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
27
+ _xmlContents = XDocument . Load ( stream ) ;
28
+ }
29
+
30
+ Packages = _xmlContents . Descendants ( elementName )
31
+ . ToDictionary (
32
+ d => packageAttributeNames . Select ( p => d . Attribute ( p ) ? . Value ?? d . Element ( p ) ? . Value ) . FirstOrDefault ( v => v != null ) ! ,
33
+ d => ParseCurrentVersion ( d , versionAttributeName )
34
+ ) ;
35
+ }
36
+
37
+ public void Update ( IEnumerable < Result > results )
38
+ {
39
+ lock ( _lock )
40
+ {
41
+ foreach ( var result in results . Where ( r => r . Latest != null ) )
42
+ {
43
+ foreach ( var element in GetElements ( result ) )
44
+ UpdateElement ( element , result . Latest ! . Version . ToString ( ) ) ;
45
+ }
46
+
47
+ using var stream = new FileStream ( FileName , FileMode . Open , FileAccess . Write ) ;
48
+ new StreamWriter ( stream ) . WriteAsync ( _xmlContents . ToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
49
+ }
50
+ }
51
+
52
+ private static PackageVersion ? ParseCurrentVersion ( XElement element , string versionAttributeName )
53
+ {
54
+ var version = element . Attribute ( versionAttributeName ) ? . Value ?? element . Element ( versionAttributeName ) ? . Value ?? string . Empty ;
55
+ return ! string . IsNullOrEmpty ( version ) ? PackageVersion . Parse ( version ) : null ;
56
+ }
57
+
58
+ private void UpdateElement ( XElement element , string latestVersion )
59
+ {
60
+ var attribute = element . Attribute ( _versionAttributeName ) ;
61
+ if ( attribute != null )
62
+ {
63
+ attribute . Value = latestVersion ;
64
+ return ;
65
+ }
66
+
67
+ var e = element . Element ( _versionAttributeName ) ;
68
+ if ( e != null )
69
+ e . Value = latestVersion ;
70
+ }
71
+
72
+ private IEnumerable < XElement > GetElements ( Result result )
73
+ {
74
+ return _xmlContents . Descendants ( _elementName )
75
+ . Where ( d => _packageAttributeNames . Any ( attributeName => ( d . Attribute ( attributeName ) ? . Value ?? d . Element ( attributeName ) ? . Value ) == result . Name
76
+ && ( d . Attribute ( _versionAttributeName ) ? . Value ?? d . Element ( _versionAttributeName ) ? . Value ) == result . Installed ? . Version . ToString ( )
77
+ )
78
+ ) ;
79
+ }
80
80
}
0 commit comments