installerbuilder/tasks/task09-平台适配接口定义.md

165 lines
5.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 任务09平台适配接口定义
## 任务描述
定义平台适配接口,用于处理不同操作系统和架构的特定逻辑。平台适配接口将抽象平台差异,为构建过程提供统一的平台操作接口,使得安装包构建能够适应不同的目标平台。
## 实现步骤
1. 定义平台信息结构:
- 在`internal/platform`包中创建`platform.go`文件
- 定义PlatformInfo结构体包含平台特定信息
```go
type PlatformInfo struct {
Name string // 平台名称 (windows, linux)
Version string // 平台版本
Arch string // 平台架构 (amd64, arm64)
DefaultInstallDir string // 默认安装目录
PathSeparator string // 路径分隔符
ExecutableExt string // 可执行文件扩展名
DynamicLibExt string // 动态库扩展名
StaticLibExt string // 静态库扩展名
ScriptExt map[string]string // 脚本扩展名映射
}
```
2. 定义平台适配器接口:
- 创建PlatformAdapter接口定义平台操作方法
```go
type PlatformAdapter interface {
// GetPlatformInfo 获取平台信息
GetPlatformInfo() PlatformInfo
// PrepareEnvironment 准备构建环境
PrepareEnvironment(config *config.Config, target config.Target) error
// CleanupEnvironment 清理构建环境
CleanupEnvironment(config *config.Config, target config.Target) error
// GetDefaultInstallDir 获取默认安装目录
GetDefaultInstallDir(appName string) string
// NormalizePath 规范化路径
NormalizePath(path string) string
// IsPathAbsolute 判断路径是否为绝对路径
IsPathAbsolute(path string) bool
// JoinPaths 连接路径
JoinPaths(elem ...string) string
// GetTempDir 获取临时目录
GetTempDir() string
// GetOSEnv 获取操作系统环境变量
GetOSEnv(name string) string
// SetFilePermissions 设置文件权限
SetFilePermissions(path string, perm string) error
}
```
3. 定义平台常量:
- 创建平台相关的常量定义:
```go
const (
// 平台名称
PlatformWindows = "windows"
PlatformLinux = "linux"
PlatformMacOS = "macos"
// 架构名称
ArchAMD64 = "amd64"
ArchARM64 = "arm64"
ArchX86 = "386"
ArchARM = "arm"
)
```
4. 定义平台工厂接口:
- 创建PlatformAdapterFactory接口用于创建平台适配器
```go
type PlatformAdapterFactory interface {
// CreateAdapter 创建平台适配器
CreateAdapter(platform string, arch string) (PlatformAdapter, error)
// GetSupportedPlatforms 获取支持的平台
GetSupportedPlatforms() []string
// GetSupportedArchs 获取支持的架构
GetSupportedArchs(platform string) []string
}
```
5. 实现默认平台工厂:
- 创建DefaultPlatformAdapterFactory结构体实现PlatformAdapterFactory接口
```go
type DefaultPlatformAdapterFactory struct {
logger log.Logger
}
```
- 实现CreateAdapter方法根据平台和架构创建适配器
6. 定义平台检测接口:
- 创建PlatformDetector接口用于检测当前平台
```go
type PlatformDetector interface {
// DetectCurrentPlatform 检测当前平台
DetectCurrentPlatform() (string, string, error)
// IsCurrentPlatform 判断是否为当前平台
IsCurrentPlatform(platform string, arch string) bool
}
```
- 实现DefaultPlatformDetector使用runtime包检测当前平台
7. 定义平台兼容性检查:
- 创建兼容性检查方法,用于验证目标平台是否受支持
- 实现跨平台构建的兼容性检查
## 单元测试要求
1. 测试平台信息结构:
- 验证结构体字段
- 测试序列化和反序列化
2. 测试平台适配器接口:
- 验证接口方法定义
- 测试接口文档
3. 测试平台常量:
- 验证常量定义
- 测试常量使用
4. 测试平台工厂接口:
- 验证接口方法定义
- 测试默认工厂实现
5. 测试平台检测接口:
- 验证接口方法定义
- 测试默认检测器实现
6. 测试平台兼容性检查:
- 验证兼容性检查方法
- 测试跨平台构建兼容性
## 依赖关系
- 依赖任务01项目初始化
- 依赖任务02日志系统实现
- 依赖任务03错误处理框架
- 依赖任务05配置模型定义
- 被任务10Windows平台适配器和任务11Linux平台适配器依赖
## 完成标准
1. PlatformInfo结构体已定义并文档完善
2. PlatformAdapter接口已定义并文档完善
3. 平台常量已定义
4. PlatformAdapterFactory接口已定义并文档完善
5. DefaultPlatformAdapterFactory已实现
6. PlatformDetector接口已定义并文档完善
7. DefaultPlatformDetector已实现
8. 平台兼容性检查方法已实现
9. 所有单元测试通过
10. 代码符合项目的Go语言开发规范
11. 接口设计满足扩展性和可测试性要求