11// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
22
33using PostSharp . Engineering . BuildTools . Build . Model ;
4+ using PostSharp . Engineering . BuildTools . Dependencies . Model ;
45using PostSharp . Engineering . BuildTools . Utilities ;
6+ using System . Diagnostics . CodeAnalysis ;
57using System . IO ;
68using System . Linq ;
79using System . Xml . Linq ;
@@ -12,16 +14,80 @@ internal static class AutoUpdatedVersionsFile
1214{
1315 public const string FileName = "AutoUpdatedVersions.props" ;
1416
15- public static bool TryWrite ( BuildContext context , bool dry , out bool hasChanges )
17+ public static bool TryRead (
18+ BuildContext context ,
19+ [ NotNullWhen ( true ) ] out string ? dependencyReleasedVersion ,
20+ [ NotNullWhen ( true ) ] out string ? releasedMainVersionPropertyValue )
21+ => TryRead (
22+ context ,
23+ context . Product . DependencyDefinition ,
24+ Path . Combine ( context . RepoDirectory , context . Product . AutoUpdatedVersionsFilePath ) ,
25+ out dependencyReleasedVersion ,
26+ out releasedMainVersionPropertyValue ) ;
27+
28+ private static bool TryRead (
29+ BuildContext context ,
30+ DependencyDefinition dependency ,
31+ string path ,
32+ [ NotNullWhen ( true ) ] out string ? dependencyReleasedVersion ,
33+ [ NotNullWhen ( true ) ] out string ? releasedMainVersionPropertyValue )
34+ {
35+ var theirAutoUpdatedVersionsDocument = XDocument . Load ( path ) ;
36+
37+ var releasedVersionPropertyName = $ "{ dependency . NameWithoutDot } ReleaseVersion";
38+
39+ dependencyReleasedVersion = theirAutoUpdatedVersionsDocument . Root
40+ ? . Element ( "PropertyGroup" )
41+ ? . Element ( releasedVersionPropertyName )
42+ ? . Value ;
43+
44+ if ( string . IsNullOrEmpty ( dependencyReleasedVersion ) )
45+ {
46+ context . Console . WriteError ( $ "The '{ releasedVersionPropertyName } ' property in '{ path } ' is not defined." ) ;
47+
48+ releasedMainVersionPropertyValue = null ;
49+ dependencyReleasedVersion = null ;
50+
51+ return false ;
52+ }
53+
54+ var releasedMainVersionPropertyName = $ "{ dependency . NameWithoutDot } ReleaseMainVersion";
55+
56+ releasedMainVersionPropertyValue = theirAutoUpdatedVersionsDocument . Root
57+ ? . Element ( "PropertyGroup" )
58+ ? . Element ( releasedMainVersionPropertyName )
59+ ? . Value ;
60+
61+ if ( string . IsNullOrEmpty ( releasedMainVersionPropertyValue ) )
62+ {
63+ context . Console . WriteError ( $ "The '{ releasedMainVersionPropertyName } ' property in '{ path } ' is not defined." ) ;
64+
65+ releasedMainVersionPropertyValue = null ;
66+ dependencyReleasedVersion = null ;
67+
68+ return false ;
69+ }
70+
71+ return true ;
72+ }
73+
74+ public static bool TryWrite (
75+ BuildContext context ,
76+ bool dry ,
77+ out bool hasDependenciesChanges ,
78+ out bool hasChanges ,
79+ [ NotNullWhen ( true ) ] out string ? packageVersion ,
80+ [ NotNullWhen ( true ) ] out string ? mainVersion )
1681 {
1782 context . Console . WriteImportantMessage ( $ "Checking versions of auto-updated dependencies." ) ;
1883
1984 hasChanges = false ;
85+ hasDependenciesChanges = false ;
2086
2187 var autoUpdatedDependencies = context . Product . DependencyDefinition . GetAllDependencies ( BuildConfiguration . Public )
2288 . Where ( d => d . Definition . AutoUpdateVersion )
2389 . ToArray ( ) ;
24-
90+
2591 // Load XML.
2692 var thisAutoUpdatedVersionsFilePath = Path . Combine ( context . RepoDirectory , context . Product . AutoUpdatedVersionsFilePath ) ;
2793 var thisAutoUpdatedVersionsDocument = XDocument . Load ( thisAutoUpdatedVersionsFilePath , LoadOptions . PreserveWhitespace ) ;
@@ -37,7 +103,13 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
37103
38104 string [ ] filePathCandidates =
39105 [
40- Path . GetFullPath ( Path . Combine ( context . RepoDirectory , context . Product . SourceDependenciesDirectory , dependency . Name , dependency . EngineeringDirectory , FileName ) ) ,
106+ Path . GetFullPath (
107+ Path . Combine (
108+ context . RepoDirectory ,
109+ context . Product . SourceDependenciesDirectory ,
110+ dependency . Name ,
111+ dependency . EngineeringDirectory ,
112+ FileName ) ) ,
41113 Path . GetFullPath ( Path . Combine ( context . RepoDirectory , ".." , dependency . Name , dependency . EngineeringDirectory , FileName ) )
42114 ] ;
43115
@@ -52,18 +124,13 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
52124 continue ;
53125 }
54126
55- var theirAutoUpdatedVersionsDocument = XDocument . Load ( theirAutoUpdatedVersionsFilePath ) ;
56-
57- var releasedVersionPropertyName = $ "{ dependency . NameWithoutDot } ReleaseVersion";
58-
59- var dependencyReleasedVersion = theirAutoUpdatedVersionsDocument . Root
60- ? . Element ( "PropertyGroup" )
61- ? . Element ( releasedVersionPropertyName )
62- ? . Value ;
63-
64- if ( string . IsNullOrEmpty ( dependencyReleasedVersion ) )
127+ if ( ! TryRead (
128+ context ,
129+ dependency ,
130+ theirAutoUpdatedVersionsFilePath ,
131+ out var dependencyReleasedVersion ,
132+ out var releasedMainVersionPropertyValue ) )
65133 {
66- context . Console . WriteError ( $ "The '{ releasedVersionPropertyName } ' property in '{ theirAutoUpdatedVersionsFilePath } ' is not defined." ) ;
67134 errors ++ ;
68135
69136 continue ;
@@ -90,40 +157,32 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
90157
91158 versionElement . Value = dependencyReleasedVersion ;
92159 hasChanges = true ;
160+ hasDependenciesChanges = true ;
93161
94162 context . Console . WriteMessage ( $ "Setting version dependency '{ dependency } ' from '{ oldVersionValue } ' to '{ dependencyReleasedVersion } '." ) ;
95163
96164 // Getting the inherited main version.
97165 if ( context . Product . MainVersionDependency == dependency )
98166 {
99- var releasedMainVersionPropertyName = $ "{ dependency . NameWithoutDot } ReleaseMainVersion";
100-
101- var releasedMainVersionPropertyValue = theirAutoUpdatedVersionsDocument . Root
102- ? . Element ( "PropertyGroup" )
103- ? . Element ( releasedMainVersionPropertyName )
104- ? . Value ;
105-
106- if ( string . IsNullOrEmpty ( releasedMainVersionPropertyValue ) )
107- {
108- context . Console . WriteError ( $ "The '{ releasedMainVersionPropertyName } ' property in '{ theirAutoUpdatedVersionsFilePath } ' is not defined." ) ;
109- errors ++ ;
110-
111- continue ;
112- }
113-
114167 inheritedMainVersion = releasedMainVersionPropertyValue ;
115168 }
116169 }
117170
118171 // Stop here if errors.
119172 if ( errors > 0 )
120173 {
174+ packageVersion = null ;
175+ mainVersion = null ;
176+
121177 return false ;
122178 }
123179
124180 // Get the version of this component.
125181 if ( ! MainVersionFile . TryRead ( context , out var mainVersionFile ) )
126182 {
183+ packageVersion = null ;
184+ mainVersion = null ;
185+
127186 return false ;
128187 }
129188
@@ -136,6 +195,9 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
136195 null ,
137196 out var versionComponents ) )
138197 {
198+ packageVersion = null ;
199+ mainVersion = null ;
200+
139201 return false ;
140202 }
141203
@@ -152,7 +214,6 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
152214 if ( thisVersionElement . Value != versionComponents . PackageVersion )
153215 {
154216 hasChanges = true ;
155- context . Console . WriteMessage ( $ "Updating '{ thisVersionElement . Name } ' to '{ versionComponents . PackageVersion } '." ) ;
156217 thisVersionElement . Value = versionComponents . PackageVersion ;
157218 }
158219
@@ -165,7 +226,6 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
165226 if ( thisMainVersionElement . Value != versionComponents . MainVersion )
166227 {
167228 hasChanges = true ;
168- context . Console . WriteMessage ( $ "Updating '{ thisMainVersionElement . Name } ' to '{ versionComponents . MainVersion } '." ) ;
169229 thisMainVersionElement . Value = versionComponents . MainVersion ;
170230 }
171231
@@ -174,22 +234,25 @@ public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges
174234 {
175235 if ( ! dry )
176236 {
177- TextFileHelper . WriteIfDifferent ( thisAutoUpdatedVersionsFilePath , thisAutoUpdatedVersionsDocument . ToString ( ) , context ) ;
237+ hasChanges = TextFileHelper . WriteIfDifferent ( thisAutoUpdatedVersionsFilePath , thisAutoUpdatedVersionsDocument , context ) ;
178238 }
179239 else
180240 {
181241 context . Console . WriteMessage ( $ "New content for '{ thisAutoUpdatedVersionsFilePath } ':" ) ;
182- context . Console . WriteMessage ( thisAutoUpdatedVersionsDocument . ToString ( ) ) ;
242+ context . Console . WriteMessage ( thisAutoUpdatedVersionsDocument . ToNiceString ( ) ) ;
183243 }
184244 }
185245
246+ packageVersion = versionComponents . PackageVersion ;
247+ mainVersion = versionComponents . MainVersion ;
248+
186249 return true ;
187250 }
188251
189252 public static bool TryWriteAndCommit ( BuildContext context , bool dry )
190253 {
191254 // Go through all dependencies and update their fixed version in AutoUpdatedVersions.props file.
192- if ( ! TryWrite ( context , dry , out var dependenciesUpdated ) )
255+ if ( ! TryWrite ( context , dry , out var dependenciesUpdated , out _ , out _ , out _ ) )
193256 {
194257 return false ;
195258 }
0 commit comments