-
Notifications
You must be signed in to change notification settings - Fork 160
Data extraction from dartdoc internals. #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# See https://github.com/dart-lang/mono_repo for details | ||
dart: | ||
- 2.0.0-dev.60.0 | ||
|
||
stages: | ||
- smoke_test: | ||
- group: | ||
- dartfmt | ||
- dartanalyzer: --fatal-infos --fatal-warnings . | ||
- unit_test: | ||
- test: --run-skipped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
analyzer: | ||
language: | ||
enablePreviewDart2: true | ||
strong-mode: | ||
implicit-casts: false | ||
errors: | ||
dead_code: error | ||
override_on_non_overriding_method: error | ||
unused_element: error | ||
unused_import: error | ||
unused_local_variable: error | ||
linter: | ||
rules: | ||
- avoid_empty_else | ||
- avoid_init_to_null | ||
- avoid_return_types_on_setters | ||
- await_only_futures | ||
- camel_case_types | ||
- comment_references | ||
- control_flow_in_finally | ||
- directives_ordering | ||
- empty_catches | ||
- empty_constructor_bodies | ||
- empty_statements | ||
- hash_and_equals | ||
- implementation_imports | ||
- library_names | ||
- library_prefixes | ||
- non_constant_identifier_names | ||
- omit_local_variable_types | ||
- only_throw_errors | ||
- prefer_final_fields | ||
- prefer_is_not_empty | ||
- prefer_single_quotes | ||
- slash_for_doc_comments | ||
- test_types_in_equals | ||
- test_types_in_equals | ||
- throw_in_finally | ||
- type_init_formals | ||
- unrelated_type_equality_checks | ||
- valid_regexps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:dartdoc/dartdoc.dart'; | ||
import 'package:dartdoc/src/html/html_generator.dart'; | ||
import 'package:dartdoc/src/logging.dart'; | ||
|
||
import 'package:pub_dartdoc/pub_data_generator.dart'; | ||
|
||
main(List<String> arguments) async { | ||
final optionSet = await DartdocOptionSet.fromOptionGenerators('pub_dartdoc', [ | ||
createDartdocOptions, | ||
createLoggingOptions, | ||
createGeneratorOptions, | ||
]); | ||
optionSet.parseArguments(arguments); | ||
|
||
final optionContext = new DartdocProgramOptionContext(optionSet, null); | ||
startLogging(optionContext); | ||
|
||
final dartdoc = await Dartdoc.withDefaultGenerators(optionContext); | ||
dartdoc.generators.add(new PubDataGenerator(optionContext.inputDir)); | ||
|
||
await dartdoc.generateDocs(); | ||
} | ||
|
||
class DartdocProgramOptionContext extends DartdocGeneratorOptionContext | ||
with LoggingContext { | ||
DartdocProgramOptionContext(DartdocOptionSet optionSet, Directory dir) | ||
: super(optionSet, dir); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Read about `build.yaml` at https://pub.dartlang.org/packages/build_config | ||
# To update generated code, run `pub run build_runner build` | ||
targets: | ||
pub_dartdoc: | ||
sources: | ||
- 'lib/model.dart' | ||
builders: | ||
json_serializable: | ||
options: | ||
header: |+ | ||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
|
||
// ignore_for_file: prefer_final_locals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
part 'model.g.dart'; | ||
|
||
@JsonSerializable() | ||
class PubDartdocData extends Object with _$PubDartdocDataSerializerMixin { | ||
final List<ApiElement> apiElements; | ||
|
||
PubDartdocData({ | ||
@required this.apiElements, | ||
}); | ||
|
||
factory PubDartdocData.fromJson(Map<String, dynamic> json) => | ||
_$PubDartdocDataFromJson(json); | ||
} | ||
|
||
@JsonSerializable() | ||
class ApiElement extends Object with _$ApiElementSerializerMixin { | ||
final String name; | ||
|
||
final String kind; | ||
|
||
@JsonKey(includeIfNull: false) | ||
final String parent; | ||
|
||
final String source; | ||
|
||
@JsonKey(includeIfNull: false) | ||
final String href; | ||
|
||
@JsonKey(includeIfNull: false) | ||
final String documentation; | ||
|
||
ApiElement({ | ||
@required this.name, | ||
@required this.kind, | ||
@required this.parent, | ||
@required this.source, | ||
@required this.href, | ||
@required this.documentation, | ||
}); | ||
|
||
factory ApiElement.fromJson(Map<String, dynamic> json) => | ||
_$ApiElementFromJson(json); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
|
||
// ignore_for_file: prefer_final_locals | ||
|
||
part of 'model.dart'; | ||
|
||
// ************************************************************************** | ||
// Generator: JsonSerializableGenerator | ||
// ************************************************************************** | ||
|
||
PubDartdocData _$PubDartdocDataFromJson(Map<String, dynamic> json) { | ||
return new PubDartdocData( | ||
apiElements: (json['apiElements'] as List) | ||
?.map((e) => e == null | ||
? null | ||
: new ApiElement.fromJson(e as Map<String, dynamic>)) | ||
?.toList()); | ||
} | ||
|
||
abstract class _$PubDartdocDataSerializerMixin { | ||
List<ApiElement> get apiElements; | ||
Map<String, dynamic> toJson() => | ||
<String, dynamic>{'apiElements': apiElements}; | ||
} | ||
|
||
ApiElement _$ApiElementFromJson(Map<String, dynamic> json) { | ||
return new ApiElement( | ||
name: json['name'] as String, | ||
kind: json['kind'] as String, | ||
parent: json['parent'] as String, | ||
source: json['source'] as String, | ||
href: json['href'] as String, | ||
documentation: json['documentation'] as String); | ||
} | ||
|
||
abstract class _$ApiElementSerializerMixin { | ||
String get name; | ||
String get kind; | ||
String get parent; | ||
String get source; | ||
String get href; | ||
String get documentation; | ||
Map<String, dynamic> toJson() { | ||
var val = <String, dynamic>{ | ||
'name': name, | ||
'kind': kind, | ||
}; | ||
|
||
void writeNotNull(String key, dynamic value) { | ||
if (value != null) { | ||
val[key] = value; | ||
} | ||
} | ||
|
||
writeNotNull('parent', parent); | ||
val['source'] = source; | ||
writeNotNull('href', href); | ||
writeNotNull('documentation', documentation); | ||
return val; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:convert' as convert; | ||
import 'dart:io'; | ||
|
||
import 'package:dartdoc/dartdoc.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
import 'model.dart'; | ||
|
||
const fileName = 'pub-data.json'; | ||
|
||
/// Generates `pub-data.json` in the output directory, containing the extracted | ||
/// [PubDartdocData] instance. | ||
class PubDataGenerator implements Generator { | ||
final _onFileCreated = new StreamController<File>(); | ||
final _writtenFiles = new Set<String>(); | ||
final String _inputDirectory; | ||
|
||
PubDataGenerator(this._inputDirectory); | ||
|
||
@override | ||
Future generate(PackageGraph packageGraph, String outputDirectoryPath) async { | ||
final modelElements = packageGraph.allCanonicalModelElements | ||
.where((elem) => elem.isPublic) | ||
.where((elem) => p.isWithin(_inputDirectory, elem.sourceFileName)) | ||
.where((elem) { | ||
// remove inherited items from dart:* libraries | ||
final inheritedFrom = elem.overriddenElement?.fullyQualifiedName; | ||
final fromDartLibs = | ||
inheritedFrom != null && inheritedFrom.startsWith('dart:'); | ||
return !fromDartLibs; | ||
}).toList(); | ||
|
||
final apiMap = <String, ApiElement>{}; | ||
void addElement(ModelElement elem) { | ||
apiMap.putIfAbsent( | ||
elem.fullyQualifiedName, | ||
() => new ApiElement( | ||
name: elem.fullyQualifiedName, | ||
kind: elem.kind, | ||
parent: elem.enclosingElement?.fullyQualifiedName, | ||
source: p.relative(elem.sourceFileName, from: _inputDirectory), | ||
href: _trimToNull(elem.href), | ||
documentation: _trimToNull(elem.documentation), | ||
)); | ||
if (elem.enclosingElement is ModelElement) { | ||
addElement(elem.enclosingElement as ModelElement); | ||
} | ||
} | ||
|
||
modelElements.forEach(addElement); | ||
|
||
final apiElements = apiMap.values.toList(); | ||
apiElements.sort((a, b) { | ||
if (a.parent == null && b.parent != null) return -1; | ||
if (a.parent != null && b.parent == null) return 1; | ||
if (a.parent != b.parent) return a.parent.compareTo(b.parent); | ||
return a.name.compareTo(b.name); | ||
}); | ||
|
||
final extract = new PubDartdocData(apiElements: apiElements); | ||
|
||
final fileName = 'pub-data.json'; | ||
final outputFile = new File(p.join(outputDirectoryPath, fileName)); | ||
await outputFile.writeAsString(convert.json.encode(extract.toJson())); | ||
_onFileCreated.add(outputFile); | ||
_writtenFiles.add(fileName); | ||
} | ||
|
||
@override | ||
Stream<File> get onFileCreated => _onFileCreated.stream; | ||
|
||
@override | ||
Set<String> get writtenFiles => _writtenFiles; | ||
} | ||
|
||
String _trimToNull(String text) { | ||
text = text?.trim(); | ||
return (text != null && text.isEmpty) ? null : text; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean outputFile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about that either, the API documentation didn't specify it exactly, but
package:dartdoc
uses the same relative path pattern, and I'd go with that.