Skip to content

Commit 689d2e4

Browse files
authored
fix: resolve AstrBot runtime version from package metadata (#143)
1 parent 0aa65de commit 689d2e4

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

scripts/prepare-resources/version-sync.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ export const readAstrbotVersionFromPyproject = async ({ sourceDir }) => {
5252
export const resolveAstrbotRuntimeVersionPath = ({ sourceDir }) =>
5353
path.join(sourceDir, 'astrbot', 'core', 'config', 'default.py');
5454

55+
const readAstrbotPackageVersion = async ({ sourceDir }) => {
56+
const initPath = path.join(sourceDir, 'astrbot', '__init__.py');
57+
if (!existsSync(initPath)) {
58+
throw new Error(`Cannot find AstrBot package version file: ${initPath}`);
59+
}
60+
61+
const content = await readFile(initPath, 'utf8');
62+
const match = /^\s*__version__\s*=\s*["']([^"']+)["']\s*(?:#.*)?$/m.exec(content);
63+
if (!match) {
64+
throw new Error(`Cannot resolve AstrBot package __version__ from ${initPath}`);
65+
}
66+
67+
return match[1].trim();
68+
};
69+
5570
export const readAstrbotRuntimeVersion = async ({ sourceDir }) => {
5671
const defaultConfigPath = resolveAstrbotRuntimeVersionPath({ sourceDir });
5772
if (!existsSync(defaultConfigPath)) {
@@ -61,6 +76,10 @@ export const readAstrbotRuntimeVersion = async ({ sourceDir }) => {
6176
const content = await readFile(defaultConfigPath, 'utf8');
6277
const match = /^\s*VERSION\s*=\s*["']([^"']+)["']\s*(?:#.*)?$/m.exec(content);
6378
if (!match) {
79+
if (/^\s*VERSION\s*=\s*__version__\s*(?:#.*)?$/m.test(content)) {
80+
return readAstrbotPackageVersion({ sourceDir });
81+
}
82+
6483
throw new Error(`Cannot resolve AstrBot runtime VERSION from ${defaultConfigPath}`);
6584
}
6685

scripts/prepare-resources/version-sync.test.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ const createTempAstrBotSource = async ({ pyprojectVersion = '1.2.3', runtimeVers
4747
const runtimeVersionPath = resolveAstrbotRuntimeVersionPath({ sourceDir: tempDir });
4848
const configDir = path.dirname(runtimeVersionPath);
4949
await mkdir(configDir, { recursive: true });
50+
await mkdir(path.join(tempDir, 'astrbot'), { recursive: true });
5051
await writeFile(
5152
path.join(tempDir, 'pyproject.toml'),
5253
`[project]\nname = "AstrBot"\nversion = "${pyprojectVersion}"\n`,
5354
'utf8',
5455
);
56+
await writeFile(path.join(tempDir, 'astrbot', '__init__.py'), `__version__ = "${runtimeVersion}"\n`, 'utf8');
5557
await writeFile(
5658
runtimeVersionPath,
5759
`import os\n\nVERSION = "${runtimeVersion}"\n`,
@@ -99,6 +101,19 @@ test('readAstrbotRuntimeVersion reads static VERSION from default.py', async ()
99101
}
100102
});
101103

104+
test('readAstrbotRuntimeVersion resolves VERSION imported from package __version__', async () => {
105+
const tempDir = await createTempAstrBotSource({ runtimeVersion: '4.26.1' });
106+
try {
107+
const runtimeVersionPath = resolveAstrbotRuntimeVersionPath({ sourceDir: tempDir });
108+
await writeFile(runtimeVersionPath, `from astrbot import __version__\n\nVERSION = __version__\n`, 'utf8');
109+
110+
const version = await readAstrbotRuntimeVersion({ sourceDir: tempDir });
111+
assert.equal(version, '4.26.1');
112+
} finally {
113+
await rm(tempDir, { recursive: true, force: true });
114+
}
115+
});
116+
102117
test('validateAstrbotRuntimeVersion rejects 0.0.0 runtime version', async () => {
103118
const tempDir = await createTempAstrBotSource({ runtimeVersion: '0.0.0' });
104119
try {

0 commit comments

Comments
 (0)