@@ -102,94 +102,55 @@ public static bool TryCompute(
102102 DependenciesConfigurationFile dependenciesConfigurationFile ,
103103 [ NotNullWhen ( true ) ] out VersionComponents ? version )
104104 {
105- var product = context . Product ;
106- var configurationLowerCase = configuration . ToString ( ) . ToLowerInvariant ( ) ;
107-
108- version = null ;
109- string ? mainVersion = null ;
105+ string ? inheritedMainVersion ;
110106
111- if ( product . MainVersionDependency != null )
107+ if ( context . Product . MainVersionDependency != null )
112108 {
113- var mainVersionDependencyName = product . MainVersionDependency . Name ;
114-
115- // The main version is defined in a dependency. Load the import file.
116-
117- if ( ! dependenciesConfigurationFile . Dependencies . TryGetValue ( mainVersionDependencyName , out var dependencySource ) )
109+ if ( ! TryInheritedReadMainVersion ( context , dependenciesConfigurationFile , settings , out inheritedMainVersion ) )
118110 {
119- context . Console . WriteError ( $ "Cannot find a dependency named ' { mainVersionDependencyName } '." ) ;
111+ version = null ;
120112
121113 return false ;
122114 }
115+ }
116+ else
117+ {
118+ inheritedMainVersion = null ;
119+ }
123120
124- // Note that the version suffix is not copied from the dependency, only the main version.
125-
126- if ( dependencySource . VersionFile == null )
127- {
128- if ( ! VersionFile . TryRead ( context , settings , out var localVersionFile ) )
129- {
130- return false ;
131- }
132-
133- if ( ! localVersionFile . Dependencies . TryGetValue ( product . MainVersionDependency . Name , out var mainDependencySource ) )
134- {
135- context . Console . WriteError ( $ "Version file doesn't contain version for { product . MainVersionDependency . Name } ." ) ;
136-
137- return false ;
138- }
139-
140- var versionString = mainDependencySource . Version ;
141-
142- if ( ! NuGetVersion . TryParse ( versionString , out var mainFullVersion ) )
143- {
144- context . Console . WriteError ( $ "Could not parse the version '{ versionString } '." ) ;
145-
146- return false ;
147- }
148-
149- mainVersion = new NuGetVersion ( mainFullVersion . Major , mainFullVersion . Minor , mainFullVersion . Patch ) . ToString ( ) ;
150- }
151- else
152- {
153- var versionFile = Project . FromFile ( dependencySource . VersionFile , MSBuildLoadOptions . IgnoreImportErrors ) ;
154-
155- var propertyName = product . MainVersionDependency ! . NameWithoutDot + "MainVersion" ;
156-
157- mainVersion = versionFile . Properties . SingleOrDefault ( p => p . Name == propertyName )
158- ? . UnevaluatedValue ;
121+ var versionSpec = settings . GetVersionSpec ( configuration ) ;
159122
160- if ( string . IsNullOrEmpty ( mainVersion ) )
161- {
162- context . Console . WriteError ( $ "The file '{ dependencySource . VersionFile } ' does not contain the { propertyName } ." ) ;
123+ return TryCompute ( context , configuration , mainVersionFile , inheritedMainVersion , versionSpec , settings . UserName , out version ) ;
124+ }
163125
164- return false ;
165- }
126+ public static bool TryCompute (
127+ BuildContext context ,
128+ BuildConfiguration configuration ,
129+ MainVersionFile mainVersionFile ,
130+ string ? inheritedMainVersion ,
131+ VersionSpec versionSpec ,
132+ string ? userName ,
133+ [ NotNullWhen ( true ) ] out VersionComponents ? version )
134+ {
135+ var product = context . Product ;
136+ var configurationLowerCase = configuration . ToString ( ) . ToLowerInvariant ( ) ;
166137
167- ProjectCollection . GlobalProjectCollection . UnloadAllProjects ( ) ;
168- }
169- }
138+ version = null ;
170139
171140 if ( ! string . IsNullOrEmpty ( mainVersionFile . OverriddenPatchVersion )
172- && ! mainVersionFile . OverriddenPatchVersion . StartsWith ( mainVersion ?? mainVersionFile . MainVersion + "." , StringComparison . Ordinal ) )
141+ && ! mainVersionFile . OverriddenPatchVersion . StartsWith ( inheritedMainVersion ?? mainVersionFile . MainVersion + "." , StringComparison . Ordinal ) )
173142 {
174143 context . Console . WriteError (
175- $ "The OverriddenPatchVersion property in MainVersion.props ({ mainVersionFile . OverriddenPatchVersion } ) does not match the MainVersion property value ({ mainVersion ?? mainVersionFile . MainVersion } )." ) ;
144+ $ "The OverriddenPatchVersion property in MainVersion.props ({ mainVersionFile . OverriddenPatchVersion } ) does not match the MainVersion property value ({ inheritedMainVersion ?? mainVersionFile . MainVersion } )." ) ;
176145
177146 return false ;
178147 }
179148
180- var versionPrefix = mainVersion ?? mainVersionFile . MainVersion ;
149+ var versionPrefix = inheritedMainVersion ?? mainVersionFile . MainVersion ;
181150 string versionSuffix ;
182151 int patchNumber ;
183152
184- var versionSpec = settings . GetVersionSpec ( configuration ) ;
185- var versionSpecKind = versionSpec . Kind ;
186-
187- if ( configuration == BuildConfiguration . Public )
188- {
189- versionSpecKind = VersionKind . Public ;
190- }
191-
192- switch ( versionSpecKind )
153+ switch ( versionSpec . Kind )
193154 {
194155 case VersionKind . Local :
195156 {
@@ -223,7 +184,6 @@ public static bool TryCompute(
223184
224185 File . WriteAllText ( localVersionFile , localVersion . ToString ( CultureInfo . InvariantCulture ) ) ;
225186
226- var userName = settings . UserName ;
227187 versionSuffix = $ "local-{ userName } -{ configurationLowerCase } ";
228188
229189 patchNumber = localVersion ;
@@ -257,7 +217,82 @@ public static bool TryCompute(
257217 throw new InvalidOperationException ( ) ;
258218 }
259219
260- version = new VersionComponents ( mainVersion ?? mainVersionFile . MainVersion , versionPrefix , patchNumber , versionSuffix , configuration , product ) ;
220+ version = new VersionComponents ( inheritedMainVersion ?? mainVersionFile . MainVersion , versionPrefix , patchNumber , versionSuffix , configuration , product ) ;
221+
222+ return true ;
223+ }
224+
225+ private static bool TryInheritedReadMainVersion (
226+ BuildContext context ,
227+ DependenciesConfigurationFile dependenciesConfigurationFile ,
228+ BuildSettings settings ,
229+ out string ? mainVersion )
230+ {
231+ var product = context . Product ;
232+ var mainVersionDependencyName = product . MainVersionDependency ! . Name ;
233+
234+ // The main version is defined in a dependency. Load the import file.
235+
236+ if ( ! dependenciesConfigurationFile . Dependencies . TryGetValue ( mainVersionDependencyName , out var dependencySource ) )
237+ {
238+ context . Console . WriteError ( $ "Cannot find a dependency named '{ mainVersionDependencyName } '." ) ;
239+
240+ mainVersion = null ;
241+
242+ return false ;
243+ }
244+
245+ // Note that the version suffix is not copied from the dependency, only the main version.
246+
247+ if ( dependencySource . VersionFile == null )
248+ {
249+ if ( ! VersionFile . TryRead ( context , settings , out var localVersionFile ) )
250+ {
251+ mainVersion = null ;
252+
253+ return false ;
254+ }
255+
256+ if ( ! localVersionFile . Dependencies . TryGetValue ( product . MainVersionDependency . Name , out var mainDependencySource ) )
257+ {
258+ context . Console . WriteError ( $ "Version file doesn't contain version for { product . MainVersionDependency . Name } ." ) ;
259+
260+ mainVersion = null ;
261+
262+ return false ;
263+ }
264+
265+ var versionString = mainDependencySource . Version ;
266+
267+ if ( ! NuGetVersion . TryParse ( versionString , out var mainFullVersion ) )
268+ {
269+ context . Console . WriteError ( $ "Could not parse the version '{ versionString } '." ) ;
270+
271+ mainVersion = null ;
272+
273+ return false ;
274+ }
275+
276+ mainVersion = new NuGetVersion ( mainFullVersion . Major , mainFullVersion . Minor , mainFullVersion . Patch ) . ToString ( ) ;
277+ }
278+ else
279+ {
280+ var versionFile = Project . FromFile ( dependencySource . VersionFile , MSBuildLoadOptions . IgnoreImportErrors ) ;
281+
282+ var propertyName = product . MainVersionDependency ! . NameWithoutDot + "MainVersion" ;
283+
284+ mainVersion = versionFile . Properties . SingleOrDefault ( p => p . Name == propertyName )
285+ ? . UnevaluatedValue ;
286+
287+ if ( string . IsNullOrEmpty ( mainVersion ) )
288+ {
289+ context . Console . WriteError ( $ "The file '{ dependencySource . VersionFile } ' does not contain the { propertyName } ." ) ;
290+
291+ return false ;
292+ }
293+
294+ ProjectCollection . GlobalProjectCollection . UnloadAllProjects ( ) ;
295+ }
261296
262297 return true ;
263298 }
0 commit comments