Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:convert' show JSON; | |
| 6 import 'dart:io'; | 5 import 'dart:io'; |
| 7 import 'package:analyzer/src/generated/source.dart' show Source; | 6 import 'package:analyzer/src/generated/source.dart' show Source; |
| 8 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 7 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 9 show InSummarySource; | 8 show InSummarySource; |
| 10 import 'package:args/args.dart' show ArgParser, ArgResults; | 9 import 'package:args/args.dart' show ArgParser, ArgResults; |
| 11 import 'package:args/command_runner.dart' show UsageException; | 10 import 'package:args/command_runner.dart' show UsageException; |
| 12 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
| 13 | 12 |
| 13 import '../analyzer/context.dart' show AnalyzerOptions; | |
| 14 import 'compiler.dart' | 14 import 'compiler.dart' |
| 15 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 15 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
| 16 import '../analyzer/context.dart' show AnalyzerOptions; | 16 import 'module_builder.dart'; |
| 17 | 17 |
| 18 final ArgParser _argParser = () { | 18 final ArgParser _argParser = () { |
| 19 var argParser = new ArgParser() | 19 var argParser = new ArgParser() |
| 20 ..addFlag('help', abbr: 'h', help: 'Display this message.') | 20 ..addFlag('help', abbr: 'h', help: 'Display this message.') |
| 21 ..addOption('out', abbr: 'o', help: 'Output file (required).') | 21 ..addOption('out', |
| 22 abbr: 'o', allowMultiple: true, help: 'Output file (required).') | |
| 22 ..addOption('module-root', | 23 ..addOption('module-root', |
| 23 help: 'Root module directory.\n' | 24 help: 'Root module directory.\n' |
| 24 'Generated module paths are relative to this root.') | 25 'Generated module paths are relative to this root.') |
| 25 ..addOption('library-root', | 26 ..addOption('library-root', |
| 26 help: 'Root of source files.\n' | 27 help: 'Root of source files.\n' |
| 27 'Generated library names are relative to this root.') | 28 'Generated library names are relative to this root.') |
| 28 ..addOption('build-root', | 29 ..addOption('build-root', |
| 29 help: 'Deprecated in favor of --library-root', hide: true); | 30 help: 'Deprecated in favor of --library-root', hide: true); |
| 31 addModuleFormatOptions(argParser, allowMultiple: true); | |
| 30 AnalyzerOptions.addArguments(argParser); | 32 AnalyzerOptions.addArguments(argParser); |
| 31 CompilerOptions.addArguments(argParser); | 33 CompilerOptions.addArguments(argParser); |
| 32 return argParser; | 34 return argParser; |
| 33 }(); | 35 }(); |
| 34 | 36 |
| 35 /// Runs a single compile for dartdevc. | 37 /// Runs a single compile for dartdevc. |
| 36 /// | 38 /// |
| 37 /// This handles argument parsing, usage, error handling. | 39 /// This handles argument parsing, usage, error handling. |
| 38 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel | 40 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel |
| 39 /// worker support. | 41 /// worker support. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 } | 83 } |
| 82 | 84 |
| 83 void _compile(ArgResults argResults, void printFn(Object obj)) { | 85 void _compile(ArgResults argResults, void printFn(Object obj)) { |
| 84 var compiler = | 86 var compiler = |
| 85 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); | 87 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
| 86 var compilerOpts = new CompilerOptions.fromArguments(argResults); | 88 var compilerOpts = new CompilerOptions.fromArguments(argResults); |
| 87 if (argResults['help']) { | 89 if (argResults['help']) { |
| 88 printFn(_usageMessage); | 90 printFn(_usageMessage); |
| 89 return; | 91 return; |
| 90 } | 92 } |
| 91 var outPath = argResults['out']; | 93 var outPaths = argResults['out'] as List<String>; |
| 94 var moduleFormats = parseModuleFormatOption(argResults); | |
| 92 | 95 |
| 93 if (outPath == null) { | 96 if (outPaths.isEmpty) { |
| 94 _usageException('Please include the output file location. For example:\n' | 97 _usageException('Please include the output file location. For example:\n' |
| 95 ' -o PATH/TO/OUTPUT_FILE.js'); | 98 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 99 } else if (outPaths.length != moduleFormats.length) { | |
| 100 _usageException('Number of output files (${outPaths.length}) must match ' | |
| 101 'number of module formats (${moduleFormats.length}).'); | |
| 96 } | 102 } |
| 97 | 103 |
| 104 // TODO(jmesserly): for now the first one is special. This will go away once | |
| 105 // we've removed the "root" and "module name" variables. | |
| 106 var outPath = outPaths[0]; | |
| 107 | |
| 98 var libraryRoot = argResults['library-root'] as String; | 108 var libraryRoot = argResults['library-root'] as String; |
| 99 libraryRoot ??= argResults['build-root'] as String; | 109 libraryRoot ??= argResults['build-root'] as String; |
| 100 if (libraryRoot != null) { | 110 if (libraryRoot != null) { |
| 101 libraryRoot = path.absolute(libraryRoot); | 111 libraryRoot = path.absolute(libraryRoot); |
| 102 } else { | 112 } else { |
| 103 libraryRoot = Directory.current.path; | 113 libraryRoot = Directory.current.path; |
| 104 } | 114 } |
| 105 var moduleRoot = argResults['module-root'] as String; | 115 var moduleRoot = argResults['module-root'] as String; |
| 106 String modulePath; | 116 String modulePath; |
| 107 if (moduleRoot != null) { | 117 if (moduleRoot != null) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 119 | 129 |
| 120 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, | 130 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, |
| 121 (source) => _moduleForLibrary(moduleRoot, source, compilerOpts)); | 131 (source) => _moduleForLibrary(moduleRoot, source, compilerOpts)); |
| 122 | 132 |
| 123 JSModuleFile module = compiler.compile(unit, compilerOpts); | 133 JSModuleFile module = compiler.compile(unit, compilerOpts); |
| 124 module.errors.forEach(printFn); | 134 module.errors.forEach(printFn); |
| 125 | 135 |
| 126 if (!module.isValid) throw new CompileErrorException(); | 136 if (!module.isValid) throw new CompileErrorException(); |
| 127 | 137 |
| 128 // Write JS file, as well as source map and summary (if requested). | 138 // Write JS file, as well as source map and summary (if requested). |
| 129 new File(outPath).writeAsStringSync(module.code); | 139 for (int i = 0; i < outPaths.length; i++) { |
|
nweiz
2016/08/24 23:33:26
Nit: "var i"
| |
| 130 if (module.sourceMap != null) { | 140 module.writeCodeSync(moduleFormats[i], outPaths[i]); |
| 131 var mapPath = outPath + '.map'; | |
| 132 new File(mapPath) | |
| 133 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); | |
| 134 } | 141 } |
| 142 | |
| 143 // TODO(jmesserly): should we write the summary next to every output? | |
|
nweiz
2016/08/24 23:33:26
IMO, yes. The extra time is likely to be negligibl
Jennifer Messerly
2016/08/25 16:21:39
good idea. done!
| |
| 135 if (module.summaryBytes != null) { | 144 if (module.summaryBytes != null) { |
| 136 var summaryPath = | 145 var summaryPath = |
| 137 path.withoutExtension(outPath) + '.${compilerOpts.summaryExtension}'; | 146 path.withoutExtension(outPath) + '.${compilerOpts.summaryExtension}'; |
| 138 new File(summaryPath).writeAsBytesSync(module.summaryBytes); | 147 new File(summaryPath).writeAsBytesSync(module.summaryBytes); |
| 139 } | 148 } |
| 140 } | 149 } |
| 141 | 150 |
| 142 String _moduleForLibrary( | 151 String _moduleForLibrary( |
| 143 String moduleRoot, Source source, CompilerOptions compilerOpts) { | 152 String moduleRoot, Source source, CompilerOptions compilerOpts) { |
| 144 if (source is InSummarySource) { | 153 if (source is InSummarySource) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 166 '\n\n${_argParser.usage}'; | 175 '\n\n${_argParser.usage}'; |
| 167 | 176 |
| 168 void _usageException(String message) { | 177 void _usageException(String message) { |
| 169 throw new UsageException(message, _usageMessage); | 178 throw new UsageException(message, _usageMessage); |
| 170 } | 179 } |
| 171 | 180 |
| 172 /// Thrown when the input source code has errors. | 181 /// Thrown when the input source code has errors. |
| 173 class CompileErrorException implements Exception { | 182 class CompileErrorException implements Exception { |
| 174 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 183 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 175 } | 184 } |
| OLD | NEW |