-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBumpCommand.cs
More file actions
212 lines (175 loc) · 7.61 KB
/
BumpCommand.cs
File metadata and controls
212 lines (175 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
using JetBrains.Annotations;
using PostSharp.Engineering.BuildTools.Build.Files;
using PostSharp.Engineering.BuildTools.Build.Helpers;
using PostSharp.Engineering.BuildTools.Utilities;
using System;
namespace PostSharp.Engineering.BuildTools.Build.Bumping;
[UsedImplicitly]
internal class BumpCommand : BaseCommand<BumpSettings>
{
protected override bool ExecuteCore( BuildContext context, BumpSettings settings ) => Execute( context, settings );
private static bool Execute( BuildContext context, BumpSettings settings )
{
var product = context.Product;
var console = context.Console;
console.WriteHeading( $"Bumping the '{product.ProductName}' version" );
var developmentBranch = product.DependencyDefinition.Branch;
if ( context.Branch != developmentBranch && !settings.Force )
{
console.WriteError( $"The version bump can only be executed on the development branch ('{developmentBranch}'), unless --force is used." );
return false;
}
if ( !GitHelper.TryConfigureCredentials( context ) )
{
console.WriteError( "Cannot configure git credentials." );
return false;
}
// It is forbidden to push to the release branch, but it occasionally happens.
// We need to make sure that there are no pending changes in the release branch to be merged to the development branch.
// Failing to do so could result in missing published changes, and it could also break the version bump.
var releaseBranch = product.DependencyDefinition.ReleaseBranch;
if ( releaseBranch == null )
{
console.WriteMessage( "Skipping check for pending changes from the release branch, as the release branch is not set for this product." );
}
else
{
console.WriteMessage( $"Checking for pending changes from the release branch ('{releaseBranch}')." );
if ( !GitHelper.TryCheckoutAndPull( context, releaseBranch ) )
{
return false;
}
if ( !GitHelper.TryCheckoutAndPull( context, context.Branch ) )
{
return false;
}
if ( !GitHelper.TryGetCommitsCount( context, "HEAD", releaseBranch, out var count ) )
{
return false;
}
if ( count > 0 )
{
console.WriteError( $"There are pending changes from the '{releaseBranch}' branch." );
console.WriteError( $"Check the relevancy of the changes and merge the '{releaseBranch}' branch to the '{developmentBranch}'." );
console.WriteError( "Failing to do so could result in invalid version number of this product." );
return false;
}
}
if ( !MainVersionFile.TryRead( context, out var currentMainVersionFile ) )
{
return false;
}
// If the version has already been dumped since the last deployment, there is nothing to do.
if ( !GitIntegrationHelper.TryAnalyzeGitHistory(
context,
currentMainVersionFile,
out var hasBumpSinceLastDeployment,
out var hasChangesSinceLastDeployment,
out _ ) )
{
return false;
}
// Doing a dry run of AutoUpdatedVersionsFile both gets the versions of all dependencies and gets the current version.
// Do not write the AutoUpdatedVersions.props file yet - we will do it after we set our own version.
if ( !AutoUpdatedVersionsFile.TryWrite(
context,
true,
out var hasChangesInDependencies,
out var hasChangesInAutoUpdatedVersionsFile,
out _,
out var currentVersion ) )
{
return false;
}
// If the currentVersion differs from MainVersion, update it now, irrespective of bumping.
if ( currentVersion != currentMainVersionFile.MainVersion )
{
if ( !currentMainVersionFile.TryWrite( context, currentVersion, null, out currentMainVersionFile ) )
{
return false;
}
if ( !GitIntegrationHelper.TryCommitMainVersion( context ) )
{
return false;
}
}
if ( hasBumpSinceLastDeployment && !settings.OverridePreviousBump )
{
console.WriteWarning( "Version has already been bumped since the last deployment." );
// Even though the version has been bumped, we still need to verify that AutoUpdatedVersions.props is correct.
if ( hasChangesInAutoUpdatedVersionsFile )
{
console.WriteWarning( "AutoUpdatedVersions.props needs to be updated. Updating now." );
if ( !AutoUpdatedVersionsFile.TryWrite( context, false, out _, out _, out _, out _ ) )
{
return false;
}
// Commit the changes.
if ( !GitIntegrationHelper.TryCommitAutoUpdatedVersions( context ) )
{
return false;
}
// If we are running in TeamCity, push.
if ( context.IsContinuousIntegrationBuild )
{
if ( !GitHelper.TryPush( context ) )
{
return false;
}
}
}
}
else if ( !hasChangesInDependencies && !hasChangesSinceLastDeployment )
{
console.WriteWarning( $"There are no changes since the last deployment." );
}
else
{
Version? oldVersion;
if ( product.MainVersionDependency == null )
{
// This updates MainVersion.props.
if ( !product.BumpStrategy.TryBumpVersion( product, context, out oldVersion, out _ ) )
{
return false;
}
}
else
{
if ( hasChangesSinceLastDeployment && !hasChangesInDependencies )
{
const string message =
"There are changes in the current repo but no changes in dependencies. However, the current repo does not have its own versioning.";
if ( settings.Force )
{
console.WriteImportantMessage( $"{message} This is being ignored using --force." );
return true;
}
console.WriteError( $"{message} Do a fake change in a parent repo or use --force." );
return false;
}
oldVersion = new Version( currentVersion );
}
// Now save AutoUpdatedVersions.props.
if ( !AutoUpdatedVersionsFile.TryWrite( context, false, out _, out _, out _, out var newVersion ) )
{
return false;
}
// Commit the version bump.
if ( !GitIntegrationHelper.TryCommitVersionFilesWithBumpMessage( context, oldVersion, new Version( newVersion ) ) )
{
return false;
}
}
// If we are running in TeamCity, push.
if ( context.IsContinuousIntegrationBuild )
{
if ( !GitHelper.TryPush( context ) )
{
return false;
}
}
return true;
}
}