OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'dart:io'; |
| 6 |
| 7 Future<String> getVersion(var options, var rootPath) { |
| 8 var versionPath = rootPath.append("tools").append("version.dart"); |
| 9 return Process.run(options.executable, |
| 10 [versionPath.toNativePath()]) |
| 11 .then((result) { |
| 12 if (result.exitCode != 0) { |
| 13 throw "Could not generate version"; |
| 14 } |
| 15 return result.stdout.trim(); |
| 16 }); |
| 17 } |
| 18 |
| 19 Future<String> getSnapshotGenerationFile(var options, var args, var rootPath) { |
| 20 var dart2js = rootPath.append(args["dart2js_main"]); |
| 21 |
| 22 return getVersion(options, rootPath).then((version) { |
| 23 var snapshotGenerationText = |
| 24 """ |
| 25 import '${dart2js}' as dart2jsMain; |
| 26 import 'dart:io'; |
| 27 |
| 28 void main() { |
| 29 Options options = new Options(); |
| 30 if (options.arguments.length < 1) throw "No tool given as argument"; |
| 31 String tool = options.arguments.removeAt(0); |
| 32 if (tool == "dart2js") { |
| 33 dart2jsMain.BUILD_ID = "$version"; |
| 34 dart2jsMain.mainWithErrorHandler(options); |
| 35 } |
| 36 } |
| 37 |
| 38 """; |
| 39 return snapshotGenerationText; |
| 40 }); |
| 41 } |
| 42 |
| 43 void writeSnapshotFile(var path, var content) { |
| 44 File file = new File(path); |
| 45 var writer = file.openSync(mode: FileMode.WRITE); |
| 46 writer.writeStringSync(content); |
| 47 writer.close(); |
| 48 } |
| 49 |
| 50 Future createSnapshot(var options, var dart_file) { |
| 51 return Process.run(options.executable, |
| 52 ["--generate-script-snapshot=$dart_file.snapshot", |
| 53 dart_file]) |
| 54 .then((result) { |
| 55 if (result.exitCode != 0) { |
| 56 throw "Could not generate snapshot"; |
| 57 } |
| 58 }); |
| 59 } |
| 60 |
| 61 /** |
| 62 * Takes the following arguments: |
| 63 * --output_dir=val The full path to the output_dir. |
| 64 * --dart2js_main=val The path to the dart2js main script releative to root. |
| 65 */ |
| 66 void main() { |
| 67 Options options = new Options(); |
| 68 var validArguments = ["--output_dir", "--dart2js_main"]; |
| 69 var args = {}; |
| 70 for (var argument in options.arguments) { |
| 71 var argumentSplit = argument.split("="); |
| 72 if (argumentSplit.length != 2) throw "Invalid argument $argument, no ="; |
| 73 if (!validArguments.contains(argumentSplit[0])) { |
| 74 throw "Invalid argument $argument"; |
| 75 } |
| 76 args[argumentSplit[0].substring(2)] = argumentSplit[1]; |
| 77 } |
| 78 if (!args.containsKey("dart2js_main")) throw "Please specify dart2js_main"; |
| 79 if (!args.containsKey("output_dir")) throw "Please specify output_dir"; |
| 80 |
| 81 var scriptFile = new File(options.script); |
| 82 var path = new Path(scriptFile.directorySync().path); |
| 83 var rootPath = path.directoryPath.directoryPath; |
| 84 |
| 85 getSnapshotGenerationFile(options, args, rootPath).then((result) { |
| 86 var wrapper = "${args['output_dir']}/utils_wrapper.dart"; |
| 87 writeSnapshotFile(wrapper, result); |
| 88 createSnapshot(options, wrapper); |
| 89 }); |
| 90 } |
OLD | NEW |