Skip to content

Commit a84abf6

Browse files
committed
Bump uses AutoUpdatedVersions.props.
1 parent b581ffa commit a84abf6

File tree

10 files changed

+160
-147
lines changed

10 files changed

+160
-147
lines changed

src/PostSharp.Engineering.BuildTools/Build/Bumping/BumpCommand.cs

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private static bool Execute( BuildContext context, BumpSettings settings )
2828

2929
return true;
3030
}
31-
31+
3232
console.WriteHeading( $"Bumping the '{product.ProductName}' version" );
3333

3434
var developmentBranch = product.DependencyDefinition.Branch;
@@ -109,22 +109,17 @@ private static bool Execute( BuildContext context, BumpSettings settings )
109109
return true;
110110
}
111111

112-
// Read the current version of the dependencies directly from source control.
113-
if ( !TryReadDependencyVersionsFromSourceRepos( context, true, out var dependencyVersions ) )
112+
if ( !AutoUpdatedVersionsFile.TryRead( context, out _, out var currentVersion ) )
114113
{
115114
return false;
116115
}
117116

118-
// Comparing the actual version of dependencies with the versions stored during the last bump.
119-
var newBumpInfoFile =
120-
new BumpInfoFile( dependencyVersions );
121-
122-
var bumpInfoFilePath = Path.Combine(
123-
context.RepoDirectory,
124-
product.BumpInfoFilePath );
125-
126-
var oldBumpFileContent = File.Exists( bumpInfoFilePath ) ? File.ReadAllText( bumpInfoFilePath ) : "";
127-
var hasChangesInDependencies = newBumpInfoFile.ToString() != oldBumpFileContent;
117+
// Doing a dry run of AutoUpdatedVersionsFile both gets the versions of all dependencies and gets the current version.
118+
// Do not write the AutoUpdatedVersions.props file yet - we will do it after we set our own version.
119+
if ( !AutoUpdatedVersionsFile.TryWrite( context, true, out var hasChangesInDependencies, out _, out _, out var currentOrInheritedVersion ) )
120+
{
121+
return false;
122+
}
128123

129124
if ( !hasChangesInDependencies && !hasChangesSinceLastDeployment )
130125
{
@@ -133,21 +128,12 @@ private static bool Execute( BuildContext context, BumpSettings settings )
133128
return true;
134129
}
135130

136-
// If there is a change in dependencies versions, we update BumpInfo.txt with changes.
137-
if ( hasChangesInDependencies )
138-
{
139-
console.WriteMessage(
140-
$"'{bumpInfoFilePath}' contents are outdated. Overwriting its old content '{oldBumpFileContent}' with new content '{newBumpInfoFile}'." );
141-
142-
File.WriteAllText( bumpInfoFilePath, newBumpInfoFile.ToString() );
143-
}
144-
145131
Version? oldVersion;
146-
Version? newVersion;
147132

148133
if ( product.MainVersionDependency == null )
149134
{
150-
if ( !product.BumpStrategy.TryBumpVersion( product, context, out oldVersion, out newVersion ) )
135+
// This updates MainVersion.props.
136+
if ( !product.BumpStrategy.TryBumpVersion( product, context, out oldVersion, out _ ) )
151137
{
152138
return false;
153139
}
@@ -171,13 +157,17 @@ private static bool Execute( BuildContext context, BumpSettings settings )
171157
return false;
172158
}
173159

174-
var oldBumpInfo = BumpInfoFile.Parse( oldBumpFileContent );
175-
newVersion = dependencyVersions[product.MainVersionDependency.Name];
176-
oldVersion = oldBumpInfo?.Dependencies[product.MainVersionDependency.Name];
160+
oldVersion = new Version( currentVersion );
161+
}
162+
163+
// Now save AutoUpdatedVersions.props.
164+
if ( !AutoUpdatedVersionsFile.TryWrite( context, false, out _, out _, out _, out var newVersion ) )
165+
{
166+
return false;
177167
}
178168

179169
// Commit the version bump.
180-
if ( !GitIntegrationHelper.TryCommitVersionBump( context, oldVersion, newVersion ) )
170+
if ( !GitIntegrationHelper.TryCommitVersionBump( context, oldVersion, new Version( newVersion ) ) )
181171
{
182172
return false;
183173
}
@@ -193,42 +183,4 @@ private static bool Execute( BuildContext context, BumpSettings settings )
193183

194184
return true;
195185
}
196-
197-
private static bool TryReadDependencyVersionsFromSourceRepos(
198-
BuildContext context,
199-
bool snapshotDependenciesOnly,
200-
[NotNullWhen( true )] out Dictionary<string, Version>? dependencyVersions )
201-
{
202-
var product = context.Product;
203-
dependencyVersions = new Dictionary<string, Version>();
204-
205-
var allDependencies =
206-
product.ParametrizedDependencies.Select( x => x.Definition )
207-
.Union( product.SourceDependencies )
208-
.Union( product.MainVersionDependency == null ? [] : [product.MainVersionDependency] );
209-
210-
foreach ( var dependency in allDependencies )
211-
{
212-
if ( snapshotDependenciesOnly && !dependency.GenerateSnapshotDependency )
213-
{
214-
continue;
215-
}
216-
217-
var mainVersionFile = $"{dependency.EngineeringDirectory}/MainVersion.props";
218-
219-
if ( !dependency.VcsRepository.TryDownloadTextFile( context.Console, dependency.Branch, mainVersionFile, out var mainVersionContent ) )
220-
{
221-
return false;
222-
}
223-
224-
if ( !MainVersionFile.TryParse( context, mainVersionContent, out var mainVersionFileInfo ) )
225-
{
226-
return false;
227-
}
228-
229-
dependencyVersions.Add( dependency.Name, Version.Parse( mainVersionFileInfo.MainVersion ) );
230-
}
231-
232-
return true;
233-
}
234186
}

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

Lines changed: 97 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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

33
using PostSharp.Engineering.BuildTools.Build.Model;
4+
using PostSharp.Engineering.BuildTools.Dependencies.Model;
45
using PostSharp.Engineering.BuildTools.Utilities;
6+
using System.Diagnostics.CodeAnalysis;
57
using System.IO;
68
using System.Linq;
79
using 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
}

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)