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