@@ -82,39 +82,92 @@ public async ValueTask ExecuteAsync(VersionBumpParameters parameters, Cancellati
8282 VersionSection sectionToBump = args . Section ;
8383 SemanticVersionDto ? versionDto = null ;
8484
85+ // Process csproj files
8586 await foreach ( XDocument document in CsProjHelpers . GetProjectFiles ( projectFiles ) ) {
86- XElement ? versionElement = document
87- . Descendants ( "PropertyGroup" )
88- . Elements ( "Version" )
89- . FirstOrDefault ( ) ;
90-
91- // Only needed for logging, so setting to "UNKNOWN" is okay
92- string projectName = document
93- . Descendants ( "PropertyGroup" )
94- . Elements ( "PackageId" )
95- . FirstOrDefault ( ) ?
96- . Value ?? "UNKNOWN" ;
97-
98- if ( versionElement == null ) {
99- ErrorMessages . Add ( $ "File { projectName } did not contain a version element") ;
100- continue ;
87+ string projectName = GetProjectNameFromCsproj ( document ) ;
88+
89+ SemanticVersionDto ? result = ProcessCsprojFile ( document , projectName , versionDto , sectionToBump ) ;
90+ if ( result is not null ) {
91+ versionDto = result ;
10192 }
93+ }
10294
103- if ( versionDto is null ) {
104- if ( ! SemanticVersionDto . TryParse ( versionElement . Value , out SemanticVersionDto ? dto ) ) {
105- ErrorMessages . Add ( $ "File { projectName } contained an invalid version element: { versionElement . Value } " ) ;
106- continue ;
107- }
95+ // Process nuspec files
96+ await ProcessNuspecFiles ( projectFiles , versionDto ) ;
97+
98+ return versionDto ;
99+ }
108100
109- dto . BumpVersion ( sectionToBump ) ;
101+ private static SemanticVersionDto ? ProcessCsprojFile ( XDocument document , string projectName , SemanticVersionDto ? currentVersion , VersionSection sectionToBump ) {
102+ XElement ? versionElement = document
103+ . Descendants ( "PropertyGroup" )
104+ . Elements ( "Version" )
105+ . FirstOrDefault ( ) ;
110106
111- versionDto = dto ;
107+ if ( versionElement == null ) {
108+ ErrorMessages . Add ( $ "File { projectName } did not contain a version element") ;
109+ return null ;
110+ }
111+
112+ SemanticVersionDto ? versionDto = currentVersion ;
113+
114+ if ( versionDto is null ) {
115+ if ( ! SemanticVersionDto . TryParse ( versionElement . Value , out SemanticVersionDto ? dto ) ) {
116+ ErrorMessages . Add ( $ "File { projectName } contained an invalid version element: { versionElement . Value } ") ;
117+ return null ;
112118 }
113119
114- versionElement . Value = versionDto . ToString ( ) ;
115- Console . WriteLine ( ConsoleTextStore . UpdatedVersion ( projectName , versionElement . Value ) ) ;
120+ dto . BumpVersion ( sectionToBump ) ;
121+ versionDto = dto ;
116122 }
117123
124+ versionElement . Value = versionDto . ToString ( ) ;
125+ Console . WriteLine ( ConsoleTextStore . UpdatedVersion ( projectName , versionElement . Value ) ) ;
118126 return versionDto ;
119127 }
128+
129+ private static async Task ProcessNuspecFiles ( string [ ] projectFiles , SemanticVersionDto ? versionDto ) {
130+ if ( versionDto == null ) return ;
131+
132+ foreach ( string projectFile in projectFiles ) {
133+ string nuspecFile = Path . ChangeExtension ( projectFile , ".nuspec" ) ;
134+ if ( ! File . Exists ( nuspecFile ) ) continue ;
135+
136+ try {
137+ XDocument nuspecDocument ;
138+ await using ( var stream = new FileStream ( nuspecFile , FileMode . Open , FileAccess . Read , FileShare . Read , 4096 , true ) ) {
139+ nuspecDocument = await XDocument . LoadAsync ( stream , LoadOptions . PreserveWhitespace , CancellationToken . None ) ;
140+ }
141+
142+ XNamespace ns = nuspecDocument . Root ? . GetDefaultNamespace ( ) ?? XNamespace . None ;
143+ XElement ? versionElement = nuspecDocument
144+ . Descendants ( ns + "metadata" )
145+ . Elements ( ns + "version" )
146+ . FirstOrDefault ( ) ;
147+
148+ if ( versionElement != null ) {
149+ versionElement . Value = versionDto . ToString ( ) ;
150+
151+ // Save the nuspec file
152+ await using var stream = new FileStream ( nuspecFile , FileMode . Create , FileAccess . Write ) ;
153+ await nuspecDocument . SaveAsync ( stream , SaveOptions . None , CancellationToken . None ) ;
154+
155+ string nuspecName = Path . GetFileNameWithoutExtension ( nuspecFile ) ;
156+ Console . WriteLine ( ConsoleTextStore . UpdatedVersion ( nuspecName , versionElement . Value ) ) ;
157+ }
158+ }
159+ catch ( Exception ex ) {
160+ ErrorMessages . Add ( $ "Failed to process nuspec file { nuspecFile } : { ex . Message } ") ;
161+ }
162+ }
163+ }
164+
165+ private static string GetProjectNameFromCsproj ( XDocument document ) {
166+ return document
167+ . Descendants ( "PropertyGroup" )
168+ . Elements ( "PackageId" )
169+ . FirstOrDefault ( ) ?
170+ . Value ?? "UNKNOWN" ;
171+ }
172+
120173}
0 commit comments