OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * A utility function for test and tools that compensates (at least for very |
| 7 * simple cases) for file-dependent programs being run from different |
| 8 * directories. The important cases are |
| 9 * - running in the directory that contains the test itself, i.e. |
| 10 * test/ or a sub-directory. |
| 11 * - running in root of this package, which is where the editor and bots will |
| 12 * run things by default |
| 13 */ |
| 14 library data_directory; |
| 15 |
| 16 import "dart:io"; |
| 17 import "package:path/path.dart" as path; |
| 18 |
| 19 String get dataDirectory { |
| 20 return path.join(intlDirectory, datesRelativeToIntl); |
| 21 } |
| 22 |
| 23 /// 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`. |
| 25 bool _isIntlRoot(String dir) { |
| 26 var file = new File(path.join(dir, 'pubspec.yaml')); |
| 27 if (!file.existsSync()) return false; |
| 28 return file.readAsStringSync().contains('name: intl\n'); |
| 29 } |
| 30 |
| 31 String get intlDirectory { |
| 32 var dir = path.fromUri(Platform.script); |
| 33 var root = path.rootPrefix(dir); |
| 34 |
| 35 while (dir != root) { |
| 36 if (_isIntlRoot(dir)) return dir; |
| 37 dir = path.dirname(dir); |
| 38 } |
| 39 throw new UnsupportedError( |
| 40 'Cannot find the root directory of the `intl` package.'); |
| 41 } |
| 42 |
| 43 String get datesRelativeToIntl => path.join('lib', 'src', 'data', 'dates'); |
OLD | NEW |