5
5
import 'dart:io' ;
6
6
import 'package:analyzer/src/command_line/arguments.dart'
7
7
show defineAnalysisArguments, ignoreUnrecognizedFlagsFlag;
8
- import 'package:analyzer/src/generated/source.dart' show Source;
9
8
import 'package:analyzer/src/summary/package_bundle_reader.dart'
10
- show ConflictingSummaryException, InSummarySource ;
9
+ show ConflictingSummaryException;
11
10
import 'package:args/args.dart' show ArgParser, ArgResults;
12
11
import 'package:args/command_runner.dart' show UsageException;
13
12
import 'package:path/path.dart' as path;
14
13
15
- import '../compiler/module_builder.dart' ;
16
14
import 'context.dart' show AnalyzerOptions;
17
15
import 'module_compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler;
18
16
@@ -58,7 +56,7 @@ int compile(List<String> args, {void printFn(Object obj)}) {
58
56
return 0 ;
59
57
} on UsageException catch (error) {
60
58
// Incorrect usage, input file not found, etc.
61
- printFn (error);
59
+ printFn ('${ error . message } \n\n $ _usageMessage ' );
62
60
return 64 ;
63
61
} on ConflictingSummaryException catch (error) {
64
62
// Same input file appears in multiple provided summaries.
@@ -106,14 +104,14 @@ ArgParser ddcArgParser({bool hide = true}) {
106
104
defaultsTo: false ,
107
105
hide: hide)
108
106
..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).' )
111
110
..addOption ('library-root' ,
112
111
help: 'Root of source files. Library names are relative to this root.' );
112
+ CompilerOptions .addArguments (argParser, hide: hide);
113
113
defineAnalysisArguments (argParser, hide: hide, ddc: true );
114
- addModuleFormatOptions (argParser, allowMultiple: true , hide: hide);
115
114
AnalyzerOptions .addArguments (argParser, hide: hide);
116
- CompilerOptions .addArguments (argParser, hide: hide);
117
115
return argParser;
118
116
}
119
117
@@ -131,23 +129,17 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
131
129
var compiler = ModuleCompiler (analyzerOptions);
132
130
var compilerOpts = CompilerOptions .fromArguments (argResults);
133
131
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;
145
133
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
+ '' );
148
138
} 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
+ '' );
151
143
}
152
144
153
145
// TODO(jmesserly): for now the first one is special. This will go away once
@@ -160,27 +152,20 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
160
152
} else {
161
153
libraryRoot = Directory .current.path;
162
154
}
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);
170
165
}
171
- modulePath =
172
- path.withoutExtension (path.relative (firstOutPath, from: moduleRoot));
173
- } else {
174
- moduleRoot = path.dirname (firstOutPath);
175
- modulePath = path.basenameWithoutExtension (firstOutPath);
176
166
}
177
167
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);
184
169
185
170
var module = compiler.compile (unit, compilerOpts);
186
171
module.errors.forEach (printFn);
@@ -193,8 +178,7 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
193
178
194
179
// Write JS file, as well as source map and summary (if requested).
195
180
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]);
198
182
}
199
183
if (module.summaryBytes != null ) {
200
184
var summaryPaths = compilerOpts.summaryOutPath != null
@@ -216,34 +200,6 @@ void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions,
216
200
}
217
201
}
218
202
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
-
247
203
String get _usageMessage =>
248
204
'The Dart Development Compiler compiles Dart sources into a JavaScript '
249
205
'module.\n\n '
@@ -262,10 +218,6 @@ String _getVersion() {
262
218
}
263
219
}
264
220
265
- void _usageException (String message) {
266
- throw UsageException (message, _usageMessage);
267
- }
268
-
269
221
/// Thrown when the input source code has errors.
270
222
class CompileErrorException implements Exception {
271
223
toString () => '\n Please fix all errors before compiling (warnings are okay).' ;
0 commit comments