@@ -114,6 +114,85 @@ function updateVersionLineInSection(
114114 return content
115115}
116116
117+ function extractTomlSectionValue ( content : string , sectionName : string , key : string ) : string | undefined {
118+ const lines = content . split ( / \r ? \n / )
119+ let inTargetSection = false
120+
121+ for ( const line of lines ) {
122+ const trimmed = line . trim ( )
123+
124+ if ( / ^ \[ .* \] $ / . test ( trimmed ) ) {
125+ inTargetSection = trimmed === `[${ sectionName } ]`
126+ continue
127+ }
128+
129+ if ( ! inTargetSection ) {
130+ continue
131+ }
132+
133+ const match = trimmed . match ( new RegExp ( `^${ key } \\s*=\\s*"([^"]+)"$` , 'u' ) )
134+ if ( match != null ) {
135+ return match [ 1 ]
136+ }
137+ }
138+
139+ return undefined
140+ }
141+
142+ function updateCargoLockPackageVersions (
143+ content : string ,
144+ packageNames : ReadonlySet < string > ,
145+ targetVersion : string ,
146+ ) : string {
147+ const lines = content . split ( / \r ? \n / )
148+ let currentPackageName : string | undefined
149+ let inPackageBlock = false
150+
151+ for ( let index = 0 ; index < lines . length ; index += 1 ) {
152+ const line = lines [ index ]
153+ const trimmed = line . trim ( )
154+
155+ if ( trimmed === '[[package]]' ) {
156+ inPackageBlock = true
157+ currentPackageName = undefined
158+ continue
159+ }
160+
161+ if ( / ^ \[ \[ .+ \] \] $ | ^ \[ .+ \] $ / u. test ( trimmed ) ) {
162+ inPackageBlock = false
163+ currentPackageName = undefined
164+ continue
165+ }
166+
167+ if ( ! inPackageBlock ) {
168+ continue
169+ }
170+
171+ const nameMatch = trimmed . match ( / ^ n a m e \s * = \s * " ( [ ^ " ] + ) " $ / u)
172+ if ( nameMatch != null ) {
173+ currentPackageName = nameMatch [ 1 ]
174+ continue
175+ }
176+
177+ if ( currentPackageName == null || ! packageNames . has ( currentPackageName ) ) {
178+ continue
179+ }
180+
181+ const versionMatch = line . match ( / ^ ( \s * v e r s i o n \s * = \s * " ) ( [ ^ " ] + ) ( " .* ) $ / u)
182+ if ( versionMatch == null ) {
183+ continue
184+ }
185+
186+ if ( versionMatch [ 2 ] === targetVersion ) {
187+ continue
188+ }
189+
190+ lines [ index ] = `${ versionMatch [ 1 ] } ${ targetVersion } ${ versionMatch [ 3 ] } `
191+ }
192+
193+ return lines . join ( '\n' )
194+ }
195+
117196function runGit ( rootDir : string , args : readonly string [ ] ) : string {
118197 return execFileSync ( 'git' , args , {
119198 cwd : rootDir ,
@@ -168,6 +247,49 @@ function syncCargoVersion(
168247 }
169248}
170249
250+ function collectCargoPackageNames ( cargoTomlPaths : readonly string [ ] ) : Set < string > {
251+ const packageNames = new Set < string > ( )
252+
253+ for ( const filePath of cargoTomlPaths ) {
254+ try {
255+ const content = readFileSync ( filePath , 'utf-8' )
256+ const packageName = extractTomlSectionValue ( content , 'package' , 'name' )
257+ if ( packageName != null && packageName !== '' ) {
258+ packageNames . add ( packageName )
259+ }
260+ } catch {
261+ console . log ( `⚠️ ${ filePath } not found or invalid, skipping` )
262+ }
263+ }
264+
265+ return packageNames
266+ }
267+
268+ function syncCargoLockVersion (
269+ filePath : string ,
270+ packageNames : ReadonlySet < string > ,
271+ targetVersion : string ,
272+ changedPaths : Set < string > ,
273+ ) : void {
274+ if ( packageNames . size === 0 ) {
275+ return
276+ }
277+
278+ try {
279+ const originalContent = readFileSync ( filePath , 'utf-8' )
280+ const updatedContent = updateCargoLockPackageVersions ( originalContent , packageNames , targetVersion )
281+
282+ if ( updatedContent === originalContent ) {
283+ return
284+ }
285+
286+ writeFileSync ( filePath , updatedContent , 'utf-8' )
287+ changedPaths . add ( filePath )
288+ } catch {
289+ console . log ( `⚠️ ${ filePath } not found or invalid, skipping` )
290+ }
291+ }
292+
171293function getStagedPackageVersionCandidates ( rootDir : string , rootVersion : string ) : Map < string , string [ ] > {
172294 const stagedFiles = runGit ( rootDir , [ 'diff' , '--cached' , '--name-only' , '--diff-filter=ACMR' ] )
173295 . split ( / \r ? \n / )
@@ -253,6 +375,7 @@ export function runSyncVersions(options: SyncVersionsOptions = {}): SyncVersions
253375 const rootDir = resolve ( options . rootDir ?? '.' )
254376 const rootPackagePath = resolve ( rootDir , 'package.json' )
255377 const rootCargoPath = resolve ( rootDir , 'Cargo.toml' )
378+ const cargoLockPath = resolve ( rootDir , 'Cargo.lock' )
256379 const rootPkg = readJsonFile ( rootPackagePath )
257380 const currentRootVersion = typeof rootPkg . version === 'string' ? rootPkg . version . trim ( ) : ''
258381
@@ -284,11 +407,14 @@ export function runSyncVersions(options: SyncVersionsOptions = {}): SyncVersions
284407 const cargoTomlPaths = discoverFilesByName ( rootDir , 'Cargo.toml' )
285408 . filter ( filePath => resolve ( filePath ) !== rootCargoPath )
286409 . sort ( )
410+ const cargoPackageNames = collectCargoPackageNames ( [ rootCargoPath , ...cargoTomlPaths ] )
287411
288412 for ( const filePath of cargoTomlPaths ) {
289413 syncCargoVersion ( filePath , 'package' , target . version , changedPaths )
290414 }
291415
416+ syncCargoLockVersion ( cargoLockPath , cargoPackageNames , target . version , changedPaths )
417+
292418 for ( const filePath of discoverFilesByName ( rootDir , 'tauri.conf.json' ) . sort ( ) ) {
293419 syncJsonVersion ( filePath , target . version , changedPaths )
294420 }
0 commit comments