Skip to content

Commit b28e539

Browse files
honor sub packages in workspaces
1 parent 2977f69 commit b28e539

File tree

5 files changed

+69
-35
lines changed

5 files changed

+69
-35
lines changed

lib/changelog_generator/changelog_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class ChangelogGenerator {
118118
final updatedContent = '$newEntry$existingContent';
119119
await changelogFile.writeAsString(updatedContent);
120120

121-
logger.info('CHANGELOG.md updated successfully.');
121+
logger.info('${changelogFile.absolute.path} updated successfully.');
122122
}
123123

124124
/// Retrieves the package version and homepage from pubspec.yaml

lib/doc_comparator/get_ref.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import 'dart:io';
33
import 'package:mtrust_api_guard/doc_comparator/parse_doc_file.dart';
44
import 'package:mtrust_api_guard/doc_generator/cache.dart';
55
import 'package:mtrust_api_guard/doc_generator/doc_generator.dart';
6+
import 'package:mtrust_api_guard/doc_generator/git_utils.dart';
67
import 'package:mtrust_api_guard/logger.dart';
78
import 'package:mtrust_api_guard/models/package_info.dart';
9+
import 'package:path/path.dart';
810

911
Future<PackageApi> getRef({
1012
required String ref,
@@ -22,11 +24,17 @@ Future<PackageApi> getRef({
2224

2325
// Handle git refs
2426
if (cache) {
25-
final cache = Cache();
27+
final cacheInstance = Cache();
28+
final repoPath = GitUtils.getRepositoryRoot(gitRoot.path);
29+
final dartRelativePath = relative(dartRoot.path, from: gitRoot.path);
2630

27-
if (cache.hasApiFile(gitRoot.path, ref)) {
31+
if (cacheInstance.hasApiFile(repoPath, ref, dartRelativePath)) {
2832
logger.success('Using cached API documentation for $ref');
29-
final cachedContent = await cache.retrieveApiFile(gitRoot.path, ref);
33+
final cachedContent = await cacheInstance.retrieveApiFile(
34+
repoPath,
35+
ref,
36+
dartRelativePath,
37+
);
3038
if (cachedContent != null) {
3139
return parsePackageApiFile(cachedContent);
3240
}

lib/doc_generator/cache.dart

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,59 @@ class Cache {
2020
return Directory(join(_cacheDir.path, repoName));
2121
}
2222

23-
/// Gets the path for an API file cached for a specific git ref
24-
File getApiFileForRef(String repoPath, String ref) {
23+
/// Sanitizes a path for use in file names
24+
String _sanitizePath(String path) {
25+
if (path == '.' || path.isEmpty) {
26+
return 'root';
27+
}
28+
// Replace path separators and invalid characters with underscores
29+
return path.replaceAll(RegExp(r'[<>:"|?*\x00-\x1f/\\]'), '_');
30+
}
31+
32+
/// Gets the path for an API file cached for a specific git ref and dart root
33+
File getApiFileForRef(String repoPath, String ref, String dartRelativePath) {
2534
final repoCacheDir = getRepositoryCacheDir(repoPath);
26-
return File(join(repoCacheDir.path, '${ref}_api.json'));
35+
final sanitizedDartPath = _sanitizePath(dartRelativePath);
36+
return File(join(repoCacheDir.path, '${ref}_${sanitizedDartPath}_api.json'));
2737
}
2838

29-
/// Stores API documentation for a specific git ref
30-
Future<void> storeApiFile(String repoPath, String ref, String content) async {
39+
/// Stores API documentation for a specific git ref and dart root
40+
Future<void> storeApiFile(
41+
String repoPath,
42+
String ref,
43+
String dartRelativePath,
44+
String content,
45+
) async {
3146
final repoCacheDir = getRepositoryCacheDir(repoPath);
3247
if (!repoCacheDir.existsSync()) {
3348
repoCacheDir.createSync(recursive: true);
3449
}
3550

36-
final apiFile = getApiFileForRef(repoPath, ref);
51+
final apiFile = getApiFileForRef(repoPath, ref, dartRelativePath);
3752
await apiFile.writeAsString(content);
3853
}
3954

40-
bool hasApiFileForRef(String repoPath, String ref) {
41-
final apiFile = getApiFileForRef(repoPath, ref);
55+
bool hasApiFileForRef(String repoPath, String ref, String dartRelativePath) {
56+
final apiFile = getApiFileForRef(repoPath, ref, dartRelativePath);
4257
return apiFile.existsSync();
4358
}
4459

45-
/// Retrieves API documentation for a specific git ref
46-
Future<String?> retrieveApiFile(String repoPath, String ref) async {
47-
final apiFile = getApiFileForRef(repoPath, ref);
60+
/// Retrieves API documentation for a specific git ref and dart root
61+
Future<String?> retrieveApiFile(
62+
String repoPath,
63+
String ref,
64+
String dartRelativePath,
65+
) async {
66+
final apiFile = getApiFileForRef(repoPath, ref, dartRelativePath);
4867
if (apiFile.existsSync()) {
4968
return await apiFile.readAsString();
5069
}
5170
return null;
5271
}
5372

54-
/// Checks if API documentation exists for a specific git ref
55-
bool hasApiFile(String repoPath, String ref) {
56-
final apiFile = getApiFileForRef(repoPath, ref);
73+
/// Checks if API documentation exists for a specific git ref and dart root
74+
bool hasApiFile(String repoPath, String ref, String dartRelativePath) {
75+
final apiFile = getApiFileForRef(repoPath, ref, dartRelativePath);
5776
return apiFile.existsSync();
5877
}
5978

lib/doc_generator/doc_generator.dart

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ Future<PackageApi> generateDocs({
8282
final classes = <DocComponent>[];
8383

8484
if (shouldCache) {
85-
if (cache.hasApiFile(repoPath, effectiveRef)) {
85+
if (cache.hasApiFile(repoPath, effectiveRef, dartRelativePath)) {
8686
logger.success('Using cached API documentation for $effectiveRef');
87-
final cachedContent = await cache.retrieveApiFile(repoPath, effectiveRef);
87+
final cachedContent = await cache.retrieveApiFile(
88+
repoPath,
89+
effectiveRef,
90+
dartRelativePath,
91+
);
8892
if (cachedContent != null) {
8993
logger.success('Using cached API documentation for $effectiveRef');
9094

@@ -112,20 +116,20 @@ Future<PackageApi> generateDocs({
112116
}
113117
}
114118

119+
logger.info('determining analysis dart root');
120+
logger.info('dartRoot: ${dartRoot.path}');
121+
logger.info('gitRoot: ${gitRoot.path}');
122+
logger.info('worktreePath: $worktreePath');
123+
115124
// Determine the root path to use for analysis
116125
// If using a worktree, calculate the dartRoot path relative to the worktree
117-
final analysisDartRoot = worktreePath != null
118-
? () {
119-
final gitRootAbs = Directory(gitRoot.path).absolute.path;
120-
final dartRootAbs = Directory(dartRoot.path).absolute.path;
121-
final relativePath = relative(dartRootAbs, from: gitRootAbs);
122-
// Handle case where dartRoot == gitRoot (relative path would be ".")
123-
if (relativePath == '.' || relativePath.isEmpty) {
124-
return Directory(worktreePath!);
125-
}
126-
return Directory(join(worktreePath!, relativePath));
127-
}()
128-
: dartRoot;
126+
Directory analysisDartRoot = dartRoot;
127+
if (worktreePath != null) {
128+
final gitRootAbs = Directory(gitRoot.path).absolute.path;
129+
final dartRootAbs = Directory(dartRoot.path).absolute.path;
130+
final relativePath = relative(dartRootAbs, from: gitRootAbs);
131+
analysisDartRoot = Directory(join(worktreePath, relativePath));
132+
}
129133

130134
try {
131135
final (config, globbedFiles) = evaluateTargetFiles(analysisDartRoot.path);
@@ -269,7 +273,7 @@ Future<PackageApi> generateDocs({
269273

270274
// Cache the generated documentation if requested
271275
if (shouldCache) {
272-
await cache.storeApiFile(repoPath, effectiveRef, output);
276+
await cache.storeApiFile(repoPath, effectiveRef, dartRelativePath, output);
273277
logger.success('Cached documentation for ref: $effectiveRef');
274278
}
275279

@@ -285,10 +289,10 @@ Future<PackageApi> generateDocs({
285289
return packageApi;
286290
} finally {
287291
// Clean up worktree if it was created
288-
if (worktreeCreated && worktreePath != null) {
292+
if (worktreeCreated) {
289293
try {
290294
logger.detail('Cleaning up worktree at $worktreePath');
291-
await GitUtils.removeWorktree(repoPath, worktreePath);
295+
await GitUtils.removeWorktree(repoPath, worktreePath!);
292296
logger.detail('Successfully cleaned up worktree');
293297
} catch (e) {
294298
logger.err('Warning: Failed to clean up worktree at $worktreePath: $e');

lib/version/version.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Future<VersionResult> version({
9797

9898
final baseVersion = PubspecUtils.getVersion(basePubSpec);
9999

100+
logger.info('Base version: $baseVersion');
101+
logger.info('Changes: $changes');
102+
100103
final highestMagnitudeChange = getHighestMagnitude(changes);
101104

102105
logger.info('Highest magnitude change: $highestMagnitudeChange');

0 commit comments

Comments
 (0)