Skip to content

Commit b8d20bf

Browse files
committed
Revert "Change checkout directories and encapsulated artifact rules from string to object."
This reverts commit 90a865c. Finished AutoUpdatedVersionsFile.
1 parent f836592 commit b8d20bf

File tree

4 files changed

+193
-82
lines changed

4 files changed

+193
-82
lines changed

src/PostSharp.Engineering.BuildTools/Build/Files/AutoUpdatedVersionsFile.cs

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
22

3-
using PostSharp.Engineering.BuildTools.Build.Publishing;
4-
using PostSharp.Engineering.BuildTools.Dependencies.Model;
3+
using PostSharp.Engineering.BuildTools.Build.Model;
54
using PostSharp.Engineering.BuildTools.Utilities;
65
using System.IO;
76
using System.Linq;
8-
using System.Text;
9-
using System.Xml;
107
using System.Xml.Linq;
118

129
namespace PostSharp.Engineering.BuildTools.Build.Files;
@@ -15,11 +12,11 @@ internal static class AutoUpdatedVersionsFile
1512
{
1613
public const string FileName = "AutoUpdatedVersions.props";
1714

18-
public static bool TryWrite( BuildContext context, bool dry, out bool dependenciesUpdated )
15+
public static bool TryWrite( BuildContext context, bool dry, out bool hasChanges )
1916
{
2017
context.Console.WriteImportantMessage( $"Checking versions of auto-updated dependencies." );
2118

22-
dependenciesUpdated = false;
19+
hasChanges = false;
2320

2421
var autoUpdatedDependencies = context.Product.DependencyDefinition.GetAllDependencies( BuildConfiguration.Public )
2522
.Where( d => d.Definition.AutoUpdateVersion )
@@ -32,13 +29,14 @@ public static bool TryWrite( BuildContext context, bool dry, out bool dependenci
3229
return true;
3330
}
3431

32+
// Load XML.
3533
var thisAutoUpdatedVersionsFilePath = context.Product.AutoUpdatedVersionsFilePath;
36-
var thisAutoUpdatedVersionsFileName = Path.GetFileName( thisAutoUpdatedVersionsFilePath );
3734
var thisAutoUpdatedVersionsDocument = XDocument.Load( thisAutoUpdatedVersionsFilePath, LoadOptions.PreserveWhitespace );
38-
3935
var thisAutoUpdatedVersionsPropertyGroupElement = thisAutoUpdatedVersionsDocument.Root!.Element( "PropertyGroup" )!;
4036

37+
// Update dependency versions.
4138
var errors = 0;
39+
string? inheritedMainVersion = null;
4240

4341
foreach ( var dependencyConfiguration in autoUpdatedDependencies )
4442
{
@@ -72,7 +70,7 @@ public static bool TryWrite( BuildContext context, bool dry, out bool dependenci
7270

7371
if ( string.IsNullOrEmpty( dependencyReleasedVersion ) )
7472
{
75-
context.Console.WriteError( $"Cannot find the '{dependencyReleasedVersion}' in '{theirAutoUpdatedVersionsFilePath}'." );
73+
context.Console.WriteError( $"The '{releasedVersionPropertyName}' property in '{theirAutoUpdatedVersionsFilePath}' is not defined." );
7674
errors++;
7775

7876
continue;
@@ -98,24 +96,98 @@ public static bool TryWrite( BuildContext context, bool dry, out bool dependenci
9896
}
9997

10098
versionElement.Value = dependencyReleasedVersion;
101-
dependenciesUpdated = true;
99+
hasChanges = true;
102100

103101
context.Console.WriteMessage( $"Setting version dependency '{dependency}' from '{oldVersionValue}' to '{dependencyReleasedVersion}'." );
102+
103+
// Getting the inherited main version.
104+
if ( context.Product.MainVersionDependency == dependency )
105+
{
106+
var releasedMainVersionPropertyName = $"{dependency.NameWithoutDot}ReleaseMainVersion";
107+
108+
var releasedMainVersionPropertyValue = theirAutoUpdatedVersionsDocument.Root?.Element( "Project" )
109+
?.Element( "PropertyGroup" )
110+
?.Element( releasedVersionPropertyName )
111+
?.Value;
112+
113+
if ( string.IsNullOrEmpty( releasedMainVersionPropertyValue ) )
114+
{
115+
context.Console.WriteError( $"The '{releasedMainVersionPropertyName}' property in '{theirAutoUpdatedVersionsFilePath}' is not defined." );
116+
errors++;
117+
118+
continue;
119+
}
120+
121+
inheritedMainVersion = releasedMainVersionPropertyValue;
122+
}
104123
}
105124

125+
// Stop here if errors.
106126
if ( errors > 0 )
107127
{
108128
return false;
109129
}
110130

111-
if ( dependenciesUpdated )
131+
// Get the version of this component.
132+
if ( !MainVersionFile.TryRead( context, out var mainVersionFile ) )
133+
{
134+
return false;
135+
}
136+
137+
if ( !VersionComponents.TryCompute(
138+
context,
139+
BuildConfiguration.Public,
140+
mainVersionFile,
141+
inheritedMainVersion,
142+
new VersionSpec( VersionKind.Public ),
143+
null,
144+
out var versionComponents ) )
145+
{
146+
return false;
147+
}
148+
149+
// Update our own version.
150+
var thisVersionElement = thisAutoUpdatedVersionsPropertyGroupElement.Element( $"{context.Product.ProductNameWithoutDot}ReleaseVersion" );
151+
var thisMainVersionElement = thisAutoUpdatedVersionsPropertyGroupElement.Element( $"{context.Product.ProductNameWithoutDot}ReleaseMainVersion" );
152+
153+
if ( thisVersionElement == null )
112154
{
113-
context.Console.WriteImportantMessage( $"{(dry ? "Dry run: " : "")}Writing updated '{thisAutoUpdatedVersionsFileName}'." );
155+
thisVersionElement = new XElement( $"{context.Product.ProductNameWithoutDot}ReleaseVersion" );
156+
thisAutoUpdatedVersionsPropertyGroupElement.Add( thisVersionElement );
157+
}
114158

159+
if ( thisVersionElement.Value != versionComponents.PackageVersion )
160+
{
161+
hasChanges = true;
162+
context.Console.WriteMessage( $"Updating '{thisVersionElement.Name}' to '{versionComponents.PackageVersion}'." );
163+
thisVersionElement.Value = versionComponents.PackageVersion;
164+
}
165+
166+
if ( thisMainVersionElement == null )
167+
{
168+
thisMainVersionElement = new XElement( $"{context.Product.ProductNameWithoutDot}ReleaseVersion" );
169+
thisAutoUpdatedVersionsPropertyGroupElement.Add( thisVersionElement );
170+
}
171+
172+
if ( thisMainVersionElement.Value != versionComponents.MainVersion )
173+
{
174+
hasChanges = true;
175+
context.Console.WriteMessage( $"Updating '{thisMainVersionElement.Name}' to '{versionComponents.MainVersion}'." );
176+
thisMainVersionElement.Value = versionComponents.MainVersion;
177+
}
178+
179+
// Write changes.
180+
if ( hasChanges )
181+
{
115182
if ( !dry )
116183
{
117184
TextFileHelper.WriteIfDifferent( thisAutoUpdatedVersionsFilePath, thisAutoUpdatedVersionsDocument.ToString(), context );
118185
}
186+
else
187+
{
188+
context.Console.WriteMessage( $"New content for '{thisAutoUpdatedVersionsFilePath}':" );
189+
context.Console.WriteMessage( thisAutoUpdatedVersionsDocument.ToString() );
190+
}
119191
}
120192

121193
return true;

src/PostSharp.Engineering.BuildTools/Build/Model/VersionComponents.cs

Lines changed: 104 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/TeamCity/BuildSteps/BuildStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected BuildStep( DockerSpec? dockerSpec )
2525

2626
public abstract string GenerateTeamCityCode();
2727

28-
public virtual void InsertPrerequisites( IReadOnlyList<BuildStep> previousSteps, Action<BuildStep> addStep )
28+
public void InsertPrerequisites( IReadOnlyList<BuildStep> previousSteps, Action<BuildStep> addStep )
2929
{
3030
if ( this._dockerSpec != null )
3131
{

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/TeamCity/BuildSteps/EngineeringPrepareImageBuildStep.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
22

33
using PostSharp.Engineering.BuildTools.Docker;
4+
using System;
45

56
namespace PostSharp.Engineering.BuildTools.ContinuousIntegration.TeamCity.BuildSteps;
67

@@ -19,4 +20,7 @@ public EngineeringPrepareImageBuildStep(
1920
{
2021
this.DockerSpec = dockerSpec;
2122
}
23+
24+
// Preparing an image from scratch (including the base image) should not take more than 60 minutes in the cloud.
25+
public override TimeSpan AdditionalTimeout => TimeSpan.FromMinutes( 60 );
2226
}

0 commit comments

Comments
 (0)