| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /// A utility function for test and tools that compensates (at least for very |
| 6 * A utility function for test and tools that compensates (at least for very | 6 /// simple cases) for file-dependent programs being run from different |
| 7 * simple cases) for file-dependent programs being run from different | 7 /// directories. The important cases are |
| 8 * directories. The important cases are | 8 /// - running in the directory that contains the test itself, i.e. |
| 9 * - running in the directory that contains the test itself, i.e. | 9 /// test/ or a sub-directory. |
| 10 * test/ or a sub-directory. | 10 /// - running in root of this package, which is where the editor and bots will |
| 11 * - running in root of this package, which is where the editor and bots will | 11 /// run things by default |
| 12 * run things by default | |
| 13 */ | |
| 14 library data_directory; | 12 library data_directory; |
| 15 | 13 |
| 16 import "dart:io"; | 14 import "dart:io"; |
| 17 import "package:path/path.dart" as path; | 15 import "package:path/path.dart" as path; |
| 18 | 16 |
| 19 String get dataDirectory { | 17 String get dataDirectory { |
| 20 return path.join(intlDirectory, datesRelativeToIntl); | 18 return path.join(intlDirectory, datesRelativeToIntl); |
| 21 } | 19 } |
| 22 | 20 |
| 23 /// Returns whether [dir] is the root of the `intl` package. We validate that it | 21 /// Returns whether [dir] is the root of the `intl` package. We validate that it |
| 24 /// is by looking for a pubspec file with the entry `name: intl`. | 22 /// is by looking for a pubspec file with the entry `name: intl`. |
| 25 bool _isIntlRoot(String dir) { | 23 bool _isIntlRoot(String dir) { |
| 26 var file = new File(path.join(dir, 'pubspec.yaml')); | 24 var file = new File(path.join(dir, 'pubspec.yaml')); |
| 27 if (!file.existsSync()) return false; | 25 if (!file.existsSync()) return false; |
| 28 return file.readAsStringSync().contains('name: intl\n'); | 26 return file.readAsStringSync().contains('name: intl\n'); |
| 29 } | 27 } |
| 30 | 28 |
| 31 String get intlDirectory { | 29 String get intlDirectory { |
| 32 var dir = path.fromUri(Platform.script); | 30 var dir; |
| 31 if (Platform.script.scheme == 'file') { |
| 32 dir = path.fromUri(Platform.script); |
| 33 } else { |
| 34 dir = Directory.current.absolute.path; |
| 35 } |
| 33 var root = path.rootPrefix(dir); | 36 var root = path.rootPrefix(dir); |
| 34 | 37 |
| 35 while (dir != root) { | 38 while (dir != root) { |
| 36 if (_isIntlRoot(dir)) return dir; | 39 if (_isIntlRoot(dir)) return dir; |
| 37 dir = path.dirname(dir); | 40 dir = path.dirname(dir); |
| 38 } | 41 } |
| 39 throw new UnsupportedError( | 42 throw new UnsupportedError( |
| 40 'Cannot find the root directory of the `intl` package.'); | 43 'Cannot find the root directory of the `intl` package.'); |
| 41 } | 44 } |
| 42 | 45 |
| 43 String get datesRelativeToIntl => path.join('lib', 'src', 'data', 'dates'); | 46 String get datesRelativeToIntl => path.join('lib', 'src', 'data', 'dates'); |
| OLD | NEW |