| 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:io'; | 5 import 'dart:io'; |
| 6 import 'package:analyzer/src/generated/source.dart' show Source; | 6 import 'package:analyzer/src/generated/source.dart' show Source; |
| 7 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 7 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 8 show InSummarySource; | 8 show InSummarySource; |
| 9 import 'package:args/args.dart' show ArgParser, ArgResults; | 9 import 'package:args/args.dart' show ArgParser, ArgResults; |
| 10 import 'package:args/command_runner.dart' show UsageException; | 10 import 'package:args/command_runner.dart' show UsageException; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 if (outPaths.isEmpty) { | 96 if (outPaths.isEmpty) { |
| 97 _usageException('Please include the output file location. For example:\n' | 97 _usageException('Please include the output file location. For example:\n' |
| 98 ' -o PATH/TO/OUTPUT_FILE.js'); | 98 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 99 } else if (outPaths.length != moduleFormats.length) { | 99 } else if (outPaths.length != moduleFormats.length) { |
| 100 _usageException('Number of output files (${outPaths.length}) must match ' | 100 _usageException('Number of output files (${outPaths.length}) must match ' |
| 101 'number of module formats (${moduleFormats.length}).'); | 101 'number of module formats (${moduleFormats.length}).'); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // TODO(jmesserly): for now the first one is special. This will go away once | 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. | 105 // we've removed the "root" and "module name" variables. |
| 106 var outPath = outPaths[0]; | 106 var firstOutPath = outPaths[0]; |
| 107 | 107 |
| 108 var libraryRoot = argResults['library-root'] as String; | 108 var libraryRoot = argResults['library-root'] as String; |
| 109 libraryRoot ??= argResults['build-root'] as String; | 109 libraryRoot ??= argResults['build-root'] as String; |
| 110 if (libraryRoot != null) { | 110 if (libraryRoot != null) { |
| 111 libraryRoot = path.absolute(libraryRoot); | 111 libraryRoot = path.absolute(libraryRoot); |
| 112 } else { | 112 } else { |
| 113 libraryRoot = Directory.current.path; | 113 libraryRoot = Directory.current.path; |
| 114 } | 114 } |
| 115 var moduleRoot = argResults['module-root'] as String; | 115 var moduleRoot = argResults['module-root'] as String; |
| 116 String modulePath; | 116 String modulePath; |
| 117 if (moduleRoot != null) { | 117 if (moduleRoot != null) { |
| 118 moduleRoot = path.absolute(moduleRoot); | 118 moduleRoot = path.absolute(moduleRoot); |
| 119 if (!path.isWithin(moduleRoot, outPath)) { | 119 if (!path.isWithin(moduleRoot, firstOutPath)) { |
| 120 _usageException('Output file $outPath must be within the module root ' | 120 _usageException('Output file $firstOutPath must be within the module ' |
| 121 'directory $moduleRoot'); | 121 'root directory $moduleRoot'); |
| 122 } | 122 } |
| 123 modulePath = | 123 modulePath = |
| 124 path.withoutExtension(path.relative(outPath, from: moduleRoot)); | 124 path.withoutExtension(path.relative(firstOutPath, from: moduleRoot)); |
| 125 } else { | 125 } else { |
| 126 moduleRoot = path.dirname(outPath); | 126 moduleRoot = path.dirname(firstOutPath); |
| 127 modulePath = path.basenameWithoutExtension(outPath); | 127 modulePath = path.basenameWithoutExtension(firstOutPath); |
| 128 } | 128 } |
| 129 | 129 |
| 130 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, | 130 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, |
| 131 (source) => _moduleForLibrary(moduleRoot, source, compilerOpts)); | 131 (source) => _moduleForLibrary(moduleRoot, source, compilerOpts)); |
| 132 | 132 |
| 133 JSModuleFile module = compiler.compile(unit, compilerOpts); | 133 var module = compiler.compile(unit, compilerOpts); |
| 134 module.errors.forEach(printFn); | 134 module.errors.forEach(printFn); |
| 135 | 135 |
| 136 if (!module.isValid) throw new CompileErrorException(); | 136 if (!module.isValid) throw new CompileErrorException(); |
| 137 | 137 |
| 138 // 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). |
| 139 for (var i = 0; i < outPaths.length; i++) { | 139 for (var i = 0; i < outPaths.length; i++) { |
| 140 module.writeCodeSync(moduleFormats[i], outPaths[i]); | 140 var outPath = outPaths[i]; |
| 141 module.writeCodeSync(moduleFormats[i], outPath); |
| 141 if (module.summaryBytes != null) { | 142 if (module.summaryBytes != null) { |
| 142 var summaryPath = | 143 var summaryPath = |
| 143 path.withoutExtension(outPath) + '.${compilerOpts.summaryExtension}'; | 144 path.withoutExtension(outPath) + '.${compilerOpts.summaryExtension}'; |
| 144 new File(summaryPath).writeAsBytesSync(module.summaryBytes); | 145 new File(summaryPath).writeAsBytesSync(module.summaryBytes); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | |
| 148 } | 148 } |
| 149 | 149 |
| 150 String _moduleForLibrary( | 150 String _moduleForLibrary( |
| 151 String moduleRoot, Source source, CompilerOptions compilerOpts) { | 151 String moduleRoot, Source source, CompilerOptions compilerOpts) { |
| 152 if (source is InSummarySource) { | 152 if (source is InSummarySource) { |
| 153 var summaryPath = source.summaryPath; | 153 var summaryPath = source.summaryPath; |
| 154 var ext = '.${compilerOpts.summaryExtension}'; | 154 var ext = '.${compilerOpts.summaryExtension}'; |
| 155 if (path.isWithin(moduleRoot, summaryPath) && summaryPath.endsWith(ext)) { | 155 if (path.isWithin(moduleRoot, summaryPath) && summaryPath.endsWith(ext)) { |
| 156 var buildUnitPath = | 156 var buildUnitPath = |
| 157 summaryPath.substring(0, summaryPath.length - ext.length); | 157 summaryPath.substring(0, summaryPath.length - ext.length); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 174 '\n\n${_argParser.usage}'; | 174 '\n\n${_argParser.usage}'; |
| 175 | 175 |
| 176 void _usageException(String message) { | 176 void _usageException(String message) { |
| 177 throw new UsageException(message, _usageMessage); | 177 throw new UsageException(message, _usageMessage); |
| 178 } | 178 } |
| 179 | 179 |
| 180 /// Thrown when the input source code has errors. | 180 /// Thrown when the input source code has errors. |
| 181 class CompileErrorException implements Exception { | 181 class CompileErrorException implements Exception { |
| 182 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 182 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 183 } | 183 } |
| OLD | NEW |