Skip to content

Commit 5c091e0

Browse files
stereotype441Commit Queue
authored andcommitted
Separate allTargets variables for api.txt code generators.
This change removes the top level variable `allTargets` from `pkg/analyzer_utilities/lib/tool/api.dart` and replaces it with a method `allTargetsForPackage`, parameterized by the name of the package for which an `api.txt` file is generated. The individual code generators that make use of this function now all have their own top level variable `allTargets`. In addition to being conceptually clearer (since we no longer have a single `allTargets` variable with different meanings depending on where it's used), this paves the way for a CL I am working on that will change the `GeneratedContent` based code generators so that they use paths relative to the `pkg` directory. That in turn should make it simpler to move some of the code generated files that have to do with error messages from `pkg/analyzer` to `pkg/_fe_analyzer_shared`. Change-Id: Id636ddbca27c46ae4242bbf8e4b6f7b8dae4d4e9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438481 Reviewed-by: Samuel Rawlins <[email protected]> Auto-Submit: Paul Berry <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 6751903 commit 5c091e0

File tree

7 files changed

+27
-22
lines changed

7 files changed

+27
-22
lines changed

pkg/analysis_server_plugin/tool/api/generate.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Future<void> main() async {
1414
await GeneratedContent.generateAll(analyzerTestingPkgPath, allTargets);
1515
}
1616

17+
/// A list of all targets generated by this code generator.
18+
final List<GeneratedContent> allTargets =
19+
allTargetsForPackage('analysis_server_plugin');
20+
1721
/// The path to the `analyzer_testing` package.
1822
final String analyzerTestingPkgPath = normalize(
1923
join(pkg_root.packageRoot, 'analysis_server_plugin'),

pkg/analysis_server_plugin/tool/api/generate_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// This test verifies that all files generated by `generate.dart` are up to
66
// date.
77

8-
import 'package:analyzer_utilities/tool/api.dart';
98
import 'package:analyzer_utilities/tools.dart';
109
import 'package:path/path.dart';
1110

pkg/analyzer/tool/api/generate.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Future<void> main() async {
1414
await GeneratedContent.generateAll(analyzerPkgPath, allTargets);
1515
}
1616

17+
/// A list of all targets generated by this code generator.
18+
final List<GeneratedContent> allTargets = allTargetsForPackage('analyzer');
19+
1720
/// The path to the `analyzer` package.
1821
final String analyzerPkgPath = normalize(
1922
join(pkg_root.packageRoot, 'analyzer'),

pkg/analyzer/tool/api/generate_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// This test verifies that all files generated by `generate.dart` are up to
66
// date.
77

8-
import 'package:analyzer_utilities/tool/api.dart';
98
import 'package:analyzer_utilities/tools.dart';
109
import 'package:path/path.dart';
1110

pkg/analyzer_testing/tool/api/generate.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Future<void> main() async {
1414
await GeneratedContent.generateAll(analyzerTestingPkgPath, allTargets);
1515
}
1616

17+
/// A list of all targets generated by this code generator.
18+
final List<GeneratedContent> allTargets = allTargetsForPackage(
19+
'analyzer_testing',
20+
);
21+
1722
/// The path to the `analyzer_testing` package.
1823
final String analyzerTestingPkgPath = normalize(
1924
join(pkg_root.packageRoot, 'analyzer_testing'),

pkg/analyzer_testing/tool/api/generate_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// This test verifies that all files generated by `generate.dart` are up to
66
// date.
77

8-
import 'package:analyzer_utilities/tool/api.dart';
98
import 'package:analyzer_utilities/tools.dart';
109
import 'package:path/path.dart';
1110

pkg/analyzer_utilities/lib/tool/api.dart

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,23 @@ import 'package:analyzer/file_system/physical_file_system.dart';
1818
import 'package:analyzer_utilities/tools.dart';
1919
import 'package:collection/collection.dart';
2020

21-
/// A list of all targets generated by this code generator.
22-
final List<GeneratedContent> allTargets = [
23-
GeneratedFile('api.txt', computeApiTxtContents),
21+
/// Computes a list of all targets generated by this code generator for a given
22+
/// package.
23+
List<GeneratedContent> allTargetsForPackage(String pkgName) => [
24+
GeneratedFile('api.txt', (pkgPath) async {
25+
var provider = PhysicalResourceProvider.INSTANCE;
26+
var collection = AnalysisContextCollection(
27+
includedPaths: [pkgPath],
28+
resourceProvider: provider,
29+
);
30+
var context = collection.contexts.first;
31+
var publicApi = ApiDescription(pkgName);
32+
var stringBuffer = StringBuffer();
33+
_printNodes(stringBuffer, await publicApi.build(context));
34+
return stringBuffer.toString();
35+
}),
2436
];
2537

26-
/// Computes what should be written to the `api.txt` file.
27-
Future<String> computeApiTxtContents(String pkgPath) async {
28-
var provider = PhysicalResourceProvider.INSTANCE;
29-
var pathContext = provider.pathContext;
30-
var pkgName = pathContext.basename(pkgPath);
31-
var collection = AnalysisContextCollection(
32-
includedPaths: [pkgPath],
33-
resourceProvider: provider,
34-
);
35-
var context = collection.contexts.first;
36-
var publicApi = ApiDescription(pkgName);
37-
var stringBuffer = StringBuffer();
38-
_printNodes(stringBuffer, await publicApi.build(context));
39-
return stringBuffer.toString();
40-
}
41-
4238
/// Outputs the contents of [nodes] to [sink], prepending [prefix] to every
4339
/// line.
4440
void _printNodes<SortKey extends Comparable<SortKey>>(

0 commit comments

Comments
 (0)