| 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; | 5 import 'dart:convert' show JSON; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'package:args/command_runner.dart'; | 7 import 'package:args/command_runner.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart' show Source; | 8 import 'package:analyzer/src/generated/source.dart' show Source; |
| 9 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 9 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 10 show InSummarySource; | 10 show InSummarySource; |
| 11 import 'compiler.dart' | 11 import 'compiler.dart' |
| 12 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 12 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
| 13 import '../analyzer/context.dart' show AnalyzerOptions; | 13 import '../analyzer/context.dart' show AnalyzerOptions; |
| 14 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
| 15 | 15 |
| 16 typedef void MessageHandler(Object message); | 16 typedef void MessageHandler(Object message); |
| 17 | 17 |
| 18 /// The command for invoking the modular compiler. | 18 /// The command for invoking the modular compiler. |
| 19 class CompileCommand extends Command { | 19 class CompileCommand extends Command { |
| 20 get name => 'compile'; | 20 get name => 'compile'; |
| 21 get description => 'Compile a set of Dart files into a JavaScript module.'; | 21 get description => 'Compile a set of Dart files into a JavaScript module.'; |
| 22 final MessageHandler messageHandler; | 22 final MessageHandler messageHandler; |
| 23 | 23 |
| 24 CompileCommand({MessageHandler messageHandler}) | 24 CompileCommand({MessageHandler messageHandler}) |
| 25 : this.messageHandler = messageHandler ?? print { | 25 : this.messageHandler = messageHandler ?? print { |
| 26 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); | 26 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); |
| 27 argParser.addOption('build-root', |
| 28 help: ''' |
| 29 Root of source files. Generated library names are relative to this root. |
| 30 '''); |
| 27 CompilerOptions.addArguments(argParser); | 31 CompilerOptions.addArguments(argParser); |
| 28 AnalyzerOptions.addArguments(argParser); | 32 AnalyzerOptions.addArguments(argParser); |
| 29 } | 33 } |
| 30 | 34 |
| 31 @override | 35 @override |
| 32 void run() { | 36 void run() { |
| 33 var compiler = | 37 var compiler = |
| 34 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); | 38 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
| 35 var compilerOptions = new CompilerOptions.fromArguments(argResults); | 39 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
| 36 var outPath = argResults['out']; | 40 var outPath = argResults['out']; |
| 37 | 41 |
| 38 if (outPath == null) { | 42 if (outPath == null) { |
| 39 usageException('Please include the output file location. For example:\n' | 43 usageException('Please include the output file location. For example:\n' |
| 40 ' -o PATH/TO/OUTPUT_FILE.js'); | 44 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 41 } | 45 } |
| 42 var unit = new BuildUnit(path.basenameWithoutExtension(outPath), | 46 |
| 47 var buildRoot = argResults['build-root'] as String; |
| 48 if (buildRoot != null) { |
| 49 buildRoot = path.absolute(buildRoot); |
| 50 } else { |
| 51 buildRoot = Directory.current.path; |
| 52 } |
| 53 var unit = new BuildUnit(path.basenameWithoutExtension(outPath), buildRoot, |
| 43 argResults.rest, _moduleForLibrary); | 54 argResults.rest, _moduleForLibrary); |
| 44 | 55 |
| 45 JSModuleFile module = compiler.compile(unit, compilerOptions); | 56 JSModuleFile module = compiler.compile(unit, compilerOptions); |
| 46 module.errors.forEach(messageHandler); | 57 module.errors.forEach(messageHandler); |
| 47 | 58 |
| 48 if (!module.isValid) throw new CompileErrorException(); | 59 if (!module.isValid) throw new CompileErrorException(); |
| 49 | 60 |
| 50 // Write JS file, as well as source map and summary (if requested). | 61 // Write JS file, as well as source map and summary (if requested). |
| 51 new File(outPath).writeAsStringSync(module.code); | 62 new File(outPath).writeAsStringSync(module.code); |
| 52 if (module.sourceMap != null) { | 63 if (module.sourceMap != null) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 'Imported file "${source.uri}" was not found as a summary or source ' | 80 'Imported file "${source.uri}" was not found as a summary or source ' |
| 70 'file. Please pass in either the summary or the source file ' | 81 'file. Please pass in either the summary or the source file ' |
| 71 'for this import.'); | 82 'for this import.'); |
| 72 } | 83 } |
| 73 } | 84 } |
| 74 | 85 |
| 75 /// Thrown when the input source code has errors. | 86 /// Thrown when the input source code has errors. |
| 76 class CompileErrorException implements Exception { | 87 class CompileErrorException implements Exception { |
| 77 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 88 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 78 } | 89 } |
| OLD | NEW |