219 lines
7.2 KiB
Dart
219 lines
7.2 KiB
Dart
import 'package:flutter/material.dart';
|
||
import '../l10n/app_localizations.dart';
|
||
|
||
/// 运行任务来源类型
|
||
enum TaskSource {
|
||
/// 系统动态探测所得(保存在内存,随项目探测刷新)
|
||
detected,
|
||
|
||
/// 用户在 .code_editor/run_tasks.json 中持久化配置
|
||
custom,
|
||
}
|
||
|
||
/// 运行任务实体模型
|
||
class RunTask {
|
||
/// 任务唯一ID
|
||
final String id;
|
||
|
||
/// 任务展示名称
|
||
final String name;
|
||
|
||
/// 发送到终端执行的 Shell 命令(支持 &&、; 串联多条命令)
|
||
final String command;
|
||
|
||
/// 任务来源(探测/用户自定义)
|
||
final TaskSource source;
|
||
|
||
/// 任务补充描述/副标题(通常是命令预览或文件路径)
|
||
final String? description;
|
||
|
||
/// 容器内相对工作路径(为 null 时默认容器工作区 /workspace)
|
||
final String? workingDir;
|
||
|
||
/// 是否在执行该命令前清屏(发送 clear)
|
||
final bool clearBeforeRun;
|
||
|
||
/// 任务所属分组(如 Gradle 的 'build', 'verification', 'application', 'other' 等)
|
||
final String? group;
|
||
|
||
/// 归属的项目探测模块ID(如 'gradle', 'cmake' 等)
|
||
final String? moduleId;
|
||
|
||
/// UI 展示图标(可选)
|
||
final IconData? icon;
|
||
|
||
const RunTask({
|
||
required this.id,
|
||
required this.name,
|
||
required this.command,
|
||
required this.source,
|
||
this.description,
|
||
this.workingDir,
|
||
this.clearBeforeRun = false,
|
||
this.group,
|
||
this.moduleId,
|
||
this.icon,
|
||
});
|
||
|
||
/// 从持久化的 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'));
|
||
|
||
final rawSource = json['source']?.toString();
|
||
final TaskSource source = rawSource == 'detected' ? TaskSource.detected : TaskSource.custom;
|
||
|
||
return RunTask(
|
||
id: json['id']?.toString() ?? 'custom_${DateTime.now().microsecondsSinceEpoch}',
|
||
name: json['name']?.toString() ?? '未命名任务',
|
||
command: json['command']?.toString() ?? '',
|
||
source: source,
|
||
description: json['description']?.toString(),
|
||
workingDir: json['workingDir']?.toString(),
|
||
clearBeforeRun: clearVal,
|
||
group: json['group']?.toString(),
|
||
moduleId: json['moduleId']?.toString(),
|
||
);
|
||
}
|
||
|
||
/// 序列化为持久化 JSON Map
|
||
Map<String, dynamic> toJson() {
|
||
return {
|
||
'id': id,
|
||
'name': name,
|
||
'command': command,
|
||
'source': source.name,
|
||
if (description != null && description!.isNotEmpty) 'description': description,
|
||
if (workingDir != null && workingDir!.isNotEmpty) 'workingDir': workingDir,
|
||
if (group != null && group!.isNotEmpty) 'group': group,
|
||
if (moduleId != null && moduleId!.isNotEmpty) 'moduleId': moduleId,
|
||
'clearBeforeRun': clearBeforeRun,
|
||
};
|
||
}
|
||
|
||
/// 获取多语言本地化的任务描述/小字(自动根据系统探测任务 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':
|
||
case 'gradle_run':
|
||
return l10n.detectedTaskGradleRunDesc;
|
||
case 'detected_gradle_assemble':
|
||
case 'gradle_assembleDebug':
|
||
return l10n.detectedTaskGradleAssembleDesc;
|
||
case 'detected_gradle_build':
|
||
case '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;
|
||
case 'detected_single_java':
|
||
return l10n.detectedTaskSingleJavaDesc;
|
||
case 'detected_single_ts':
|
||
return l10n.detectedTaskSingleTsDesc;
|
||
case 'detected_single_lua':
|
||
return l10n.detectedTaskSingleLuaDesc;
|
||
case 'detected_single_perl':
|
||
return l10n.detectedTaskSinglePerlDesc;
|
||
case 'detected_single_php':
|
||
return l10n.detectedTaskSinglePhpDesc;
|
||
case 'detected_python_pip_install':
|
||
return l10n.detectedTaskPythonPipInstallDesc;
|
||
case 'detected_python_run_main':
|
||
case 'detected_python_run_app':
|
||
return l10n.detectedTaskPythonRunMainDesc;
|
||
case 'detected_python_pytest':
|
||
return l10n.detectedTaskPythonPytestDesc;
|
||
case 'detected_maven_package':
|
||
return l10n.detectedTaskMavenPackageDesc;
|
||
case 'detected_maven_compile':
|
||
return l10n.detectedTaskMavenCompileDesc;
|
||
case 'detected_maven_test':
|
||
return l10n.detectedTaskMavenTestDesc;
|
||
case 'detected_maven_clean':
|
||
return l10n.detectedTaskMavenCleanDesc;
|
||
default:
|
||
return description ?? '';
|
||
}
|
||
}
|
||
|
||
/// 获取多语言本地化的任务名称(自定义且为默认名时返回对应语言的“未命名任务”)
|
||
String getLocalizedName(AppLocalizations l10n) {
|
||
if (name == '未命名任务' || name == 'Unnamed Task') {
|
||
return l10n.unnamedTask;
|
||
}
|
||
return name;
|
||
}
|
||
|
||
RunTask copyWith({
|
||
String? id,
|
||
String? name,
|
||
String? command,
|
||
TaskSource? source,
|
||
String? description,
|
||
String? workingDir,
|
||
bool? clearBeforeRun,
|
||
String? group,
|
||
String? moduleId,
|
||
IconData? icon,
|
||
}) {
|
||
return RunTask(
|
||
id: id ?? this.id,
|
||
name: name ?? this.name,
|
||
command: command ?? this.command,
|
||
source: source ?? this.source,
|
||
description: description ?? this.description,
|
||
workingDir: workingDir ?? this.workingDir,
|
||
clearBeforeRun: clearBeforeRun ?? this.clearBeforeRun,
|
||
group: group ?? this.group,
|
||
moduleId: moduleId ?? this.moduleId,
|
||
icon: icon ?? this.icon,
|
||
);
|
||
}
|
||
|
||
@override
|
||
bool operator ==(Object other) =>
|
||
identical(this, other) ||
|
||
other is RunTask &&
|
||
runtimeType == other.runtimeType &&
|
||
id == other.id &&
|
||
name == other.name &&
|
||
command == other.command &&
|
||
source == other.source;
|
||
|
||
@override
|
||
int get hashCode => id.hashCode ^ name.hashCode ^ command.hashCode ^ source.hashCode;
|
||
}
|