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 final MessageHandler messageHandler; | 20 final MessageHandler messageHandler; |
21 CompilerOptions _compilerOptions; | 21 CompilerOptions _compilerOptions; |
22 | 22 |
23 CompileCommand({MessageHandler messageHandler}) | 23 CompileCommand({MessageHandler messageHandler}) |
24 : this.messageHandler = messageHandler ?? print { | 24 : this.messageHandler = messageHandler ?? print { |
25 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); | 25 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); |
26 argParser.addOption('module-root', | 26 argParser.addOption('module-root', |
27 help: 'Root module directory. ' | 27 help: 'Root module directory. ' |
28 'Generated module paths are relative to this root.'); | 28 'Generated module paths are relative to this root.'); |
29 argParser.addOption('build-root', | 29 argParser.addOption('library-root', |
30 help: 'Root of source files. ' | 30 help: 'Root of source files. ' |
31 'Generated library names are relative to this root.'); | 31 'Generated library names are relative to this root.'); |
| 32 argParser.addOption('build-root', |
| 33 help: 'Deprecated in favor of --library-root'); |
32 CompilerOptions.addArguments(argParser); | 34 CompilerOptions.addArguments(argParser); |
33 AnalyzerOptions.addArguments(argParser); | 35 AnalyzerOptions.addArguments(argParser); |
34 } | 36 } |
35 | 37 |
36 get name => 'compile'; | 38 get name => 'compile'; |
37 get description => 'Compile a set of Dart files into a JavaScript module.'; | 39 get description => 'Compile a set of Dart files into a JavaScript module.'; |
38 | 40 |
39 @override | 41 @override |
40 void run() { | 42 void run() { |
41 var compiler = | 43 var compiler = |
42 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); | 44 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
43 _compilerOptions = new CompilerOptions.fromArguments(argResults); | 45 _compilerOptions = new CompilerOptions.fromArguments(argResults); |
44 var outPath = argResults['out']; | 46 var outPath = argResults['out']; |
45 | 47 |
46 if (outPath == null) { | 48 if (outPath == null) { |
47 usageException('Please include the output file location. For example:\n' | 49 usageException('Please include the output file location. For example:\n' |
48 ' -o PATH/TO/OUTPUT_FILE.js'); | 50 ' -o PATH/TO/OUTPUT_FILE.js'); |
49 } | 51 } |
50 | 52 |
51 var buildRoot = argResults['build-root'] as String; | 53 var libraryRoot = argResults['library-root'] as String; |
52 if (buildRoot != null) { | 54 libraryRoot ??= argResults['build-root'] as String; |
53 buildRoot = path.absolute(buildRoot); | 55 if (libraryRoot != null) { |
| 56 libraryRoot = path.absolute(libraryRoot); |
54 } else { | 57 } else { |
55 buildRoot = Directory.current.path; | 58 libraryRoot = Directory.current.path; |
56 } | 59 } |
57 var moduleRoot = argResults['module-root'] as String; | 60 var moduleRoot = argResults['module-root'] as String; |
58 String modulePath; | 61 String modulePath; |
59 if (moduleRoot != null) { | 62 if (moduleRoot != null) { |
60 moduleRoot = path.absolute(moduleRoot); | 63 moduleRoot = path.absolute(moduleRoot); |
61 if (!path.isWithin(moduleRoot, outPath)) { | 64 if (!path.isWithin(moduleRoot, outPath)) { |
62 usageException('Output file $outPath must be within the module root ' | 65 usageException('Output file $outPath must be within the module root ' |
63 'directory $moduleRoot'); | 66 'directory $moduleRoot'); |
64 } | 67 } |
65 modulePath = | 68 modulePath = |
66 path.withoutExtension(path.relative(outPath, from: moduleRoot)); | 69 path.withoutExtension(path.relative(outPath, from: moduleRoot)); |
67 } else { | 70 } else { |
68 moduleRoot = path.dirname(outPath); | 71 moduleRoot = path.dirname(outPath); |
69 modulePath = path.basenameWithoutExtension(outPath); | 72 modulePath = path.basenameWithoutExtension(outPath); |
70 } | 73 } |
71 | 74 |
72 if (argResults.rest.isEmpty) { | 75 if (argResults.rest.isEmpty) { |
73 usageException('Please pass at least one source file as an argument.'); | 76 usageException('Please pass at least one source file as an argument.'); |
74 } | 77 } |
75 | 78 |
76 var unit = new BuildUnit(modulePath, buildRoot, argResults.rest, | 79 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, |
77 (source) => _moduleForLibrary(moduleRoot, source)); | 80 (source) => _moduleForLibrary(moduleRoot, source)); |
78 | 81 |
79 JSModuleFile module = compiler.compile(unit, _compilerOptions); | 82 JSModuleFile module = compiler.compile(unit, _compilerOptions); |
80 module.errors.forEach(messageHandler); | 83 module.errors.forEach(messageHandler); |
81 | 84 |
82 if (!module.isValid) throw new CompileErrorException(); | 85 if (!module.isValid) throw new CompileErrorException(); |
83 | 86 |
84 // Write JS file, as well as source map and summary (if requested). | 87 // Write JS file, as well as source map and summary (if requested). |
85 new File(outPath).writeAsStringSync(module.code); | 88 new File(outPath).writeAsStringSync(module.code); |
86 if (module.sourceMap != null) { | 89 if (module.sourceMap != null) { |
(...skipping 27 matching lines...) Expand all Loading... |
114 'Imported file "${source.uri}" was not found as a summary or source ' | 117 'Imported file "${source.uri}" was not found as a summary or source ' |
115 'file. Please pass in either the summary or the source file ' | 118 'file. Please pass in either the summary or the source file ' |
116 'for this import.'); | 119 'for this import.'); |
117 } | 120 } |
118 } | 121 } |
119 | 122 |
120 /// Thrown when the input source code has errors. | 123 /// Thrown when the input source code has errors. |
121 class CompileErrorException implements Exception { | 124 class CompileErrorException implements Exception { |
122 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 125 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
123 } | 126 } |
OLD | NEW |