2
This commit is contained in:
1 parent
5ac164b407
commit
f93f6095af
94 files changed
+7877
-4018
No files matched your search
@@ -1,9 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../models/distro_manifest.dart';
|
||||
@@ -11,7 +9,7 @@ import '../widgets/distro_extract_dialog.dart';
|
||||
import 'distro_installer.dart';
|
||||
import 'distro_manager.dart';
|
||||
|
||||
/// 内部私有代码智能引擎服务 (files/internal_engine/alpine)
|
||||
/// 代码智能与运行时引擎服务(基于统一主系统 Ubuntu 24.04)
|
||||
class InternalEngineService extends ChangeNotifier {
|
||||
static final InternalEngineService instance = InternalEngineService._();
|
||||
InternalEngineService._();
|
||||
@@ -23,48 +21,36 @@ class InternalEngineService extends ChangeNotifier {
|
||||
bool _isChecking = false;
|
||||
bool get isChecking => _isChecking;
|
||||
|
||||
/// 获取引擎主目录: `<files>/internal_engine/alpine`
|
||||
Future<Directory> getEngineDir() async {
|
||||
/// 获取主系统 Ubuntu rootfs 目录
|
||||
Future<Directory> getRootfsDir() async {
|
||||
if (customEngineDir != null) {
|
||||
return customEngineDir!;
|
||||
}
|
||||
final appDir = await getApplicationSupportDirectory();
|
||||
final rootPath = p.basename(appDir.path) == 'files'
|
||||
? appDir.path
|
||||
: p.join(appDir.path, 'files');
|
||||
final engineDir = Directory(p.join(rootPath, 'internal_engine', 'alpine'));
|
||||
if (!engineDir.existsSync()) {
|
||||
engineDir.createSync(recursive: true);
|
||||
}
|
||||
return engineDir;
|
||||
return DistroManager().getSystemRootDir(DistroRepository.defaultSystemName);
|
||||
}
|
||||
|
||||
/// 获取 rootfs 目录: `<files>/internal_engine/alpine/rootfs`
|
||||
Future<Directory> getRootfsDir() async {
|
||||
final engineDir = await getEngineDir();
|
||||
return Directory(p.join(engineDir.path, 'rootfs'));
|
||||
}
|
||||
/// 兼容旧方法命名
|
||||
Future<Directory> getEngineDir() => getRootfsDir();
|
||||
|
||||
/// 检查内置 Alpine 引擎是否已完整安装
|
||||
/// 检查主系统 Ubuntu 是否已完整安装
|
||||
Future<bool> isEngineInstalled() async {
|
||||
try {
|
||||
final engineDir = await getEngineDir();
|
||||
final marker = File(p.join(engineDir.path, '.installed'));
|
||||
if (!marker.existsSync()) return false;
|
||||
if (customEngineDir != null) {
|
||||
final marker = File(p.join(customEngineDir!.path, '.installed'));
|
||||
if (marker.existsSync()) return true;
|
||||
final binDir = Directory(p.join(customEngineDir!.path, 'bin'));
|
||||
final etcDir = Directory(p.join(customEngineDir!.path, 'etc'));
|
||||
return binDir.existsSync() && etcDir.existsSync();
|
||||
}
|
||||
|
||||
final rootfs = await getRootfsDir();
|
||||
if (!rootfs.existsSync()) return false;
|
||||
|
||||
final binDir = Directory(p.join(rootfs.path, 'bin'));
|
||||
final etcDir = Directory(p.join(rootfs.path, 'etc'));
|
||||
return binDir.existsSync() && etcDir.existsSync();
|
||||
return await DistroManager().isSystemInstalled(DistroRepository.defaultSystemName);
|
||||
} catch (e) {
|
||||
debugPrint('检查内置引擎异常: $e');
|
||||
debugPrint('检查代码智能运行引擎异常: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 确保内置引擎已准备完毕;若未安装,弹出模态解压弹窗进行解压
|
||||
/// 确保内置 Ubuntu 引擎已准备完毕;若未安装,弹出模态解压弹窗进行解压
|
||||
Future<bool> ensureEngineReady(BuildContext context) async {
|
||||
if (await isEngineInstalled()) {
|
||||
return true;
|
||||
@@ -79,7 +65,7 @@ class InternalEngineService extends ChangeNotifier {
|
||||
try {
|
||||
final success = await DistroExtractDialog.show(
|
||||
context: context,
|
||||
systemName: l10n?.internalEngineTitle ?? '内部代码智能引擎 (Alpine)',
|
||||
systemName: l10n?.internalEngineTitle ?? '代码运行与智能补全引擎 (Ubuntu)',
|
||||
task: (onProgress, isCancelled) async {
|
||||
await extractEngine(
|
||||
onProgress: onProgress,
|
||||
@@ -94,41 +80,31 @@ class InternalEngineService extends ChangeNotifier {
|
||||
} catch (e) {
|
||||
_isChecking = false;
|
||||
notifyListeners();
|
||||
debugPrint('解压内部引擎失败: $e');
|
||||
debugPrint('解压 Ubuntu 引擎失败: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 解压 Asset 中的 Alpine minirootfs 到私有引擎目录
|
||||
/// 解压 Asset 中的 Ubuntu 到主系统目录
|
||||
Future<void> extractEngine({
|
||||
InstallProgressCallback? onProgress,
|
||||
bool Function()? isCancelled,
|
||||
}) async {
|
||||
final assetPath = DistroRepository.builtinAlpineAssetPath;
|
||||
onProgress?.call(0.02, '正在读取应用内置 Alpine 系统资源包...');
|
||||
|
||||
final byteData = await rootBundle.load(assetPath);
|
||||
final bytes = byteData.buffer.asUint8List();
|
||||
|
||||
final rootfsDir = await getRootfsDir();
|
||||
if (!rootfsDir.existsSync()) {
|
||||
rootfsDir.createSync(recursive: true);
|
||||
if (customEngineDir != null) {
|
||||
// 测试模式下解压或标记
|
||||
final marker = File(p.join(customEngineDir!.path, '.installed'));
|
||||
await marker.writeAsString('ready');
|
||||
return;
|
||||
}
|
||||
|
||||
await DistroInstaller.installFromBytes(
|
||||
tarGzBytes: bytes,
|
||||
targetDir: rootfsDir,
|
||||
await DistroManager().importBuiltinUbuntu(
|
||||
systemName: DistroRepository.defaultSystemName,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
|
||||
// 写入安装完成标记文件
|
||||
final engineDir = await getEngineDir();
|
||||
final marker = File(p.join(engineDir.path, '.installed'));
|
||||
await marker.writeAsString('ready');
|
||||
}
|
||||
|
||||
/// 检查某命令(如 clangd, rust-analyzer)是否在内置 Alpine 中已安装
|
||||
/// 检查某命令(如 clangd, rust-analyzer, pylsp)是否在系统已安装
|
||||
Future<bool> isCommandInstalled(String command) async {
|
||||
if (!await isEngineInstalled() || command.trim().isEmpty) {
|
||||
return false;
|
||||
@@ -137,39 +113,126 @@ class InternalEngineService extends ChangeNotifier {
|
||||
try {
|
||||
final rootfs = await getRootfsDir();
|
||||
final res = await DistroManager().runHeadlessCommand(
|
||||
systemName: 'alpine_internal',
|
||||
systemName: DistroRepository.defaultSystemName,
|
||||
customRootDir: rootfs,
|
||||
command: 'command -v ${command.trim()} >/dev/null 2>&1 && echo CE_OK',
|
||||
timeout: const Duration(seconds: 15),
|
||||
);
|
||||
return res?.stdout.toString().contains('CE_OK') ?? false;
|
||||
} catch (e) {
|
||||
debugPrint('检查内置引擎命令异常: $e');
|
||||
debugPrint('检查引擎命令异常: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 在内置 Alpine 中安装指定 apk 软件包
|
||||
/// 在 Ubuntu 系统中安装指定 apt 软件包
|
||||
Future<bool> installPackage(
|
||||
String apkPackage, {
|
||||
String packageName, {
|
||||
void Function(String chunk)? onOutput,
|
||||
}) async {
|
||||
if (!await isEngineInstalled() || apkPackage.trim().isEmpty) {
|
||||
if (!await isEngineInstalled() || packageName.trim().isEmpty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
final rootfs = await getRootfsDir();
|
||||
final installCmd =
|
||||
'export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true; '
|
||||
'dpkg --configure -a 2>/dev/null || true; '
|
||||
'apt-get update && '
|
||||
'apt-get install -y --no-install-recommends ${packageName.trim()}';
|
||||
|
||||
final res = await DistroManager().runHeadlessCommand(
|
||||
systemName: 'alpine_internal',
|
||||
systemName: DistroRepository.defaultSystemName,
|
||||
customRootDir: rootfs,
|
||||
command: 'apk add --no-cache ${apkPackage.trim()}',
|
||||
timeout: const Duration(minutes: 5),
|
||||
command: installCmd,
|
||||
timeout: const Duration(minutes: 10),
|
||||
onStdout: onOutput,
|
||||
);
|
||||
return res != null && res.exitCode == 0;
|
||||
} catch (e) {
|
||||
debugPrint('安装内置引擎软件包异常: $e');
|
||||
debugPrint('安装引擎软件包异常: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 在 Ubuntu 系统中彻底卸载指定 apt 软件包及清理对应命令
|
||||
Future<bool> uninstallPackage(
|
||||
String packageName, {
|
||||
String? serverCommand,
|
||||
void Function(String chunk)? onOutput,
|
||||
}) async {
|
||||
final cleanPkg = packageName.trim();
|
||||
if (!await isEngineInstalled() || cleanPkg.isEmpty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
final rootfs = await getRootfsDir();
|
||||
final cleanCmd = serverCommand?.trim();
|
||||
|
||||
// 1. 若指定了服务命令,先在容器内强杀残留进程,避免文件占用导致卸载失败
|
||||
final killSection = (cleanCmd != null && cleanCmd.isNotEmpty)
|
||||
? 'pkill -9 -f "$cleanCmd" 2>/dev/null || true; '
|
||||
: '';
|
||||
|
||||
// 2. 针对特定虚包/版本衍生包进行包名扩展 purge
|
||||
// 例如 clangd 会衍生出 clangd-15, clang-tools-15 等
|
||||
final String targetsToPurge;
|
||||
if (cleanPkg == 'clangd') {
|
||||
targetsToPurge = 'clangd clangd* clang-tools*';
|
||||
} else {
|
||||
targetsToPurge = cleanPkg;
|
||||
}
|
||||
|
||||
// 3. 二次兜底检测:如果命令仍残留,尝试通过 dpkg -S 查询实际宿主包并 purge,或清理残留可执行文件
|
||||
final cmdCleanupSection = (cleanCmd != null && cleanCmd.isNotEmpty)
|
||||
? '''
|
||||
BIN_PATH=\$(command -v "$cleanCmd" 2>/dev/null || true)
|
||||
if [ -n "\$BIN_PATH" ]; then
|
||||
OWNER_PKG=\$(dpkg -S "\$BIN_PATH" 2>/dev/null | cut -d: -f1 | head -n 1 || true)
|
||||
if [ -n "\$OWNER_PKG" ]; then
|
||||
apt-get remove --purge -y "\$OWNER_PKG" 2>/dev/null || true
|
||||
fi
|
||||
rm -f "\$BIN_PATH" 2>/dev/null || true
|
||||
fi
|
||||
'''
|
||||
: '';
|
||||
|
||||
final uninstallCmd = '''
|
||||
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
|
||||
$killSection
|
||||
dpkg --configure -a 2>/dev/null || true
|
||||
apt-get remove --purge -y $targetsToPurge
|
||||
apt-get autoremove --purge -y
|
||||
$cmdCleanupSection
|
||||
''';
|
||||
|
||||
final res = await DistroManager().runHeadlessCommand(
|
||||
systemName: DistroRepository.defaultSystemName,
|
||||
customRootDir: rootfs,
|
||||
command: uninstallCmd,
|
||||
timeout: const Duration(minutes: 5),
|
||||
onStdout: onOutput,
|
||||
);
|
||||
|
||||
final success = res != null && res.exitCode == 0;
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4. 若提供了 serverCommand,确凿验证该命令在系统中是否已不存在
|
||||
if (cleanCmd != null && cleanCmd.isNotEmpty) {
|
||||
final stillExists = await isCommandInstalled(cleanCmd);
|
||||
if (stillExists) {
|
||||
debugPrint('[InternalEngineService] 警告: 卸载脚本完成后命令 $cleanCmd 仍残留');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
debugPrint('卸载引擎软件包异常: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user