Skip to content

Commit 6ed6450

Browse files
authored
Simpler SDK index content loading: library descriptions are now part of the index.json file. (#8679)
1 parent 4d9c132 commit 6ed6450

7 files changed

+37
-81
lines changed

app/lib/search/backend.dart

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import 'package:clock/clock.dart';
1313
import 'package:collection/collection.dart';
1414
import 'package:gcloud/service_scope.dart' as ss;
1515
import 'package:gcloud/storage.dart';
16-
import 'package:html/parser.dart' as html_parser;
1716
import 'package:logging/logging.dart';
1817
import 'package:meta/meta.dart';
1918
// ignore: implementation_imports
@@ -441,47 +440,6 @@ class SearchBackend {
441440
return rs.body;
442441
}
443442

444-
/// Downloads the remote SDK page and tries to extract the first paragraph of the content.
445-
Future<String?> _fetchSdkLibraryDescription({
446-
required Uri baseUri,
447-
required String relativePath,
448-
}) async {
449-
try {
450-
final content = await fetchSdkIndexContentAsString(
451-
baseUri: baseUri, relativePath: relativePath);
452-
final parsed = html_parser.parse(content);
453-
final descr = parsed.body
454-
?.querySelector('section.desc.markdown')
455-
?.querySelector('p')
456-
?.text
457-
.trim();
458-
return descr == null ? null : compactDescription(descr);
459-
} catch (e) {
460-
_logger.info(
461-
'Unable to fetch SDK library description $baseUri $relativePath', e);
462-
return null;
463-
}
464-
}
465-
466-
/// Downloads the remote SDK page and tries to extract the first paragraph of the content
467-
/// for each library in [libraryRelativeUrls].
468-
Future<Map<String, String>> fetchSdkLibraryDescriptions({
469-
required Uri baseUri,
470-
required Map<String, String> libraryRelativeUrls,
471-
}) async {
472-
final values = <String, String>{};
473-
for (final library in libraryRelativeUrls.keys) {
474-
final descr = await _fetchSdkLibraryDescription(
475-
baseUri: baseUri,
476-
relativePath: libraryRelativeUrls[library]!,
477-
);
478-
if (descr != null) {
479-
values[library] = descr;
480-
}
481-
}
482-
return values;
483-
}
484-
485443
Future<List<PackageDocument>?> fetchSnapshotDocuments() async {
486444
try {
487445
final map = await _snapshotStorage.getContentAsJsonMap();

app/lib/search/dart_sdk_mem_index.dart

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,11 @@ SdkMemIndex? get dartSdkMemIndex =>
3838
Future<SdkMemIndex?> createDartSdkMemIndex() async {
3939
try {
4040
final index = await SdkMemIndex.dart();
41-
final content = DartdocIndex.parseJsonText(
42-
await searchBackend.fetchSdkIndexContentAsString(
43-
baseUri: index.baseUri,
44-
relativePath: 'index.json',
45-
),
46-
);
47-
await index.addDartdocIndex(content);
48-
index.addLibraryDescriptions(
49-
await searchBackend.fetchSdkLibraryDescriptions(
50-
baseUri: index.baseUri,
51-
libraryRelativeUrls: content.libraryRelativeUrls,
52-
),
41+
final content = await searchBackend.fetchSdkIndexContentAsString(
42+
baseUri: index.baseUri,
43+
relativePath: 'index.json',
5344
);
45+
await index.addDartdocIndex(DartdocIndex.parseJsonText(content));
5446
index.updateWeights(
5547
libraryWeights: dartSdkLibraryWeights,
5648
apiPageDirWeights: {},

app/lib/search/flutter_sdk_mem_index.dart

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,12 @@ SdkMemIndex? get flutterSdkMemIndex =>
5555
Future<SdkMemIndex?> createFlutterSdkMemIndex() async {
5656
try {
5757
final index = SdkMemIndex.flutter();
58-
final content = DartdocIndex.parseJsonText(
59-
await searchBackend.fetchSdkIndexContentAsString(
60-
baseUri: index.baseUri,
61-
relativePath: 'index.json',
62-
),
63-
);
64-
await index.addDartdocIndex(content, allowedLibraries: _allowedLibraries);
65-
index.addLibraryDescriptions(
66-
await searchBackend.fetchSdkLibraryDescriptions(
67-
baseUri: index.baseUri,
68-
libraryRelativeUrls: content.libraryRelativeUrls,
69-
),
58+
final content = await searchBackend.fetchSdkIndexContentAsString(
59+
baseUri: index.baseUri,
60+
relativePath: 'index.json',
7061
);
62+
await index.addDartdocIndex(DartdocIndex.parseJsonText(content),
63+
allowedLibraries: _allowedLibraries);
7164
index.updateWeights(
7265
libraryWeights: {},
7366
apiPageDirWeights: _flutterApiPageDirWeights,

app/lib/search/sdk_mem_index.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:math';
66

77
import 'package:_pub_shared/utils/http.dart';
8+
import 'package:meta/meta.dart';
89
// ignore: implementation_imports
910
import 'package:pana/src/dartdoc/dartdoc_index.dart';
1011
import 'package:path/path.dart' as p;
@@ -94,6 +95,11 @@ class SdkMemIndex {
9495
}
9596
if (f.isLibrary) {
9697
_baseUriPerLibrary[library] = _baseUri.resolve(f.href!).toString();
98+
99+
final desc = f.desc?.replaceAll(RegExp(r'\s+'), ' ').trim();
100+
if (desc != null && desc.isNotEmpty) {
101+
_descriptionPerLibrary[library] = desc;
102+
}
97103
}
98104

99105
final text = f.qualifiedName?.replaceAll('.', ' ').replaceAll(':', ' ');
@@ -107,11 +113,6 @@ class SdkMemIndex {
107113
}
108114
}
109115

110-
/// Adds the descriptions for each library.
111-
void addLibraryDescriptions(Map<String, String> descriptions) {
112-
_descriptionPerLibrary.addAll(descriptions);
113-
}
114-
115116
/// Updates the non-default weight for libraries.
116117
void updateWeights({
117118
required Map<String, double> libraryWeights,
@@ -183,6 +184,10 @@ class SdkMemIndex {
183184
))
184185
.toList();
185186
}
187+
188+
@visibleForTesting
189+
String? getLibraryDescription(String library) =>
190+
_descriptionPerLibrary[library];
186191
}
187192

188193
class _Hit {

app/test/search/backend_test.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ void main() {
1414
group('search backend', () {
1515
testWithProfile('fetch SDK library description', fn: () async {
1616
final index = await SdkMemIndex.dart();
17-
final descr = await searchBackend.fetchSdkLibraryDescriptions(
17+
final content = await searchBackend.fetchSdkIndexContentAsString(
1818
baseUri: index.baseUri,
19-
libraryRelativeUrls: {
20-
'dart:async': 'dart-async/index.html',
21-
},
19+
relativePath: 'index.json',
20+
);
21+
await index.addDartdocIndex(DartdocIndex.parseJsonText(content));
22+
expect(
23+
index.getLibraryDescription('dart:async'),
24+
'Support for asynchronous programming, with classes such as Future and Stream.',
2225
);
23-
expect(descr, {
24-
'dart:async':
25-
'Support for asynchronous programming, with classes such as Future and Stream.',
26-
});
2726
});
2827

2928
testWithProfile('updates snapshot storage', fn: () async {

app/test/search/dartdoc_index_parsing_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void main() {
9797
{
9898
'sdk': 'flutter',
9999
'library': 'widgets',
100+
'description': 'The Flutter widgets framework.',
100101
'url': contains('https://api.flutter.dev/flutter/widgets'),
101102
'score': closeTo(0.98, 0.01),
102103
'apiPages': [

app/test/search/sdk_mem_index_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void main() {
3232
'kind': 8,
3333
'overriddenDepth': 0,
3434
'packageName': 'Dart',
35+
'desc': 'async description',
3536
},
3637
{
3738
'name': 'AsyncError',
@@ -60,6 +61,15 @@ void main() {
6061
'packageName': 'Dart',
6162
'enclosedBy': {'name': 'AsyncError', 'kind': 3},
6263
},
64+
{
65+
'name': 'dart:html',
66+
'qualifiedName': 'dart:html',
67+
'href': 'dart-html',
68+
'kind': 8,
69+
'overriddenDepth': 0,
70+
'packageName': 'HTML',
71+
'desc': 'html description',
72+
},
6373
{
6474
'name': 'Window',
6575
'qualifiedName': 'dart:html.Window',
@@ -79,8 +89,6 @@ void main() {
7989
'enclosedBy': {'name': 'dart:html.FakeIcons', 'kind': 3},
8090
},
8191
]));
82-
index.addLibraryDescriptions({'dart:async': 'async description'});
83-
index.addLibraryDescriptions({'dart:html': 'html description'});
8492
});
8593

8694
test('AsyncError', () async {

0 commit comments

Comments
 (0)