OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. |
| 5 |
| 6 import 'dart:io'; |
| 7 |
| 8 import 'package:args/args.dart' as args; |
| 9 import 'package:path/path.dart' as path; |
| 10 |
| 11 args.ArgParser parser = new args.ArgParser(allowTrailingOptions: true) |
| 12 ..addOption("sdk", |
| 13 abbr: "s", |
| 14 help: "Path to the Dart SDK. By default it will be searched at the path\n" |
| 15 "'../../../out/ReleaseX64/patched_sdk' relative to the directory\n" |
| 16 "of 'reified_dart'.", |
| 17 defaultsTo: null) |
| 18 ..addOption("dartk", |
| 19 abbr: "k", |
| 20 help: "Path to 'dartk' executable. By default it will be searched for\n" |
| 21 "in the same directory as 'reified_dart'.", |
| 22 defaultsTo: null) |
| 23 ..addOption("dill-output", |
| 24 abbr: "d", |
| 25 help: "Path to intermediate reified .dill file. If not specified,\n" |
| 26 "the intermediate file is created in a temporary location\n" |
| 27 "and is removed after program execution.", |
| 28 defaultsTo: null); |
| 29 |
| 30 String getUsage() => """ |
| 31 Usage: reified_dart [options] FILE |
| 32 |
| 33 Reifies generic types in FILE and runs the transformed program. |
| 34 |
| 35 Examples: |
| 36 reified_dart foo.dart |
| 37 reified_dart --sdk=/path/to/sdk foo.dart |
| 38 reified_dart --sdk=/path/to/sdk --dartk=/path/to/dartk foo.dart |
| 39 |
| 40 Options: |
| 41 ${parser.usage} |
| 42 """; |
| 43 |
| 44 void fail(String message) { |
| 45 stderr.writeln(message); |
| 46 exit(1); |
| 47 } |
| 48 |
| 49 args.ArgResults options; |
| 50 |
| 51 void checkIsDirectory(String path, {String option, String description}) { |
| 52 description = (description == null ? "" : "$description\n"); |
| 53 switch (new File(path).statSync().type) { |
| 54 case FileSystemEntityType.DIRECTORY: |
| 55 case FileSystemEntityType.LINK: |
| 56 return; |
| 57 case FileSystemEntityType.NOT_FOUND: |
| 58 throw fail('$description$option not found: $path'); |
| 59 default: |
| 60 fail('$description$option is not a directory: $path'); |
| 61 } |
| 62 } |
| 63 |
| 64 void checkIsFile(String path, {String option, String description}) { |
| 65 description = (description == null ? "" : "$description\n"); |
| 66 var stat = new File(path).statSync(); |
| 67 switch (stat.type) { |
| 68 case FileSystemEntityType.DIRECTORY: |
| 69 throw fail('$description$option is a directory: $path'); |
| 70 |
| 71 case FileSystemEntityType.NOT_FOUND: |
| 72 throw fail('$description$option not found: $path'); |
| 73 } |
| 74 } |
| 75 |
| 76 String getDefaultSdk() { |
| 77 String currentFile = Platform.script.toFilePath(); |
| 78 |
| 79 // Respect different path separators. |
| 80 String relativePath = "../../../out/ReleaseX64/patched_sdk"; |
| 81 List<String> components = relativePath.split("/"); |
| 82 relativePath = ""; |
| 83 for (String component in components) { |
| 84 relativePath = path.join(relativePath, component); |
| 85 } |
| 86 |
| 87 String currentDir = path.dirname(currentFile); |
| 88 String sdkPath = path.normalize(path.join(currentDir, relativePath)); |
| 89 |
| 90 checkIsDirectory(sdkPath, |
| 91 option: "Path to Dart SDK", |
| 92 description: "The --sdk option wasn't specified, " |
| 93 "so default location was checked."); |
| 94 |
| 95 return sdkPath; |
| 96 } |
| 97 |
| 98 String getDefaultDartk() { |
| 99 String currentFile = Platform.script.toFilePath(); |
| 100 String dartkPath = path.join(path.dirname(currentFile), "dartk.dart"); |
| 101 |
| 102 checkIsFile(dartkPath, |
| 103 option: "Path to 'dartk'", |
| 104 description: "The --dartk option wasn't specified, " |
| 105 "so default location was checked."); |
| 106 |
| 107 return dartkPath; |
| 108 } |
| 109 |
| 110 main(List<String> arguments) async { |
| 111 if (arguments.length == 0) { |
| 112 fail(getUsage()); |
| 113 } |
| 114 |
| 115 try { |
| 116 options = parser.parse(arguments); |
| 117 } on FormatException catch (e) { |
| 118 fail(e.message); |
| 119 } |
| 120 |
| 121 if (options.rest.length != 1) { |
| 122 fail("Exactly one FILE should be given."); |
| 123 } |
| 124 |
| 125 String inputFilename = options.rest.single; |
| 126 checkIsFile(inputFilename, option: "Input file"); |
| 127 |
| 128 String sdkPath = options["sdk"] ?? getDefaultSdk(); |
| 129 checkIsDirectory(sdkPath, option: "Path to Dart SDK"); |
| 130 |
| 131 String dartkPath = options["dartk"] ?? getDefaultDartk(); |
| 132 checkIsFile(dartkPath, option: "Path to 'dartk'"); |
| 133 |
| 134 String dillOutput = options["dill-output"]; |
| 135 File tempFile = null; |
| 136 if (dillOutput == null) { |
| 137 Directory tmp = await Directory.systemTemp.createTemp(); |
| 138 Uri uri = tmp.uri.resolve("generated.dill"); |
| 139 dillOutput = uri.toFilePath(); |
| 140 tempFile = new File.fromUri(uri); |
| 141 } |
| 142 |
| 143 ProcessResult result = await Process.run(dartkPath, [ |
| 144 "--sdk=$sdkPath", |
| 145 "--target=vmreify", |
| 146 "--link", |
| 147 "--out=$dillOutput", |
| 148 inputFilename, |
| 149 ]); |
| 150 if (result.exitCode != 0) { |
| 151 tempFile?.parent?.delete(recursive: true); |
| 152 stdout.write(result.stdout); |
| 153 stderr.write(result.stderr); |
| 154 stderr.writeln("ERROR: execution of 'dartk' failed with exit code " |
| 155 "${result.exitCode}"); |
| 156 exit(result.exitCode); |
| 157 } |
| 158 |
| 159 result = await Process.run("/usr/bin/env", [ |
| 160 "dart", |
| 161 dillOutput, |
| 162 inputFilename, |
| 163 ]); |
| 164 |
| 165 stdout.write(result.stdout); |
| 166 stderr.write(result.stderr); |
| 167 tempFile?.parent?.delete(recursive: true); |
| 168 if (result.exitCode != 0) { |
| 169 stderr.writeln("ERROR: execution of 'dart' failed with exit code " |
| 170 "${result.exitCode}"); |
| 171 exit(result.exitCode); |
| 172 } |
| 173 } |
OLD | NEW |