From 50db340e2634150c09aca4eb87428666b35e9ac7 Mon Sep 17 00:00:00 2001 From: birjuvachhani Date: Thu, 14 Nov 2024 17:20:49 +0530 Subject: [PATCH] :wrench: update docs gen script to include extension types and toc. --- scripts/extensions_docs_generator.dart | 77 ++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/scripts/extensions_docs_generator.dart b/scripts/extensions_docs_generator.dart index f046144..1abfe3f 100644 --- a/scripts/extensions_docs_generator.dart +++ b/scripts/extensions_docs_generator.dart @@ -18,6 +18,7 @@ class Stats { List classes = []; List variables = []; List extensions = []; + List extensionTypes = []; List typedefs = []; List mixins = []; @@ -26,6 +27,7 @@ class Stats { required this.classes, required this.variables, required this.extensions, + required this.extensionTypes, required this.typedefs, required this.mixins, }); @@ -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, ); @@ -79,18 +82,24 @@ void main(List 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) { @@ -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 @@ -211,12 +275,14 @@ Future getStats(String library) async { var helpersClasses = []; var helperVariables = []; var extensions = []; + var extensionTypes = []; var typedefs = []; var mixins = []; 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(); @@ -228,6 +294,7 @@ Future getStats(String library) async { classes: helpersClasses, functions: helpersFunctions, extensions: extensions, + extensionTypes: extensionTypes, typedefs: typedefs, mixins: mixins, ); @@ -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();