Skip to content

Update nnbd branch for latest changes and new resources-dir option #2867

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 8 commits into from
Dec 30, 2021
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
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ analyzer:
linter:
rules:
always_declare_return_types: true
annotate_overrides: true
avoid_dynamic_calls: true
avoid_single_cascade_in_expression_statements: true
avoid_unused_constructor_parameters: true
Expand Down
1 change: 0 additions & 1 deletion analysis_options_presubmit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ analyzer:
linter:
rules:
always_declare_return_types: true
annotate_overrides: true
avoid_dynamic_calls: true
avoid_single_cascade_in_expression_statements: true
avoid_unused_constructor_parameters: true
Expand Down
3 changes: 2 additions & 1 deletion lib/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ class DartdocGeneratorOptionContext extends DartdocOptionContext {
String get relCanonicalPrefix =>
optionSet['relCanonicalPrefix'].valueAt(context);

/// The 'templatesDir' Dartdoc option if one was specified; otherwise `null`.
String? get templatesDir => optionSet['templatesDir'].valueAt(context);

// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
@override
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);

String? get resourcesDir => optionSet['resourcesDir'].valueAt(context);
}

class DartdocProgramOptionContext extends DartdocGeneratorOptionContext
Expand Down
2 changes: 2 additions & 0 deletions lib/src/dartdoc_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,8 @@ Future<List<DartdocOption>> createDartdocOptions(
help:
'A list of package names to place first when grouping libraries in '
'packages. Unmentioned packages are sorted after these.'),
DartdocOptionArgOnly<String?>('resourcesDir', null, resourceProvider,
help: "An absolute path to dartdoc's resources directory.", hide: true),
DartdocOptionArgOnly<bool>('sdkDocs', false, resourceProvider,
help: 'Generate ONLY the docs for the Dart SDK.'),
DartdocOptionArgSynth<String?>('sdkDir',
Expand Down
5 changes: 4 additions & 1 deletion lib/src/generator/dartdoc_generator_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
@override
final String customInnerFooterText;

final String? resourcesDir;

DartdocGeneratorBackendOptions.fromContext(
DartdocGeneratorOptionContext context)
: relCanonicalPrefix = context.relCanonicalPrefix,
Expand All @@ -47,7 +49,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
useBaseHref = context.useBaseHref,
customHeaderContent = context.header,
customFooterContent = context.footer,
customInnerFooterText = context.footerText;
customInnerFooterText = context.footerText,
resourcesDir = context.resourcesDir;
}

class SidebarGenerator<T extends TemplateData> {
Expand Down
26 changes: 14 additions & 12 deletions lib/src/generator/html_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,28 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
// Allow overwrite of favicon.
var bytes = resourceProvider.getFile(favicon).readAsBytesSync();
writer.writeBytes(
resourceProvider.pathContext.join('static-assets', 'favicon.png'),
_pathJoin('static-assets', 'favicon.png'),
bytes,
allowOverwrite: true,
);
}
}

Future<void> _copyResources(FileWriter writer) async {
for (var resourcePath in resources.resourceNames) {
if (!resourcePath.startsWith(_dartdocResourcePrefix)) {
throw StateError('Resource paths must start with '
'$_dartdocResourcePrefix, encountered $resourcePath');
}
var destFileName = resourcePath.substring(_dartdocResourcePrefix.length);
var destFilePath =
resourceProvider.pathContext.join('static-assets', destFileName);
writer.writeBytes(destFilePath,
await resourceProvider.loadResourceAsBytes(resourcePath));
var resourcesDir = options.resourcesDir ??
(await resourceProvider.getResourceFolder(_dartdocResourcePrefix)).path;
for (var resourceFileName in resources.resourceNames) {
var destinationPath = _pathJoin('static-assets', resourceFileName);
var sourcePath = _pathJoin(resourcesDir, resourceFileName);
writer.writeBytes(
destinationPath,
resourceProvider.getFile(sourcePath).readAsBytesSync(),
);
}
}

static const _dartdocResourcePrefix = 'package:dartdoc/resources/';
String _pathJoin(String a, String b) =>
resourceProvider.pathContext.join(a, b);

static const _dartdocResourcePrefix = 'package:dartdoc/resources';
}
14 changes: 7 additions & 7 deletions lib/src/generator/html_resources.g.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// WARNING: This file is auto-generated. Do not edit.

const List<String> resourceNames = [
'package:dartdoc/resources/favicon.png',
'package:dartdoc/resources/github.css',
'package:dartdoc/resources/highlight.pack.js',
'package:dartdoc/resources/play_button.svg',
'package:dartdoc/resources/readme.md',
'package:dartdoc/resources/script.js',
'package:dartdoc/resources/styles.css'
'favicon.png',
'github.css',
'highlight.pack.js',
'play_button.svg',
'readme.md',
'script.js',
'styles.css',
];
2 changes: 1 addition & 1 deletion lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ abstract class ModelElement extends Canonicalization
if (e is FieldElement) {
if (enclosingContainer == null) {
if (e.isEnumConstant) {
var index = e.computeConstantValue()!.getField(e.name)!.toIntValue();
var index = e.computeConstantValue()!.getField('index')!.toIntValue();
newModelElement =
EnumField.forConstant(index, e, library, packageGraph, getter);
} else if (e.enclosingElement is ExtensionElement) {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/model/package_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import 'package:analyzer/src/dart/sdk/sdk.dart'
// ignore: implementation_imports
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
// ignore: implementation_imports
import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
// ignore: implementation_imports
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
import 'package:collection/collection.dart' show IterableExtension;
import 'package:dartdoc/src/dartdoc_options.dart';
Expand Down Expand Up @@ -144,16 +142,18 @@ class PubPackageBuilder implements PackageBuilder {
/// If [filePath] is not a library, returns null.
Future<DartDocResolvedLibrary?> processLibrary(String filePath) async {
var name = filePath;
var directoryCurrentPath = resourceProvider.pathContext.current;
var pathContext = resourceProvider.pathContext;
var directoryCurrentPath = pathContext.current;

if (name.startsWith(directoryCurrentPath)) {
name = name.substring(directoryCurrentPath.length);
if (name.startsWith(resourceProvider.pathContext.separator)) {
if (name.startsWith(pathContext.separator)) {
name = name.substring(1);
}
}
var javaFile = JavaFile(filePath).getAbsoluteFile();
filePath = javaFile.getPath();

// TODO(scheglov) Do we need this? Maybe the argument is already valid?
filePath = pathContext.normalize(pathContext.absolute(filePath));

var analysisContext = contextCollection!.contextFor(config.inputDir);
var session = analysisContext.currentSession;
Expand Down
3 changes: 1 addition & 2 deletions test/html_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ void main() {
.getFolder(pathContext.join(outputPath, 'static-assets'));
expect(output, doesExist);

for (var resource in resourceNames.map((r) =>
pathContext.relative(Uri.parse(r).path, from: 'dartdoc/resources'))) {
for (var resource in resourceNames) {
expect(
resourceProvider.getFile(pathContext.join(output.path, resource)),
doesExist);
Expand Down
1 change: 1 addition & 0 deletions testing/test_package/lib/features/nullable_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void set nullableSetter(String? value) {
}

/// This should have return type of `Future?`.
// ignore: unnecessary_question_mark
dynamic? oddAsyncFunction() async {}

/// This should also have return type of `Future?`.
Expand Down
1 change: 1 addition & 0 deletions testing/test_package/lib/features/opt_out_of_nnbd.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

library opt_out_of_nnbd;

// ignore: dead_code
String notOptedIn = false ? 'hi' : null;
2 changes: 2 additions & 0 deletions testing/test_package/lib/src/somelib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class ExtendedBaseReexported extends BaseReexported {}

/// A private extension.
extension _Unseen on Object {
// ignore: unused_element
void doYouSeeMe() {}
}

/// An extension without a name
extension on List {
// ignore: unused_element
void somethingNew() {}
}

Expand Down
17 changes: 10 additions & 7 deletions tool/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ String _resourcesFile(Iterable<String> packagePaths) => '''
// WARNING: This file is auto-generated. Do not edit.

const List<String> resourceNames = [
${packagePaths.map((p) => " '$p'").join(',\n')}
${packagePaths.map((p) => " '$p',").join('\n')}
];
''';

class ResourceBuilder implements Builder {
final BuilderOptions builderOptions;

ResourceBuilder(this.builderOptions);

static final _allResources = Glob('lib/resources/**');
static const _resourcesPath = 'lib/resources';

@override
Future<void> build(BuildStep buildStep) async {
var packagePaths = <String>[];
await for (AssetId asset in buildStep.findAssets(_allResources)) {
packagePaths.add(asset.uri.toString());
}
packagePaths.sort();
var resourceAssets =
await buildStep.findAssets(Glob('$_resourcesPath/**')).toList();
var packagePaths = [
for (var asset in resourceAssets)
path.url.relative(asset.path, from: _resourcesPath),
]..sort();
await buildStep.writeAsString(
AssetId(buildStep.inputId.package,
path.url.join('lib', 'src', 'generator', 'html_resources.g.dart')),
Expand Down
18 changes: 13 additions & 5 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,10 @@ void analyzeTestPackages() async {
workingDirectory: testPackagePath,
);
await SubprocessLauncher('analyze-test-package').runStreamed(
sdkBin('dartanalyzer'),
['.'],
sdkBin('dart'),
// TODO(srawlins): Analyze the whole directory by ignoring the pubspec
// reports.
['analyze', 'lib'],
workingDirectory: testPackagePath,
);
}
Expand Down Expand Up @@ -548,9 +550,15 @@ Future<void> testWithAnalyzerSdk() async {
var sdkDartdoc = await createSdkDartdoc();
var defaultGrindParameter =
Platform.environment['DARTDOC_GRIND_STEP'] ?? 'test';
await launcher.runStreamed(
sdkBin('pub'), ['run', 'grinder', defaultGrindParameter],
workingDirectory: sdkDartdoc);
// TODO(srawlins): Re-enable sdk-analyzer when dart_style is published using
// analyzer 3.0.0.
try {
await launcher.runStreamed(
sdkBin('pub'), ['run', 'grinder', defaultGrindParameter],
workingDirectory: sdkDartdoc);
} catch (e, st) {
print('Warning: SDK analyzer job threw "$e":\n$st');
}
}

Future<Iterable<Map<String, Object>>> _buildSdkDocs(
Expand Down