Skip to content

Commit f9098a8

Browse files
ci: 在 PR 评论中添加 API 文档预览diff (#13)
Co-authored-by: 黎伟杰 <674416404@qq.com>
1 parent b6db37f commit f9098a8

11 files changed

Lines changed: 519 additions & 330 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env dart
2+
/// CI 专用:生成 PR 文档 diff 评论 Markdown。
3+
///
4+
/// 用法(在 tdesign-flutter 根目录):
5+
/// dart run <tools-repo>/.github/scripts/build_doc_diff_comment.dart \
6+
/// --out /path/to/doc-diff-comment.md
7+
import 'dart:io';
8+
9+
import 'package:tdesign_flutter_tools/doc_diff_comment.dart';
10+
11+
Future<void> main(List<String> args) async {
12+
String? out;
13+
var repo = '.';
14+
15+
for (var i = 0; i < args.length; i++) {
16+
switch (args[i]) {
17+
case '--out':
18+
out = args[++i];
19+
case '--repo':
20+
repo = args[++i];
21+
}
22+
}
23+
if (out == null) {
24+
stderr.writeln('用法: build_doc_diff_comment.dart --out <file> [--repo <flutter-root>]');
25+
exit(1);
26+
}
27+
28+
await DocDiffComment(repoRoot: repo, outPath: out).write();
29+
}

.github/scripts/cross_platform_verify.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ void main(List<String> args) async {
1818
print('Binary: $binaryAbsPath');
1919
print('Repo: $componentDir');
2020

21-
// 验证二进制文件存在
2221
if (!File(binaryAbsPath).existsSync()) {
2322
print('ERROR: Binary not found: $binaryAbsPath');
2423
exit(1);
2524
}
2625

27-
// 用一个真实的 dart 文件测试 generate 命令
2826
final testFile = '$componentDir/lib/src/components/button/t_button.dart';
2927
if (!File(testFile).existsSync()) {
3028
print('ERROR: Test file not found: $testFile');
@@ -52,7 +50,6 @@ void main(List<String> args) async {
5250
exit(result.exitCode);
5351
}
5452

55-
// 验证输出文件生成
5653
final outputFile = File('${outputDir}button_api.md');
5754
if (outputFile.existsSync()) {
5855
print('\nSUCCESS: button_api.md generated (${outputFile.lengthSync()} bytes)');
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env dart
2+
/// CI 专用:在克隆的 tdesign-component 工作区批量重生成 API md(供 doc-diff 对比)。
3+
/// 不在 bin/main.dart 暴露;tdesign-flutter 本地维护请用 generate / update。
4+
///
5+
/// 用法(在 tdesign-component 目录):
6+
/// dart run <tools-repo>/.github/scripts/regenerate_api_for_diff.dart
7+
import 'dart:io';
8+
9+
import 'package:path/path.dart';
10+
import 'package:tdesign_flutter_tools/model.dart';
11+
import 'package:tdesign_flutter_tools/smart_create.dart';
12+
13+
Future<void> main(List<String> args) async {
14+
final componentRoot = Directory.current.path;
15+
final folders = args.isEmpty
16+
? <String>[]
17+
: args.first.split(',').map((s) => s.trim()).where((s) => s.isNotEmpty).toList();
18+
19+
final componentsRoot = Directory(join(componentRoot, 'lib/src/components'));
20+
if (!componentsRoot.existsSync()) {
21+
stderr.writeln('ERROR: 请在 tdesign-component 根目录执行,且存在 lib/src/components');
22+
exit(1);
23+
}
24+
25+
print('${DateTime.now().toLocal()} [CI] 批量生成 API 文档(仅用于 diff 预览)...');
26+
var count = 0;
27+
final base = componentRoot.endsWith(Platform.pathSeparator)
28+
? componentRoot
29+
: '$componentRoot${Platform.pathSeparator}';
30+
31+
for (final entity in componentsRoot.listSync()) {
32+
if (entity is! Directory) continue;
33+
final folder = basename(entity.path);
34+
if (folders.isNotEmpty && !folders.contains(folder)) continue;
35+
if (!File(join(entity.path, 't_$folder.dart')).existsSync()) continue;
36+
37+
final className = _folderToMainClassName(folder);
38+
final relFile = 'lib/src/components/$folder/t_$folder.dart';
39+
await SmartCreator(
40+
isFileMode: true,
41+
onlyApi: true,
42+
nameList: [className],
43+
basePath: base,
44+
path: relFile,
45+
output: 'example/assets/api/',
46+
folderName: folder,
47+
commandInfo: CommandInfo()
48+
..file = relFile
49+
..folderName = folder
50+
..widgetNames = className
51+
..isOnlyApi = true,
52+
).run();
53+
count++;
54+
}
55+
print('${DateTime.now().toLocal()} 完成,共 $count 个组件');
56+
}
57+
58+
String _folderToMainClassName(String folder) {
59+
final camel = folder.split('_').map((s) {
60+
if (s.isEmpty) return s;
61+
return '${s[0].toUpperCase()}${s.substring(1)}';
62+
}).join();
63+
return 'T$camel';
64+
}

.github/workflows/build-multi-platform.yml

Lines changed: 70 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,134 +2,129 @@ name: Build Multi-Platform Binaries
22

33
on:
44
pull_request:
5+
branches: [develop, main]
56
types: [opened, synchronize, reopened]
67

8+
concurrency:
9+
group: pr-binaries-${{ github.event.pull_request.number }}
10+
cancel-in-progress: true
11+
12+
permissions:
13+
contents: read
14+
pull-requests: write
15+
716
jobs:
817
build-macos-intel:
918
runs-on: macos-latest
10-
outputs:
11-
artifact_id: ${{ steps.upload.outputs.artifact-id }}
12-
artifact_url: ${{ steps.upload.outputs.artifact-url }}
1319
steps:
14-
- uses: actions/checkout@v6
15-
- name: Setup Flutter
16-
uses: subosito/flutter-action@v2
20+
- uses: actions/checkout@v4
21+
- uses: subosito/flutter-action@v2
1722
with:
18-
flutter-version: '3.32.0'
19-
- name: Install dependencies
20-
run: flutter pub get
21-
- name: Build macOS Intel
22-
run: dart compile exe bin/main.dart -o api_tool_macos_intel
23+
flutter-version: "3.32.0"
24+
channel: stable
25+
cache: true
26+
- run: flutter pub get
27+
- run: dart compile exe bin/main.dart -o api_tool_macos_intel
2328
- name: Verify binary
2429
run: |
2530
git clone --depth 1 --branch develop https://github.com/Tencent/tdesign-flutter.git
2631
dart run .github/scripts/cross_platform_verify.dart tdesign-flutter api_tool_macos_intel
27-
- name: Upload Artifact
28-
id: upload
29-
uses: actions/upload-artifact@v4
32+
- uses: actions/upload-artifact@v4
3033
with:
3134
name: api_tool_macos_intel
3235
path: api_tool_macos_intel
3336

3437
build-macos-arm64:
3538
runs-on: macos-latest
36-
outputs:
37-
artifact_id: ${{ steps.upload.outputs.artifact-id }}
38-
artifact_url: ${{ steps.upload.outputs.artifact-url }}
3939
steps:
40-
- uses: actions/checkout@v6
41-
- name: Setup Flutter
42-
uses: subosito/flutter-action@v2
40+
- uses: actions/checkout@v4
41+
- uses: subosito/flutter-action@v2
4342
with:
44-
flutter-version: '3.32.0'
45-
- name: Install dependencies
46-
run: flutter pub get
47-
- name: Build macOS ARM64
48-
run: dart compile exe bin/main.dart -o api_tool_macos_arm64 --target-os=macos --target-arch=arm64
43+
flutter-version: "3.32.0"
44+
channel: stable
45+
cache: true
46+
- run: flutter pub get
47+
- run: dart compile exe bin/main.dart -o api_tool_macos_arm64 --target-os=macos --target-arch=arm64
4948
- name: Verify binary
5049
run: |
5150
git clone --depth 1 --branch develop https://github.com/Tencent/tdesign-flutter.git
5251
dart run .github/scripts/cross_platform_verify.dart tdesign-flutter api_tool_macos_arm64
53-
- name: Upload Artifact
54-
id: upload
55-
uses: actions/upload-artifact@v4
52+
- uses: actions/upload-artifact@v4
5653
with:
5754
name: api_tool_macos_arm64
5855
path: api_tool_macos_arm64
5956

6057
build-linux-x64:
6158
runs-on: ubuntu-latest
62-
outputs:
63-
artifact_id: ${{ steps.upload.outputs.artifact-id }}
64-
artifact_url: ${{ steps.upload.outputs.artifact-url }}
6559
steps:
66-
- uses: actions/checkout@v6
67-
- name: Setup Flutter
68-
uses: subosito/flutter-action@v2
60+
- uses: actions/checkout@v4
61+
- uses: subosito/flutter-action@v2
6962
with:
70-
flutter-version: '3.32.0'
71-
- name: Install dependencies
72-
run: flutter pub get
73-
- name: Build Linux x64
74-
run: dart compile exe bin/main.dart -o api_tool_linux --target-arch=x64
63+
flutter-version: "3.32.0"
64+
channel: stable
65+
cache: true
66+
- run: flutter pub get
67+
- run: dart compile exe bin/main.dart -o api_tool_linux --target-arch=x64
7568
- name: Verify binary
7669
run: |
7770
git clone --depth 1 --branch develop https://github.com/Tencent/tdesign-flutter.git
7871
dart run .github/scripts/cross_platform_verify.dart tdesign-flutter api_tool_linux
79-
- name: Upload Artifact
80-
id: upload
81-
uses: actions/upload-artifact@v4
72+
- uses: actions/upload-artifact@v4
8273
with:
8374
name: api_tool_linux
8475
path: api_tool_linux
8576

8677
build-windows-x64:
8778
runs-on: windows-latest
88-
outputs:
89-
artifact_id: ${{ steps.upload.outputs.artifact-id }}
90-
artifact_url: ${{ steps.upload.outputs.artifact-url }}
9179
steps:
92-
- uses: actions/checkout@v6
93-
- name: Setup Flutter
94-
uses: subosito/flutter-action@v2
80+
- uses: actions/checkout@v4
81+
- uses: subosito/flutter-action@v2
9582
with:
96-
flutter-version: '3.32.0'
97-
- name: Install dependencies
98-
run: flutter pub get
99-
- name: Build Windows x64
100-
run: dart compile exe bin/main.dart -o api_tool_windows.exe
83+
flutter-version: "3.32.0"
84+
channel: stable
85+
cache: true
86+
- run: flutter pub get
87+
- run: dart compile exe bin/main.dart -o api_tool_windows.exe
10188
- name: Verify binary
89+
shell: bash
10290
run: |
10391
git clone --depth 1 --branch develop https://github.com/Tencent/tdesign-flutter.git
10492
dart run .github/scripts/cross_platform_verify.dart tdesign-flutter api_tool_windows.exe
105-
- name: Upload Artifact
106-
id: upload
107-
uses: actions/upload-artifact@v4
93+
- uses: actions/upload-artifact@v4
10894
with:
10995
name: api_tool_windows
11096
path: api_tool_windows.exe
11197

11298
comment-on-pr:
99+
name: 更新二进制产物 PR 评论
113100
needs: [build-macos-intel, build-macos-arm64, build-linux-x64, build-windows-x64]
114101
runs-on: ubuntu-latest
115102
steps:
116-
- name: Find Comment
117-
id: find-comment
118-
uses: peter-evans/find-comment@v3
119-
with:
120-
issue-number: ${{ github.event.pull_request.number }}
121-
comment-author: 'github-actions[bot]'
122-
body-includes: 编译完成,产物如下
123-
124-
- name: Create or Update PR Comment
125-
uses: peter-evans/create-or-update-comment@v4
103+
- name: 组装产物下载链接
104+
id: binary-comment
105+
env:
106+
GH_REPO: ${{ github.repository }}
107+
RUN_ID: ${{ github.run_id }}
108+
run: |
109+
base="https://github.com/${GH_REPO}/actions/runs/${RUN_ID}"
110+
{
111+
echo 'body<<EOF'
112+
echo "✅ 编译完成,产物如下:"
113+
echo "- [api_tool_macos_intel](${base}/artifacts)(job: build-macos-intel)"
114+
echo "- [api_tool_macos_arm64](${base}/artifacts)(job: build-macos-arm64)"
115+
echo "- [api_tool_linux](${base}/artifacts)(job: build-linux-x64)"
116+
echo "- [api_tool_windows](${base}/artifacts)(job: build-windows-x64)"
117+
echo ""
118+
echo "<!-- AUTO_BINARY_BUILD_HOOK -->"
119+
echo ""
120+
echo "_在 Actions 运行页 Artifacts 区域按平台名称下载。_"
121+
echo 'EOF'
122+
} >> "$GITHUB_OUTPUT"
123+
124+
- name: 更新 PR 评论
125+
uses: TDesignOteam/workflows/actions/maintain-one-comment@main
126126
with:
127-
comment-id: ${{ steps.find-comment.outputs.comment-id }}
128-
issue-number: ${{ github.event.pull_request.number }}
129-
body: |
130-
✅ 编译完成,产物如下:
131-
- [api_tool_macos_intel](${{ needs.build-macos-intel.outputs.artifact_url }})
132-
- [api_tool_macos_arm64](${{ needs.build-macos-arm64.outputs.artifact_url }})
133-
- [api_tool_linux](${{ needs.build-linux-x64.outputs.artifact_url }})
134-
- [api_tool_windows](${{ needs.build-windows-x64.outputs.artifact_url }})
135-
edit-mode: replace
127+
token: ${{ secrets.GITHUB_TOKEN }}
128+
body: ${{ steps.binary-comment.outputs.body }}
129+
number: ${{ github.event.pull_request.number }}
130+
body-include: "<!-- AUTO_BINARY_BUILD_HOOK -->"

0 commit comments

Comments
 (0)