Chromium Code Reviews| 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 var stat = new File(path).statSync(); | |
|
karlklose
2017/02/17 12:35:19
How about 'type' or 'kind' instead of 'stat'? You
Dmitry Stefantsov
2017/02/17 13:10:34
Done.
| |
| 54 switch (stat.type) { | |
| 55 case FileSystemEntityType.DIRECTORY: | |
| 56 case FileSystemEntityType.LINK: | |
| 57 return; | |
| 58 case FileSystemEntityType.NOT_FOUND: | |
| 59 throw fail('$description$option not found: $path'); | |
| 60 default: | |
| 61 fail('$description$option is not a directory: $path'); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void checkIsFile(String path, {String option, String description}) { | |
| 66 description = (description == null ? "" : "$description\n"); | |
| 67 var stat = new File(path).statSync(); | |
| 68 switch (stat.type) { | |
| 69 case FileSystemEntityType.DIRECTORY: | |
| 70 throw fail('$description$option is a directory: $path'); | |
| 71 | |
| 72 case FileSystemEntityType.NOT_FOUND: | |
| 73 throw fail('$description$option not found: $path'); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 String defaultSdk() { | |
| 78 String currentFile = Platform.script.toFilePath(); | |
| 79 | |
| 80 // Respect different path separators. | |
| 81 String relativePath = "../../../out/ReleaseX64/patched_sdk"; | |
| 82 List<String> components = relativePath.split("/"); | |
| 83 relativePath = ""; | |
| 84 for (String component in components) { | |
| 85 relativePath = path.join(relativePath, component); | |
| 86 } | |
| 87 | |
| 88 String currentDir = path.dirname(currentFile); | |
| 89 String sdkPath = path.join(currentDir, relativePath); | |
| 90 sdkPath = path.normalize(sdkPath); | |
|
karlklose
2017/02/17 12:35:19
Add call to 'normalize' to previous line?
Dmitry Stefantsov
2017/02/17 13:10:34
Done.
| |
| 91 | |
| 92 return sdkPath; | |
| 93 } | |
| 94 | |
| 95 String defaultDartk() { | |
| 96 String currentFile = Platform.script.toFilePath(); | |
| 97 String dartkPath = path.join(path.dirname(currentFile), "dartk.dart"); | |
| 98 return dartkPath; | |
| 99 } | |
| 100 | |
| 101 main(List<String> arguments) async { | |
| 102 if (arguments.length == 0) { | |
| 103 fail(getUsage()); | |
| 104 } | |
| 105 | |
| 106 try { | |
| 107 options = parser.parse(arguments); | |
| 108 } on FormatException catch (e) { | |
| 109 fail(e.message); | |
| 110 } | |
| 111 | |
| 112 if (options.rest.length != 1) { | |
| 113 fail("Exactly one FILE should be given."); | |
| 114 } | |
| 115 | |
| 116 String inputFilename = options.rest.single; | |
| 117 checkIsFile(inputFilename, option: "Input file"); | |
| 118 | |
| 119 String sdkPath = options["sdk"]; | |
| 120 if (sdkPath == null) { | |
|
karlklose
2017/02/17 12:35:19
Consider moving this whole logic into getDefaultSd
Dmitry Stefantsov
2017/02/17 13:10:34
Done.
| |
| 121 sdkPath = defaultSdk(); | |
| 122 checkIsDirectory(sdkPath, | |
| 123 option: "Path to Dart SDK", | |
| 124 description: "The --sdk option wasn't specified, " | |
| 125 "so default location was checked."); | |
| 126 } else { | |
| 127 checkIsDirectory(sdkPath, option: "Path to Dart SDK"); | |
| 128 } | |
| 129 | |
| 130 String dartkPath = options["dartk"]; | |
| 131 if (dartkPath == null) { | |
| 132 dartkPath = defaultDartk(); | |
| 133 checkIsFile(dartkPath, | |
| 134 option: "Path to 'dartk'", | |
| 135 description: "The --dartk option wasn't specified, " | |
| 136 "so default location was checked."); | |
| 137 } else { | |
| 138 checkIsFile(dartkPath, option: "Path to 'dartk'"); | |
| 139 } | |
| 140 | |
| 141 String dillOutput = options["dill-output"]; | |
| 142 File tempFile = null; | |
| 143 if (dillOutput == null) { | |
| 144 Directory tmp = await Directory.systemTemp.createTemp(); | |
| 145 Uri uri = tmp.uri.resolve("generated.dill"); | |
| 146 dillOutput = uri.toFilePath(); | |
| 147 tempFile = new File.fromUri(uri); | |
| 148 } | |
| 149 | |
| 150 ProcessResult result = Process.runSync(dartkPath, [ | |
|
karlklose
2017/02/17 12:35:19
Since main is already async you could also use 'aw
Dmitry Stefantsov
2017/02/17 13:10:34
Done.
| |
| 151 "--sdk=$sdkPath", | |
| 152 "--target=vmreify", | |
| 153 "--link", | |
| 154 "--out=$dillOutput", | |
| 155 inputFilename, | |
| 156 ]); | |
| 157 if (result.exitCode != 0) { | |
| 158 tempFile?.parent?.delete(recursive: true); | |
| 159 stderr.write(result.stderr); | |
|
karlklose
2017/02/17 12:35:19
Maybe print something like 'execution failed with
Dmitry Stefantsov
2017/02/17 13:10:34
Done.
| |
| 160 exit(result.exitCode); | |
| 161 } | |
| 162 | |
| 163 result = Process.runSync("/usr/bin/env", [ | |
| 164 "dart", | |
| 165 dillOutput, | |
| 166 inputFilename, | |
| 167 ]); | |
| 168 if (result.exitCode != 0) { | |
|
karlklose
2017/02/17 12:35:19
I would print result.stdOut even with failures.
T
Dmitry Stefantsov
2017/02/17 13:10:34
Done.
| |
| 169 tempFile?.parent?.delete(recursive: true); | |
| 170 stderr.write(result.stderr); | |
| 171 exit(result.exitCode); | |
| 172 } | |
| 173 | |
| 174 stdout.write(result.stdout); | |
| 175 stderr.write(result.stderr); | |
| 176 tempFile?.parent?.delete(recursive: true); | |
| 177 } | |
| OLD | NEW |