From 6156d2e741b00056bffd4e8d8ba6fda8cdd1bbc7 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 27 May 2025 20:46:02 -0700 Subject: [PATCH] Remove Templates.fromContext and as a result, make code more synchronous. Since "runtime template" support was removed, we could remove the `forceRuntimeTemplates` parameter from `Templates.fromContext`. However, the result of this is that calling `Templates.fromContext` would not need the `DartdocGeneratorOptionContext` parameter either, and is then just an alias for the `HtmlAotTemplates` constructor. So we can remove the function altogether. Since we no longer need to `await` the result of calling this function (which is now a constructor call), we can make more and more functions synchronous (see https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/): `bin/dartdoc.dart`'s `main`, `Dartdoc.fromContext` (which can new be a factory constructor), `initHtmlGenerator`, `buildDartdoc`, `generatorContextFromArgv`, `createPackageBuilder`. We can also remove some more code that becomes dead: `RuntimeTemplates._create`, ResourceProvider utilities, and some trivial `buildDartdoc` test helpers. --- bin/dartdoc.dart | 6 +- lib/src/dartdoc.dart | 11 +- lib/src/generator/html_generator.dart | 11 +- lib/src/generator/resource_loader.dart | 23 +---- lib/src/generator/templates.dart | 76 +------------- test/dartdoc_test_base.dart | 8 +- test/end2end/dartdoc_test.dart | 24 ++--- test/mustachio/renderers_output_test.dart | 118 ---------------------- test/options_test.dart | 35 +++---- test/resource_loader_test.dart | 24 ----- test/src/utils.dart | 4 +- test/templates/category_test.dart | 20 ++-- test/templates/enum_test.dart | 4 +- test/templates/extension_test.dart | 17 ++-- test/templates/template_test_base.dart | 2 +- 15 files changed, 61 insertions(+), 322 deletions(-) delete mode 100644 test/mustachio/renderers_output_test.dart delete mode 100644 test/resource_loader_test.dart diff --git a/bin/dartdoc.dart b/bin/dartdoc.dart index 1427a4e841..a79652d2ff 100644 --- a/bin/dartdoc.dart +++ b/bin/dartdoc.dart @@ -2,8 +2,6 @@ // 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 'package:dartdoc/src/dartdoc.dart'; import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/model/model.dart'; @@ -12,7 +10,7 @@ import 'package:dartdoc/src/package_meta.dart'; /// Analyzes Dart files and generates a representation of included libraries, /// classes, and members. Uses the current directory to look for libraries. -Future main(List arguments) async { +void main(List arguments) { var config = parseOptions(pubPackageMetaProvider, arguments); if (config == null) { // Do not run dartdoc as there was either a fatal error parsing options, or @@ -23,7 +21,7 @@ Future main(List arguments) async { final packageBuilder = PubPackageBuilder(config, pubPackageMetaProvider, packageConfigProvider); final dartdoc = config.generateDocs - ? await Dartdoc.fromContext(config, packageBuilder) + ? Dartdoc.fromContext(config, packageBuilder) : Dartdoc.withEmptyGenerator(config, packageBuilder); dartdoc.executeGuarded(); } diff --git a/lib/src/dartdoc.dart b/lib/src/dartdoc.dart index ccbcb28c8f..4ed7e9abb6 100644 --- a/lib/src/dartdoc.dart +++ b/lib/src/dartdoc.dart @@ -145,7 +145,7 @@ class Dartdoc { set generator(Generator newGenerator) => _generator = newGenerator; /// Factory method that builds Dartdoc with an empty generator. - static Dartdoc withEmptyGenerator( + factory Dartdoc.withEmptyGenerator( DartdocOptionContext config, PackageBuilder packageBuilder, ) { @@ -157,12 +157,11 @@ class Dartdoc { ); } - /// Asynchronous factory method that builds Dartdoc with a generator - /// determined by the given context. - static Future fromContext( + /// Builds Dartdoc with a generator determined by [context]. + factory Dartdoc.fromContext( DartdocGeneratorOptionContext context, PackageBuilder packageBuilder, - ) async { + ) { var resourceProvider = context.resourceProvider; var outputPath = resourceProvider.pathContext.absolute(context.output); var outputDir = resourceProvider.getFolder(outputPath)..create(); @@ -175,7 +174,7 @@ class Dartdoc { return Dartdoc._( context, outputDir, - await initHtmlGenerator(context, writer: writer), + initHtmlGenerator(context, writer: writer), packageBuilder, ); } diff --git a/lib/src/generator/html_generator.dart b/lib/src/generator/html_generator.dart index a3a2623fef..c179e3eefe 100644 --- a/lib/src/generator/html_generator.dart +++ b/lib/src/generator/html_generator.dart @@ -12,18 +12,13 @@ import 'package:dartdoc/src/generator/template_data.dart'; import 'package:dartdoc/src/generator/templates.dart'; import 'package:dartdoc/src/model/model.dart'; import 'package:dartdoc/src/runtime_stats.dart'; -import 'package:meta/meta.dart'; /// Creates a [Generator] with an [HtmlGeneratorBackend] backend. -/// -/// [forceRuntimeTemplates] should only be given `true` during tests. -Future initHtmlGenerator( +Generator initHtmlGenerator( DartdocGeneratorOptionContext context, { required FileWriter writer, - @visibleForTesting bool forceRuntimeTemplates = false, -}) async { - var templates = await Templates.fromContext(context, - forceRuntimeTemplates: forceRuntimeTemplates); +}) { + var templates = HtmlAotTemplates(); var options = DartdocGeneratorBackendOptions.fromContext(context); var backend = HtmlGeneratorBackend( options, templates, writer, context.resourceProvider); diff --git a/lib/src/generator/resource_loader.dart b/lib/src/generator/resource_loader.dart index 70b0a1094d..847b59aca3 100644 --- a/lib/src/generator/resource_loader.dart +++ b/lib/src/generator/resource_loader.dart @@ -5,33 +5,12 @@ /// Make it possible to load resources from the dartdoc code repository. library; -import 'dart:convert' show utf8; import 'dart:isolate' show Isolate; + import 'package:analyzer/file_system/file_system.dart'; import 'package:meta/meta.dart'; extension ResourceLoader on ResourceProvider { - /// Loads a `package:` resource as a String. - Future loadResourceAsString(String path) async { - var bytes = await loadResourceAsBytes(path); - - return utf8.decode(bytes); - } - - /// Loads a `package:` resource as an [List]. - Future> loadResourceAsBytes(String path) async { - if (!path.startsWith('package:')) { - throw ArgumentError('path must begin with package:'); - } - - return (await getResourceFile(path)).readAsBytesSync(); - } - - Future getResourceFile(String path) async { - var uri = await resolveResourceUri(Uri.parse(path)); - return getFile(uri.toFilePath()); - } - Future getResourceFolder(String path) async { var uri = await resolveResourceUri(Uri.parse(path)); return getFolder(uri.toFilePath()); diff --git a/lib/src/generator/templates.dart b/lib/src/generator/templates.dart index f7b3993cb7..daff1f0e67 100644 --- a/lib/src/generator/templates.dart +++ b/lib/src/generator/templates.dart @@ -42,11 +42,7 @@ @Renderer(#renderTypedef, Context(), 'typedef') library; -import 'package:analyzer/file_system/file_system.dart'; -import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/element_type.dart'; -import 'package:dartdoc/src/failure.dart'; -import 'package:dartdoc/src/generator/resource_loader.dart'; import 'package:dartdoc/src/generator/template_data.dart'; import 'package:dartdoc/src/generator/templates.aot_renderers_for_html.dart' as aot_renderers_for_html; @@ -115,23 +111,6 @@ abstract class Templates { String renderSidebarForLibrary(TemplateDataWithLibrary context); String renderTopLevelProperty(TopLevelPropertyTemplateData context); String renderTypedef(TypedefTemplateData context); - - /// Creates a [Templates] instance from the default set of templates. - /// - /// [forceRuntimeTemplates] should only be given `true` during tests. - static Future fromContext(DartdocGeneratorOptionContext context, - // TODO(srawlins): Remove this option, as runtime templates are no longer - // supported. - {bool forceRuntimeTemplates = false}) async { - if (forceRuntimeTemplates) { - var directory = await context.resourceProvider - .getResourceFolder('package:dartdoc/templates'); - return RuntimeTemplates._create(directory, - resourceProvider: context.resourceProvider); - } else { - return HtmlAotTemplates(); - } - } } /// The [Templates] implementation which uses the render functions generated @@ -221,6 +200,8 @@ class HtmlAotTemplates implements Templates { } /// The collection of [Template] objects parsed at runtime. +// TODO(srawlins): Remove this class, and `templates.runtime_renderers.dart`, as +// runtime templates are no longer supported. class RuntimeTemplates implements Templates { @override String renderCategory(CategoryTemplateData context) => @@ -325,59 +306,6 @@ class RuntimeTemplates implements Templates { final Template _topLevelPropertyTemplate; final Template _typedefTemplate; - /// Creates a [Templates] from a custom set of template files, found in [dir]. - static Future _create(Folder dir, - {required ResourceProvider resourceProvider}) async { - Future