Skip to content

Commit 8ef1a3a

Browse files
committed
Check if dartdoc can run on the package.
1 parent 3ed123d commit 8ef1a3a

File tree

7 files changed

+104
-2
lines changed

7 files changed

+104
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
* `ToolEnvironment.runAnalyzer` returns the text output of the process.
88

9+
Updates:
10+
11+
* Check if `dartdoc` can run on the package.
12+
913
## 0.10.6
1014

1115
* Enable Dart 2 Preview in analyzer options (including non-Flutter packages).

lib/src/maintenance.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Future<Maintenance> detectMaintenance(
164164
List<Suggestion> dartFileSuggestions,
165165
List<PkgDependency> unconstrainedDeps, {
166166
@required DartPlatform pkgPlatform,
167+
@required bool dartdocSuccessful,
167168
}) async {
168169
final pkgName = pubspec.name;
169170
final maintenanceSuggestions = <Suggestion>[];
@@ -217,6 +218,14 @@ Future<Maintenance> detectMaintenance(
217218
}
218219
}
219220

221+
if (!dartdocSuccessful) {
222+
maintenanceSuggestions.add(new Suggestion.error(
223+
'Running `dartdoc` failed.',
224+
'Make sure `dartdoc` runs without any issues.',
225+
penalty: new Penalty(fraction: 1000),
226+
));
227+
}
228+
220229
if (pkgPlatform.hasConflict) {
221230
maintenanceSuggestions.add(new Suggestion.error(
222231
'Fix platform conflicts.', pkgPlatform.reason,

lib/src/package_analyzer.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ class PackageAnalyzer {
176176

177177
Set<CodeProblem> analyzerItems;
178178

179+
var dartdocSuccessful = false;
180+
if (pkgResolution != null) {
181+
try {
182+
dartdocSuccessful = await _toolEnv.checkDartdoc(pkgDir);
183+
} catch (e, st) {
184+
log.severe('Could not run dartdoc.', e, st);
185+
}
186+
}
187+
179188
if (pkgResolution != null) {
180189
try {
181190
var overrides = [
@@ -314,6 +323,7 @@ class PackageAnalyzer {
314323
dartFileSuggestions,
315324
pkgResolution?.getUnconstrainedDeps(onlyDirect: true),
316325
pkgPlatform: platform,
326+
dartdocSuccessful: dartdocSuccessful,
317327
);
318328
suggestions.sort();
319329

lib/src/sdk_env.dart

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ class ToolEnvironment {
2222
final String _pubCmd;
2323
final String _dartAnalyzerCmd;
2424
final String _dartfmtCmd;
25+
final String _dartdocCmd;
2526
final String _flutterCmd;
2627
final Map<String, String> _environment;
2728
DartSdkInfo _dartSdkInfo;
29+
bool _useGlobalDartdoc;
2830

2931
ToolEnvironment._(
3032
this.dartSdkDir,
@@ -33,8 +35,10 @@ class ToolEnvironment {
3335
this._pubCmd,
3436
this._dartAnalyzerCmd,
3537
this._dartfmtCmd,
38+
this._dartdocCmd,
3639
this._flutterCmd,
3740
this._environment,
41+
this._useGlobalDartdoc,
3842
);
3943

4044
DartSdkInfo get dartSdkInfo => _dartSdkInfo;
@@ -51,6 +55,7 @@ class ToolEnvironment {
5155
String flutterSdkDir,
5256
String pubCacheDir,
5357
Map<String, String> environment,
58+
bool useGlobalDartdoc: false,
5459
}) async {
5560
Future<String> resolve(String dir) async {
5661
if (dir == null) return null;
@@ -65,7 +70,7 @@ class ToolEnvironment {
6570
env.addAll(environment ?? const {});
6671

6772
if (resolvedPubCache != null) {
68-
env['PUB_CACHE'] = resolvedPubCache;
73+
env[_pubCacheKey] = resolvedPubCache;
6974
}
7075

7176
final pubEnvValues = <String>[];
@@ -84,8 +89,10 @@ class ToolEnvironment {
8489
_join(resolvedDartSdk, 'bin', 'pub'),
8590
_join(resolvedDartSdk, 'bin', 'dartanalyzer'),
8691
_join(resolvedDartSdk, 'bin', 'dartfmt'),
92+
_join(resolvedDartSdk, 'bin', 'dartdoc'),
8793
_join(resolvedFlutterSdk, 'bin', 'flutter'),
8894
env,
95+
useGlobalDartdoc,
8996
);
9097
await toolEnv._init();
9198
return toolEnv;
@@ -227,6 +234,45 @@ class ToolEnvironment {
227234
return result;
228235
}
229236

237+
Map<String, String> _globalDartdocEnv() {
238+
final env = new Map<String, String>.from(_environment);
239+
if (pubCacheDir != null) {
240+
env.remove(_pubCacheKey);
241+
}
242+
return env;
243+
}
244+
245+
Future activateGlobalDartdoc(String version) async {
246+
handleProcessErrors(await runProc(
247+
_pubCmd,
248+
['global', 'activate', 'dartdoc', version],
249+
environment: _globalDartdocEnv(),
250+
));
251+
_useGlobalDartdoc = true;
252+
}
253+
254+
Future<bool> checkDartdoc(String packageDir) async {
255+
ProcessResult pr;
256+
final args = ['--exclude', dartdocExcludedLibraries.join(',')];
257+
if (_useGlobalDartdoc) {
258+
pr = await runProc(
259+
_pubCmd,
260+
['global', 'run', 'dartdoc']..addAll(args),
261+
workingDirectory: packageDir,
262+
environment: _globalDartdocEnv(),
263+
);
264+
} else {
265+
pr = await runProc(
266+
_dartdocCmd,
267+
args,
268+
workingDirectory: packageDir,
269+
environment: _environment,
270+
);
271+
}
272+
// TODO: check generated content e.g. index.html and index.json
273+
return pr.exitCode == 0;
274+
}
275+
230276
Future<PackageLocation> getLocation(String package, {String version}) async {
231277
var args = ['cache', 'add', '--verbose'];
232278
if (version != null) {
@@ -326,9 +372,23 @@ class PackageLocation {
326372
PackageLocation(this.package, this.version, this.location);
327373
}
328374

375+
const dartdocExcludedLibraries = const <String>[
376+
'dart:async',
377+
'dart:collection',
378+
'dart:convert',
379+
'dart:core',
380+
'dart:developer',
381+
'dart:io',
382+
'dart:isolate',
383+
'dart:math',
384+
'dart:typed_data',
385+
'dart:ui',
386+
];
387+
329388
final _versionDownloadRegexp =
330389
new RegExp(r"^MSG : (?:Downloading |Already cached )([\w-]+) (.+)$");
331390

391+
const _pubCacheKey = 'PUB_CACHE';
332392
const _pubEnvironmentKey = 'PUB_ENVIRONMENT';
333393

334394
String _join(String path, String binDir, String executable) =>

test/end2end/skiplist_data.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ final _data = {
285285
'warningCount': 0,
286286
'hintCount': 1,
287287
'suggestions': [
288+
{
289+
'level': 'error',
290+
'title': 'Running `dartdoc` failed.',
291+
'description': 'Make sure `dartdoc` runs without any issues.',
292+
'penalty': {'amount': 0, 'fraction': 1000},
293+
},
288294
{
289295
'level': 'warning',
290296
'title': 'Fix analysis and formatting issues.',

test/end2end/stream_broken_data.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ final _data = {
5656
'Error(s) prevent platform classification:\n\nMake sure `dartfmt` runs.',
5757
'penalty': {'amount': 0, 'fraction': 2000}
5858
},
59+
{
60+
'level': 'error',
61+
'title': 'Running `dartdoc` failed.',
62+
'description': 'Make sure `dartdoc` runs without any issues.',
63+
'penalty': {'amount': 0, 'fraction': 1000},
64+
},
5965
{
6066
'level': 'warning',
6167
'title': 'Maintain `CHANGELOG.md`.',

test/maintenance_test.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ final _withIssuesJson = {
3131
'description': 'conflict description',
3232
'penalty': {'amount': 0, 'fraction': 2000}
3333
},
34+
{
35+
'level': 'error',
36+
'title': 'Running `dartdoc` failed.',
37+
'description': 'Make sure `dartdoc` runs without any issues.',
38+
'penalty': {'amount': 0, 'fraction': 1000},
39+
},
3440
{
3541
'level': 'warning',
3642
'title': 'Maintain `CHANGELOG.md`.',
@@ -119,6 +125,7 @@ void main() {
119125
suggestions,
120126
[new PkgDependency('foo', 'direct', 'empty', null, null, null, null)],
121127
pkgPlatform: new DartPlatform.conflict('conflict description'),
128+
dartdocSuccessful: false,
122129
);
123130

124131
expect(json.decode(json.encode(maintenance.toJson())), _withIssuesJson);
@@ -127,7 +134,7 @@ void main() {
127134

128135
group('getMaintenanceScore', () {
129136
test('with issues', () {
130-
expect(_withIssues.getMaintenanceScore(), closeTo(0.530, 0.001));
137+
expect(_withIssues.getMaintenanceScore(), closeTo(0.476, 0.001));
131138
});
132139

133140
test('perfect', () {

0 commit comments

Comments
 (0)