skills/oh-pdd-code-generator/SKILL.md
基于设计文档自动生成鸿蒙系统代码框架,包括 IDL 接口定义、目录结构、头文件、源文件模板、构建配置等。生成前会分析 OpenHarmony 存量代码,学习现有代码风格和架构规范。适用于用户请求:(1) 生成代码框架, (2) 创建 IDL 接口, (3) 生成 BUILD.gn 配置, (4) 创建服务代码, (5) 初始化项目结构, (6) 参考现有代码风格。关键词:code generation, IDL, BUILD.gn, bundle.json, SA profile, OpenHarmony code style, 代码生成, 接口定义, 构建配置, OH代码风格
npx skillsauth add openharmonyinsight/openharmony-skills oh-pdd-code-generatorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
基于设计文档自动生成 OpenHarmony 系统代码框架,包括目录结构、IDL 接口、头文件、源文件、构建配置等。
提供设计文档路径:
基于 {功能设计文档} 生成代码框架
指定模块名称:
为 {模块名} 生成完整代码,基于 {功能设计文档}
在生成代码前,需要分析 OpenHarmony 存量代码,确保生成的代码符合现有代码风格和架构规范。
使用 Glob 工具发现现有模块的组织结构:
# 查找相似模块的目录结构
foundation/{subsystem}/*/services/
foundation/{subsystem}/*/interfaces/
foundation/{subsystem}/*/tests/
使用 Grep 工具搜索代码模式:
| 搜索目标 | 搜索关键字 | 提取信息 |
|----------|------------|----------|
| 类命名 | class.*Service | 服务类命名 |
| 命名空间 | namespace.* | 命名空间组织 |
| 日志标签 | HiLogLabel | 日志使用方式 |
| 错误码 | ERROR_.*=.*[0-9] | 错误码分配 |
| 宏定义 | #define.*_ENABLE | 条件编译开关 |
| 初始化函数 | bool Init\(\) | 初始化模式 |
| 清理函数 | void.*Release\(\) | 资源清理模式 |
使用 Read 工具读取相似模块的实现:
*_service.cpp*_provider.cppI*.idlBUILD.gn使用 Task 工具启动 Explore 代理:
OpenHarmony 标准目录结构:
foundation/{subsystem}/{part}/
├── bundle.json # 部件配置
├── BUILD.gn # 主构建文件
├── {part}.gni # 模块配置
├── services/
│ └── native/
│ ├── {sa_id}.json # SA 配置
│ ├── include/ # 对外头文件
│ └── src/ # 实现文件
├── interfaces/
│ ├── idl/ # IDL 接口
│ ├── innerkits/ # 内部 Kit
│ └── kits/ # 对外 Kit
└── tests/ # 测试代码
| 代码元素 | 风格规则 | 示例 |
|----------|----------|------|
| 类名 | 大驼峰 + 后缀 | DiskInfoService |
| 方法名 | 大驼峰 | GetDiskList() |
| 成员变量 | 小驼峰 + 下划线后缀 | mutex_, inited_ |
| 命名空间 | 小写双冒号分隔 | OHOS::DiskManagement |
| 文件名 | 小写下划线 | disk_info_service.cpp |
| 宏定义 | 大写下划线 | DISK_MANAGEMENT_FORMAT_ENABLE |
| 依赖 | external_deps 格式 | 用途 |
|------|-------------------|------|
| HiLog | hilog:libhilog | 日志输出 |
| c_utils | c_utils:utils | C 工具函数 |
| ipc_core | ipc_core:ipc_core | IPC 通信 |
| AccessToken | access_token:libaccesstoken | 权限管理 |
| HiSysEvent | hisysevent:libhisysevent | 事件上报 |
| HiTrace | hitrace_native:hitrace_meter | 链路追踪 |
自动创建符合 OpenHarmony 规范的目录结构:
disk_management/
├── bundle.json # 部件配置
├── BUILD.gn # 主构建配置
├── disk_management.gni # 模块配置
├── services/
│ ├── native/
│ │ ├── 5001.json # SA 配置文件
│ │ ├── disk_management.cfg # Init 配置文件
│ │ ├── include/ # 头文件目录
│ │ └── src/ # 源文件目录
│ └── disk_info_service/ # 各服务目录
│ ├── include/
│ └── src/
├── interfaces/
│ ├── idl/OHOS/DiskManagement/ # IDL 接口定义
│ ├── innerkits/native/ # Native Kit
│ └── kits/js/ # JS Kit
└── tests/ # 测试目录
生成 IDL 接口定义文件,格式:
/* Copyright (c) 2026 Huawei Device Co., Ltd. */
package OHOS.DiskManagement;
import "DiskInfoTypes.idl";
interface IDiskInfoService {
/// 获取系统所有磁盘列表
GetDiskList(): DiskInfo[];
/// 获取指定磁盘的详细信息
GetDiskInfo([in] string diskId): DiskInfo;
/// 注册磁盘变化监听器
RegisterChangeListener([in] IDiskChangeListener listener): void;
};
生成符合 OpenHarmony 编码规范的头文件:
/* Copyright (c) 2026 Huawei Device Co., Ltd. */
#ifndef DISK_INFO_SERVICE_H
#define DISK_INFO_SERVICE_H
#include <string>
#include <vector>
#include <mutex>
namespace OHOS {
namespace DiskManagement {
class DiskInfoService {
public:
DiskInfoService();
~DiskInfoService();
bool Init();
void Release();
std::vector<DiskInfo> GetDiskList();
DiskInfo GetDiskInfo(const std::string& diskId);
private:
std::mutex mutex_;
bool inited_;
};
} // namespace DiskManagement
} // namespace OHOS
#endif // DISK_INFO_SERVICE_H
生成基础实现框架:
/* Copyright (c) 2026 Huawei Device Co., Ltd. */
#include "disk_info_service.h"
#include "hilog/log.h"
namespace OHOS {
namespace DiskManagement {
static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD00430D, "DiskInfoService" };
DiskInfoService::DiskInfoService() : inited_(false)
{
HiLog::Info(LABEL, "DiskInfoService constructed");
}
DiskInfoService::~DiskInfoService()
{
Release();
}
bool DiskInfoService::Init()
{
std::lock_guard<std::mutex> lock(mutex_);
if (inited_) {
HiLog::Warn(LABEL, "Already initialized");
return true;
}
// TODO: 初始化逻辑
inited_ = true;
return true;
}
void DiskInfoService::Release()
{
std::lock_guard<std::mutex> lock(mutex_);
if (!inited_) {
return;
}
// TODO: 清理资源
inited_ = false;
}
} // namespace DiskManagement
} // namespace OHOS
生成构建配置文件:
import("//build/ohos.gni")
import("//build/test.gni")
import("//foundation/filemanagement/disk_management/disk_management.gni")
ohos_shared_library("disk_management_sa") {
subsystem_name = "filemanagement"
part_name = "disk_management"
sources = [
"src/disk_management_service.cpp",
"src/main.cpp",
]
include_dirs = [
"include",
"${disk_management_path}/include",
"//utils/native/base/include",
]
deps = [
":disk_management_idl",
"//foundation/hiviewdfx/hilog/native/framework:libhilog",
]
external_deps = [
"c_utils:c_utils",
]
branch_protector_ret = "pac_ret"
sanitize = {
integer_overflow = true
cfi = true
debug = false
}
}
ohos_unittest("disk_management_unittest") {
subsystem_name = "filemanagement"
part_name = "disk_management"
test_type = "unittest"
test_time_out = 300
sources = [
"test/disk_management_test.cpp",
]
deps = [
":disk_management_sa",
]
external_deps = [
"googletest:gmock_main",
"googletest:gtest_main",
]
cflags_cc = [ "--coverage" ]
ldflags = [ "--coverage" ]
}
{
"sa-id": 5001,
"sa-name": "disk_management",
"run-on-create": true,
"auto-start": true,
"start-mode": "boot",
"process": "disk_management_service",
"dump-level": "control",
"critical": [1, 4, 240],
"libpath": "libdisk_management_sa.z.so"
}
{
"name": "disk_management",
"description": "HM Desktop Disk Management Service",
"version": "1.0.0",
"component": {
"name": "disk_management",
"subsystem": "filemanagement",
"syscap": [
"SystemCapability.DiskManagement.DiskInfo",
"SystemCapability.DiskManagement.Format"
]
}
}
| 设计文档术语 | 代码术语 | 规则 | |--------------|----------|------| | 服务名称 | 类名 | 大驼峰 + Service 后缀 | | 模块名称 | 命名空间 | 小写下划线分隔 | | 接口名称 | IDL 接口 | I + 大驼峰 | | 数据结构 | struct | 大驼峰 | | 方法名 | 函数名 | 大驼峰 | | 成员变量 | 成员变量 | 小驼峰 + 下划线后缀 |
生成内容:
示例:DiskInfoService
生成内容:
示例:FormatManagerService, RepairManagerService
生成内容:
示例:DiskChangeListener, FormatProgressListener
生成完整代码框架:
基于 {功能设计文档} 为 {模块名} 模块生成完整代码
仅生成 IDL 接口:
只生成 IDL 接口文件,基于 {功能设计文档}
指定 SA ID:
生成代码,SA ID 使用 {SA_ID},基于 {功能设计文档}
预览模式:
预览将要生成的文件,不实际创建,基于 {功能设计文档}
指定输出目录:
生成代码到 ./code_output 目录,基于 {功能设计文档}
生成的代码支持功能条件编译:
# 在 .gni 文件中定义
declare_args() {
disk_management_enable_format = true
disk_management_enable_repair = true
}
# 在 BUILD.gn 中使用
if (disk_management_enable_format) {
sources += [ "src/format_service.cpp" ]
defines += [ "DISK_MANAGEMENT_FORMAT_ENABLE" ]
}
同时生成单元测试框架:
/* Copyright (c) 2026 Huawei Device Co., Ltd. */
#include <gtest/gtest.h>
#include "disk_info_service.h"
namespace OHOS {
namespace DiskManagement {
class DiskInfoServiceTest : public testing::Test {
protected:
void SetUp() override {
service_ = std::make_unique<DiskInfoService>();
ASSERT_TRUE(service_->Init());
}
void TearDown() override {
service_->Release();
}
std::unique_ptr<DiskInfoService> service_;
};
TEST_F(DiskInfoServiceTest, GetDiskList) {
auto disks = service_->GetDiskList();
EXPECT_GE(disks.size(), 0u);
}
} // namespace DiskManagement
} // namespace OHOS
| 错误类型 | 处理方式 | |----------|----------| | 设计文档格式错误 | 提示具体缺失章节 | | IDL 语法错误 | 标记错误行号,提供正确格式 | | 目录创建失败 | 检查权限,提示用户手动创建 | | 文件已存在 | 使用 --force 覆盖或跳过 |
生成完成后,输出文件列表:
[INFO] 代码框架生成完成!
[INFO]
[INFO] 生成的文件:
[INFO] 目录: disk_management/
[INFO] 配置: bundle.json, BUILD.gn, disk_management.gni
[INFO] SA配置: services/native/5001.json, disk_management.cfg
[INFO] IDL: interfaces/idl/OHOS/DiskManagement/*.idl
[INFO] 头文件: services/native/include/*.h
[INFO] 源文件: services/native/src/*.cpp
[INFO] 测试: test/unittest/*_test.cpp
[INFO]
[INFO] 总计: 15 个文件, 5 个目录
development
Run local code quality checks covering a subset of OpenHarmony gate CI (copyright, CodeArts C/C++) plus additional local checks (pylint/flake8, shellcheck/bashate, gn format). Use before committing to reduce gate failures. Triggers on: /oh-precommit-codecheck, "门禁检查", "门禁预检", "检查代码", "run codecheck", "check code quality", "lint my code", "代码检查", or after completing code implementation. WHEN to use: before git commit, before creating PR, after modifying C/C++/Python/Shell/GN files, when gate CI fails with codecheck defects, or when you want to preview what gate will flag.
development
OpenHarmony PR full lifecycle workflow. Five modes: - Commit: standardized commit with DCO sign-off and Issue linking - Create PR: commit + push to fork + create Issue + create PR on upstream - Fix Codecheck: fetch gate CI codecheck defects from a PR and auto-fix them - Review PR: fetch a PR's changes to local for code review - Fix Review: fetch unresolved review comments from a PR and auto-fix them Triggers on: /oh-pr-workflow, "提交代码", "创建PR", "提个PR", "commit", "修复告警", "修复门禁", "修复codecheck", "fix codecheck", "review pr", "review这个pr", "看下这个pr", "检视pr", "修复review", "修复检视意见", "fix review", or a GitCode PR URL with fix/review intent.
testing
分析 HM Desktop PRD 文档,提取需求信息、验证完整性、检查章节顺序(需求来源→需求背景→需求价值分析→竞品分析→需求描述)、检查 KEP 定义、检测需求冲突并生成结构化分析报告。适用于用户请求:(1) 分析或审查 PRD 文档, (2) 从需求中提取 KEP 列表, (3) 检查 PRD 完整性或一致性, (4) 将需求映射到模块架构, (5) 验证 PRD 格式合规性, (6) 验证竞品分析章节完整性。关键词:PRD分析, requirement extraction, KEP验证, completeness check, chapter order validation, 竞品分析检查, analyze PRD, 需求提取, 完整性检查, 章节顺序验证
development
基于 PRD 文档自动生成鸿蒙系统设计文档,包括架构设计文档和功能设计文档。生成前会分析 OpenHarmony 存量代码结构,确保与现有架构兼容。架构设计文档第2章必须为竞品方案分析,位于需求背景之后。适用于用户请求:(1) 生成架构设计文档, (2) 生成功能设计文档, (3) 从 PRD 生成设计文档, (4) 创建系统架构设计, (5) 编写功能规格说明, (6) 分析 OH 代码结构。关键词:architecture design, functional design, design doc, 竞品方案分析, OpenHarmony code analysis, 架构设计, 功能设计, 设计文档生成, OH代码分析, analyze codebase, competitor analysis