Skip to content

Commit 96200cb

Browse files
committed
pre-cleanup windows fix
1 parent 4d49b17 commit 96200cb

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

lib/src/model/package_graph.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import 'package:dartdoc/src/render/renderer_factory.dart';
2222
import 'package:dartdoc/src/special_elements.dart';
2323
import 'package:dartdoc/src/tuple.dart';
2424
import 'package:dartdoc/src/warnings.dart';
25-
import 'package:glob/glob.dart';
25+
import 'package:dartdoc/src/model_utils.dart' show matchGlobs;
2626

2727
class PackageGraph {
2828
PackageGraph.UninitializedPackageGraph(
@@ -964,9 +964,7 @@ class PackageGraph {
964964
// might not be where the element was defined, which is what's important
965965
// for nodoc's semantics.
966966
List<String> globs = config.optionSet['nodoc'].valueAt(file.parent);
967-
_configSetsNodocFor[fullName] = globs.any((g) =>
968-
Glob(resourceProvider.pathContext.canonicalize(g))
969-
.matches(resourceProvider.pathContext.canonicalize(fullName)));
967+
_configSetsNodocFor[fullName] = matchGlobs(globs, fullName);
970968
}
971969
return _configSetsNodocFor[fullName];
972970
}

lib/src/model_utils.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,42 @@ import 'package:analyzer/dart/ast/ast.dart';
1111
import 'package:analyzer/dart/element/element.dart';
1212
import 'package:analyzer/src/dart/ast/utilities.dart';
1313
import 'package:dartdoc/src/model/model.dart';
14+
import 'package:path/path.dart' as path;
15+
import 'package:glob/glob.dart';
16+
17+
final _driveLetterMatcher = RegExp(r'^\w:\\');
1418

1519
final Map<String, String> _fileContents = <String, String>{};
1620

21+
/// This will handle matching globs, including on Windows.
22+
///
23+
/// Assumes that globs and resource provider are from the same drive, which
24+
/// will be the case for globs relative to dartdoc_options.yaml.
25+
///
26+
/// On windows, globs are assumed to use Windows paths in combination with
27+
/// globs, e.g. `C:\foo\bar\*.txt`.
28+
bool matchGlobs(List<String> globs, String fullName, {bool isWindows}) {
29+
isWindows ??= Platform.isWindows;
30+
var filteredGlobs = <String>[];
31+
32+
if (isWindows) {
33+
assert(_driveLetterMatcher.hasMatch(fullName),
34+
'can not find drive letter in $fullName');
35+
// TODO(jcollins-g): handle globs referencing different drives?
36+
fullName = fullName.replaceFirst(_driveLetterMatcher, r'\');
37+
for (var glob in globs) {
38+
// `C:\` => `\` for rejoining via posix.
39+
glob = glob.replaceFirst(_driveLetterMatcher, r'/');
40+
filteredGlobs.add(path.posix.joinAll(path.windows.split(glob)));
41+
}
42+
} else {
43+
filteredGlobs.addAll(globs);
44+
}
45+
46+
return filteredGlobs.any((g) =>
47+
Glob(g, context: isWindows ? path.windows : null).matches(fullName));
48+
}
49+
1750
/// Returns the [AstNode] for a given [Element].
1851
///
1952
/// Uses a precomputed map of [element.source.fullName] to [CompilationUnit]

test/model_utils_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import 'package:dartdoc/src/model_utils.dart';
88
import 'package:test/test.dart';
99

1010
void main() {
11+
group('match glob', () {
12+
test('basic POSIX', () {
13+
expect(
14+
matchGlobs(['/a/b/*', '/b/c/*'], '/b/c/d', isWindows: false), isTrue);
15+
expect(matchGlobs(['/q/r/s'], '/foo', isWindows: false), isFalse);
16+
});
17+
18+
test('basic Windows', () {
19+
expect(matchGlobs([r'C:\a\b\*'], r'c:\a\b\d', isWindows: true), isTrue);
20+
});
21+
});
22+
1123
group('model_utils stripIndentFromSource', () {
1224
test('no indent', () {
1325
expect(stripIndentFromSource('void foo() {\n print(1);\n}\n'),

0 commit comments

Comments
 (0)