✨ feat: 实现扩展模块和设置命令
This commit is contained in:
146
aide-program/aide/env/modules/android.py
vendored
Normal file
146
aide-program/aide/env/modules/android.py
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
"""Android 开发环境检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class AndroidModule(BaseModule):
|
||||
"""Android 开发环境检测模块(类型A:无需配置)。
|
||||
|
||||
检测 Android SDK 和相关工具:
|
||||
- ANDROID_HOME / ANDROID_SDK_ROOT 环境变量
|
||||
- Android SDK 目录结构
|
||||
- 关键工具:adb, aapt, sdkmanager
|
||||
"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="android",
|
||||
description="Android SDK",
|
||||
capabilities=["check"],
|
||||
requires_config=False,
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 Android 开发环境。"""
|
||||
# 检测 ANDROID_HOME 或 ANDROID_SDK_ROOT
|
||||
sdk_root = self._get_sdk_root()
|
||||
if not sdk_root:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="ANDROID_HOME 或 ANDROID_SDK_ROOT 未设置",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
sdk_path = Path(sdk_root)
|
||||
if not sdk_path.exists():
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"Android SDK 目录不存在: {sdk_root}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 检测关键目录
|
||||
platform_tools = sdk_path / "platform-tools"
|
||||
build_tools = sdk_path / "build-tools"
|
||||
platforms = sdk_path / "platforms"
|
||||
|
||||
missing = []
|
||||
if not platform_tools.exists():
|
||||
missing.append("platform-tools")
|
||||
if not build_tools.exists():
|
||||
missing.append("build-tools")
|
||||
if not platforms.exists():
|
||||
missing.append("platforms")
|
||||
|
||||
if missing:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"缺少 SDK 组件: {', '.join(missing)}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 获取版本信息
|
||||
build_tools_versions = self._get_build_tools_versions(build_tools)
|
||||
platform_versions = self._get_platform_versions(platforms)
|
||||
|
||||
# 检测 adb
|
||||
adb_version = self._get_adb_version(platform_tools)
|
||||
|
||||
# 构建版本信息
|
||||
version_info = []
|
||||
if adb_version:
|
||||
version_info.append(f"adb {adb_version}")
|
||||
if build_tools_versions:
|
||||
version_info.append(f"build-tools {build_tools_versions[0]}")
|
||||
if platform_versions:
|
||||
version_info.append(f"API {platform_versions[0]}")
|
||||
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=sdk_root,
|
||||
message=", ".join(version_info) if version_info else None,
|
||||
)
|
||||
|
||||
def _get_sdk_root(self) -> str | None:
|
||||
"""获取 Android SDK 根目录。"""
|
||||
return os.environ.get("ANDROID_HOME") or os.environ.get("ANDROID_SDK_ROOT")
|
||||
|
||||
def _get_adb_version(self, platform_tools: Path) -> str | None:
|
||||
"""获取 adb 版本。"""
|
||||
adb_path = platform_tools / "adb"
|
||||
if not adb_path.exists():
|
||||
return None
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[str(adb_path), "version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
# Android Debug Bridge version 1.0.41
|
||||
lines = result.stdout.strip().split("\n")
|
||||
if lines:
|
||||
parts = lines[0].split()
|
||||
if len(parts) >= 5:
|
||||
return parts[4]
|
||||
return None
|
||||
except (subprocess.TimeoutExpired, Exception):
|
||||
return None
|
||||
|
||||
def _get_build_tools_versions(self, build_tools: Path) -> list[str]:
|
||||
"""获取已安装的 build-tools 版本列表(降序)。"""
|
||||
if not build_tools.exists():
|
||||
return []
|
||||
|
||||
versions = []
|
||||
for item in build_tools.iterdir():
|
||||
if item.is_dir() and item.name[0].isdigit():
|
||||
versions.append(item.name)
|
||||
|
||||
return sorted(versions, reverse=True)
|
||||
|
||||
def _get_platform_versions(self, platforms: Path) -> list[str]:
|
||||
"""获取已安装的 platform 版本列表(降序)。"""
|
||||
if not platforms.exists():
|
||||
return []
|
||||
|
||||
versions = []
|
||||
for item in platforms.iterdir():
|
||||
if item.is_dir() and item.name.startswith("android-"):
|
||||
api_level = item.name.replace("android-", "")
|
||||
versions.append(api_level)
|
||||
|
||||
return sorted(versions, key=lambda x: int(x) if x.isdigit() else 0, reverse=True)
|
||||
|
||||
|
||||
module = AndroidModule()
|
||||
132
aide-program/aide/env/modules/flutter.py
vendored
Normal file
132
aide-program/aide/env/modules/flutter.py
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
"""Flutter SDK 检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class FlutterModule(BaseModule):
|
||||
"""Flutter SDK 检测模块(类型A:无需配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="flutter",
|
||||
description="Flutter SDK",
|
||||
capabilities=["check"],
|
||||
requires_config=False,
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 Flutter SDK。"""
|
||||
flutter_version = self._get_flutter_version()
|
||||
dart_version = self._get_dart_version()
|
||||
|
||||
if not flutter_version:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="flutter 未安装",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 检查最低版本要求(如果配置了)
|
||||
min_version = config.get("min_version")
|
||||
if min_version:
|
||||
if not self._version_satisfies(flutter_version, min_version):
|
||||
return CheckResult(
|
||||
success=False,
|
||||
version=flutter_version,
|
||||
message=f"版本不足,要求>={min_version},当前 {flutter_version}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 构建版本信息
|
||||
extra = f"dart {dart_version}" if dart_version else ""
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=flutter_version,
|
||||
message=extra,
|
||||
)
|
||||
|
||||
def _get_flutter_version(self) -> str | None:
|
||||
"""获取 Flutter 版本。"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["flutter", "--version", "--machine"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
# 尝试解析 JSON 输出
|
||||
import json
|
||||
try:
|
||||
data = json.loads(result.stdout)
|
||||
return data.get("frameworkVersion")
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
# 回退到普通版本输出
|
||||
result = subprocess.run(
|
||||
["flutter", "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
# Flutter 3.16.0 • channel stable • ...
|
||||
output = result.stdout.strip()
|
||||
lines = output.split("\n")
|
||||
if lines:
|
||||
parts = lines[0].split()
|
||||
if len(parts) >= 2 and parts[0] == "Flutter":
|
||||
return parts[1]
|
||||
return None
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return None
|
||||
|
||||
def _get_dart_version(self) -> str | None:
|
||||
"""获取 Dart 版本。"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["dart", "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
# Dart SDK version: 3.2.0 (stable) ...
|
||||
output = result.stdout.strip()
|
||||
if not output:
|
||||
output = result.stderr.strip() # dart 有时输出到 stderr
|
||||
parts = output.split()
|
||||
for i, part in enumerate(parts):
|
||||
if part == "version:" and i + 1 < len(parts):
|
||||
return parts[i + 1]
|
||||
return None
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return None
|
||||
|
||||
def _version_satisfies(self, current: str, minimum: str) -> bool:
|
||||
"""检查版本是否满足最低要求。"""
|
||||
current_parts = self._parse_version(current)
|
||||
min_parts = self._parse_version(minimum)
|
||||
return current_parts >= min_parts
|
||||
|
||||
@staticmethod
|
||||
def _parse_version(version: str) -> tuple[int, ...]:
|
||||
"""解析版本号字符串。"""
|
||||
parts = []
|
||||
for part in version.split("."):
|
||||
try:
|
||||
parts.append(int(part))
|
||||
except ValueError:
|
||||
break
|
||||
return tuple(parts)
|
||||
|
||||
|
||||
module = FlutterModule()
|
||||
93
aide-program/aide/env/modules/node.py
vendored
Normal file
93
aide-program/aide/env/modules/node.py
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
"""Node.js 环境检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class NodeModule(BaseModule):
|
||||
"""Node.js 检测模块(类型A:无需配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="node",
|
||||
description="Node.js 运行时",
|
||||
capabilities=["check"],
|
||||
requires_config=False,
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 Node.js 版本。"""
|
||||
node_version = self._get_version("node")
|
||||
npm_version = self._get_version("npm")
|
||||
|
||||
if not node_version:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="node 未安装",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 检查最低版本要求(如果配置了)
|
||||
min_version = config.get("min_version")
|
||||
if min_version:
|
||||
if not self._version_satisfies(node_version, min_version):
|
||||
return CheckResult(
|
||||
success=False,
|
||||
version=node_version,
|
||||
message=f"版本不足,要求>={min_version},当前 {node_version}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 构建版本信息
|
||||
extra = f"npm {npm_version}" if npm_version else "npm 未安装"
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=node_version,
|
||||
message=extra,
|
||||
)
|
||||
|
||||
def _get_version(self, cmd: str) -> str | None:
|
||||
"""获取命令版本。"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[cmd, "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
# node: v20.10.0 -> 20.10.0
|
||||
# npm: 10.2.3 -> 10.2.3
|
||||
output = result.stdout.strip()
|
||||
if output.startswith("v"):
|
||||
output = output[1:]
|
||||
return output
|
||||
return None
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return None
|
||||
|
||||
def _version_satisfies(self, current: str, minimum: str) -> bool:
|
||||
"""检查版本是否满足最低要求。"""
|
||||
current_parts = self._parse_version(current)
|
||||
min_parts = self._parse_version(minimum)
|
||||
return current_parts >= min_parts
|
||||
|
||||
@staticmethod
|
||||
def _parse_version(version: str) -> tuple[int, ...]:
|
||||
"""解析版本号字符串。"""
|
||||
parts = []
|
||||
for part in version.split("."):
|
||||
try:
|
||||
parts.append(int(part))
|
||||
except ValueError:
|
||||
break
|
||||
return tuple(parts)
|
||||
|
||||
|
||||
module = NodeModule()
|
||||
141
aide-program/aide/env/modules/node_deps.py
vendored
Normal file
141
aide-program/aide/env/modules/node_deps.py
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
"""Node.js 项目依赖检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class NodeDepsModule(BaseModule):
|
||||
"""Node.js 项目依赖检测模块(类型B:需要配置)。
|
||||
|
||||
支持多种包管理器:npm, pnpm, yarn, bun
|
||||
自动根据锁文件检测包管理器类型
|
||||
"""
|
||||
|
||||
# 锁文件到包管理器的映射
|
||||
LOCK_FILES = {
|
||||
"pnpm-lock.yaml": "pnpm",
|
||||
"yarn.lock": "yarn",
|
||||
"bun.lockb": "bun",
|
||||
"package-lock.json": "npm",
|
||||
}
|
||||
|
||||
# 包管理器安装命令
|
||||
INSTALL_COMMANDS = {
|
||||
"npm": ["npm", "install"],
|
||||
"pnpm": ["pnpm", "install"],
|
||||
"yarn": ["yarn", "install"],
|
||||
"bun": ["bun", "install"],
|
||||
}
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="node_deps",
|
||||
description="Node.js 项目依赖",
|
||||
capabilities=["check", "ensure"],
|
||||
requires_config=True,
|
||||
config_keys=["path"],
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 Node.js 项目依赖。"""
|
||||
project_path = root / config["path"]
|
||||
|
||||
# 检测 package.json 是否存在
|
||||
package_json = project_path / "package.json"
|
||||
if not package_json.exists():
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"package.json 不存在: {config['path']}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 检测 node_modules 是否存在
|
||||
node_modules = project_path / "node_modules"
|
||||
if not node_modules.exists():
|
||||
manager = self._detect_manager(project_path, config)
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"node_modules 不存在",
|
||||
can_ensure=True,
|
||||
)
|
||||
|
||||
# 检测包管理器
|
||||
manager = self._detect_manager(project_path, config)
|
||||
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=config["path"],
|
||||
message=manager,
|
||||
)
|
||||
|
||||
def ensure(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""安装 Node.js 项目依赖。"""
|
||||
project_path = root / config["path"]
|
||||
manager = self._detect_manager(project_path, config)
|
||||
|
||||
# 检测包管理器是否已安装
|
||||
if not self._is_manager_installed(manager):
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"{manager} 未安装",
|
||||
)
|
||||
|
||||
# 运行安装命令
|
||||
install_cmd = self.INSTALL_COMMANDS.get(manager, ["npm", "install"])
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
install_cmd,
|
||||
cwd=project_path,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=config["path"],
|
||||
message=f"已安装 ({manager})",
|
||||
)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
error_msg = exc.stderr.decode() if exc.stderr else str(exc)
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"安装失败: {error_msg[:100]}",
|
||||
)
|
||||
|
||||
def _detect_manager(self, project_path: Path, config: dict[str, Any]) -> str:
|
||||
"""检测包管理器类型。
|
||||
|
||||
优先使用配置指定的 manager,否则根据锁文件自动检测。
|
||||
"""
|
||||
# 优先使用配置指定的 manager
|
||||
if "manager" in config:
|
||||
return config["manager"]
|
||||
|
||||
# 根据锁文件检测
|
||||
for lock_file, manager in self.LOCK_FILES.items():
|
||||
if (project_path / lock_file).exists():
|
||||
return manager
|
||||
|
||||
# 默认使用 npm
|
||||
return "npm"
|
||||
|
||||
def _is_manager_installed(self, manager: str) -> bool:
|
||||
"""检测包管理器是否已安装。"""
|
||||
try:
|
||||
subprocess.run(
|
||||
[manager, "--version"],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
return True
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return False
|
||||
|
||||
|
||||
module = NodeDepsModule()
|
||||
98
aide-program/aide/env/modules/rust.py
vendored
Normal file
98
aide-program/aide/env/modules/rust.py
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
"""Rust 工具链检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class RustModule(BaseModule):
|
||||
"""Rust 工具链检测模块(类型A:无需配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="rust",
|
||||
description="Rust 工具链",
|
||||
capabilities=["check"],
|
||||
requires_config=False,
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 Rust 工具链(rustc 和 cargo)。"""
|
||||
rustc_version = self._get_version("rustc")
|
||||
cargo_version = self._get_version("cargo")
|
||||
|
||||
if not rustc_version:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="rustc 未安装",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
if not cargo_version:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="cargo 未安装",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
# 检查最低版本要求(如果配置了)
|
||||
min_version = config.get("min_version")
|
||||
if min_version:
|
||||
if not self._version_satisfies(rustc_version, min_version):
|
||||
return CheckResult(
|
||||
success=False,
|
||||
version=rustc_version,
|
||||
message=f"版本不足,要求>={min_version},当前 {rustc_version}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=rustc_version,
|
||||
message=f"cargo {cargo_version}",
|
||||
)
|
||||
|
||||
def _get_version(self, cmd: str) -> str | None:
|
||||
"""获取命令版本。"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[cmd, "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
# rustc 1.75.0 (xxx) -> 1.75.0
|
||||
# cargo 1.75.0 (xxx) -> 1.75.0
|
||||
output = result.stdout.strip()
|
||||
parts = output.split()
|
||||
if len(parts) >= 2:
|
||||
return parts[1]
|
||||
return None
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return None
|
||||
|
||||
def _version_satisfies(self, current: str, minimum: str) -> bool:
|
||||
"""检查版本是否满足最低要求。"""
|
||||
current_parts = self._parse_version(current)
|
||||
min_parts = self._parse_version(minimum)
|
||||
return current_parts >= min_parts
|
||||
|
||||
@staticmethod
|
||||
def _parse_version(version: str) -> tuple[int, ...]:
|
||||
"""解析版本号字符串。"""
|
||||
parts = []
|
||||
for part in version.split("."):
|
||||
try:
|
||||
parts.append(int(part))
|
||||
except ValueError:
|
||||
break
|
||||
return tuple(parts)
|
||||
|
||||
|
||||
module = RustModule()
|
||||
Reference in New Issue
Block a user