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; | 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('module-root', | |
| 28 help: 'Root module directory. ' | |
| 29 'Generated module paths are relative to this root.'); | |
| 27 argParser.addOption('build-root', | 30 argParser.addOption('build-root', |
|
Siggi Cherem (dart-lang)
2016/05/06 01:09:49
not for right now, but just thinking about how to
vsm
2016/05/06 01:17:48
Acknowledged.
| |
| 28 help: ''' | 31 help: 'Root of source files. ' |
| 29 Root of source files. Generated library names are relative to this root. | 32 'Generated library names are relative to this root.'); |
| 30 '''); | |
| 31 CompilerOptions.addArguments(argParser); | 33 CompilerOptions.addArguments(argParser); |
| 32 AnalyzerOptions.addArguments(argParser); | 34 AnalyzerOptions.addArguments(argParser); |
| 33 } | 35 } |
| 34 | 36 |
| 35 @override | 37 @override |
| 36 void run() { | 38 void run() { |
| 37 var compiler = | 39 var compiler = |
| 38 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); | 40 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
| 39 var compilerOptions = new CompilerOptions.fromArguments(argResults); | 41 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
| 40 var outPath = argResults['out']; | 42 var outPath = argResults['out']; |
| 41 | 43 |
| 42 if (outPath == null) { | 44 if (outPath == null) { |
| 43 usageException('Please include the output file location. For example:\n' | 45 usageException('Please include the output file location. For example:\n' |
| 44 ' -o PATH/TO/OUTPUT_FILE.js'); | 46 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 45 } | 47 } |
| 46 | 48 |
| 47 var buildRoot = argResults['build-root'] as String; | 49 var buildRoot = argResults['build-root'] as String; |
| 48 if (buildRoot != null) { | 50 if (buildRoot != null) { |
| 49 buildRoot = path.absolute(buildRoot); | 51 buildRoot = path.absolute(buildRoot); |
| 50 } else { | 52 } else { |
| 51 buildRoot = Directory.current.path; | 53 buildRoot = Directory.current.path; |
| 52 } | 54 } |
| 53 var unit = new BuildUnit(path.basenameWithoutExtension(outPath), buildRoot, | 55 var moduleRoot = argResults['module-root'] as String; |
| 54 argResults.rest, _moduleForLibrary); | 56 String modulePath; |
| 57 if (moduleRoot != null) { | |
| 58 moduleRoot = path.absolute(moduleRoot); | |
| 59 if (!path.isWithin(moduleRoot, outPath)) { | |
| 60 usageException('Output file $outPath must be within the $moduleRoot ' | |
|
Siggi Cherem (dart-lang)
2016/05/06 01:09:49
nit: maybe spell out "module root" to make it clea
vsm
2016/05/06 01:17:48
Done.
| |
| 61 'directory'); | |
| 62 } | |
| 63 modulePath = | |
| 64 path.withoutExtension(path.relative(outPath, from: moduleRoot)); | |
| 65 } else { | |
| 66 moduleRoot = path.dirname(outPath); | |
| 67 modulePath = path.basenameWithoutExtension(outPath); | |
| 68 } | |
| 69 | |
| 70 var unit = new BuildUnit(modulePath, buildRoot, argResults.rest, | |
| 71 (source) => _moduleForLibrary(moduleRoot, source)); | |
| 55 | 72 |
| 56 JSModuleFile module = compiler.compile(unit, compilerOptions); | 73 JSModuleFile module = compiler.compile(unit, compilerOptions); |
| 57 module.errors.forEach(messageHandler); | 74 module.errors.forEach(messageHandler); |
| 58 | 75 |
| 59 if (!module.isValid) throw new CompileErrorException(); | 76 if (!module.isValid) throw new CompileErrorException(); |
| 60 | 77 |
| 61 // Write JS file, as well as source map and summary (if requested). | 78 // Write JS file, as well as source map and summary (if requested). |
| 62 new File(outPath).writeAsStringSync(module.code); | 79 new File(outPath).writeAsStringSync(module.code); |
| 63 if (module.sourceMap != null) { | 80 if (module.sourceMap != null) { |
| 64 var mapPath = outPath + '.map'; | 81 var mapPath = outPath + '.map'; |
| 65 new File(mapPath) | 82 new File(mapPath) |
| 66 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); | 83 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); |
| 67 } | 84 } |
| 68 if (module.summaryBytes != null) { | 85 if (module.summaryBytes != null) { |
| 69 var summaryPath = path.withoutExtension(outPath) + '.sum'; | 86 var summaryPath = path.withoutExtension(outPath) + '.sum'; |
| 70 new File(summaryPath).writeAsBytesSync(module.summaryBytes); | 87 new File(summaryPath).writeAsBytesSync(module.summaryBytes); |
| 71 } | 88 } |
| 72 } | 89 } |
| 73 | 90 |
| 74 String _moduleForLibrary(Source source) { | 91 String _moduleForLibrary(String moduleRoot, Source source) { |
| 75 if (source is InSummarySource) { | 92 if (source is InSummarySource) { |
| 76 return path.basenameWithoutExtension(source.summaryPath); | 93 var summaryPath = source.summaryPath; |
| 94 if (path.isWithin(moduleRoot, summaryPath)) { | |
| 95 return path | |
| 96 .withoutExtension(path.relative(summaryPath, from: moduleRoot)); | |
| 97 } | |
| 98 | |
| 99 throw usageException( | |
| 100 'Imported file ${source.uri} is not within the $moduleRoot ' | |
|
Siggi Cherem (dart-lang)
2016/05/06 01:09:49
here too
vsm
2016/05/06 01:17:48
Done.
| |
| 101 'directory'); | |
| 77 } | 102 } |
| 78 | 103 |
| 79 throw usageException( | 104 throw usageException( |
| 80 'Imported file "${source.uri}" was not found as a summary or source ' | 105 'Imported file "${source.uri}" was not found as a summary or source ' |
| 81 'file. Please pass in either the summary or the source file ' | 106 'file. Please pass in either the summary or the source file ' |
| 82 'for this import.'); | 107 'for this import.'); |
| 83 } | 108 } |
| 84 } | 109 } |
| 85 | 110 |
| 86 /// Thrown when the input source code has errors. | 111 /// Thrown when the input source code has errors. |
| 87 class CompileErrorException implements Exception { | 112 class CompileErrorException implements Exception { |
| 88 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 113 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 89 } | 114 } |
| OLD | NEW |