Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scripts/prepare-resources.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url';
import {
readAstrbotVersionFromPyproject,
syncDesktopVersionFiles,
validateAstrbotRuntimeVersion,
} from './prepare-resources/version-sync.mjs';
import {
ensureSourceRepo,
Expand Down Expand Up @@ -60,6 +61,10 @@ const main = async () => {
const astrbotVersion =
desktopVersionOverride || (await readAstrbotVersionFromPyproject({ sourceDir }));

if (!desktopVersionOverride) {
await validateAstrbotRuntimeVersion({ sourceDir, expectedVersion: astrbotVersion });
Comment on lines +64 to +65

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Consider validating runtime VERSION even when a desktopVersionOverride is supplied, at least for the 0.0.0 guard.

Because validateAstrbotRuntimeVersion is only called when desktopVersionOverride is unset, a runtime VERSION of 0.0.0 (or drift from pyproject) is silently accepted whenever an override is used, despite the error text implying 0.0.0 is always invalid. To align behavior with the messaging, consider always enforcing at least the 0.0.0 (and maybe drift) checks even when an override is present, or clarify in the code/message that overrides bypass these checks.

}

if (desktopVersionOverride && needsSourceRepo) {
const sourceVersion = await readAstrbotVersionFromPyproject({ sourceDir });
if (sourceVersion !== desktopVersionOverride) {
Expand Down
31 changes: 31 additions & 0 deletions scripts/prepare-resources/version-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ export const readAstrbotVersionFromPyproject = async ({ sourceDir }) => {
throw new Error(`Cannot resolve [project].version from ${pyprojectPath}`);
};

export const readAstrbotRuntimeVersion = async ({ sourceDir }) => {
const defaultConfigPath = path.join(sourceDir, 'astrbot', 'core', 'config', 'default.py');
if (!existsSync(defaultConfigPath)) {
throw new Error(`Cannot find AstrBot runtime version file: ${defaultConfigPath}`);
}

const content = await readFile(defaultConfigPath, 'utf8');
const match = /^\s*VERSION\s*=\s*["']([^"']+)["']\s*(?:#.*)?$/m.exec(content);
if (!match) {
throw new Error(`Cannot resolve AstrBot runtime VERSION from ${defaultConfigPath}`);
}

return match[1].trim();
};

export const validateAstrbotRuntimeVersion = async ({ sourceDir, expectedVersion }) => {
const runtimeVersion = await readAstrbotRuntimeVersion({ sourceDir });
if (runtimeVersion === '0.0.0') {
throw new Error(
`AstrBot runtime VERSION resolved to 0.0.0 in ${sourceDir}. Use an AstrBot source ref that contains the static runtime VERSION fix.`,
);
}
if (runtimeVersion !== expectedVersion) {
throw new Error(
`AstrBot version mismatch in ${sourceDir}: pyproject.toml has ${expectedVersion}, but runtime VERSION is ${runtimeVersion}.`,
);
}

return runtimeVersion;
};

const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const CARGO_LOCK_PACKAGE_HEADER = /^\s*\[\[package\]\]\s*(?:#.*)?$/;
const CARGO_LOCK_ANY_HEADER = /^\s*\[\[/;
Expand Down
57 changes: 57 additions & 0 deletions scripts/prepare-resources/version-sync.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import assert from 'node:assert/strict';
import {
DESKTOP_TAURI_CRATE_NAME,
normalizeDesktopVersionOverride,
readAstrbotRuntimeVersion,
readAstrbotVersionFromPyproject,
syncDesktopVersionFiles,
validateAstrbotRuntimeVersion,
} from './version-sync.mjs';

const createTempDesktopProject = async ({ cargoLockContents, version = '0.1.0' }) => {
Expand Down Expand Up @@ -39,6 +41,23 @@ const createTempDesktopProject = async ({ cargoLockContents, version = '0.1.0' }
return { tempDir, srcTauriDir };
};

const createTempAstrBotSource = async ({ pyprojectVersion = '1.2.3', runtimeVersion = '1.2.3' }) => {
const tempDir = await mkdtemp(path.join(os.tmpdir(), 'astrbot-source-'));
const configDir = path.join(tempDir, 'astrbot', 'core', 'config');
await mkdir(configDir, { recursive: true });
await writeFile(
path.join(tempDir, 'pyproject.toml'),
`[project]\nname = "AstrBot"\nversion = "${pyprojectVersion}"\n`,
'utf8',
);
await writeFile(
path.join(configDir, 'default.py'),
`import os\n\nVERSION = "${runtimeVersion}"\n`,
'utf8',
);
return tempDir;
};

test('normalizeDesktopVersionOverride trims and strips leading v', () => {
assert.equal(normalizeDesktopVersionOverride(' v1.2.3 '), '1.2.3');
assert.equal(normalizeDesktopVersionOverride('2.0.0'), '2.0.0');
Expand Down Expand Up @@ -68,6 +87,44 @@ version = "1.9.1"
}
});

test('readAstrbotRuntimeVersion reads static VERSION from default.py', async () => {
const tempDir = await createTempAstrBotSource({ runtimeVersion: '4.26.0-beta.10' });
try {
const version = await readAstrbotRuntimeVersion({ sourceDir: tempDir });
assert.equal(version, '4.26.0-beta.10');
} finally {
await rm(tempDir, { recursive: true, force: true });
}
});

test('validateAstrbotRuntimeVersion rejects 0.0.0 runtime version', async () => {
const tempDir = await createTempAstrBotSource({ runtimeVersion: '0.0.0' });
try {
await assert.rejects(
validateAstrbotRuntimeVersion({ sourceDir: tempDir, expectedVersion: '4.26.0-beta.10' }),
/runtime VERSION resolved to 0\.0\.0/,
);
} finally {
await rm(tempDir, { recursive: true, force: true });
}
});

test('validateAstrbotRuntimeVersion rejects runtime version drift', async () => {
const tempDir = await createTempAstrBotSource({
pyprojectVersion: '4.26.0-beta.10',
runtimeVersion: '4.26.0-beta.9',
});
try {
const expectedVersion = await readAstrbotVersionFromPyproject({ sourceDir: tempDir });
await assert.rejects(
validateAstrbotRuntimeVersion({ sourceDir: tempDir, expectedVersion }),
/pyproject\.toml has 4\.26\.0-beta\.10, but runtime VERSION is 4\.26\.0-beta\.9/,
);
} finally {
await rm(tempDir, { recursive: true, force: true });
}
});

test('syncDesktopVersionFiles updates package.json, tauri.conf.json, Cargo.toml and Cargo.lock', async () => {
let tempDir;
let srcTauriDir;
Expand Down