Skip to content

Commit d247b3e

Browse files
authored
Handle custom hosted dependency (#288)
1 parent 7a35fc7 commit d247b3e

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

Diff for: packages/custom_lint/lib/src/workspace.dart

+16-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,22 @@ String _buildDependencyConstraint(
110110

111111
switch (sharedConstraint) {
112112
case HostedDependency():
113-
return ' ${sharedConstraint.getDisplayString()}';
113+
final hosted = sharedConstraint.hosted;
114+
if (hosted == null) {
115+
return ' ${sharedConstraint.getDisplayString()}';
116+
}
117+
final result = StringBuffer();
118+
result.writeln();
119+
result.writeln(' hosted:');
120+
if (hosted.declaredName != null) {
121+
result.writeln(' name: ${hosted.declaredName}');
122+
}
123+
if (hosted.url != null) {
124+
result.writeln(' url: ${hosted.url}');
125+
}
126+
result.write(' version: ${sharedConstraint.getDisplayString()}');
127+
return result.toString();
128+
114129
case PathDependency():
115130
// Use appropriate path separators across platforms
116131
final path = posix.prettyUri(sharedConstraint.path);

Diff for: packages/custom_lint/test/src/workspace_test.dart

+101-1
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ extension on Dependency {
4646
final that = this;
4747
if (that is HostedDependency) {
4848
if (that.hosted != null) {
49+
String? safeName;
50+
try {
51+
safeName = that.hosted!.name;
52+
53+
// `that.hosted!.name` could throw an error if `_nameOfPackage` is null in the getter.
54+
// We need to safely handle this scenario because we can't guarantee that the value is not null.
55+
// ignore: avoid_catching_errors
56+
} on Error catch (_) {}
57+
4958
return {
5059
'hosted': {
51-
'name': that.hosted!.name,
60+
if (safeName != null) 'name': safeName,
5261
'url': that.hosted!.url.toString(),
5362
},
5463
'version': that.version.toString(),
@@ -2174,6 +2183,97 @@ dependencies:
21742183
plugin1: ">=1.5.0 <2.0.0"
21752184
''');
21762185
});
2186+
group(
2187+
'Support hosted project with custom source',
2188+
() {
2189+
test(
2190+
'If a dependency comes from a custom hosted source, the generated pubspec.yaml should contain the hosted source',
2191+
() async {
2192+
final workingDir = await createSimpleWorkspace([
2193+
Pubspec(
2194+
'plugin1',
2195+
dependencies: {
2196+
'custom_lint_builder': HostedDependency(),
2197+
},
2198+
),
2199+
Pubspec(
2200+
'a',
2201+
devDependencies: {
2202+
'plugin1': HostedDependency(
2203+
hosted: HostedDetails(
2204+
'plugin1',
2205+
Uri.parse('https://custom.com'),
2206+
),
2207+
version: Version(1, 0, 0),
2208+
),
2209+
},
2210+
),
2211+
]);
2212+
2213+
final workspace = await fromContextRootsFromPaths(
2214+
['a'],
2215+
workingDirectory: workingDir,
2216+
);
2217+
2218+
expect(workspace.computePubspec(), '''
2219+
name: custom_lint_client
2220+
description: A client for custom_lint
2221+
version: 0.0.1
2222+
publish_to: 'none'
2223+
2224+
dependencies:
2225+
plugin1:
2226+
hosted:
2227+
name: plugin1
2228+
url: https://custom.com
2229+
version: "1.0.0"
2230+
''');
2231+
});
2232+
test(
2233+
'Hosted withouth name should still work',
2234+
() async {
2235+
final workingDir = await createSimpleWorkspace([
2236+
Pubspec(
2237+
'plugin1',
2238+
dependencies: {
2239+
'custom_lint_builder': HostedDependency(),
2240+
},
2241+
),
2242+
Pubspec(
2243+
'a',
2244+
devDependencies: {
2245+
'plugin1': HostedDependency(
2246+
hosted: HostedDetails(
2247+
null,
2248+
Uri.parse('https://custom.com'),
2249+
),
2250+
version: Version(1, 0, 0),
2251+
),
2252+
},
2253+
),
2254+
]);
2255+
2256+
final workspace = await fromContextRootsFromPaths(
2257+
['a'],
2258+
workingDirectory: workingDir,
2259+
);
2260+
2261+
expect(workspace.computePubspec(), '''
2262+
name: custom_lint_client
2263+
description: A client for custom_lint
2264+
version: 0.0.1
2265+
publish_to: 'none'
2266+
2267+
dependencies:
2268+
plugin1:
2269+
hosted:
2270+
url: https://custom.com
2271+
version: "1.0.0"
2272+
''');
2273+
},
2274+
);
2275+
},
2276+
);
21772277
});
21782278

21792279
group(CustomLintWorkspace.fromPaths, () {

0 commit comments

Comments
 (0)