| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016, 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 library fasta.environment_variable; | |
| 6 | |
| 7 import 'dart:async' show Future; | |
| 8 | |
| 9 import 'dart:io' show Directory, File, Platform; | |
| 10 | |
| 11 import 'errors.dart' show inputError; | |
| 12 | |
| 13 class EnvironmentVariable { | |
| 14 final String name; | |
| 15 | |
| 16 final String what; | |
| 17 | |
| 18 const EnvironmentVariable(this.name, this.what); | |
| 19 | |
| 20 Future<String> get value async { | |
| 21 String value = Platform.environment[name]; | |
| 22 if (value == null) return variableNotDefined(); | |
| 23 await validate(value); | |
| 24 return value; | |
| 25 } | |
| 26 | |
| 27 Future<Null> validate(String value) => new Future<Null>.value(); | |
| 28 | |
| 29 variableNotDefined() { | |
| 30 inputError( | |
| 31 null, null, "The environment variable '$name' isn't defined. $what"); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 class EnvironmentVariableFile extends EnvironmentVariable { | |
| 36 const EnvironmentVariableFile(String name, String what) : super(name, what); | |
| 37 | |
| 38 Future<Null> validate(String value) async { | |
| 39 if (!await new File(value).exists()) notFound(value); | |
| 40 return null; | |
| 41 } | |
| 42 | |
| 43 notFound(String value) { | |
| 44 inputError( | |
| 45 null, | |
| 46 null, | |
| 47 "The environment variable '$name' has the value " | |
| 48 "'$value', that isn't a file. $what"); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 class EnvironmentVariableDirectory extends EnvironmentVariable { | |
| 53 const EnvironmentVariableDirectory(String name, String what) | |
| 54 : super(name, what); | |
| 55 | |
| 56 Future<Null> validate(String value) async { | |
| 57 if (!await new Directory(value).exists()) notFound(value); | |
| 58 return null; | |
| 59 } | |
| 60 | |
| 61 notFound(String value) { | |
| 62 inputError( | |
| 63 null, | |
| 64 null, | |
| 65 "The environment variable '$name' has the value " | |
| 66 "'$value', that isn't a directory. $what"); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 Future<bool> fileExists(Uri base, String path) async { | |
| 71 return await new File.fromUri(base.resolve(path)).exists(); | |
| 72 } | |
| OLD | NEW |