@@ -14,6 +14,167 @@ var exec = require("child_process").exec;
14
14
15
15
const mappings = require ( "./api-specs.json" ) ;
16
16
const defaultSpecRoot = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main" ;
17
+ const repoRoot = path . resolve ( __dirname , "../.." ) ;
18
+
19
+ // Get current version from eng/versioning/version_client.txt normal entry
20
+ function getCurrentVersionForArtifact ( artifactId ) {
21
+ try {
22
+ const versionClientPath = path . resolve ( repoRoot , "eng/versioning/version_client.txt" ) ;
23
+ if ( ! fs . existsSync ( versionClientPath ) ) {
24
+ return undefined ;
25
+ }
26
+ const content = fs . readFileSync ( versionClientPath , "utf8" ) ;
27
+ const lines = content . split ( / \r ? \n / ) ;
28
+ const normalPrefix = `com.azure.resourcemanager:${ artifactId } ;` ;
29
+ const normalLine = lines . find ( ( l ) => l . startsWith ( normalPrefix ) ) ;
30
+ if ( ! normalLine ) return undefined ;
31
+ const parts = normalLine . split ( ";" ) ;
32
+ return parts . length >= 3 ? parts [ 2 ] . trim ( ) : undefined ;
33
+ } catch ( _ ) {
34
+ return undefined ;
35
+ }
36
+ }
37
+
38
+ // Determine whether project has been split to sdk/{service}
39
+ function isSplitProject ( project ) {
40
+ const dir = mappings [ project ] && mappings [ project ] . dir ;
41
+ if ( ! dir ) return false ;
42
+ // Split projects use ../{service}/azure-resourcemanager-xxx
43
+ // Exclude ../resourcemanagerhybrid
44
+ return / ^ \. \. \/ (? ! r e s o u r c e m a n a g e r h y b r i d \/ ) .+ / . test ( dir ) ;
45
+ }
46
+
47
+ // ensure unreleased entry exists in eng/versioning/version_client.txt
48
+ function ensureUnreleasedVersionClientEntry ( artifactId ) {
49
+ try {
50
+ const versionClientPath = path . resolve ( repoRoot , "eng/versioning/version_client.txt" ) ;
51
+ if ( ! fs . existsSync ( versionClientPath ) ) {
52
+ console . warn ( `version_client.txt not found at ${ versionClientPath } ` ) ;
53
+ return ;
54
+ }
55
+ const content = fs . readFileSync ( versionClientPath , "utf8" ) ;
56
+ const lines = content . split ( / \r ? \n / ) ;
57
+
58
+ const unreleasedPrefix = `unreleased_com.azure.resourcemanager:${ artifactId } ;` ;
59
+ const normalPrefix = `com.azure.resourcemanager:${ artifactId } ;` ;
60
+
61
+ // If unreleased entry already exists, nothing to do
62
+ if ( lines . some ( ( l ) => l . startsWith ( unreleasedPrefix ) ) ) {
63
+ return ;
64
+ }
65
+
66
+ // Find normal line to get current version (the 3rd semicolon-delimited token)
67
+ const normalLine = lines . find ( ( l ) => l . startsWith ( normalPrefix ) ) ;
68
+ if ( ! normalLine ) {
69
+ console . warn ( `Normal version entry for ${ artifactId } not found in version_client.txt` ) ;
70
+ return ;
71
+ }
72
+ const parts = normalLine . split ( ";" ) ;
73
+ // Expected: group:artifact;released;current
74
+ let currentVersion = parts . length >= 3 ? parts [ 2 ] . trim ( ) : "" ;
75
+ if ( ! currentVersion ) {
76
+ console . warn ( `Unable to parse current version for ${ artifactId } from: ${ normalLine } ` ) ;
77
+ return ;
78
+ }
79
+
80
+ const newLine = `${ unreleasedPrefix } ${ currentVersion } ` ;
81
+
82
+ // Insert within the Unreleased dependencies section using the blank line after the last unreleased_ entry
83
+ const unreleasedHeaderIndex = lines . findIndex ( ( l ) => l . startsWith ( "# Unreleased dependencies" ) ) ;
84
+ if ( unreleasedHeaderIndex !== - 1 ) {
85
+ // Scan for the unreleased block and blank line after it
86
+ let lastUnreleasedIdx = - 1 ;
87
+ let endOfSectionIdx = - 1 ;
88
+ let seenUnreleased = false ;
89
+ for ( let i = unreleasedHeaderIndex + 1 ; i < lines . length ; i ++ ) {
90
+ const line = lines [ i ] ;
91
+ if ( line . startsWith ( "unreleased_" ) ) {
92
+ seenUnreleased = true ;
93
+ lastUnreleasedIdx = i ;
94
+ continue ;
95
+ }
96
+ if ( seenUnreleased ) {
97
+ // First blank line after we started seeing unreleased entries marks end of section
98
+ if ( line . trim ( ) === "" ) {
99
+ endOfSectionIdx = i ; // insert before this blank line
100
+ break ;
101
+ }
102
+ // If not blank, but a new header starts, still treat as end of section
103
+ if ( line . startsWith ( "# " ) ) {
104
+ endOfSectionIdx = i ;
105
+ break ;
106
+ }
107
+ }
108
+ }
109
+
110
+ let insertIndex ;
111
+ if ( lastUnreleasedIdx !== - 1 ) {
112
+ // There are existing unreleased entries
113
+ insertIndex = endOfSectionIdx !== - 1 ? endOfSectionIdx : lastUnreleasedIdx + 1 ;
114
+ } else {
115
+ // No existing entries, insert after header comments and the following blank line if present
116
+ insertIndex = unreleasedHeaderIndex + 1 ;
117
+ // Skip comment lines immediately following the header
118
+ while ( insertIndex < lines . length && lines [ insertIndex ] . startsWith ( "#" ) ) insertIndex ++ ;
119
+ // If next is a blank line, insert after it to keep formatting clean
120
+ if ( insertIndex < lines . length && lines [ insertIndex ] . trim ( ) === "" ) insertIndex ++ ;
121
+ }
122
+
123
+ lines . splice ( insertIndex , 0 , newLine ) ;
124
+ const updated = lines . join ( "\n" ) ;
125
+ fs . writeFileSync ( versionClientPath , updated . endsWith ( "\n" ) ? updated : updated + "\n" , "utf8" ) ;
126
+ } else {
127
+ // Fallback: append at end of file
128
+ const updated = content . endsWith ( "\n" ) ? content + newLine + "\n" : content + "\n" + newLine + "\n" ;
129
+ fs . writeFileSync ( versionClientPath , updated , "utf8" ) ;
130
+ }
131
+ console . log ( `Added unreleased entry to version_client.txt: ${ newLine } ` ) ;
132
+ } catch ( e ) {
133
+ console . warn ( `Failed to update version_client.txt for ${ artifactId } : ${ e && e . message ? e . message : e } ` ) ;
134
+ }
135
+ }
136
+
137
+ // Update sdk/resourcemanager/azure-resourcemanager/pom.xml to refer to the package by unreleased version
138
+ function updateAggregatorPomUnreleased ( artifactId ) {
139
+ try {
140
+ const pomPath = path . resolve ( __dirname , "azure-resourcemanager/pom.xml" ) ;
141
+ if ( ! fs . existsSync ( pomPath ) ) {
142
+ console . warn ( `azure-resourcemanager/pom.xml not found at ${ pomPath } ` ) ;
143
+ return ;
144
+ }
145
+ let pom = fs . readFileSync ( pomPath , "utf8" ) ;
146
+
147
+ // Only update if this artifact is referenced
148
+ const artifactRef = `<artifactId>${ artifactId } </artifactId>` ;
149
+ if ( pom . indexOf ( artifactRef ) === - 1 ) {
150
+ return ; // dependency not present in aggregator pom
151
+ }
152
+
153
+ // Update the x-version-update comment target to unreleased_ for this artifact
154
+ const currentVersion = getCurrentVersionForArtifact ( artifactId ) ;
155
+ if ( currentVersion ) {
156
+ const groupId = "com.azure.resourcemanager" ;
157
+ const project = `${ groupId } :${ artifactId } ` ;
158
+ // Match either existing or already-unreleased marker in the version update comment
159
+ const dependencyPattern = new RegExp (
160
+ `(<groupId>com.azure.resourcemanager</groupId>\\s*<artifactId>${ artifactId } </artifactId>\\s*<version>)[^<]+(</version>\\s*<!-- \\{x-version-update;)(?:unreleased_)?${ project } (;dependency\\} -->)` ,
161
+ 'gs'
162
+ ) ;
163
+
164
+ newPom = pom . replace (
165
+ dependencyPattern ,
166
+ ( _ , g1 , g2 , g3 ) => `${ g1 } ${ currentVersion } ${ g2 } unreleased_${ project } ${ g3 } `
167
+ ) ;
168
+ }
169
+
170
+ if ( newPom !== pom ) {
171
+ fs . writeFileSync ( pomPath , newPom , "utf8" ) ;
172
+ console . log ( `Updated azure-resourcemanager/pom.xml to use unreleased reference for ${ artifactId } with version ${ currentVersion || "(unchanged)" } ` ) ;
173
+ }
174
+ } catch ( e ) {
175
+ console . warn ( `Failed to update azure-resourcemanager/pom.xml for ${ artifactId } : ${ e && e . message ? e . message : e } ` ) ;
176
+ }
177
+ }
17
178
18
179
async function defaultInfo ( ) {
19
180
console . log (
@@ -170,6 +331,13 @@ function codegen(project, cb) {
170
331
copyFolderRecursiveSync ( generatedSamplesSource , generatedSamplesTarget ) ;
171
332
deleteFolderRecursive ( generatedSamplesSource ) ;
172
333
334
+ // If already split, ensure version_client and pom updates
335
+ if ( isSplitProject ( project ) ) {
336
+ const artifactId = path . basename ( path . resolve ( mappings [ project ] . dir ) ) ; // e.g., azure-resourcemanager-search
337
+ ensureUnreleasedVersionClientEntry ( artifactId ) ;
338
+ updateAggregatorPomUnreleased ( artifactId ) ;
339
+ }
340
+
173
341
return autorest_result ;
174
342
}
175
343
0 commit comments