@@ -11,14 +11,22 @@ import 'package:modular_test/src/io_pipeline.dart';
11
11
import 'package:modular_test/src/pipeline.dart' ;
12
12
import 'package:modular_test/src/suite.dart' ;
13
13
import 'package:modular_test/src/runner.dart' ;
14
+ import 'package:package_config/package_config.dart' ;
14
15
16
+ String packageConfigJsonPath = '.dart_tool/package_config.json' ;
15
17
Uri sdkRoot = Platform .script.resolve ('../../../' );
18
+ Uri packageConfigUri = sdkRoot.resolve (packageConfigJsonPath);
16
19
Options _options;
17
20
String _dartdevcScript;
18
21
String _kernelWorkerScript;
19
22
23
+ // TODO(joshualitt): Figure out a way to support package configs in
24
+ // tests/modular.
25
+ PackageConfig _packageConfig;
26
+
20
27
void main (List <String > args) async {
21
28
_options = Options .parse (args);
29
+ _packageConfig = await loadPackageConfigUri (packageConfigUri);
22
30
await _resolveScripts ();
23
31
await runSuite (
24
32
sdkRoot.resolve ('tests/modular/' ),
@@ -35,6 +43,17 @@ const dillId = DataId('dill');
35
43
const jsId = DataId ('js' );
36
44
const txtId = DataId ('txt' );
37
45
46
+ String _packageConfigEntry (String name, Uri root,
47
+ {Uri packageRoot, LanguageVersion version}) {
48
+ var fields = [
49
+ '"name": "${name }"' ,
50
+ '"rootUri": "$root "' ,
51
+ if (packageRoot != null ) '"packageUri": "$packageRoot "' ,
52
+ if (version != null ) '"languageVersion": "$version "'
53
+ ];
54
+ return '{${fields .join (',' )}}' ;
55
+ }
56
+
38
57
class SourceToSummaryDillStep implements IOModularStep {
39
58
@override
40
59
List <DataId > get resultData => const [dillId];
@@ -301,30 +320,51 @@ String get _d8executable {
301
320
302
321
Future <void > _createPackagesFile (
303
322
Module module, Uri root, Set <Module > transitiveDependencies) async {
304
- // We create a .packages file which defines the location of this module if
305
- // it is a package. The CFE requires that if a `package:` URI of a
306
- // dependency is used in an import, then we need that package entry in the
307
- // .packages file. However, after it checks that the definition exists, the
308
- // CFE will not actually use the resolved URI if a library for the import
309
- // URI is already found in one of the provided .dill files of the
310
- // dependencies. For that reason, and to ensure that a step only has access
311
- // to the files provided in a module, we generate a .packages with invalid
312
- // folders for other packages.
323
+ // We create both a .packages and package_config.json file which defines
324
+ // the location of this module if it is a package. The CFE requires that
325
+ // if a `package:` URI of a dependency is used in an import, then we need
326
+ // that package entry in the associated file. However, after it checks that
327
+ // the definition exists, the CFE will not actually use the resolved URI if
328
+ // a library for the import URI is already found in one of the provide
329
+ // .dill files of the dependencies. For that reason, and to ensure that
330
+ // a step only has access to the files provided in a module, we generate a
331
+ // config file with invalid folders for other packages.
313
332
// TODO(sigmund): follow up with the CFE to see if we can remove the need
314
- // for the .packages entry altogether if they won't need to read the
315
- // sources.
333
+ // for these dummy entries..
334
+ // TODO(joshualitt): Generate just the json file.
335
+ var packagesJson = [];
316
336
var packagesContents = StringBuffer ();
317
337
if (module.isPackage) {
318
338
packagesContents.write ('${module .name }:${module .packageBase }\n ' );
339
+ packagesJson.add (_packageConfigEntry (
340
+ module.name, Uri .parse ('../${module .packageBase }' )));
319
341
}
320
342
var unusedNum = 0 ;
321
343
for (var dependency in transitiveDependencies) {
322
344
if (dependency.isPackage) {
345
+ // rootUri should be ignored for dependent modules, so we pass in a
346
+ // bogus value.
347
+ var rootUri = Uri .parse ('unused$unusedNum ' );
323
348
unusedNum++ ;
324
- packagesContents.write ('${dependency .name }:unused$unusedNum \n ' );
349
+ var dependentPackage = _packageConfig[dependency.name];
350
+ var packageJson = dependentPackage == null
351
+ ? _packageConfigEntry (dependency.name, rootUri)
352
+ : _packageConfigEntry (dependentPackage.name, rootUri,
353
+ version: dependentPackage.languageVersion);
354
+ packagesJson.add (packageJson);
355
+ packagesContents.write ('${dependency .name }:$rootUri \n ' );
325
356
}
326
357
}
327
358
359
+ if (module.isPackage) {
360
+ await File .fromUri (root.resolve (packageConfigJsonPath))
361
+ .create (recursive: true );
362
+ await File .fromUri (root.resolve (packageConfigJsonPath)).writeAsString ('{'
363
+ ' "configVersion": ${_packageConfig .version },'
364
+ ' "packages": [ ${packagesJson .join (',' )} ]'
365
+ '}' );
366
+ }
367
+
328
368
await File .fromUri (root.resolve ('.packages' ))
329
369
.writeAsString ('$packagesContents ' );
330
370
}
0 commit comments