Skip to content

Commit d7b069e

Browse files
committed
initial generat_api_report_command
1 parent 8376905 commit d7b069e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import 'dart:io';
2+
3+
import 'package:aft/aft.dart';
4+
5+
class GenerateApiReportCommand extends AmplifyCommand {
6+
7+
@override
8+
// TODO: implement description
9+
String get description => 'Generates API reports for Amplify packages and creates an API change report for amplify_core';
10+
11+
@override
12+
// TODO: implement name
13+
String get name => 'api-report';
14+
15+
final List<String> _categoryPackages = [
16+
'packages/amplify_core',
17+
'packages/analytics/amplify_analytics_pinpoint',
18+
'packages/api/amplify_api',
19+
'packages/auth/amplify_auth_cognito',
20+
'packages/authenticator/amplify_authenticator',
21+
'packages/notifications/push/amplify_push_notifications_pinpoint',
22+
'packages/storage/amplify_storage_s3',
23+
];
24+
25+
@override
26+
Future<void> run() async {
27+
try {
28+
// First, ensure dart-apitool is installed
29+
await _installDartApiTool();
30+
31+
// Generate api.json for all packages
32+
for (final package in _categoryPackages) {
33+
await _generateApiJson(package);
34+
}
35+
36+
// Generate API change report for amplify_core
37+
await _generateApiChangeReport();
38+
39+
logger.info('API reports generated successfully');
40+
} catch (e) {
41+
logger.error('Failed to generate API reports: $e');
42+
exit(1);
43+
}
44+
}
45+
46+
Future<void> _installDartApiTool() async {
47+
final result = await Process.run(
48+
'dart',
49+
['pub', 'global', 'activate', 'dart_apitool'],
50+
);
51+
52+
if (result.exitCode != 0) {
53+
throw Exception('Failed to install dart-apitool: ${result.stderr}');
54+
}
55+
}
56+
57+
Future<void> _generateApiJson(String packagePath) async {
58+
final result = await Process.run(
59+
'dart-apitool',
60+
[
61+
'extract',
62+
'--input',
63+
packagePath,
64+
'--output',
65+
'$packagePath/api.json',
66+
],
67+
);
68+
69+
if (result.exitCode != 0) {
70+
throw Exception('Failed to generate api.json for $packagePath: ${result.stderr}');
71+
}
72+
}
73+
74+
Future<void> _generateApiChangeReport() async {
75+
// Read the current version of amplify_core from pubspec.yaml
76+
final pubspecFile = File('packages/amplify_core/pubspec.yaml');
77+
final pubspecContent = await pubspecFile.readAsString();
78+
final versionMatch = RegExp(r'version:\s*([\d\.]+)').firstMatch(pubspecContent);
79+
final currentVersion = versionMatch?.group(1) ?? '2.4.1'; // Default to 2.4.1 if not found
80+
81+
final result = await Process.run(
82+
'dart-apitool',
83+
[
84+
'diff',
85+
'--old',
86+
'pub://amplify_core/$currentVersion',
87+
'--new',
88+
'./packages/amplify_core',
89+
'--report-format',
90+
'markdown',
91+
'--report-file-path',
92+
'./packages/amplify_core/api_changes_report.md',
93+
],
94+
);
95+
96+
if (result.exitCode != 0) {
97+
throw Exception('Failed to generate API change report: ${result.stderr}');
98+
}
99+
}
100+
101+
}

0 commit comments

Comments
 (0)