@@ -46,9 +46,18 @@ extension on Dependency {
46
46
final that = this ;
47
47
if (that is HostedDependency ) {
48
48
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
+
49
58
return {
50
59
'hosted' : {
51
- 'name' : that.hosted ! .name ,
60
+ if (safeName != null ) 'name' : safeName ,
52
61
'url' : that.hosted! .url.toString (),
53
62
},
54
63
'version' : that.version.toString (),
@@ -2174,6 +2183,97 @@ dependencies:
2174
2183
plugin1: ">=1.5.0 <2.0.0"
2175
2184
''' );
2176
2185
});
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
+ );
2177
2277
});
2178
2278
2179
2279
group (CustomLintWorkspace .fromPaths, () {
0 commit comments