| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:args/args.dart'; |
| 10 |
| 11 import 'cache.dart'; |
| 12 |
| 9 /// Split [text] using [infixes] as infix markers. | 13 /// Split [text] using [infixes] as infix markers. |
| 10 List<String> split(String text, List<String> infixes) { | 14 List<String> split(String text, List<String> infixes) { |
| 11 List<String> result = <String>[]; | 15 List<String> result = <String>[]; |
| 12 int start = 0; | 16 int start = 0; |
| 13 for (String infix in infixes) { | 17 for (String infix in infixes) { |
| 14 int index = text.indexOf(infix, start); | 18 int index = text.indexOf(infix, start); |
| 15 if (index == -1) | 19 if (index == -1) |
| 16 throw "'$infix' not found in '$text' from offset ${start}."; | 20 throw "'$infix' not found in '$text' from offset ${start}."; |
| 17 result.add(text.substring(start, index)); | 21 result.add(text.substring(start, index)); |
| 18 start = index + infix.length; | 22 start = index + infix.length; |
| 19 } | 23 } |
| 20 result.add(text.substring(start)); | 24 result.add(text.substring(start)); |
| 21 return result; | 25 return result; |
| 22 } | 26 } |
| 23 | 27 |
| 24 /// Pad [text] with spaces to the right to fit [length]. | 28 /// Pad [text] with spaces to the right to fit [length]. |
| 25 String padRight(String text, int length) { | 29 String padRight(String text, int length) { |
| 26 if (text.length < length) return '${text}${' ' * (length - text.length)}'; | 30 if (text.length < length) return '${text}${' ' * (length - text.length)}'; |
| 27 return text; | 31 return text; |
| 28 } | 32 } |
| 29 | 33 |
| 30 const bool LOG = const bool.fromEnvironment('LOG', defaultValue: false); | 34 /// Pad [text] with spaces to the left to fit [length]. |
| 35 String padLeft(String text, int length) { |
| 36 if (text.length < length) return '${' ' * (length - text.length)}${text}'; |
| 37 return text; |
| 38 } |
| 31 | 39 |
| 32 void log(String text) { | 40 bool LOG = const bool.fromEnvironment('LOG', defaultValue: false); |
| 41 |
| 42 void log(Object text) { |
| 33 if (LOG) print(text); | 43 if (LOG) print(text); |
| 34 } | 44 } |
| 35 | 45 |
| 36 /// Reads the content of [uri] as text. | 46 /// Reads the content of [uri] as text. |
| 37 Future<String> readUriAsText(HttpClient client, Uri uri) async { | 47 Future<String> readUriAsText(HttpClient client, Uri uri) async { |
| 38 HttpClientRequest request = await client.getUrl(uri); | 48 HttpClientRequest request = await client.getUrl(uri); |
| 39 HttpClientResponse response = await request.close(); | 49 HttpClientResponse response = await request.close(); |
| 40 return await response.transform(UTF8.decoder).join(); | 50 return await response.transform(UTF8.decoder).join(); |
| 41 } | 51 } |
| 52 |
| 53 ArgParser createArgParser() { |
| 54 ArgParser argParser = new ArgParser(); |
| 55 argParser.addFlag('verbose', |
| 56 abbr: 'v', negatable: false, help: "Turn on logging output."); |
| 57 argParser.addOption('cache', |
| 58 help: "Use <dir> for caching test output.\n" |
| 59 "Defaults to 'temp/gardening-cache/'."); |
| 60 return argParser; |
| 61 } |
| 62 |
| 63 void processArgResults(ArgResults argResults) { |
| 64 if (argResults['verbose']) { |
| 65 LOG = true; |
| 66 } |
| 67 if (argResults['cache'] != null) { |
| 68 cache.base = Uri.base.resolve('${argResults['cache']}/'); |
| 69 } |
| 70 } |
| OLD | NEW |