Skip to content

Commit 0149d94

Browse files
authored
Merge pull request #122 from TrueNine/dev
Sync Cargo.lock in version updates
2 parents 3b000f7 + a01cc80 commit 0149d94

File tree

21 files changed

+247
-30
lines changed

21 files changed

+247
-30
lines changed

.githooks/sync-versions.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/^name\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*version\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+
117196
function 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+
171293
function 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
}

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ members = [
1010
]
1111

1212
[workspace.package]
13-
version = "2026.10402.109"
13+
version = "2026.10402.110"
1414
edition = "2024"
1515
rust-version = "1.88"
1616
license = "AGPL-3.0-only"

cli/npm/darwin-arm64/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@truenine/memory-sync-cli-darwin-arm64",
3-
"version": "2026.10402.109",
3+
"version": "2026.10402.110",
44
"os": [
55
"darwin"
66
],

cli/npm/darwin-x64/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@truenine/memory-sync-cli-darwin-x64",
3-
"version": "2026.10402.109",
3+
"version": "2026.10402.110",
44
"os": [
55
"darwin"
66
],

cli/npm/linux-arm64-gnu/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@truenine/memory-sync-cli-linux-arm64-gnu",
3-
"version": "2026.10402.109",
3+
"version": "2026.10402.110",
44
"os": [
55
"linux"
66
],

cli/npm/linux-x64-gnu/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@truenine/memory-sync-cli-linux-x64-gnu",
3-
"version": "2026.10402.109",
3+
"version": "2026.10402.110",
44
"os": [
55
"linux"
66
],

cli/npm/win32-x64-msvc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@truenine/memory-sync-cli-win32-x64-msvc",
3-
"version": "2026.10402.109",
3+
"version": "2026.10402.110",
44
"os": [
55
"win32"
66
],

cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@truenine/memory-sync-cli",
33
"type": "module",
4-
"version": "2026.10402.109",
4+
"version": "2026.10402.110",
55
"description": "TrueNine Memory Synchronization CLI shell",
66
"author": "TrueNine",
77
"license": "AGPL-3.0-only",

doc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@truenine/memory-sync-docs",
3-
"version": "2026.10402.109",
3+
"version": "2026.10402.110",
44
"private": true,
55
"description": "Chinese-first manifesto-led documentation site for @truenine/memory-sync.",
66
"engines": {

0 commit comments

Comments
 (0)