44using AterraEngine . Unions ;
55using CodeOfChaos . CliArgsParser . Attributes ;
66using CodeOfChaos . CliArgsParser . Contracts ;
7- using System . Diagnostics ;
8- using System . Xml ;
7+ using CodeOfChaos . CliArgsParser . Library . Shared ;
98using System . Xml . Linq ;
109
1110namespace CodeOfChaos . CliArgsParser . Library . CommandAtlases . VersionBump ;
1211// ---------------------------------------------------------------------------------------------------------------------
1312// Code
1413// ---------------------------------------------------------------------------------------------------------------------
15- [ CliArgsCommand ( "version-bump" ) ]
14+ [ CliArgsCommand ( "git- version-bump" ) ]
1615[ CliArgsDescription ( "Bumps the version of the projects specified in the projects argument." ) ]
1716public partial class VersionBumpCommand : ICommand < VersionBumpParameters > {
1817
@@ -30,14 +29,14 @@ public async Task ExecuteAsync(VersionBumpParameters parameters) {
3029 SemanticVersionDto updatedVersion = bumpResult . AsSuccess . Value ;
3130
3231 Console . WriteLine ( "Git committing ..." ) ;
33- SuccessOrFailure gitCommitResult = await TryCreateGitCommit ( updatedVersion ) ;
32+ SuccessOrFailure gitCommitResult = await GitHelpers . TryCreateGitCommit ( updatedVersion ) ;
3433 if ( gitCommitResult is { IsFailure : true , AsFailure . Value : var errorCommiting } ) {
3534 Console . WriteLine ( errorCommiting ) ;
3635 return ;
3736 }
3837
3938 Console . WriteLine ( "Git tagging ..." ) ;
40- SuccessOrFailure gitTagResult = await TryCreateGitTag ( updatedVersion ) ;
39+ SuccessOrFailure gitTagResult = await GitHelpers . TryCreateGitTag ( updatedVersion ) ;
4140 if ( gitTagResult is { IsFailure : true , AsFailure . Value : var errorTagging } ) {
4241 Console . WriteLine ( errorTagging ) ;
4342 return ;
@@ -48,7 +47,7 @@ public async Task ExecuteAsync(VersionBumpParameters parameters) {
4847 if ( ! parameters . PushToRemote ) return ;
4948
5049 Console . WriteLine ( "Pushing to origin ..." ) ;
51- SuccessOrFailure pushResult = await TryPushToOrigin ( ) ;
50+ SuccessOrFailure pushResult = await GitHelpers . TryPushToOrigin ( ) ;
5251 if ( pushResult is { IsFailure : true , AsFailure . Value : var errorPushing } ) {
5352 Console . WriteLine ( errorPushing ) ;
5453 return ;
@@ -57,87 +56,29 @@ public async Task ExecuteAsync(VersionBumpParameters parameters) {
5756 Console . WriteLine ( "Pushed to origin successfully." ) ;
5857 }
5958
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-
10859
10960 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 ( ) ) ;
11162 if ( projectFiles . Length == 0 ) {
11263 return new Failure < string > ( "No projects specified" ) ;
11364 }
11465
11566 VersionSection sectionToBump = args . Section ;
11667 SemanticVersionDto ? versionDto = null ;
11768
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 ) ) {
12970 XElement ? versionElement = document
13071 . Descendants ( "PropertyGroup" )
13172 . Elements ( "Version" )
13273 . FirstOrDefault ( ) ;
13374
13475 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") ;
13677 }
13778
13879 if ( versionDto is null ) {
13980 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 } ") ;
14182 }
14283
14384 dto . BumpVersion ( sectionToBump ) ;
@@ -146,20 +87,7 @@ private static async Task<SuccessOrFailure<SemanticVersionDto>> BumpVersion(Vers
14687 }
14788
14889 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 } ") ;
16391 }
16492
16593 return versionDto is not null
0 commit comments