Skip to content
Merged
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
19 changes: 19 additions & 0 deletions scripts/prepare-resources/version-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ export const readAstrbotVersionFromPyproject = async ({ sourceDir }) => {
export const resolveAstrbotRuntimeVersionPath = ({ sourceDir }) =>
path.join(sourceDir, 'astrbot', 'core', 'config', 'default.py');

const readAstrbotPackageVersion = async ({ sourceDir }) => {
const initPath = path.join(sourceDir, 'astrbot', '__init__.py');
if (!existsSync(initPath)) {
throw new Error(`Cannot find AstrBot package version file: ${initPath}`);
}

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

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

export const readAstrbotRuntimeVersion = async ({ sourceDir }) => {
const defaultConfigPath = resolveAstrbotRuntimeVersionPath({ sourceDir });
if (!existsSync(defaultConfigPath)) {
Expand All @@ -61,6 +76,10 @@ export const readAstrbotRuntimeVersion = async ({ sourceDir }) => {
const content = await readFile(defaultConfigPath, 'utf8');
const match = /^\s*VERSION\s*=\s*["']([^"']+)["']\s*(?:#.*)?$/m.exec(content);
if (!match) {
if (/^\s*VERSION\s*=\s*__version__\s*(?:#.*)?$/m.test(content)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The PR description mentions supporting cases where default.py assigns VERSION from astrbot.__version__. However, the current regular expression only matches __version__ directly. To fully support both VERSION = __version__ and VERSION = astrbot.__version__ as described, we should make the astrbot. prefix optional in the regex.

Suggested change
if (/^\s*VERSION\s*=\s*__version__\s*(?:#.*)?$/m.test(content)) {
if (/^\s*VERSION\s*=\s*(?:astrbot\.)?__version__\s*(?:#.*)?$/m.test(content)) {

return readAstrbotPackageVersion({ sourceDir });
}

throw new Error(`Cannot resolve AstrBot runtime VERSION from ${defaultConfigPath}`);
}

Expand Down
15 changes: 15 additions & 0 deletions scripts/prepare-resources/version-sync.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ const createTempAstrBotSource = async ({ pyprojectVersion = '1.2.3', runtimeVers
const runtimeVersionPath = resolveAstrbotRuntimeVersionPath({ sourceDir: tempDir });
const configDir = path.dirname(runtimeVersionPath);
await mkdir(configDir, { recursive: true });
await mkdir(path.join(tempDir, 'astrbot'), { recursive: true });
await writeFile(
path.join(tempDir, 'pyproject.toml'),
`[project]\nname = "AstrBot"\nversion = "${pyprojectVersion}"\n`,
'utf8',
);
await writeFile(path.join(tempDir, 'astrbot', '__init__.py'), `__version__ = "${runtimeVersion}"\n`, 'utf8');
await writeFile(
runtimeVersionPath,
`import os\n\nVERSION = "${runtimeVersion}"\n`,
Expand Down Expand Up @@ -99,6 +101,19 @@ test('readAstrbotRuntimeVersion reads static VERSION from default.py', async ()
}
});

test('readAstrbotRuntimeVersion resolves VERSION imported from package __version__', async () => {
const tempDir = await createTempAstrBotSource({ runtimeVersion: '4.26.1' });
try {
const runtimeVersionPath = resolveAstrbotRuntimeVersionPath({ sourceDir: tempDir });
await writeFile(runtimeVersionPath, `from astrbot import __version__\n\nVERSION = __version__\n`, 'utf8');

const version = await readAstrbotRuntimeVersion({ sourceDir: tempDir });
assert.equal(version, '4.26.1');
} 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 {
Expand Down