Skip to content

Commit 3c49073

Browse files
committed
feat: enhance build tool checks and add environment variable wrapper for Hamlib
1 parent 28cb737 commit 3c49073

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
"build": "node-gyp configure && node-gyp build",
4242
"rebuild": "node-gyp rebuild",
4343
"clean": "node-gyp clean",
44-
"test": "node test/test_loader.js",
45-
"test:network": "node test/test_network.js",
44+
"test": "node scripts/run-with-env.js 'node test/test_loader.js'",
45+
"test:network": "node scripts/run-with-env.js 'node test/test_network.js'",
46+
"test:dummy": "node scripts/run-with-env.js 'node test/test_dummy_complete.js'",
47+
"test:serial": "node scripts/run-with-env.js 'node test/test_serial_config.js'",
4648
"prebuild": "prebuildify --napi --strip",
4749
"prepare": "",
4850
"bundle:macos": "bash scripts/bundle-macos.sh",

scripts/build-hamlib.js

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,48 +146,25 @@ function isCI() {
146146
function checkBuildTools(platform) {
147147
logStep('1/6', '检查构建工具');
148148

149-
const requiredTools = ['git', 'autoconf', 'automake', 'libtool', 'make'];
149+
const requiredTools = ['git', 'autoconf', 'automake', 'libtoolize', 'make'];
150150
const missingTools = [];
151151

152152
for (const tool of requiredTools) {
153-
let found = false;
154-
let toolPath = '';
155-
156153
try {
157-
// 尝试直接运行工具的 --version 来验证(最可靠的方法)
158-
const versionCheck = exec(`${tool} --version 2>&1`, { silent: true, ignoreError: false });
159-
if (versionCheck) {
160-
found = true;
161-
// 尝试获取工具路径
162-
const pathCheck = exec(`command -v ${tool} 2>/dev/null || which ${tool} 2>/dev/null || echo "(installed)"`, { silent: true, ignoreError: true });
163-
toolPath = pathCheck ? pathCheck.trim() : '(installed)';
164-
}
154+
// 简化:只检测工具是否可执行,不获取详细路径
155+
exec(`${tool} --version 2>&1`, { silent: true, ignoreError: false });
156+
logSuccess(`找到 ${tool}`);
165157
} catch (error) {
166-
// 工具不存在或执行失败
167-
found = false;
168-
}
169-
170-
if (found) {
171-
logSuccess(`找到 ${tool}${toolPath !== '(installed)' ? ': ' + toolPath : ''}`);
172-
} else {
173158
missingTools.push(tool);
174159
}
175160
}
176161

177162
if (missingTools.length > 0) {
178-
logError('缺少必需的构建工具:');
179-
console.log(' ' + missingTools.join(', '));
180-
console.log();
181-
182-
if (platform === 'macos') {
183-
log('在 macOS 上安装构建工具:', 'yellow');
184-
log(' brew install autoconf automake libtool', 'bright');
185-
} else if (platform === 'linux') {
186-
log('在 Linux 上安装构建工具:', 'yellow');
187-
log(' sudo apt-get install autoconf automake libtool pkg-config # Debian/Ubuntu', 'bright');
188-
log(' sudo yum install autoconf automake libtool # CentOS/RHEL', 'bright');
189-
}
190-
163+
logError(`缺少必需的构建工具: ${missingTools.join(', ')}`);
164+
const installCmd = platform === 'macos'
165+
? 'brew install autoconf automake libtool'
166+
: 'sudo apt-get install autoconf automake libtool pkg-config';
167+
log(`安装命令: ${installCmd}`, 'bright');
191168
process.exit(1);
192169
}
193170

scripts/run-with-env.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* 环境变量 wrapper - 自动设置 Hamlib 环境变量后运行命令
5+
* 用法: node scripts/run-with-env.js <command>
6+
*/
7+
8+
const { execSync } = require('child_process');
9+
const path = require('path');
10+
const fs = require('fs');
11+
12+
const rootDir = path.resolve(__dirname, '..');
13+
const hamlibBuildDir = path.join(rootDir, 'hamlib-build');
14+
15+
// 检查是否存在本地编译的 Hamlib
16+
if (fs.existsSync(hamlibBuildDir)) {
17+
const libDir = path.join(hamlibBuildDir, 'lib');
18+
const pkgConfigDir = path.join(libDir, 'pkgconfig');
19+
20+
// 设置环境变量
21+
process.env.HAMLIB_PREFIX = hamlibBuildDir;
22+
process.env.LD_LIBRARY_PATH = `${libDir}:${process.env.LD_LIBRARY_PATH || ''}`;
23+
process.env.DYLD_LIBRARY_PATH = `${libDir}:${process.env.DYLD_LIBRARY_PATH || ''}`;
24+
process.env.PKG_CONFIG_PATH = `${pkgConfigDir}:${process.env.PKG_CONFIG_PATH || ''}`;
25+
26+
console.log(`✓ 已设置 Hamlib 环境变量: ${hamlibBuildDir}`);
27+
}
28+
29+
// 获取要执行的命令
30+
const command = process.argv.slice(2).join(' ');
31+
32+
if (!command) {
33+
console.error('错误: 请提供要执行的命令');
34+
console.error('用法: node scripts/run-with-env.js <command>');
35+
process.exit(1);
36+
}
37+
38+
// 执行命令
39+
try {
40+
execSync(command, {
41+
stdio: 'inherit',
42+
cwd: rootDir,
43+
shell: true
44+
});
45+
} catch (error) {
46+
process.exit(error.status || 1);
47+
}

0 commit comments

Comments
 (0)