Skip to content

Commit 1a122e3

Browse files
Jenny Messerlycommit-bot@chromium.org
Jenny Messerly
authored andcommitted
Deprecate module-root option in dartdevc, part of #32272
Adds an option for specifying the output JS module name if that is needed (only applies for some module formats). Also removes repl-compile (it's set via API, not the command line). Refactors dartdevk options to match dartdevc so we can migrate more easily. Moves shared code into a shared location and removes copied code. Change-Id: I966343ecbbc962f5d0f14ea7e65d78660159f420 Reviewed-on: https://dart-review.googlesource.com/64823 Commit-Queue: Jenny Messerly <[email protected]> Reviewed-by: Bob Nystrom <[email protected]>
1 parent 0998153 commit 1a122e3

16 files changed

+590
-703
lines changed

pkg/dev_compiler/lib/src/analyzer/code_generator.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,11 @@ class CodeGenerator extends Object
559559
if (source.uri.scheme == 'dart') {
560560
return JS.dartSdkModule;
561561
}
562-
var moduleName = _buildUnit.libraryToModule(source);
562+
var summaryPath = (source as InSummarySource).summaryPath;
563+
var moduleName = options.summaryModules[summaryPath];
563564
if (moduleName == null) {
564-
throw StateError('Could not find module containing "$library".');
565+
throw StateError('Could not find module name for library "$library" '
566+
'from summary path "$summaryPath".');
565567
}
566568
return moduleName;
567569
}

pkg/dev_compiler/lib/src/analyzer/command.dart

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import 'dart:io';
66
import 'package:analyzer/src/command_line/arguments.dart'
77
show defineAnalysisArguments, ignoreUnrecognizedFlagsFlag;
8-
import 'package:analyzer/src/generated/source.dart' show Source;
98
import 'package:analyzer/src/summary/package_bundle_reader.dart'
10-
show ConflictingSummaryException, InSummarySource;
9+
show ConflictingSummaryException;
1110
import 'package:args/args.dart' show ArgParser, ArgResults;
1211
import 'package:args/command_runner.dart' show UsageException;
1312
import 'package:path/path.dart' as path;
1413

15-
import '../compiler/module_builder.dart';
1614
import 'context.dart' show AnalyzerOptions;
1715
import 'module_compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler;
1816

@@ -58,7 +56,7 @@ int compile(List<String> args, {void printFn(Object obj)}) {
5856
return 0;
5957
} on UsageException catch (error) {
6058
// Incorrect usage, input file not found, etc.
61-
printFn(error);
59+
printFn('${error.message}\n\n$_usageMessage');
6260
return 64;
6361
} on ConflictingSummaryException catch (error) {
6462
// Same input file appears in multiple provided summaries.
@@ -106,14 +104,14 @@ ArgParser ddcArgParser({bool hide = true}) {
106104
defaultsTo: false,
107105
hide: hide)
108106
..addMultiOption('out', abbr: 'o', help: 'Output file (required).')
109-
..addOption('module-root',
110-
help: 'Root module directory. Module paths are relative to this root.')
107+
..addOption('module-name',
108+
help: 'The output module name, used in some JS module formats.\n'
109+
'Defaults to the output file name (without .js).')
111110
..addOption('library-root',
112111
help: 'Root of source files. Library names are relative to this root.');
112+
CompilerOptions.addArguments(argParser, hide: hide);
113113
defineAnalysisArguments(argParser, hide: hide, ddc: true);
114-
addModuleFormatOptions(argParser, allowMultiple: true, hide: hide);
115114
AnalyzerOptions.addArguments(argParser, hide: hide);
116-
CompilerOptions.addArguments(argParser, hide: hide);
117115
return argParser;
118116
}
119117

@@ -131,23 +129,17 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
131129
var compiler = ModuleCompiler(analyzerOptions);
132130
var compilerOpts = CompilerOptions.fromArguments(argResults);
133131
var outPaths = argResults['out'] as List<String>;
134-
var moduleFormats = parseModuleFormatOption(argResults);
135-
bool singleOutFile = argResults['single-out-file'];
136-
if (singleOutFile) {
137-
for (var format in moduleFormats) {
138-
if (format != ModuleFormat.amd && format != ModuleFormat.legacy) {
139-
_usageException('Format $format cannot be combined with '
140-
'single-out-file. Only amd and legacy modes are supported.');
141-
}
142-
}
143-
}
144-
132+
var moduleFormats = compilerOpts.moduleFormats;
145133
if (outPaths.isEmpty) {
146-
_usageException('Please include the output file location. For example:\n'
147-
' -o PATH/TO/OUTPUT_FILE.js');
134+
throw UsageException(
135+
'Please specify the output file location. For example:\n'
136+
' -o PATH/TO/OUTPUT_FILE.js',
137+
'');
148138
} else if (outPaths.length != moduleFormats.length) {
149-
_usageException('Number of output files (${outPaths.length}) must match '
150-
'number of module formats (${moduleFormats.length}).');
139+
throw UsageException(
140+
'Number of output files (${outPaths.length}) must match '
141+
'number of module formats (${moduleFormats.length}).',
142+
'');
151143
}
152144

153145
// TODO(jmesserly): for now the first one is special. This will go away once
@@ -160,27 +152,20 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
160152
} else {
161153
libraryRoot = Directory.current.path;
162154
}
163-
var moduleRoot = argResults['module-root'] as String;
164-
String modulePath;
165-
if (moduleRoot != null) {
166-
moduleRoot = path.absolute(moduleRoot);
167-
if (!path.isWithin(moduleRoot, firstOutPath)) {
168-
_usageException('Output file $firstOutPath must be within the module '
169-
'root directory $moduleRoot');
155+
var moduleName = argResults['module-name'] as String;
156+
if (moduleName == null) {
157+
var moduleRoot = compilerOpts.moduleRoot;
158+
if (moduleRoot != null) {
159+
// TODO(jmesserly): remove this legacy support after a deprecation period.
160+
// (Mainly this is to give time for migrating build rules.)
161+
moduleName =
162+
path.withoutExtension(path.relative(firstOutPath, from: moduleRoot));
163+
} else {
164+
moduleName = path.basenameWithoutExtension(firstOutPath);
170165
}
171-
modulePath =
172-
path.withoutExtension(path.relative(firstOutPath, from: moduleRoot));
173-
} else {
174-
moduleRoot = path.dirname(firstOutPath);
175-
modulePath = path.basenameWithoutExtension(firstOutPath);
176166
}
177167

178-
var unit = BuildUnit(
179-
modulePath,
180-
libraryRoot,
181-
argResults.rest,
182-
(source) =>
183-
_moduleForLibrary(moduleRoot, source, analyzerOptions, compilerOpts));
168+
var unit = BuildUnit(moduleName, libraryRoot, argResults.rest);
184169

185170
var module = compiler.compile(unit, compilerOpts);
186171
module.errors.forEach(printFn);
@@ -193,8 +178,7 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
193178

194179
// Write JS file, as well as source map and summary (if requested).
195180
for (var i = 0; i < outPaths.length; i++) {
196-
module.writeCodeSync(moduleFormats[i], outPaths[i],
197-
singleOutFile: singleOutFile);
181+
module.writeCodeSync(moduleFormats[i], outPaths[i]);
198182
}
199183
if (module.summaryBytes != null) {
200184
var summaryPaths = compilerOpts.summaryOutPath != null
@@ -216,34 +200,6 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
216200
}
217201
}
218202

219-
String _moduleForLibrary(String moduleRoot, Source source,
220-
AnalyzerOptions analyzerOptions, CompilerOptions compilerOpts) {
221-
if (source is InSummarySource) {
222-
var summaryPath = source.summaryPath;
223-
224-
if (analyzerOptions.customSummaryModules.containsKey(summaryPath)) {
225-
return analyzerOptions.customSummaryModules[summaryPath];
226-
}
227-
228-
var ext = '.${compilerOpts.summaryExtension}';
229-
if (path.isWithin(moduleRoot, summaryPath) && summaryPath.endsWith(ext)) {
230-
var buildUnitPath =
231-
summaryPath.substring(0, summaryPath.length - ext.length);
232-
return path.url
233-
.joinAll(path.split(path.relative(buildUnitPath, from: moduleRoot)));
234-
}
235-
236-
_usageException('Imported file ${source.uri} is not within the module root '
237-
'directory $moduleRoot');
238-
}
239-
240-
_usageException(
241-
'Imported file "${source.uri}" was not found as a summary or source '
242-
'file. Please pass in either the summary or the source file '
243-
'for this import.');
244-
return null; // unreachable
245-
}
246-
247203
String get _usageMessage =>
248204
'The Dart Development Compiler compiles Dart sources into a JavaScript '
249205
'module.\n\n'
@@ -262,10 +218,6 @@ String _getVersion() {
262218
}
263219
}
264220

265-
void _usageException(String message) {
266-
throw UsageException(message, _usageMessage);
267-
}
268-
269221
/// Thrown when the input source code has errors.
270222
class CompileErrorException implements Exception {
271223
toString() => '\nPlease fix all errors before compiling (warnings are okay).';

pkg/dev_compiler/lib/src/analyzer/context.dart

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class AnalyzerOptions {
3131
/// Package root when resolving 'package:' urls the standard way.
3232
String get packageRoot => contextBuilderOptions.defaultPackagesDirectoryPath;
3333

34-
/// List of summary file paths.
35-
final List<String> summaryPaths;
36-
37-
final Map<String, String> customSummaryModules = {};
38-
3934
/// Path to the dart-sdk, or `null` if the path couldn't be determined.
4035
final String dartSdkPath;
4136

@@ -49,63 +44,49 @@ class AnalyzerOptions {
4944

5045
AnalyzerOptions._(
5146
{this.contextBuilderOptions,
52-
List<String> summaryPaths,
5347
String dartSdkPath,
5448
this.customUrlMappings = const {}})
55-
: dartSdkPath = dartSdkPath ?? getSdkDir().path,
56-
summaryPaths = summaryPaths ?? const [] {
49+
: dartSdkPath = dartSdkPath ?? getSdkDir().path {
5750
contextBuilderOptions.declaredVariables ??= const {};
58-
_parseCustomSummaryModules();
5951
}
6052

6153
factory AnalyzerOptions.basic(
62-
{String dartSdkPath,
63-
String dartSdkSummaryPath,
64-
List<String> summaryPaths}) {
65-
var contextBuilderOptions = ContextBuilderOptions()
66-
..defaultOptions = (AnalysisOptionsImpl()..previewDart2 = true)
67-
..dartSdkSummaryPath = dartSdkSummaryPath;
68-
54+
{String dartSdkPath, String dartSdkSummaryPath}) {
6955
return AnalyzerOptions._(
70-
contextBuilderOptions: contextBuilderOptions,
71-
dartSdkPath: dartSdkPath,
72-
summaryPaths: summaryPaths);
56+
contextBuilderOptions: ContextBuilderOptions()
57+
..defaultOptions = (AnalysisOptionsImpl()..previewDart2 = true)
58+
..dartSdkSummaryPath = dartSdkSummaryPath,
59+
dartSdkPath: dartSdkPath);
7360
}
7461

7562
factory AnalyzerOptions.fromArguments(ArgResults args,
76-
{String dartSdkSummaryPath, List<String> summaryPaths}) {
77-
var contextBuilderOptions =
63+
{String dartSdkSummaryPath}) {
64+
var contextOpts =
7865
createContextBuilderOptions(args, trackCacheDependencies: false);
79-
(contextBuilderOptions.defaultOptions as AnalysisOptionsImpl).previewDart2 =
80-
true;
66+
(contextOpts.defaultOptions as AnalysisOptionsImpl).previewDart2 = true;
8167

8268
var dartSdkPath = args['dart-sdk'] as String ?? getSdkDir().path;
8369

84-
dartSdkSummaryPath ??= contextBuilderOptions.dartSdkSummaryPath;
85-
dartSdkSummaryPath ??=
70+
dartSdkSummaryPath ??= contextOpts.dartSdkSummaryPath ??
8671
path.join(dartSdkPath, 'lib', '_internal', 'ddc_sdk.sum');
8772
// For building the SDK, we explicitly set the path to none.
8873
if (dartSdkSummaryPath == 'build') dartSdkSummaryPath = null;
89-
contextBuilderOptions.dartSdkSummaryPath = dartSdkSummaryPath;
74+
contextOpts.dartSdkSummaryPath = dartSdkSummaryPath;
9075

9176
return AnalyzerOptions._(
92-
contextBuilderOptions: contextBuilderOptions,
93-
summaryPaths: summaryPaths ?? args['summary'] as List<String>,
77+
contextBuilderOptions: contextOpts,
9478
dartSdkPath: dartSdkPath,
9579
customUrlMappings:
9680
_parseUrlMappings(args['url-mapping'] as List<String>));
9781
}
9882

9983
static void addArguments(ArgParser parser, {bool hide = true}) {
100-
parser
101-
..addOption('summary',
102-
abbr: 's', help: 'summary file(s) to include', allowMultiple: true)
103-
..addOption('url-mapping',
104-
help: '--url-mapping=libraryUri,/path/to/library.dart uses\n'
105-
'library.dart as the source for an import of of "libraryUri".',
106-
allowMultiple: true,
107-
splitCommas: false,
108-
hide: hide);
84+
parser.addOption('url-mapping',
85+
help: '--url-mapping=libraryUri,/path/to/library.dart uses\n'
86+
'library.dart as the source for an import of of "libraryUri".',
87+
allowMultiple: true,
88+
splitCommas: false,
89+
hide: hide);
10990
}
11091

11192
static Map<String, String> _parseUrlMappings(List<String> argument) {
@@ -118,23 +99,6 @@ class AnalyzerOptions {
11899
}
119100
return mappings;
120101
}
121-
122-
/// A summary path can contain "=" followed by an explicit module name to
123-
/// allow working with summaries whose physical location is outside of the
124-
/// module root directory.
125-
///
126-
/// Removes any explicit module names from [summaryPaths] and populates with
127-
/// [customSummaryModules] with them.
128-
void _parseCustomSummaryModules() {
129-
for (var i = 0; i < summaryPaths.length; i++) {
130-
var summaryPath = summaryPaths[i];
131-
var pipe = summaryPath.indexOf("=");
132-
if (pipe != -1) {
133-
summaryPaths[i] = summaryPath.substring(0, pipe);
134-
customSummaryModules[summaryPaths[i]] = summaryPath.substring(pipe + 1);
135-
}
136-
}
137-
}
138102
}
139103

140104
/// Creates a SourceFactory configured by the [options].
@@ -147,19 +111,17 @@ SourceFactory createSourceFactory(AnalyzerOptions options,
147111
SummaryDataStore summaryData,
148112
ResourceProvider resourceProvider}) {
149113
resourceProvider ??= PhysicalResourceProvider.INSTANCE;
150-
var resolvers = <UriResolver>[];
114+
var resolvers = <UriResolver>[sdkResolver];
151115
if (options.customUrlMappings.isNotEmpty) {
152116
resolvers
153117
.add(CustomUriResolver(resourceProvider, options.customUrlMappings));
154118
}
155-
resolvers.add(sdkResolver);
156119
if (summaryData != null) {
157120
resolvers.add(InSummaryUriResolver(resourceProvider, summaryData));
158121
}
159122

160-
if (fileResolvers == null)
161-
fileResolvers =
162-
createFileResolvers(options, resourceProvider: resourceProvider);
123+
fileResolvers ??=
124+
createFileResolvers(options, resourceProvider: resourceProvider);
163125
resolvers.addAll(fileResolvers);
164126
return SourceFactory(resolvers, null, resourceProvider);
165127
}

0 commit comments

Comments
 (0)