Skip to content

Remove all use of package:build #3448

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 10 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/src/generator/html_resources.g.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: This file is auto-generated. Do not edit.

// TODO(srawlins): Generate this code with a simple script. See
// https://github.com/dart-lang/dartdoc/blob/6df872abbdfbbc4fdcfc795c1836e0613c7f6c8b/tool/builder.dart
// for the previous 'package:build' implementation.
const List<String> resourceNames = [
'docs.dart.js',
'docs.dart.js.map',
Expand Down
32 changes: 16 additions & 16 deletions lib/src/mustachio/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ class Renderer {
String standardTemplateBasename, {
this.visibleTypes = const {},
}) : standardHtmlTemplate =
'package:dartdoc/templates/html/$standardTemplateBasename.html',
standardMdTemplate =
'package:dartdoc/templates/md/$standardTemplateBasename.md';
'lib/templates/html/$standardTemplateBasename.html',
standardMdTemplate = 'lib/templates/md/$standardTemplateBasename.md';

@visibleForTesting
const Renderer.forTest(
this.name,
this.context,
String standardTemplateBasename, {
this.visibleTypes = const {},
}) : standardHtmlTemplate = 'templates/$standardTemplateBasename.html',
standardMdTemplate = 'templates/$standardTemplateBasename.md';
}) : standardHtmlTemplate =
'test/mustachio/templates/$standardTemplateBasename.html',
standardMdTemplate =
'test/mustachio/templates/$standardTemplateBasename.md';
}

/// A container for a type, [T], which is the type of a context object,
Expand Down Expand Up @@ -94,29 +95,28 @@ class RendererSpec {

final String standardMdTemplate;

final Map<TemplateFormat, Uri?> standardTemplateUris;
final Map<TemplateFormat, String?> standardTemplatePaths;

RendererSpec(
this.name,
this.contextType,
this.visibleTypes,
this.standardHtmlTemplate,
this.standardMdTemplate,
) : standardTemplateUris = {
TemplateFormat.html: _parseUriFromAnnotation(standardHtmlTemplate),
TemplateFormat.md: _parseUriFromAnnotation(standardMdTemplate),
) : standardTemplatePaths = {
TemplateFormat.html: standardHtmlTemplate,
TemplateFormat.md: standardMdTemplate,
};

/// Parses a URI from a String which comes from a const annotation object.
///
/// The String value may be the literal value, 'null'.
static Uri? _parseUriFromAnnotation(String unparsed) =>
unparsed == 'null' ? null : Uri.parse(unparsed);

InterfaceElement get contextElement => contextType.element;
}

enum TemplateFormat {
html,
md,
md;

String aotLibraryPath(String base) => switch (this) {
TemplateFormat.html => '$base.aot_renderers_for_html.dart',
TemplateFormat.md => '$base.aot_renderers_for_md.dart',
};
}
4 changes: 2 additions & 2 deletions lib/src/mustachio/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class MustachioParser {
/// The index of the character currently being parsed.
int _index = 0;

MustachioParser(this.template, Uri url)
MustachioParser(this.template, String path)
: _templateLength = template.length,
_sourceFile = SourceFile.fromString(template, url: url);
_sourceFile = SourceFile.fromString(template, url: path);

/// Parses [template] into a sequence of [MustachioNode]s.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/mustachio/renderer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Template {
// 2) In the case of a reference from a top-level template, user code has
// called [Template.parse], and the user is responsible for handling the
// exception.
var ast = MustachioParser(file.readAsStringSync(), file.toUri()).parse();
var ast = MustachioParser(file.readAsStringSync(), file.path).parse();
var nodeQueue = Queue.of(ast);
var partials = <String, File>{};

Expand Down
4 changes: 0 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ dependencies:

dev_dependencies:
async: ^2.11.0
build: ^2.4.0
build_runner: ^2.4.2
build_test: ^2.1.7
build_version: ^2.1.1
code_builder: ^4.4.0
dart_style: ^2.3.1
grinder: ^0.9.4
Expand Down
126 changes: 66 additions & 60 deletions test/mustachio/aot_compiler_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@
// 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.

@Timeout.factor(4)
import 'dart:convert';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
@TestOn('vm && !windows')
library;

import 'dart:io';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import 'builder_test_base.dart';

void main() {
late InMemoryAssetWriter writer;

Future<LibraryElement> resolveGeneratedLibrary() async {
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var writtenStrings = writer.assets
.map((id, content) => MapEntry(id.toString(), utf8.decode(content)));
return await resolveSources(writtenStrings,
(Resolver resolver) => resolver.libraryFor(rendererAsset));
}

setUp(() {
writer = InMemoryAssetWriter();
});

test('builds renderers from multiple annotations', () async {
await testMustachioBuilder(
writer,
'''
class Foo {
String s1 = 'hello';
Expand All @@ -40,14 +25,17 @@ class Baz {}
@Renderer(#renderFoo, Context<Foo>(), 'foo')
@Renderer(#renderBar, Context<Bar>(), 'bar')
library foo;
import 'package:mustachio/annotations.dart';
import 'annotations.dart';
''',
additionalAssets: {
'foo|lib/templates/html/foo.html': '{{ >foo_header }}',
'foo|lib/templates/html/_foo_header.html': 'EMPTY',
},
additionalAssets: () => [
d.dir('lib/templates/html', [
d.file('foo.html', '{{ >foo_header }}'),
d.file('_foo_header.html', 'EMPTY'),
]),
],
);
var renderersLibrary = await resolveGeneratedLibrary();
var renderersLibrary =
await resolveGeneratedLibrary(aotRenderersForHtmlPath);

expect(renderersLibrary.getTopLevelFunction('renderFoo'), isNotNull);
expect(renderersLibrary.getTopLevelFunction('renderBar'), isNotNull);
Expand All @@ -57,24 +45,33 @@ import 'package:mustachio/annotations.dart';
}, timeout: Timeout.factor(2));

test('builds a public API render function', () async {
await testMustachioBuilder(writer, '''
await testMustachioBuilder(
'''
class Foo<T> {
String s1 = 'hello';
}
''', libraryFrontMatter: '''
''',
libraryFrontMatter: '''
@Renderer(#renderFoo, Context<Foo>(), 'foo')
library foo;
import 'package:mustachio/annotations.dart';
''');
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
import 'annotations.dart';
''',
additionalAssets: () => [
d.dir('lib/templates/html', [
d.file('foo.html', 's1 is {{ s1 }}'),
]),
d.dir('md', [
d.file('foo.md', 's1 is {{ s1 }}'),
]),
],
);
var generatedContent = await File(aotRenderersForHtmlPath).readAsString();
expect(
generatedContent, contains('String renderFoo<T>(_i1.Foo<T> context0)'));
});

test('builds a private render function for a partial', () async {
await testMustachioBuilder(
writer,
'''
class Foo<T> {
String s1 = 'hello';
Expand All @@ -83,30 +80,36 @@ class Foo<T> {
libraryFrontMatter: '''
@Renderer(#renderFoo, Context<Foo>(), 'foo')
library foo;
import 'package:mustachio/annotations.dart';
import 'annotations.dart';
''',
additionalAssets: {
'foo|lib/templates/html/foo.html': '{{ >foo_header }}',
'foo|lib/templates/html/_foo_header.html': 's1 is {{ s1 }}',
},
additionalAssets: () => [
d.dir('lib', [
d.dir('templates', [
d.dir('html', [
d.file('foo.html', '{{ >foo_header }}'),
d.file('_foo_header.html', 's1 is {{ s1 }}'),
]),
]),
]),
],
);
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
var generatedContent = await File(aotRenderersForHtmlPath).readAsString();
expect(
generatedContent,
contains(
'String _renderFoo_partial_foo_header_0<T>(_i1.Foo<T> context0)'));
});

test('builds a renderer for a generic, bounded type', () async {
await testMustachioBuilder(writer, '''
await testMustachioBuilder('''
class Foo<T extends num> {
String s1 = 'hello';
}
class Bar {}
class Baz {}
''');
var renderersLibrary = await resolveGeneratedLibrary();
var renderersLibrary =
await resolveGeneratedLibrary(aotRenderersForHtmlPath);

var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!;
expect(fooRenderFunction.typeParameters, hasLength(1));
Expand All @@ -116,7 +119,6 @@ class Baz {}

test('deduplicates partials which share context type LUB', () async {
await testMustachioBuilder(
writer,
'''
abstract class Base {
String get s1;
Expand All @@ -136,16 +138,17 @@ class Bar implements Base {
@Renderer(#renderFoo, Context<Foo>(), 'foo')
@Renderer(#renderBar, Context<Bar>(), 'bar')
library foo;
import 'package:mustachio/annotations.dart';
import 'annotations.dart';
''',
additionalAssets: {
'foo|lib/templates/html/foo.html': '{{ >base }}',
'foo|lib/templates/html/bar.html': '{{ >base }}',
'foo|lib/templates/html/_base.html': 's1 is {{ s1 }}',
},
additionalAssets: () => [
d.dir('lib/templates/html', [
d.file('foo.html', '{{ >base }}'),
d.file('bar.html', '{{ >base }}'),
d.file('_base.html', 's1 is {{ s1 }}'),
]),
],
);
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
var generatedContent = await File(aotRenderersForHtmlPath).readAsString();
expect(
generatedContent,
contains('String _renderFoo_partial_base_0(_i1.Foo context0) =>\n'
Expand All @@ -165,7 +168,6 @@ import 'package:mustachio/annotations.dart';
test('does not deduplicate partials when attempting to do so throws',
() async {
await testMustachioBuilder(
writer,
'''
abstract class Base {}

Expand All @@ -183,16 +185,17 @@ class Bar implements Base {
@Renderer(#renderFoo, Context<Foo>(), 'foo')
@Renderer(#renderBar, Context<Bar>(), 'bar')
library foo;
import 'package:mustachio/annotations.dart';
import 'annotations.dart';
''',
additionalAssets: {
'foo|lib/templates/html/foo.html': '{{ >base }}',
'foo|lib/templates/html/bar.html': '{{ >base }}',
'foo|lib/templates/html/_base.html': 's1 is {{ s1 }}',
},
additionalAssets: () => [
d.dir('lib/templates/html', [
d.file('foo.html', '{{ >base }}'),
d.file('bar.html', '{{ >base }}'),
d.file('_base.html', 's1 is {{ s1 }}'),
]),
],
);
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
var generatedContent = await File(aotRenderersForHtmlPath).readAsString();
expect(
generatedContent,
contains('String _renderFoo_partial_base_0(_i1.Foo context0) {'),
Expand All @@ -203,3 +206,6 @@ import 'package:mustachio/annotations.dart';
);
});
}

String get aotRenderersForHtmlPath =>
'${d.sandbox}/foo_package/lib/foo.aot_renderers_for_html.dart';
Loading