Skip to content

Commit 19d12d1

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Add a tool to write all combined macro generated content.
Change-Id: I08554397016255bff82e6c420b8795999ba5990f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210427 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Jake Macdonald <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent c98ee0b commit 19d12d1

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

pkg/analyzer/tool/macro/generate.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io' as io;
6+
7+
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
8+
import 'package:analyzer/dart/analysis/results.dart';
9+
import 'package:analyzer/file_system/physical_file_system.dart';
10+
import 'package:analyzer/src/dart/element/element.dart';
11+
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
12+
13+
void main(List<String> arguments) async {
14+
if (arguments.length != 1) {
15+
_printUsage();
16+
io.exit(1);
17+
}
18+
19+
var resourceProvider = PhysicalResourceProvider.INSTANCE;
20+
var pathContext = resourceProvider.pathContext;
21+
22+
// The directory must exist.
23+
var packagePath = arguments[0];
24+
var packageFolder = resourceProvider.getFolder(packagePath);
25+
if (!packageFolder.exists) {
26+
print('Error: $packagePath does not exist.');
27+
io.exit(1);
28+
}
29+
30+
// The directory must be a Pub package.
31+
var pubspecYamlFile = packageFolder.getChildAssumingFile(
32+
file_paths.pubspecYaml,
33+
);
34+
if (!pubspecYamlFile.exists) {
35+
print('Error: ${pubspecYamlFile.path} does not exist.');
36+
io.exit(1);
37+
}
38+
39+
var collection = AnalysisContextCollection(
40+
includedPaths: [packagePath],
41+
);
42+
for (var analysisContext in collection.contexts) {
43+
var analyzedPaths = analysisContext.contextRoot.analyzedFiles();
44+
for (var path in analyzedPaths) {
45+
if (file_paths.isDart(pathContext, path)) {
46+
var session = analysisContext.currentSession;
47+
var unitElementResult = await session.getUnitElement(path);
48+
if (unitElementResult is UnitElementResult) {
49+
var unitElement =
50+
unitElementResult.element as CompilationUnitElementImpl;
51+
// If has macro-generated content, write it.
52+
var macroGeneratedContent = unitElement.macroGeneratedContent;
53+
if (macroGeneratedContent != null) {
54+
var relativePath = pathContext.relative(path, from: packagePath);
55+
var combinedPath = pathContext.join(
56+
packagePath, '.dart_tool', 'analyzer', 'macro', relativePath);
57+
resourceProvider.getFile(combinedPath)
58+
..parent2.create()
59+
..writeAsStringSync(macroGeneratedContent);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
void _printUsage() {
68+
print('''
69+
Usage: dart generate.dart path-to-pub-package
70+
Write combined code of files that have macro-generated declarations.
71+
''');
72+
}

0 commit comments

Comments
 (0)