OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** The entry point to the compiler. Used to implement `bin/dwc.dart`. */ | 5 /** The entry point to the compiler. Used to implement `bin/dwc.dart`. */ |
6 library dwc; | 6 library dwc; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'package:logging/logging.dart' show Level; | 10 import 'package:logging/logging.dart' show Level; |
11 | 11 |
12 import 'src/compiler.dart'; | 12 import 'src/compiler.dart'; |
13 import 'src/file_system.dart'; | 13 import 'src/file_system.dart'; |
14 import 'src/file_system/console.dart'; | 14 import 'src/file_system/console.dart'; |
15 import 'src/files.dart'; | 15 import 'src/files.dart'; |
16 import 'src/messages.dart'; | 16 import 'src/messages.dart'; |
17 import 'src/compiler_options.dart'; | 17 import 'src/compiler_options.dart'; |
18 import 'src/utils.dart'; | 18 import 'src/utils.dart'; |
19 | 19 |
20 FileSystem fileSystem; | 20 FileSystem _fileSystem; |
21 | 21 |
22 void main() { | 22 void main() { |
23 run(new Options().arguments).then((result) { | 23 run(new Options().arguments).then((result) { |
24 exit(result.success ? 0 : 1); | 24 exit(result.success ? 0 : 1); |
25 }); | 25 }); |
26 } | 26 } |
27 | 27 |
28 /** Contains the result of a compiler run. */ | 28 /** Contains the result of a compiler run. */ |
29 class CompilerResult { | 29 class CompilerResult { |
30 final bool success; | 30 final bool success; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 * See [CompilerOptions] for the definition of valid arguments. | 64 * See [CompilerOptions] for the definition of valid arguments. |
65 */ | 65 */ |
66 // TODO(jmesserly): fix this to return a proper exit code | 66 // TODO(jmesserly): fix this to return a proper exit code |
67 // TODO(justinfagnani): return messages in the result | 67 // TODO(justinfagnani): return messages in the result |
68 Future<CompilerResult> run(List<String> args, {bool printTime, | 68 Future<CompilerResult> run(List<String> args, {bool printTime, |
69 bool shouldPrint: true}) { | 69 bool shouldPrint: true}) { |
70 var options = CompilerOptions.parse(args); | 70 var options = CompilerOptions.parse(args); |
71 if (options == null) return new Future.value(new CompilerResult()); | 71 if (options == null) return new Future.value(new CompilerResult()); |
72 if (printTime == null) printTime = options.verbose; | 72 if (printTime == null) printTime = options.verbose; |
73 | 73 |
74 fileSystem = new ConsoleFileSystem(); | 74 _fileSystem = new ConsoleFileSystem(); |
75 var messages = new Messages(options: options, shouldPrint: shouldPrint); | 75 var messages = new Messages(options: options, shouldPrint: shouldPrint); |
76 | 76 |
77 return asyncTime('Total time spent on ${options.inputFile}', () { | 77 return asyncTime('Total time spent on ${options.inputFile}', () { |
78 var compiler = new Compiler(fileSystem, options, messages); | 78 var compiler = new Compiler(_fileSystem, options, messages); |
79 var res; | 79 var res; |
80 return compiler.run() | 80 return compiler.run() |
81 .then((_) { | 81 .then((_) { |
82 var success = messages.messages.every((m) => m.level != Level.SEVERE); | 82 var success = messages.messages.every((m) => m.level != Level.SEVERE); |
83 var msgs = options.jsonFormat | 83 var msgs = options.jsonFormat |
84 ? messages.messages.map((m) => m.toJson()) | 84 ? messages.messages.map((m) => m.toJson()) |
85 : messages.messages.map((m) => m.toString()); | 85 : messages.messages.map((m) => m.toString()); |
86 res = new CompilerResult._(success, msgs.toList(), | 86 res = new CompilerResult._(success, msgs.toList(), |
87 compiler.output, compiler.files); | 87 compiler.output, compiler.files); |
88 }) | 88 }) |
89 .then((_) => symlinkPubPackages(res, options, messages)) | 89 .then((_) => _symlinkPubPackages(res, options, messages)) |
90 .then((_) => emitFiles(compiler.output, options.clean)) | 90 .then((_) => _emitFiles(compiler.output, options.clean)) |
91 .then((_) => res); | 91 .then((_) => res); |
92 }, printTime: printTime, useColors: options.useColors); | 92 }, printTime: printTime, useColors: options.useColors); |
93 } | 93 } |
94 | 94 |
95 Future emitFiles(List<OutputFile> outputs, bool clean) { | 95 Future _emitFiles(List<OutputFile> outputs, bool clean) { |
96 outputs.forEach((f) => writeFile(f.path, f.contents, clean)); | 96 outputs.forEach((f) => _writeFile(f.path, f.contents, clean)); |
97 return fileSystem.flush(); | 97 return _fileSystem.flush(); |
98 } | 98 } |
99 | 99 |
100 void writeFile(String filePath, String contents, bool clean) { | 100 void _writeFile(String filePath, String contents, bool clean) { |
101 if (clean) { | 101 if (clean) { |
102 File fileOut = new File(filePath); | 102 File fileOut = new File(filePath); |
103 if (fileOut.existsSync()) { | 103 if (fileOut.existsSync()) { |
104 fileOut.deleteSync(); | 104 fileOut.deleteSync(); |
105 } | 105 } |
106 } else { | 106 } else { |
107 _createIfNeeded(path.dirname(filePath)); | 107 _createIfNeeded(path.dirname(filePath)); |
108 fileSystem.writeString(filePath, contents); | 108 _fileSystem.writeString(filePath, contents); |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 void _createIfNeeded(String outdir) { | 112 void _createIfNeeded(String outdir) { |
113 if (outdir.isEmpty) return; | 113 if (outdir.isEmpty) return; |
114 var outDirectory = new Directory(outdir); | 114 var outDirectory = new Directory(outdir); |
115 if (!outDirectory.existsSync()) { | 115 if (!outDirectory.existsSync()) { |
116 _createIfNeeded(path.dirname(outdir)); | 116 _createIfNeeded(path.dirname(outdir)); |
117 outDirectory.createSync(); | 117 outDirectory.createSync(); |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
121 /** | 121 /** |
122 * Creates a symlink to the pub packages directory in the output location. The | 122 * Creates a symlink to the pub packages directory in the output location. The |
123 * returned future completes when the symlink was created (or immediately if it | 123 * returned future completes when the symlink was created (or immediately if it |
124 * already exists). | 124 * already exists). |
125 */ | 125 */ |
126 Future symlinkPubPackages(CompilerResult result, CompilerOptions options, | 126 Future _symlinkPubPackages(CompilerResult result, CompilerOptions options, |
127 Messages messages) { | 127 Messages messages) { |
128 if (options.outputDir == null || result.bootstrapFile == null | 128 if (options.outputDir == null || result.bootstrapFile == null |
129 || options.packageRoot != null) { | 129 || options.packageRoot != null) { |
130 // We don't need to copy the packages directory if the output was generated | 130 // We don't need to copy the packages directory if the output was generated |
131 // in-place where the input lives, if the compiler was called without an | 131 // in-place where the input lives, if the compiler was called without an |
132 // entry-point file, or if the compiler was called with a package-root | 132 // entry-point file, or if the compiler was called with a package-root |
133 // option. | 133 // option. |
134 return new Future.value(null); | 134 return new Future.value(null); |
135 } | 135 } |
136 | 136 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 if (result.exitCode != 0) { | 189 if (result.exitCode != 0) { |
190 var details = 'subprocess stdout:\n${result.stdout}\n' | 190 var details = 'subprocess stdout:\n${result.stdout}\n' |
191 'subprocess stderr:\n${result.stderr}'; | 191 'subprocess stderr:\n${result.stderr}'; |
192 messages.error( | 192 messages.error( |
193 'unable to create symlink\n target: $target\n link:$link\n$details', | 193 'unable to create symlink\n target: $target\n link:$link\n$details', |
194 null); | 194 null); |
195 } | 195 } |
196 return null; | 196 return null; |
197 }); | 197 }); |
198 } | 198 } |
OLD | NEW |