2
This commit is contained in:
1 parent
cce34e7960
commit
b49e096355
56 files changed
+8343
-296
No files matched your search
@@ -0,0 +1,273 @@
|
||||
import 'dart:ffi';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 系统架构类型
|
||||
enum DistroArch {
|
||||
arm64,
|
||||
x86_64,
|
||||
}
|
||||
|
||||
/// LXC 容器镜像元数据规格(用于根据下载源动态解析真实下载路径)
|
||||
class LxcImageSpec {
|
||||
/// 发行版代号 (例如 'debian', 'archlinux', 'fedora')
|
||||
final String distribution;
|
||||
|
||||
/// 版本标识 (例如 'bookworm', 'current', '43')
|
||||
final String release;
|
||||
|
||||
/// 变体标识 (例如 'default', 'cloud')
|
||||
final String variant;
|
||||
|
||||
const LxcImageSpec({
|
||||
required this.distribution,
|
||||
required this.release,
|
||||
this.variant = 'default',
|
||||
});
|
||||
}
|
||||
|
||||
/// 发行版下载镜像源定义
|
||||
class DistroMirror {
|
||||
final String id;
|
||||
final String name;
|
||||
final String baseUrl;
|
||||
final bool isDefault;
|
||||
|
||||
const DistroMirror({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.baseUrl,
|
||||
this.isDefault = false,
|
||||
});
|
||||
}
|
||||
|
||||
/// 发行版条目清单模型
|
||||
class DistroManifestItem {
|
||||
/// 唯一标识符 (例如 'ubuntu', 'alpine', 'debian')
|
||||
final String id;
|
||||
|
||||
/// 显示名称 (例如 'Ubuntu', 'Alpine Linux', 'Debian')
|
||||
final String name;
|
||||
|
||||
/// 版本文本 (例如 '24.04.5 LTS', '3.20.3', '12 (Bookworm)')
|
||||
final String version;
|
||||
|
||||
/// 是否推荐
|
||||
final bool isRecommended;
|
||||
|
||||
/// 是否为软件内置安装包(打包在 assets 中,受删除保护且无需网络下载)
|
||||
final bool isBuiltin;
|
||||
|
||||
/// LXC 镜像元数据(在线下载型系统专属,用于动态源解析器自动发现真实路径)
|
||||
final LxcImageSpec? lxcSpec;
|
||||
|
||||
/// 对应架构资源包映射 (架构 -> 下载 URL 或 Asset 路径)
|
||||
final Map<DistroArch, String> packages;
|
||||
|
||||
/// 系统图标或装饰图标
|
||||
final IconData iconData;
|
||||
|
||||
/// 品牌主题色(用于图标底色/装饰)
|
||||
final Color brandColor;
|
||||
|
||||
/// 本地资产图标路径 (例如 'assets/icons/distros/ubuntu.png')
|
||||
final String? assetIconPath;
|
||||
|
||||
const DistroManifestItem({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.version,
|
||||
this.isRecommended = false,
|
||||
this.isBuiltin = false,
|
||||
this.lxcSpec,
|
||||
required this.packages,
|
||||
required this.iconData,
|
||||
required this.brandColor,
|
||||
this.assetIconPath,
|
||||
});
|
||||
|
||||
/// 获取当前运行环境的 CPU 架构
|
||||
static DistroArch get currentArch {
|
||||
final abi = Abi.current();
|
||||
if (abi == Abi.androidArm64 || abi == Abi.linuxArm64) {
|
||||
return DistroArch.arm64;
|
||||
}
|
||||
return DistroArch.x86_64;
|
||||
}
|
||||
|
||||
/// 当前机器架构是否有可用的资源包
|
||||
bool get isSupportedOnCurrentDevice {
|
||||
return packages.containsKey(currentArch);
|
||||
}
|
||||
|
||||
/// 获取当前设备适用的资源包路径或 URL
|
||||
String? get currentPackageSource {
|
||||
return packages[currentArch];
|
||||
}
|
||||
}
|
||||
|
||||
/// 系统资源库静态目录(包含内置与经过验证的高速在线源)
|
||||
class DistroRepository {
|
||||
static const List<DistroManifestItem> availableDistros = [
|
||||
// 1. Ubuntu 24.04 LTS (推荐,软件内置)
|
||||
DistroManifestItem(
|
||||
id: 'ubuntu',
|
||||
name: 'Ubuntu',
|
||||
version: '24.04.5 LTS (Noble Numbat)',
|
||||
isRecommended: true,
|
||||
isBuiltin: true,
|
||||
iconData: Icons.donut_large_rounded,
|
||||
brandColor: Color(0xFFE95420), // Ubuntu Orange
|
||||
assetIconPath: 'assets/icons/distros/ubuntu.png',
|
||||
packages: {
|
||||
DistroArch.arm64: 'assets/distros/ubuntu-base-24.04.5-base-arm64.tar.gz',
|
||||
DistroArch.x86_64: 'assets/distros/ubuntu-base-24.04.5-base-x86_64.tar.gz',
|
||||
},
|
||||
),
|
||||
|
||||
// 2. Alpine 3.20 (软件内置)
|
||||
DistroManifestItem(
|
||||
id: 'alpine',
|
||||
name: 'Alpine Linux',
|
||||
version: '3.20.3 (musl libc)',
|
||||
isRecommended: false,
|
||||
isBuiltin: true,
|
||||
iconData: Icons.landscape_rounded,
|
||||
brandColor: Color(0xFF0D597F), // Alpine Blue
|
||||
assetIconPath: 'assets/icons/distros/alpine.png',
|
||||
packages: {
|
||||
DistroArch.arm64: 'assets/distros/alpine-minirootfs-3.20.3-aarch64.tar.gz',
|
||||
DistroArch.x86_64: 'assets/distros/alpine-minirootfs-3.20.3-x86_64.tar.gz',
|
||||
},
|
||||
),
|
||||
|
||||
// 3. Debian 12 (Bookworm) - 在线下载,不占 APK 体积
|
||||
DistroManifestItem(
|
||||
id: 'debian',
|
||||
name: 'Debian',
|
||||
version: '12 (Bookworm)',
|
||||
isRecommended: false,
|
||||
isBuiltin: false,
|
||||
lxcSpec: LxcImageSpec(
|
||||
distribution: 'debian',
|
||||
release: 'bookworm',
|
||||
variant: 'default',
|
||||
),
|
||||
iconData: Icons.all_inclusive_rounded,
|
||||
brandColor: Color(0xFFA80030), // Debian Red
|
||||
assetIconPath: 'assets/icons/distros/debian.png',
|
||||
packages: {
|
||||
DistroArch.arm64: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/debian/bookworm/arm64/default/20260913_05:24/rootfs.tar.xz',
|
||||
DistroArch.x86_64: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/debian/bookworm/amd64/default/20260913_05:24/rootfs.tar.xz',
|
||||
},
|
||||
),
|
||||
|
||||
// 4. Arch Linux - 在线源
|
||||
DistroManifestItem(
|
||||
id: 'arch',
|
||||
name: 'Arch Linux',
|
||||
version: 'Rolling Release',
|
||||
isRecommended: false,
|
||||
isBuiltin: false,
|
||||
lxcSpec: LxcImageSpec(
|
||||
distribution: 'archlinux',
|
||||
release: 'current',
|
||||
variant: 'default',
|
||||
),
|
||||
iconData: Icons.change_history_rounded,
|
||||
brandColor: Color(0xFF1793D1), // Arch Cyan
|
||||
assetIconPath: 'assets/icons/distros/arch.png',
|
||||
packages: {
|
||||
DistroArch.arm64: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/archlinux/current/arm64/default/20260913_04:18/rootfs.tar.xz',
|
||||
DistroArch.x86_64: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/archlinux/current/amd64/default/20260913_04:18/rootfs.tar.xz',
|
||||
},
|
||||
),
|
||||
|
||||
// 5. Fedora 43 - 在线源
|
||||
DistroManifestItem(
|
||||
id: 'fedora',
|
||||
name: 'Fedora',
|
||||
version: '43 (Container Base)',
|
||||
isRecommended: false,
|
||||
isBuiltin: false,
|
||||
lxcSpec: LxcImageSpec(
|
||||
distribution: 'fedora',
|
||||
release: '43',
|
||||
variant: 'default',
|
||||
),
|
||||
iconData: Icons.language_rounded,
|
||||
brandColor: Color(0xFF51A2DA), // Fedora Blue
|
||||
assetIconPath: 'assets/icons/distros/fedora.png',
|
||||
packages: {
|
||||
DistroArch.arm64: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/fedora/43/arm64/default/20260913_20:33/rootfs.tar.xz',
|
||||
DistroArch.x86_64: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/fedora/43/amd64/default/20260913_20:33/rootfs.tar.xz',
|
||||
},
|
||||
),
|
||||
];
|
||||
|
||||
/// 可选的高速开源软件镜像源
|
||||
static const List<DistroMirror> availableMirrors = [
|
||||
DistroMirror(
|
||||
id: 'tsinghua',
|
||||
name: 'Tsinghua TUNA Mirror',
|
||||
baseUrl: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/',
|
||||
isDefault: true,
|
||||
),
|
||||
DistroMirror(
|
||||
id: 'bfsu',
|
||||
name: 'BFSU Open Source Mirror',
|
||||
baseUrl: 'https://mirrors.bfsu.edu.cn/lxc-images/',
|
||||
),
|
||||
DistroMirror(
|
||||
id: 'iscas',
|
||||
name: 'ISCAS Open Source Mirror',
|
||||
baseUrl: 'https://mirror.iscas.ac.cn/lxc-images/',
|
||||
),
|
||||
DistroMirror(
|
||||
id: 'official',
|
||||
name: 'LinuxContainers Official Mirror',
|
||||
baseUrl: 'https://images.linuxcontainers.org/',
|
||||
),
|
||||
];
|
||||
|
||||
/// 默认推荐镜像源(清华大学源)
|
||||
static DistroMirror get defaultMirror =>
|
||||
availableMirrors.firstWhere((m) => m.isDefault, orElse: () => availableMirrors.first);
|
||||
|
||||
/// 根据 ID 检索镜像源
|
||||
static DistroMirror getMirrorById(String id) {
|
||||
return availableMirrors.firstWhere(
|
||||
(m) => m.id == id,
|
||||
orElse: () => defaultMirror,
|
||||
);
|
||||
}
|
||||
|
||||
/// 根据当前机器架构过滤,仅返回本架构支持的发行版列表
|
||||
static List<DistroManifestItem> get currentArchDistros {
|
||||
return availableDistros.where((d) => d.isSupportedOnCurrentDevice).toList();
|
||||
}
|
||||
|
||||
/// 默认系统实例名称
|
||||
static const String defaultSystemName = 'ubuntu';
|
||||
|
||||
/// 根据 ID 查找发行版配置
|
||||
static DistroManifestItem? getById(String id) {
|
||||
try {
|
||||
return availableDistros.firstWhere((e) => e.id == id);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// 内置 Ubuntu 资源包路径(自动匹配当前 CPU 架构)
|
||||
static String get builtinUbuntuAssetPath {
|
||||
return getById('ubuntu')?.currentPackageSource ??
|
||||
'assets/distros/ubuntu-base-24.04.5-base-arm64.tar.gz';
|
||||
}
|
||||
|
||||
/// 内置 Alpine 资源包路径(自动匹配当前 CPU 架构)
|
||||
static String get builtinAlpineAssetPath {
|
||||
return getById('alpine')?.currentPackageSource ??
|
||||
'assets/distros/alpine-minirootfs-3.20.3-aarch64.tar.gz';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../l10n/app_localizations.dart';
|
||||
|
||||
/// 运行任务来源类型
|
||||
enum TaskSource {
|
||||
@@ -48,14 +49,19 @@ class RunTask {
|
||||
|
||||
/// 从持久化的 JSON Map 构建自定义运行任务
|
||||
factory RunTask.fromJson(Map<String, dynamic> json) {
|
||||
final rawClear = json['clearBeforeRun'];
|
||||
final bool clearVal = (rawClear is bool)
|
||||
? rawClear
|
||||
: (rawClear is num ? rawClear != 0 : (rawClear?.toString().toLowerCase() == 'true'));
|
||||
|
||||
return RunTask(
|
||||
id: json['id'] as String? ?? 'custom_${DateTime.now().microsecondsSinceEpoch}',
|
||||
name: json['name'] as String? ?? '未命名任务',
|
||||
command: json['command'] as String? ?? '',
|
||||
id: json['id']?.toString() ?? 'custom_${DateTime.now().microsecondsSinceEpoch}',
|
||||
name: json['name']?.toString() ?? '未命名任务',
|
||||
command: json['command']?.toString() ?? '',
|
||||
source: TaskSource.custom,
|
||||
description: json['description'] as String?,
|
||||
workingDir: json['workingDir'] as String?,
|
||||
clearBeforeRun: json['clearBeforeRun'] as bool? ?? false,
|
||||
description: json['description']?.toString(),
|
||||
workingDir: json['workingDir']?.toString(),
|
||||
clearBeforeRun: clearVal,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,6 +77,62 @@ class RunTask {
|
||||
};
|
||||
}
|
||||
|
||||
/// 获取多语言本地化的任务描述/小字(自动根据系统探测任务 ID 映射,未匹配或自定义任务返回原本的 description)
|
||||
String getLocalizedDescription(AppLocalizations l10n) {
|
||||
switch (id) {
|
||||
case 'detected_cmake_build_run':
|
||||
return l10n.detectedTaskCmakeBuildRunDesc;
|
||||
case 'detected_cmake_build':
|
||||
return l10n.detectedTaskCmakeBuildDesc;
|
||||
case 'detected_cmake_clean':
|
||||
return l10n.detectedTaskCmakeCleanDesc;
|
||||
case 'detected_gradle_run':
|
||||
return l10n.detectedTaskGradleRunDesc;
|
||||
case 'detected_gradle_assemble':
|
||||
return l10n.detectedTaskGradleAssembleDesc;
|
||||
case 'detected_gradle_build':
|
||||
return l10n.detectedTaskGradleBuildDesc;
|
||||
case 'detected_make_default':
|
||||
return l10n.detectedTaskMakeDefaultDesc;
|
||||
case 'detected_npm_start':
|
||||
return l10n.detectedTaskNpmStartDesc;
|
||||
case 'detected_npm_test':
|
||||
return l10n.detectedTaskNpmTestDesc;
|
||||
case 'detected_cargo_run':
|
||||
return l10n.detectedTaskCargoRunDesc;
|
||||
case 'detected_cargo_build':
|
||||
return l10n.detectedTaskCargoBuildDesc;
|
||||
case 'detected_dart_run':
|
||||
return l10n.detectedTaskDartRunDesc;
|
||||
case 'detected_single_python':
|
||||
return l10n.detectedTaskSinglePythonDesc;
|
||||
case 'detected_single_c':
|
||||
return l10n.detectedTaskSingleCDesc;
|
||||
case 'detected_single_cpp':
|
||||
return l10n.detectedTaskSingleCppDesc;
|
||||
case 'detected_single_sh':
|
||||
return l10n.detectedTaskSingleShDesc;
|
||||
case 'detected_single_dart':
|
||||
return l10n.detectedTaskSingleDartDesc;
|
||||
case 'detected_single_go':
|
||||
return l10n.detectedTaskSingleGoDesc;
|
||||
case 'detected_single_rust':
|
||||
return l10n.detectedTaskSingleRustDesc;
|
||||
case 'detected_single_js':
|
||||
return l10n.detectedTaskSingleJsDesc;
|
||||
default:
|
||||
return description ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取多语言本地化的任务名称(自定义且为默认名时返回对应语言的“未命名任务”)
|
||||
String getLocalizedName(AppLocalizations l10n) {
|
||||
if (name == '未命名任务' || name == 'Unnamed Task') {
|
||||
return l10n.unnamedTask;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
RunTask copyWith({
|
||||
String? id,
|
||||
String? name,
|
||||
|
||||
@@ -18,6 +18,7 @@ class TerminalSession {
|
||||
Pty? _pty;
|
||||
StreamSubscription? _ptyOutputSub;
|
||||
bool _isProcessRunning = false;
|
||||
Completer<bool>? _processCompleter;
|
||||
VoidCallback? onProcessTerminated;
|
||||
|
||||
TerminalSession({
|
||||
@@ -40,6 +41,9 @@ class TerminalSession {
|
||||
|
||||
bool get isProcessRunning => _isProcessRunning;
|
||||
|
||||
/// 获取终端进程启动就绪的 Future
|
||||
Future<bool>? get onProcessReady => _processCompleter?.future;
|
||||
|
||||
/// 获取终端缓冲区当前全部可见文本(常用于测试与日志断言)
|
||||
String get bufferText => terminal.buffer.getText();
|
||||
|
||||
@@ -65,6 +69,9 @@ class TerminalSession {
|
||||
|
||||
/// 启动底层真实 PTY 进程(连接 PRoot 容器或 Host Shell)
|
||||
Future<void> startProcess({DistroManager? distroManager, String? workspacePath}) async {
|
||||
if (_processCompleter == null || _processCompleter!.isCompleted) {
|
||||
_processCompleter = Completer<bool>();
|
||||
}
|
||||
final manager = distroManager ?? DistroManager();
|
||||
final effectiveWorkspace = workspacePath ?? this.workspacePath;
|
||||
try {
|
||||
@@ -86,6 +93,9 @@ class TerminalSession {
|
||||
columns: initialCols,
|
||||
);
|
||||
_isProcessRunning = true;
|
||||
if (!(_processCompleter?.isCompleted ?? true)) {
|
||||
_processCompleter?.complete(true);
|
||||
}
|
||||
|
||||
// PTY 输出数据流实时解码并写入终端
|
||||
_ptyOutputSub = _pty!.output.listen(
|
||||
@@ -108,11 +118,50 @@ class TerminalSession {
|
||||
});
|
||||
} catch (e) {
|
||||
_isProcessRunning = false;
|
||||
if (!(_processCompleter?.isCompleted ?? true)) {
|
||||
_processCompleter?.complete(false);
|
||||
}
|
||||
terminal.write('\x1b[31m[PTY launch failed: $e]\x1b[0m\r\n');
|
||||
onProcessTerminated?.call();
|
||||
}
|
||||
}
|
||||
|
||||
/// 等待终端底层伪终端进程启动并就绪
|
||||
Future<bool> waitForReady({Duration timeout = const Duration(seconds: 10)}) async {
|
||||
if (_isProcessRunning && _pty != null) return true;
|
||||
if (_processCompleter != null) {
|
||||
try {
|
||||
return await _processCompleter!.future.timeout(timeout, onTimeout: () => false);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// 向终端底层伪终端 (PTY) 真正写入待执行指令
|
||||
///
|
||||
/// 该方法通过 [_pty.write] 向进程标准输入通道写入指令并换行,触发 Shell 真正执行任务,
|
||||
/// 所有输出实时经由 PTY 流向终端画布呈现给用户。
|
||||
Future<void> executeCommand(String command) async {
|
||||
if (!_isProcessRunning && _processCompleter == null) {
|
||||
unawaited(startProcess());
|
||||
}
|
||||
|
||||
final isReady = await waitForReady();
|
||||
if (isReady && _pty != null && _isProcessRunning) {
|
||||
// 适度微延迟确保 Shell 终端行纪律(termios)已就绪并开始监听 stdin
|
||||
await Future<void>.delayed(const Duration(milliseconds: 60));
|
||||
try {
|
||||
_pty!.write(utf8.encode('$command\n'));
|
||||
} catch (e) {
|
||||
terminal.write('\r\n\x1b[31m[写入终端失败: $e]\x1b[0m\r\n');
|
||||
}
|
||||
} else {
|
||||
terminal.write('\r\n\x1b[31m[无法执行命令:终端环境未启动或启动失败]\x1b[0m\r\n');
|
||||
}
|
||||
}
|
||||
|
||||
/// 向终端写入文本(支持 VT100 / ANSI 转义序列)
|
||||
void write(String text) {
|
||||
terminal.write(text);
|
||||
|
||||
Reference in new issue
Block a user