Skip to content

Commit

Permalink
🔧 update docs gen script to include extension types and toc.
Browse files Browse the repository at this point in the history
  • Loading branch information
BirjuVachhani committed Nov 14, 2024
1 parent 8c34351 commit 50db340
Showing 1 changed file with 72 additions and 5 deletions.
77 changes: 72 additions & 5 deletions scripts/extensions_docs_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Stats {
List<ClassElement> classes = [];
List<PropertyAccessorElement> variables = [];
List<ExtensionElement> extensions = [];
List<ExtensionTypeElement> extensionTypes = [];
List<TypeDefiningElement> typedefs = [];
List<MixinElement> mixins = [];

Expand All @@ -26,6 +27,7 @@ class Stats {
required this.classes,
required this.variables,
required this.extensions,
required this.extensionTypes,
required this.typedefs,
required this.mixins,
});
Expand All @@ -35,6 +37,7 @@ class Stats {
classes: classes + other.classes,
variables: variables + other.variables,
extensions: extensions + other.extensions,
extensionTypes: extensionTypes + other.extensionTypes,
typedefs: typedefs + other.typedefs,
mixins: mixins + other.mixins,
);
Expand Down Expand Up @@ -79,18 +82,24 @@ void main(List<String> args) async {
sink = stdout;
}

sink.writeln('# Docs');
sink.writeln(
'> *This file is auto generated. Do not edit this file manually.*');
sink.writeln(
'> *Last Updated: ${DateFormat('EEE, MMM dd, yyyy - hh:mm a').format(DateTime.now())}*');
sink.writeln('\n');
generateTableOfContents(sink, stats);
sink.writeln('\n');

outputExtensions(sink, stats, isDry: isDry);
outputFunctions(sink, stats, isDry: isDry);
outputExtensionTypes(sink, stats, isDry: isDry);

if (!isDry) sink.close();
}

void outputExtensions(IOSink sink, Stats stats, {required bool isDry}) {
sink.writeln('# Extensions');
sink.writeln(
'> *This file is auto generated. Do not edit this file manually.*');
sink.writeln(
'> *Last Updated: ${DateFormat('EEE, MMM dd, yyyy - hh:mm a').format(DateTime.now())}*');
sink.writeln('\n');

for (final extension in stats.extensions) {
Expand Down Expand Up @@ -168,6 +177,61 @@ void outputFunctions(IOSink sink, Stats stats, {required bool isDry}) {
sink.writeln('\n');
}

void outputExtensionTypes(IOSink sink, Stats stats, {required bool isDry}) {
sink.writeln('# Extensions Types (${stats.extensionTypes.length})');
sink.writeln('\n');

// table header
sink.writeln('| Extension Type | on | Description |');
sink.writeln('|---|---|---|');

for (final extensionType in stats.extensionTypes) {
String path = extensionType.source.toString();
final String githubFilePath = path.replaceAll(RegExp(r'.*lib'),
'https://github.com/BirjuVachhani/screwdriver/blob/main/lib');

// Get the line number of the method
final lineNumber = LineInfo.fromContent(extensionType.source.contents.data)
.getLocation(extensionType.nameOffset)
.lineNumber;

final description = sanitizeDocComment(
extensionType.documentationComment ?? 'Not provided');

sink.writeln(
'| [`${extensionType.displayName}`]($githubFilePath#L$lineNumber) | `${extensionType.representation.type.element?.name ?? 'N/A'}` | $description |');
}
sink.writeln('\n');
}

void generateTableOfContents(IOSink sink, Stats stats) {
sink.writeln('## Table of Contents');

// Extensions
sink.writeln('- [Extensions](#extensions)');
for (final extension in stats.extensions) {
final DartType type = extension.extendedType;
if (type is TypeParameterType) {
sink.writeln(' - [${type.bound}](#on-${type.bound})');
} else {
sink.writeln(
' - [${extension.extendedType}](#on-${extension.extendedType})');
}
}

// Functions
sink.writeln('- [Functions](#functions)');
for (final function in stats.functions) {
sink.writeln(' - [${function.displayName}](#functions)');
}

// Extension Types
sink.writeln('- [Extension Types](#extension-types)');
for (final extensionType in stats.extensionTypes) {
sink.writeln(' - [${extensionType.displayName}](#extension-types)');
}
}

String sanitizeDocComment(String comment) {
String sanitized = comment;
sanitized = sanitized
Expand Down Expand Up @@ -211,12 +275,14 @@ Future<Stats> getStats(String library) async {
var helpersClasses = <ClassElement>[];
var helperVariables = <PropertyAccessorElement>[];
var extensions = <ExtensionElement>[];
var extensionTypes = <ExtensionTypeElement>[];
var typedefs = <TypeDefiningElement>[];
var mixins = <MixinElement>[];

for (final part in result.element.units) {
helpersFunctions += part.functions.wherePublic().toList();
extensions += part.extensions.wherePublic().toList();
extensionTypes += part.extensionTypes.wherePublic().toList();
helpersClasses += part.classes.wherePublic().toList();
helperVariables += part.accessors.wherePublic().toList();
typedefs += part.typeAliases.wherePublic().toList();
Expand All @@ -228,6 +294,7 @@ Future<Stats> getStats(String library) async {
classes: helpersClasses,
functions: helpersFunctions,
extensions: extensions,
extensionTypes: extensionTypes,
typedefs: typedefs,
mixins: mixins,
);
Expand All @@ -247,7 +314,7 @@ void collectExports(LibraryOrAugmentationElement element, Stats stats,
stats.functions.addAll(unit.functions.wherePublic());
stats.variables.addAll(unit.accessors.wherePublic());
stats.extensions += unit.extensions.wherePublic().toList();
stats.extensions += unit.extensions.wherePublic().toList();
stats.extensionTypes += unit.extensionTypes.wherePublic().toList();
stats.typedefs += unit.typeAliases.wherePublic().toList();
stats.mixins += unit.mixins.wherePublic().toList();

Expand Down

0 comments on commit 50db340

Please sign in to comment.