Repository navigation
Expand file tree
/
Copy pathversion.mjs
More file actions
188 lines (157 loc) · 6.98 KB
/
Copy pathversion.mjs
File metadata and controls
188 lines (157 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-License-Identifier: LGPL-3.0-only
import { readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { ROOT_DIR, fail, readJson } from './core.mjs'
import { validateDappnodeVersions } from './dappnode.mjs'
export const PACKAGE_FILES = [
'package.json',
'packages/interfold-config/package.json',
'packages/interfold-contracts/package.json',
'crates/wasm/package.json',
'packages/interfold-sdk/package.json',
'packages/interfold-react/package.json',
'packages/interfold-mcp/package.json',
]
// Guests are independent Cargo workspaces; all five locks record local release versions.
export const CARGO_WORKSPACES = ['.', 'templates/default', 'templates/default/guest']
export function normalizeVersion(input) {
const version = input.replace(/^v/, '')
const semanticVersion = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/
if (!semanticVersion.test(version)) {
fail(`${version} is not a semantic version`)
}
return version
}
// The release channel is the npm distribution tag and the container image alias of a release.
export function releaseChannel(version) {
return version.includes('-') ? 'dev' : 'latest'
}
// A branch test release is published by an explicit workflow dispatch from a branch. Its version is
// `<core>-<label>.g<short sha>`, for example `0.20.0-lbfv.g1a2b3c4`. The `g` keeps the identifier
// non-numeric: SemVer rejects a numeric identifier with a leading zero, and a short SHA can have one.
const TEST_RELEASE_VERSION = /^\d+\.\d+\.\d+-([a-z]+)\.g[0-9a-f]{7}$/
const TEST_RELEASE_LABEL = /^[a-z]{2,16}$/
// These names already mean something to npm, to the container aliases, or to the install tooling.
const RESERVED_TEST_LABELS = ['latest', 'dev', 'stable', 'main', 'next', 'alpha', 'beta', 'rc', 'canary']
export function validateTestReleaseLabel(label) {
if (!TEST_RELEASE_LABEL.test(label ?? '')) {
fail(`test release label "${label}" must be 2 to 16 lowercase letters`)
}
if (RESERVED_TEST_LABELS.includes(label)) {
fail(`test release label "${label}" is reserved`)
}
return label
}
/** True for a branch test release version. Install tooling and changelogs skip these versions. */
export function isTestReleaseVersion(version) {
return TEST_RELEASE_VERSION.test(version)
}
/**
* The range that a published package uses for an Interfold package that it depends on. A stable or dev
* release accepts later compatible versions. A test release needs the exact build, because a caret range
* of a prerelease also matches other test builds with the same label and later versions of the same core.
*/
export function internalDependencyRange(version) {
return isTestReleaseVersion(version) ? version : `^${version}`
}
/** The label of a branch test release version, or undefined for any other version. */
export function testReleaseLabel(version) {
const label = TEST_RELEASE_VERSION.exec(version)?.[1]
return label === undefined ? undefined : validateTestReleaseLabel(label)
}
export function testReleaseVersion(currentVersion, label, sha) {
validateTestReleaseLabel(label)
if (!/^[0-9a-f]{40}$/.test(sha ?? '')) {
fail(`${sha} is not a full commit SHA`)
}
const version = `${normalizeVersion(currentVersion).split(/[-+]/)[0]}-${label}.g${sha.slice(0, 7)}`
testReleaseLabel(version)
return version
}
export function workspaceVersion(rootDir) {
const cargoToml = readFileSync(join(rootDir, 'Cargo.toml'), 'utf8')
const workspacePackage = cargoToml.match(/\[workspace\.package\]([\s\S]*?)(?=\n\[|$)/)?.[1]
const version = workspacePackage?.match(/^version\s*=\s*"([^"]+)"/m)?.[1]
if (!version) {
fail('Cargo.toml does not contain workspace.package.version')
}
return version
}
function requireEqual(actual, expected, source) {
if (actual !== expected) {
fail(`${source} has ${actual}, expected ${expected}`)
}
}
export function validateReleaseVersion(versionInput, rootDir = ROOT_DIR) {
const version = normalizeVersion(versionInput)
requireEqual(workspaceVersion(rootDir), version, 'Cargo.toml')
for (const packageFile of PACKAGE_FILES) {
requireEqual(readJson(rootDir, packageFile).version, version, packageFile)
}
requireEqual(
readJson(rootDir, 'crates/zk/prover/versions.json').required_circuits_version,
version,
'crates/zk/prover/versions.json required_circuits_version',
)
if (version.includes('-')) {
return version
}
validateDappnodeVersions(rootDir, version)
return version
}
/** Change only the workspace package and local path dependencies, never registry pins. */
export function bumpCargoManifest(content, version) {
let section = ''
let foundVersion = false
const result = content
.split('\n')
.map((line) => {
if (/^\s*\[/.test(line)) section = line.trim()
if (section === '[workspace.package]' && /^version\s*=/.test(line)) {
foundVersion = true
return line.replace(/"[^"]*"/, `"${version}"`)
}
if (section === '[workspace.dependencies]' && /\bpath\s*=\s*"/.test(line))
return line.replace(/\bversion\s*=\s*"[^"]*"/, `version = "${version}"`)
return line
})
.join('\n')
if (!foundVersion) fail('Cargo.toml does not contain workspace.package.version')
return result
}
/** Change only the local packages that have the previous workspace version. Registry packages have a source. */
export function bumpCargoLock(content, previous, version) {
const entry = new RegExp(`^version = "${previous.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"$`, 'm')
return content
.split('\n\n')
.map((block) =>
block.startsWith('[[package]]') && !/^source = /m.test(block) ? block.replace(entry, `version = "${version}"`) : block,
)
.join('\n\n')
}
/**
* Write a branch test release version into the checkout. The files that the build and publication jobs
* read change; nothing is committed. The manifests must agree on the current version before any change.
*/
export function applyTestReleaseVersion(versionInput, rootDir = ROOT_DIR) {
const version = normalizeVersion(versionInput)
if (testReleaseLabel(version) === undefined) {
fail(`${version} is not a branch test release version`)
}
const previous = workspaceVersion(rootDir)
validateReleaseVersion(previous, rootDir)
writeFileSync(join(rootDir, 'Cargo.toml'), bumpCargoManifest(readFileSync(join(rootDir, 'Cargo.toml'), 'utf8'), version))
const lockPath = join(rootDir, 'Cargo.lock')
writeFileSync(lockPath, bumpCargoLock(readFileSync(lockPath, 'utf8'), previous, version))
for (const packageFile of PACKAGE_FILES) {
writeFileSync(join(rootDir, packageFile), `${JSON.stringify({ ...readJson(rootDir, packageFile), version }, null, 2)}\n`)
}
const versionsPath = 'crates/zk/prover/versions.json'
writeFileSync(
join(rootDir, versionsPath),
`${JSON.stringify({ ...readJson(rootDir, versionsPath), required_circuits_version: version }, null, 2)}\n`,
)
validateReleaseVersion(version, rootDir)
console.log(`Applied test release version ${version} (was ${previous}); nothing is committed.`)
return version
}