OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library pub.barback.transformer_id; | 5 library pub.barback.transformer_id; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
11 import 'package:source_maps/source_maps.dart'; | 11 import 'package:source_span/source_span.dart'; |
12 | 12 |
13 import '../io.dart'; | 13 import '../io.dart'; |
14 import '../utils.dart'; | 14 import '../utils.dart'; |
15 | 15 |
16 /// A list of the names of all built-in transformers that pub exposes. | 16 /// A list of the names of all built-in transformers that pub exposes. |
17 const _BUILT_IN_TRANSFORMERS = const ['\$dart2js']; | 17 const _BUILT_IN_TRANSFORMERS = const ['\$dart2js']; |
18 | 18 |
19 /// An identifier that indicates the library that contains a transformer. | 19 /// An identifier that indicates the library that contains a transformer. |
20 /// | 20 /// |
21 /// It's possible that the library identified by [this] defines multiple | 21 /// It's possible that the library identified by [this] defines multiple |
22 /// transformers. If so, they're all always loaded in the same phase. | 22 /// transformers. If so, they're all always loaded in the same phase. |
23 class TransformerId { | 23 class TransformerId { |
24 /// The package containing the library where the transformer is defined. | 24 /// The package containing the library where the transformer is defined. |
25 final String package; | 25 final String package; |
26 | 26 |
27 /// The `/`-separated path to the library that contains this transformer. | 27 /// The `/`-separated path to the library that contains this transformer. |
28 /// | 28 /// |
29 /// This is relative to the `lib/` directory in [package], and doesn't end in | 29 /// This is relative to the `lib/` directory in [package], and doesn't end in |
30 /// `.dart`. | 30 /// `.dart`. |
31 /// | 31 /// |
32 /// This can be null; if so, it indicates that the transformer(s) should be | 32 /// This can be null; if so, it indicates that the transformer(s) should be |
33 /// loaded from `lib/transformer.dart` if that exists, and `lib/$package.dart` | 33 /// loaded from `lib/transformer.dart` if that exists, and `lib/$package.dart` |
34 /// otherwise. | 34 /// otherwise. |
35 final String path; | 35 final String path; |
36 | 36 |
37 /// The source span from which this id was parsed. | 37 /// The source span from which this id was parsed. |
38 final Span span; | 38 final SourceSpan span; |
39 | 39 |
40 /// Whether this ID points to a built-in transformer exposed by pub. | 40 /// Whether this ID points to a built-in transformer exposed by pub. |
41 bool get isBuiltInTransformer => package.startsWith('\$'); | 41 bool get isBuiltInTransformer => package.startsWith('\$'); |
42 | 42 |
43 /// Parses a transformer identifier. | 43 /// Parses a transformer identifier. |
44 /// | 44 /// |
45 /// A transformer identifier is a string of the form "package_name" or | 45 /// A transformer identifier is a string of the form "package_name" or |
46 /// "package_name/path/to/library". It does not have a trailing extension. If | 46 /// "package_name/path/to/library". It does not have a trailing extension. If |
47 /// it just has a package name, it expands to lib/transformer.dart if that | 47 /// it just has a package name, it expands to lib/transformer.dart if that |
48 /// exists, or lib/${package}.dart otherwise. Otherwise, it expands to | 48 /// exists, or lib/${package}.dart otherwise. Otherwise, it expands to |
49 /// lib/${path}.dart. In either case it's located in the given package. | 49 /// lib/${path}.dart. In either case it's located in the given package. |
50 factory TransformerId.parse(String identifier, Span span) { | 50 factory TransformerId.parse(String identifier, SourceSpan span) { |
51 if (identifier.isEmpty) { | 51 if (identifier.isEmpty) { |
52 throw new FormatException('Invalid library identifier: "".'); | 52 throw new FormatException('Invalid library identifier: "".'); |
53 } | 53 } |
54 | 54 |
55 var parts = split1(identifier, "/"); | 55 var parts = split1(identifier, "/"); |
56 if (parts.length == 1) { | 56 if (parts.length == 1) { |
57 return new TransformerId(parts.single, null, span); | 57 return new TransformerId(parts.single, null, span); |
58 } | 58 } |
59 | 59 |
60 return new TransformerId(parts.first, parts.last, span); | 60 return new TransformerId(parts.first, parts.last, span); |
61 } | 61 } |
62 | 62 |
63 TransformerId(this.package, this.path, this.span) { | 63 TransformerId(this.package, this.path, this.span) { |
64 if (!package.startsWith('\$')) return; | 64 if (!package.startsWith('\$')) return; |
65 if (_BUILT_IN_TRANSFORMERS.contains(package)) return; | 65 if (_BUILT_IN_TRANSFORMERS.contains(package)) return; |
66 throw new SpanFormatException('Unsupported built-in transformer $package.', | 66 throw new SourceSpanFormatException( |
67 span); | 67 'Unsupported built-in transformer $package.', span); |
68 } | 68 } |
69 | 69 |
70 bool operator==(other) => | 70 bool operator==(other) => |
71 other is TransformerId && other.package == package && other.path == path; | 71 other is TransformerId && other.package == package && other.path == path; |
72 | 72 |
73 int get hashCode => package.hashCode ^ path.hashCode; | 73 int get hashCode => package.hashCode ^ path.hashCode; |
74 | 74 |
75 String toString() => path == null ? package : '$package/$path'; | 75 String toString() => path == null ? package : '$package/$path'; |
76 | 76 |
77 /// Returns the asset id for the library identified by this transformer id. | 77 /// Returns the asset id for the library identified by this transformer id. |
(...skipping 18 matching lines...) Expand all Loading... |
96 /// | 96 /// |
97 /// If `path` is null, this will determine which library to load. | 97 /// If `path` is null, this will determine which library to load. |
98 String getFullPath(String packageDir) { | 98 String getFullPath(String packageDir) { |
99 if (path != null) return p.join(packageDir, 'lib', p.fromUri('$path.dart')); | 99 if (path != null) return p.join(packageDir, 'lib', p.fromUri('$path.dart')); |
100 | 100 |
101 var transformerPath = p.join(packageDir, 'lib', 'transformer.dart'); | 101 var transformerPath = p.join(packageDir, 'lib', 'transformer.dart'); |
102 if (fileExists(transformerPath)) return transformerPath; | 102 if (fileExists(transformerPath)) return transformerPath; |
103 return p.join(packageDir, 'lib', '$package.dart'); | 103 return p.join(packageDir, 'lib', '$package.dart'); |
104 } | 104 } |
105 } | 105 } |
OLD | NEW |