Skip to content

Commit 55ca10f

Browse files
parloughscheglovsrawlins
authored
Update nnbd branch for latest changes and new resources-dir option (#2867)
* Stop using JavaFile. (#2856) * Add a resources-dir option (#2857) * Skip sdk-analyzer job (#2863) * Use 'index' to access the enum value index. (#2865) Co-authored-by: Sam Rawlins <[email protected]> * Re-enable sdk-analyzer job, but make it not failing (#2864) * Update new resources-dir option for null safety * Fix html generator test merge Co-authored-by: Konstantin Scheglov <[email protected]> Co-authored-by: Sam Rawlins <[email protected]>
1 parent 4fe6b1d commit 55ca10f

15 files changed

+64
-44
lines changed

analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ analyzer:
2020
linter:
2121
rules:
2222
always_declare_return_types: true
23-
annotate_overrides: true
2423
avoid_dynamic_calls: true
2524
avoid_single_cascade_in_expression_statements: true
2625
avoid_unused_constructor_parameters: true

analysis_options_presubmit.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ analyzer:
2525
linter:
2626
rules:
2727
always_declare_return_types: true
28-
annotate_overrides: true
2928
avoid_dynamic_calls: true
3029
avoid_single_cascade_in_expression_statements: true
3130
avoid_unused_constructor_parameters: true

lib/options.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ class DartdocGeneratorOptionContext extends DartdocOptionContext {
4141
String get relCanonicalPrefix =>
4242
optionSet['relCanonicalPrefix'].valueAt(context);
4343

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

4746
// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
4847
@override
4948
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
49+
50+
String? get resourcesDir => optionSet['resourcesDir'].valueAt(context);
5051
}
5152

5253
class DartdocProgramOptionContext extends DartdocGeneratorOptionContext

lib/src/dartdoc_options.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,8 @@ Future<List<DartdocOption>> createDartdocOptions(
15071507
help:
15081508
'A list of package names to place first when grouping libraries in '
15091509
'packages. Unmentioned packages are sorted after these.'),
1510+
DartdocOptionArgOnly<String?>('resourcesDir', null, resourceProvider,
1511+
help: "An absolute path to dartdoc's resources directory.", hide: true),
15101512
DartdocOptionArgOnly<bool>('sdkDocs', false, resourceProvider,
15111513
help: 'Generate ONLY the docs for the Dart SDK.'),
15121514
DartdocOptionArgSynth<String?>('sdkDir',

lib/src/generator/dartdoc_generator_backend.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
3838
@override
3939
final String customInnerFooterText;
4040

41+
final String? resourcesDir;
42+
4143
DartdocGeneratorBackendOptions.fromContext(
4244
DartdocGeneratorOptionContext context)
4345
: relCanonicalPrefix = context.relCanonicalPrefix,
@@ -47,7 +49,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
4749
useBaseHref = context.useBaseHref,
4850
customHeaderContent = context.header,
4951
customFooterContent = context.footer,
50-
customInnerFooterText = context.footerText;
52+
customInnerFooterText = context.footerText,
53+
resourcesDir = context.resourcesDir;
5154
}
5255

5356
class SidebarGenerator<T extends TemplateData> {

lib/src/generator/html_generator.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,28 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
5252
// Allow overwrite of favicon.
5353
var bytes = resourceProvider.getFile(favicon).readAsBytesSync();
5454
writer.writeBytes(
55-
resourceProvider.pathContext.join('static-assets', 'favicon.png'),
55+
_pathJoin('static-assets', 'favicon.png'),
5656
bytes,
5757
allowOverwrite: true,
5858
);
5959
}
6060
}
6161

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

76-
static const _dartdocResourcePrefix = 'package:dartdoc/resources/';
75+
String _pathJoin(String a, String b) =>
76+
resourceProvider.pathContext.join(a, b);
77+
78+
static const _dartdocResourcePrefix = 'package:dartdoc/resources';
7779
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// WARNING: This file is auto-generated. Do not edit.
22

33
const List<String> resourceNames = [
4-
'package:dartdoc/resources/favicon.png',
5-
'package:dartdoc/resources/github.css',
6-
'package:dartdoc/resources/highlight.pack.js',
7-
'package:dartdoc/resources/play_button.svg',
8-
'package:dartdoc/resources/readme.md',
9-
'package:dartdoc/resources/script.js',
10-
'package:dartdoc/resources/styles.css'
4+
'favicon.png',
5+
'github.css',
6+
'highlight.pack.js',
7+
'play_button.svg',
8+
'readme.md',
9+
'script.js',
10+
'styles.css',
1111
];

lib/src/model/model_element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ abstract class ModelElement extends Canonicalization
177177
if (e is FieldElement) {
178178
if (enclosingContainer == null) {
179179
if (e.isEnumConstant) {
180-
var index = e.computeConstantValue()!.getField(e.name)!.toIntValue();
180+
var index = e.computeConstantValue()!.getField('index')!.toIntValue();
181181
newModelElement =
182182
EnumField.forConstant(index, e, library, packageGraph, getter);
183183
} else if (e.enclosingElement is ExtensionElement) {

lib/src/model/package_builder.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import 'package:analyzer/src/dart/sdk/sdk.dart'
1919
// ignore: implementation_imports
2020
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
2121
// ignore: implementation_imports
22-
import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
23-
// ignore: implementation_imports
2422
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
2523
import 'package:collection/collection.dart' show IterableExtension;
2624
import 'package:dartdoc/src/dartdoc_options.dart';
@@ -144,16 +142,18 @@ class PubPackageBuilder implements PackageBuilder {
144142
/// If [filePath] is not a library, returns null.
145143
Future<DartDocResolvedLibrary?> processLibrary(String filePath) async {
146144
var name = filePath;
147-
var directoryCurrentPath = resourceProvider.pathContext.current;
145+
var pathContext = resourceProvider.pathContext;
146+
var directoryCurrentPath = pathContext.current;
148147

149148
if (name.startsWith(directoryCurrentPath)) {
150149
name = name.substring(directoryCurrentPath.length);
151-
if (name.startsWith(resourceProvider.pathContext.separator)) {
150+
if (name.startsWith(pathContext.separator)) {
152151
name = name.substring(1);
153152
}
154153
}
155-
var javaFile = JavaFile(filePath).getAbsoluteFile();
156-
filePath = javaFile.getPath();
154+
155+
// TODO(scheglov) Do we need this? Maybe the argument is already valid?
156+
filePath = pathContext.normalize(pathContext.absolute(filePath));
157157

158158
var analysisContext = contextCollection!.contextFor(config.inputDir);
159159
var session = analysisContext.currentSession;

test/html_generator_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ void main() {
146146
.getFolder(pathContext.join(outputPath, 'static-assets'));
147147
expect(output, doesExist);
148148

149-
for (var resource in resourceNames.map((r) =>
150-
pathContext.relative(Uri.parse(r).path, from: 'dartdoc/resources'))) {
149+
for (var resource in resourceNames) {
151150
expect(
152151
resourceProvider.getFile(pathContext.join(output.path, resource)),
153152
doesExist);

testing/test_package/lib/features/nullable_elements.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void set nullableSetter(String? value) {
1515
}
1616

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

2021
/// This should also have return type of `Future?`.

testing/test_package/lib/features/opt_out_of_nnbd.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77
library opt_out_of_nnbd;
88

9+
// ignore: dead_code
910
String notOptedIn = false ? 'hi' : null;

testing/test_package/lib/src/somelib.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class ExtendedBaseReexported extends BaseReexported {}
1818

1919
/// A private extension.
2020
extension _Unseen on Object {
21+
// ignore: unused_element
2122
void doYouSeeMe() {}
2223
}
2324

2425
/// An extension without a name
2526
extension on List {
27+
// ignore: unused_element
2628
void somethingNew() {}
2729
}
2830

tool/builder.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ String _resourcesFile(Iterable<String> packagePaths) => '''
1212
// WARNING: This file is auto-generated. Do not edit.
1313
1414
const List<String> resourceNames = [
15-
${packagePaths.map((p) => " '$p'").join(',\n')}
15+
${packagePaths.map((p) => " '$p',").join('\n')}
1616
];
1717
''';
1818

1919
class ResourceBuilder implements Builder {
2020
final BuilderOptions builderOptions;
21+
2122
ResourceBuilder(this.builderOptions);
2223

23-
static final _allResources = Glob('lib/resources/**');
24+
static const _resourcesPath = 'lib/resources';
25+
2426
@override
2527
Future<void> build(BuildStep buildStep) async {
26-
var packagePaths = <String>[];
27-
await for (AssetId asset in buildStep.findAssets(_allResources)) {
28-
packagePaths.add(asset.uri.toString());
29-
}
30-
packagePaths.sort();
28+
var resourceAssets =
29+
await buildStep.findAssets(Glob('$_resourcesPath/**')).toList();
30+
var packagePaths = [
31+
for (var asset in resourceAssets)
32+
path.url.relative(asset.path, from: _resourcesPath),
33+
]..sort();
3134
await buildStep.writeAsString(
3235
AssetId(buildStep.inputId.package,
3336
path.url.join('lib', 'src', 'generator', 'html_resources.g.dart')),

tool/grind.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ void analyzeTestPackages() async {
260260
workingDirectory: testPackagePath,
261261
);
262262
await SubprocessLauncher('analyze-test-package').runStreamed(
263-
sdkBin('dartanalyzer'),
264-
['.'],
263+
sdkBin('dart'),
264+
// TODO(srawlins): Analyze the whole directory by ignoring the pubspec
265+
// reports.
266+
['analyze', 'lib'],
265267
workingDirectory: testPackagePath,
266268
);
267269
}
@@ -548,9 +550,15 @@ Future<void> testWithAnalyzerSdk() async {
548550
var sdkDartdoc = await createSdkDartdoc();
549551
var defaultGrindParameter =
550552
Platform.environment['DARTDOC_GRIND_STEP'] ?? 'test';
551-
await launcher.runStreamed(
552-
sdkBin('pub'), ['run', 'grinder', defaultGrindParameter],
553-
workingDirectory: sdkDartdoc);
553+
// TODO(srawlins): Re-enable sdk-analyzer when dart_style is published using
554+
// analyzer 3.0.0.
555+
try {
556+
await launcher.runStreamed(
557+
sdkBin('pub'), ['run', 'grinder', defaultGrindParameter],
558+
workingDirectory: sdkDartdoc);
559+
} catch (e, st) {
560+
print('Warning: SDK analyzer job threw "$e":\n$st');
561+
}
554562
}
555563

556564
Future<Iterable<Map<String, Object>>> _buildSdkDocs(

0 commit comments

Comments
 (0)