This commit is contained in:
LZX committed 2026-09-14 00:06:49 +08:00
1 parent 9fbb414ee6
commit 4554f22c87
233 files changed
+147430 -605

No files matched your search

+3 -1
View File
@@ -14,6 +14,7 @@ class FileService {
int depth = 0,
List<String> openDirectoryPaths = const [],
int maxDepth = 10,
bool showHiddenFiles = true,
}) async {
if(depth > maxDepth) {
return [];
@@ -45,7 +46,7 @@ class FileService {
for(var entity in entities) {
try {
final fileName = p.basename(entity.path);
if (fileName.startsWith('.')) continue;
if (!showHiddenFiles && fileName.startsWith('.')) continue;
final entityPath = p.normalize(entity.path);
String? relativePath;
@@ -64,6 +65,7 @@ class FileService {
depth: depth + 1,
openDirectoryPaths: openDirectoryPaths,
maxDepth: maxDepth,
showHiddenFiles: showHiddenFiles,
);
}
items.add(FileItem(
+279
View File
@@ -0,0 +1,279 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import '../models/run_task.dart';
/// 负责根据工程文件和当前打开的文件,在内存中动态推导可用的运行任务
class ProjectTaskDetector {
/// 核心探测入口
/// [projectRoot]: 工程根目录(宿主绝对路径)
/// [currentFilePath]: 当前编辑器激活打开的文件(宿主绝对路径,可能为 null)
static Future<List<RunTask>> detect({
required String projectRoot,
String? currentFilePath,
}) async {
final List<RunTask> tasks = [];
final projectDir = Directory(projectRoot);
if (!projectDir.existsSync()) {
return tasks;
}
// 1. 工程级特征文件检测
tasks.addAll(await _detectProjectBuildTasks(projectRoot));
// 2. 当前活跃文件(单文件运行)检测
if (currentFilePath != null && currentFilePath.isNotEmpty) {
final singleTask = _detectSingleFileTask(projectRoot, currentFilePath);
if (singleTask != null) {
// 单文件即时运行置于首位,方便用户直接点击运行
tasks.insert(0, singleTask);
}
}
return tasks;
}
/// 探测构建工程任务(CMake / Gradle / Makefile / npm / Cargo / Dart 等)
static Future<List<RunTask>> _detectProjectBuildTasks(String projectRoot) async {
final List<RunTask> buildTasks = [];
// 1. CMakeLists.txt
final cmakeFile = File(p.join(projectRoot, 'CMakeLists.txt'));
if (cmakeFile.existsSync()) {
final projectName = p.basename(projectRoot);
buildTasks.addAll([
RunTask(
id: 'detected_cmake_build_run',
name: 'CMake: Build & Run',
command: 'cmake -B build && cmake --build build && ./build/$projectName',
source: TaskSource.detected,
description: '配置、编译并尝试启动生成的目标程序',
icon: Icons.play_circle_outline,
),
RunTask(
id: 'detected_cmake_build',
name: 'CMake: Build Only',
command: 'cmake -B build && cmake --build build',
source: TaskSource.detected,
description: '仅执行 cmake 生成与构建',
icon: Icons.build_outlined,
),
RunTask(
id: 'detected_cmake_clean',
name: 'CMake: Clean',
command: 'rm -rf build',
source: TaskSource.detected,
description: '清理构建缓存目录',
icon: Icons.cleaning_services_outlined,
),
]);
}
// 2. Gradle (build.gradle / gradlew)
final gradlewFile = File(p.join(projectRoot, 'gradlew'));
final gradleFile = File(p.join(projectRoot, 'build.gradle'));
final gradleKtsFile = File(p.join(projectRoot, 'build.gradle.kts'));
if (gradlewFile.existsSync() || gradleFile.existsSync() || gradleKtsFile.existsSync()) {
final exec = gradlewFile.existsSync() ? './gradlew' : 'gradle';
buildTasks.addAll([
RunTask(
id: 'detected_gradle_run',
name: 'Gradle: Run',
command: '$exec run',
source: TaskSource.detected,
description: '执行应用程序主入口',
icon: Icons.play_circle_outline,
),
RunTask(
id: 'detected_gradle_assemble',
name: 'Gradle: Assemble Debug',
command: '$exec assembleDebug',
source: TaskSource.detected,
description: '构建调试输出包',
icon: Icons.build_outlined,
),
RunTask(
id: 'detected_gradle_build',
name: 'Gradle: Build',
command: '$exec build',
source: TaskSource.detected,
description: '执行完整构建与测试',
icon: Icons.done_all_outlined,
),
]);
}
// 3. Makefile
final makeFile = File(p.join(projectRoot, 'Makefile'));
if (makeFile.existsSync()) {
buildTasks.add(
const RunTask(
id: 'detected_make_default',
name: 'Make: Default',
command: 'make',
source: TaskSource.detected,
description: '执行默认 Makefile 构建目标',
icon: Icons.build_circle_outlined,
),
);
}
// 4. package.json (Node/Web)
final packageJsonFile = File(p.join(projectRoot, 'package.json'));
if (packageJsonFile.existsSync()) {
buildTasks.addAll([
const RunTask(
id: 'detected_npm_start',
name: 'NPM: Start',
command: 'npm start',
source: TaskSource.detected,
description: '启动 Node 服务或前端开发环境',
icon: Icons.rocket_launch_outlined,
),
const RunTask(
id: 'detected_npm_test',
name: 'NPM: Test',
command: 'npm test',
source: TaskSource.detected,
description: '执行 npm test 测试套件',
icon: Icons.checklist_outlined,
),
]);
}
// 5. Cargo.toml (Rust)
final cargoFile = File(p.join(projectRoot, 'Cargo.toml'));
if (cargoFile.existsSync()) {
buildTasks.addAll([
const RunTask(
id: 'detected_cargo_run',
name: 'Cargo: Run',
command: 'cargo run',
source: TaskSource.detected,
description: '编译并运行 Rust 项目',
icon: Icons.play_circle_outline,
),
const RunTask(
id: 'detected_cargo_build',
name: 'Cargo: Build',
command: 'cargo build',
source: TaskSource.detected,
description: '仅编译 Rust 项目',
icon: Icons.build_outlined,
),
]);
}
// 6. pubspec.yaml (Dart)
final pubspecFile = File(p.join(projectRoot, 'pubspec.yaml'));
if (pubspecFile.existsSync()) {
buildTasks.add(
const RunTask(
id: 'detected_dart_run',
name: 'Dart: Run',
command: 'dart run',
source: TaskSource.detected,
description: '启动 Dart 应用',
icon: Icons.play_circle_outline,
),
);
}
return buildTasks;
}
/// 探测单文件运行指令(基于当前打开的文件)
static RunTask? _detectSingleFileTask(String projectRoot, String currentFilePath) {
final file = File(currentFilePath);
if (!file.existsSync()) return null;
final fileName = p.basename(currentFilePath);
final ext = p.extension(currentFilePath).toLowerCase();
// 转换相对容器的路径(在容器内 /workspace 下执行)
String relPath = p.relative(currentFilePath, from: projectRoot).replaceAll(r'\', '/');
if (!relPath.startsWith('./') && !relPath.startsWith('/')) {
relPath = './$relPath';
}
switch (ext) {
case '.py':
return RunTask(
id: 'detected_single_python',
name: 'Python: $fileName',
command: 'python3 "$relPath"',
source: TaskSource.detected,
description: '运行当前 Python 脚本',
icon: Icons.code,
);
case '.c':
return RunTask(
id: 'detected_single_c',
name: 'GCC: $fileName',
command: 'gcc "$relPath" -o /tmp/a.out && /tmp/a.out',
source: TaskSource.detected,
description: '编译并执行当前 C 源文件',
icon: Icons.terminal,
);
case '.cpp':
case '.cc':
case '.cxx':
return RunTask(
id: 'detected_single_cpp',
name: 'G++: $fileName',
command: 'g++ "$relPath" -o /tmp/a.out && /tmp/a.out',
source: TaskSource.detected,
description: '编译并执行当前 C++ 源文件',
icon: Icons.terminal,
);
case '.sh':
case '.bash':
return RunTask(
id: 'detected_single_sh',
name: 'Shell: $fileName',
command: 'sh "$relPath"',
source: TaskSource.detected,
description: '运行当前 Shell 脚本',
icon: Icons.terminal,
);
case '.dart':
return RunTask(
id: 'detected_single_dart',
name: 'Dart: $fileName',
command: 'dart run "$relPath"',
source: TaskSource.detected,
description: '运行当前 Dart 文件',
icon: Icons.code,
);
case '.go':
return RunTask(
id: 'detected_single_go',
name: 'Go: $fileName',
command: 'go run "$relPath"',
source: TaskSource.detected,
description: '运行当前 Go 源文件',
icon: Icons.code,
);
case '.rs':
return RunTask(
id: 'detected_single_rust',
name: 'Rust: $fileName',
command: 'rustc "$relPath" -o /tmp/a.out && /tmp/a.out',
source: TaskSource.detected,
description: '编译并执行当前 Rust 源文件',
icon: Icons.code,
);
case '.js':
return RunTask(
id: 'detected_single_js',
name: 'Node: $fileName',
command: 'node "$relPath"',
source: TaskSource.detected,
description: '运行当前 JS 脚本',
icon: Icons.javascript,
);
default:
return null;
}
}
}
@@ -0,0 +1,97 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as p;
import '../models/run_task.dart';
/// 负责工程目录下 `.code_editor/run_tasks.json` 的读写与持久化管理
class RunTaskStorageService {
static const String configDirName = '.code_editor';
static const String configFileName = 'run_tasks.json';
/// 获取配置文件的绝对 File 对象
File getConfigFile(String projectRoot) {
return File(p.join(projectRoot, configDirName, configFileName));
}
/// 从工程的 `.code_editor/run_tasks.json` 中读取持久化的配置(包括自定义任务与最近运行任务ID)
Future<({List<RunTask> tasks, String? lastRunTaskId})> loadConfig(String projectRoot) async {
try {
final file = getConfigFile(projectRoot);
if (!await file.exists()) {
return (tasks: <RunTask>[], lastRunTaskId: null);
}
final content = await file.readAsString();
if (content.trim().isEmpty) {
return (tasks: <RunTask>[], lastRunTaskId: null);
}
final dynamic decoded = jsonDecode(content);
if (decoded is Map<String, dynamic>) {
final lastId = decoded['lastRunTaskId'] as String?;
final tasksRaw = decoded['tasks'];
List<RunTask> list = [];
if (tasksRaw is List) {
list = tasksRaw
.whereType<Map<String, dynamic>>()
.map((item) => RunTask.fromJson(item))
.toList();
}
return (tasks: list, lastRunTaskId: lastId);
} else if (decoded is List) {
final list = decoded
.whereType<Map<String, dynamic>>()
.map((item) => RunTask.fromJson(item))
.toList();
return (tasks: list, lastRunTaskId: null);
}
return (tasks: <RunTask>[], lastRunTaskId: null);
} catch (e) {
debugPrint('[RunTaskStorageService] 读取 run_tasks.json 失败: $e');
return (tasks: <RunTask>[], lastRunTaskId: null);
}
}
/// 兼容旧方法:直接返回 tasks
Future<List<RunTask>> loadTasks(String projectRoot) async {
final result = await loadConfig(projectRoot);
return result.tasks;
}
/// 将自定义任务列表与最近运行的任务ID持久化保存到 `.code_editor/run_tasks.json`
Future<void> saveConfig({
required String projectRoot,
required List<RunTask> tasks,
String? lastRunTaskId,
}) async {
try {
final file = getConfigFile(projectRoot);
final dir = file.parent;
if (!await dir.exists()) {
await dir.create(recursive: true);
}
final data = {
'version': '1.0',
if (lastRunTaskId != null && lastRunTaskId.isNotEmpty) 'lastRunTaskId': lastRunTaskId,
'tasks': tasks
.where((t) => t.source == TaskSource.custom)
.map((t) => t.toJson())
.toList(),
};
const encoder = JsonEncoder.withIndent(' ');
final jsonStr = encoder.convert(data);
await file.writeAsString(jsonStr, flush: true);
} catch (e) {
debugPrint('[RunTaskStorageService] 保存 run_tasks.json 失败: $e');
rethrow;
}
}
/// 兼容旧方法
Future<void> saveTasks(String projectRoot, List<RunTask> tasks) async {
await saveConfig(projectRoot: projectRoot, tasks: tasks);
}
}