4
4
using AterraEngine . Unions ;
5
5
using CodeOfChaos . CliArgsParser . Attributes ;
6
6
using CodeOfChaos . CliArgsParser . Contracts ;
7
- using System . Diagnostics ;
8
- using System . Xml ;
7
+ using CodeOfChaos . CliArgsParser . Library . Shared ;
9
8
using System . Xml . Linq ;
10
9
11
10
namespace CodeOfChaos . CliArgsParser . Library . CommandAtlases . VersionBump ;
12
11
// ---------------------------------------------------------------------------------------------------------------------
13
12
// Code
14
13
// ---------------------------------------------------------------------------------------------------------------------
15
- [ CliArgsCommand ( "version-bump" ) ]
14
+ [ CliArgsCommand ( "git- version-bump" ) ]
16
15
[ CliArgsDescription ( "Bumps the version of the projects specified in the projects argument." ) ]
17
16
public partial class VersionBumpCommand : ICommand < VersionBumpParameters > {
18
17
@@ -30,14 +29,14 @@ public async Task ExecuteAsync(VersionBumpParameters parameters) {
30
29
SemanticVersionDto updatedVersion = bumpResult . AsSuccess . Value ;
31
30
32
31
Console . WriteLine ( "Git committing ..." ) ;
33
- SuccessOrFailure gitCommitResult = await TryCreateGitCommit ( updatedVersion ) ;
32
+ SuccessOrFailure gitCommitResult = await GitHelpers . TryCreateGitCommit ( updatedVersion ) ;
34
33
if ( gitCommitResult is { IsFailure : true , AsFailure . Value : var errorCommiting } ) {
35
34
Console . WriteLine ( errorCommiting ) ;
36
35
return ;
37
36
}
38
37
39
38
Console . WriteLine ( "Git tagging ..." ) ;
40
- SuccessOrFailure gitTagResult = await TryCreateGitTag ( updatedVersion ) ;
39
+ SuccessOrFailure gitTagResult = await GitHelpers . TryCreateGitTag ( updatedVersion ) ;
41
40
if ( gitTagResult is { IsFailure : true , AsFailure . Value : var errorTagging } ) {
42
41
Console . WriteLine ( errorTagging ) ;
43
42
return ;
@@ -48,7 +47,7 @@ public async Task ExecuteAsync(VersionBumpParameters parameters) {
48
47
if ( ! parameters . PushToRemote ) return ;
49
48
50
49
Console . WriteLine ( "Pushing to origin ..." ) ;
51
- SuccessOrFailure pushResult = await TryPushToOrigin ( ) ;
50
+ SuccessOrFailure pushResult = await GitHelpers . TryPushToOrigin ( ) ;
52
51
if ( pushResult is { IsFailure : true , AsFailure . Value : var errorPushing } ) {
53
52
Console . WriteLine ( errorPushing ) ;
54
53
return ;
@@ -57,87 +56,29 @@ public async Task ExecuteAsync(VersionBumpParameters parameters) {
57
56
Console . WriteLine ( "Pushed to origin successfully." ) ;
58
57
}
59
58
60
- private static async Task < SuccessOrFailure > TryPushToOrigin ( ) {
61
- var gitTagInfo = new ProcessStartInfo ( "git" , "push origin --tags" ) {
62
- RedirectStandardOutput = true ,
63
- UseShellExecute = false ,
64
- CreateNoWindow = true
65
- } ;
66
-
67
- using Process ? gitTagProcess = Process . Start ( gitTagInfo ) ;
68
- Console . WriteLine ( await gitTagProcess ? . StandardOutput . ReadToEndAsync ( ) ! ) ;
69
- await gitTagProcess . WaitForExitAsync ( ) ;
70
-
71
- if ( gitTagProcess . ExitCode != 0 ) return "Push to origin failed" ;
72
-
73
- return new Success ( ) ;
74
- }
75
-
76
- private static async Task < SuccessOrFailure > TryCreateGitTag ( SemanticVersionDto updatedVersion ) {
77
- var gitTagInfo = new ProcessStartInfo ( "git" , "tag v" + updatedVersion ) {
78
- RedirectStandardOutput = true ,
79
- UseShellExecute = false ,
80
- CreateNoWindow = true
81
- } ;
82
-
83
- using Process ? gitTagProcess = Process . Start ( gitTagInfo ) ;
84
- Console . WriteLine ( await gitTagProcess ? . StandardOutput . ReadToEndAsync ( ) ! ) ;
85
- await gitTagProcess . WaitForExitAsync ( ) ;
86
-
87
- if ( gitTagProcess . ExitCode != 0 ) return "Git Tagging failed" ;
88
-
89
- return new Success ( ) ;
90
- }
91
-
92
- private static async Task < SuccessOrFailure > TryCreateGitCommit ( SemanticVersionDto updatedVersion ) {
93
- var gitCommitInfo = new ProcessStartInfo ( "git" , $ "commit -am \" VersionBump : v{ updatedVersion } \" ") {
94
- RedirectStandardOutput = true ,
95
- UseShellExecute = false ,
96
- CreateNoWindow = true
97
- } ;
98
-
99
- using Process ? gitCommitProcess = Process . Start ( gitCommitInfo ) ;
100
- Console . WriteLine ( await gitCommitProcess ? . StandardOutput . ReadToEndAsync ( ) ! ) ;
101
- await gitCommitProcess . WaitForExitAsync ( ) ;
102
-
103
- if ( gitCommitProcess . ExitCode != 0 ) return "Git Commit failed" ;
104
-
105
- return new Success ( ) ;
106
- }
107
-
108
59
109
60
private static async Task < SuccessOrFailure < SemanticVersionDto > > BumpVersion ( VersionBumpParameters args ) {
110
- string [ ] projectFiles = args . GetProjects ( ) ;
61
+ string [ ] projectFiles = CsProjHelpers . AsProjectPaths ( args . Root , args . SourceFolder , args . GetProjects ( ) ) ;
111
62
if ( projectFiles . Length == 0 ) {
112
63
return new Failure < string > ( "No projects specified" ) ;
113
64
}
114
65
115
66
VersionSection sectionToBump = args . Section ;
116
67
SemanticVersionDto ? versionDto = null ;
117
68
118
- foreach ( string projectFile in projectFiles ) {
119
- string path = Path . Combine ( args . Root , args . SourceFolder , projectFile , projectFile + ".csproj" ) ;
120
- if ( ! File . Exists ( path ) ) {
121
- return new Failure < string > ( $ "Could not find project file { projectFile } ") ;
122
- }
123
-
124
- XDocument document ;
125
- await using ( var stream = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . Read , 4096 , true ) ) {
126
- document = await XDocument . LoadAsync ( stream , LoadOptions . PreserveWhitespace , CancellationToken . None ) ;
127
- }
128
-
69
+ await foreach ( XDocument document in CsProjHelpers . GetProjectFiles ( projectFiles ) ) {
129
70
XElement ? versionElement = document
130
71
. Descendants ( "PropertyGroup" )
131
72
. Elements ( "Version" )
132
73
. FirstOrDefault ( ) ;
133
74
134
75
if ( versionElement == null ) {
135
- return new Failure < string > ( $ "File { projectFile } did not contain a version element") ;
76
+ return new Failure < string > ( $ "File did not contain a version element") ;
136
77
}
137
78
138
79
if ( versionDto is null ) {
139
80
if ( ! SemanticVersionDto . TryParse ( versionElement . Value , out SemanticVersionDto ? dto ) ) {
140
- return new Failure < string > ( $ "File { projectFile } contained an invalid version element: { versionElement . Value } ") ;
81
+ return new Failure < string > ( $ "File contained an invalid version element: { versionElement . Value } ") ;
141
82
}
142
83
143
84
dto . BumpVersion ( sectionToBump ) ;
@@ -146,20 +87,7 @@ private static async Task<SuccessOrFailure<SemanticVersionDto>> BumpVersion(Vers
146
87
}
147
88
148
89
versionElement . Value = versionDto . ToString ( ) ;
149
-
150
- var settings = new XmlWriterSettings {
151
- Indent = true ,
152
- IndentChars = " " ,
153
- Async = true ,
154
- OmitXmlDeclaration = true
155
- } ;
156
-
157
- await using ( var stream = new FileStream ( path , FileMode . Create , FileAccess . Write , FileShare . None , 4096 , true ) ) {
158
- await using var writer = XmlWriter . Create ( stream , settings ) ;
159
- document . Save ( writer ) ;
160
- }
161
-
162
- Console . WriteLine ( $ "Updated { projectFile } version to { versionElement . Value } ") ;
90
+ Console . WriteLine ( $ "Updated version to { versionElement . Value } ") ;
163
91
}
164
92
165
93
return versionDto is not null
0 commit comments