@@ -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];
@@ -298,30 +317,51 @@ String get _d8executable {
298
317
299
318
Future <void > _createPackagesFile (
300
319
Module module, Uri root, Set <Module > transitiveDependencies) async {
301
- // We create a .packages file which defines the location of this module if
302
- // it is a package. The CFE requires that if a `package:` URI of a
303
- // dependency is used in an import, then we need that package entry in the
304
- // .packages file. However, after it checks that the definition exists, the
305
- // CFE will not actually use the resolved URI if a library for the import
306
- // URI is already found in one of the provided .dill files of the
307
- // dependencies. For that reason, and to ensure that a step only has access
308
- // to the files provided in a module, we generate a .packages with invalid
309
- // folders for other packages.
320
+ // We create both a .packages and package_config.json file which defines
321
+ // the location of this module if it is a package. The CFE requires that
322
+ // if a `package:` URI of a dependency is used in an import, then we need
323
+ // that package entry in the associated file. However, after it checks that
324
+ // the definition exists, the CFE will not actually use the resolved URI if
325
+ // a library for the import URI is already found in one of the provide
326
+ // .dill files of the dependencies. For that reason, and to ensure that
327
+ // a step only has access to the files provided in a module, we generate a
328
+ // config file with invalid folders for other packages.
310
329
// TODO(sigmund): follow up with the CFE to see if we can remove the need
311
- // for the .packages entry altogether if they won't need to read the
312
- // sources.
330
+ // for these dummy entries..
331
+ // TODO(joshualitt): Generate just the json file.
332
+ var packagesJson = [];
313
333
var packagesContents = StringBuffer ();
314
334
if (module.isPackage) {
315
335
packagesContents.write ('${module .name }:${module .packageBase }\n ' );
336
+ packagesJson.add (_packageConfigEntry (
337
+ module.name, Uri .parse ('../${module .packageBase }' )));
316
338
}
317
339
var unusedNum = 0 ;
318
340
for (var dependency in transitiveDependencies) {
319
341
if (dependency.isPackage) {
342
+ // rootUri should be ignored for dependent modules, so we pass in a
343
+ // bogus value.
344
+ var rootUri = Uri .parse ('unused$unusedNum ' );
320
345
unusedNum++ ;
321
- packagesContents.write ('${dependency .name }:unused$unusedNum \n ' );
346
+ var dependentPackage = _packageConfig[dependency.name];
347
+ var packageJson = dependentPackage == null
348
+ ? _packageConfigEntry (dependency.name, rootUri)
349
+ : _packageConfigEntry (dependentPackage.name, rootUri,
350
+ version: dependentPackage.languageVersion);
351
+ packagesJson.add (packageJson);
352
+ packagesContents.write ('${dependency .name }:$rootUri \n ' );
322
353
}
323
354
}
324
355
356
+ if (module.isPackage) {
357
+ await File .fromUri (root.resolve (packageConfigJsonPath))
358
+ .create (recursive: true );
359
+ await File .fromUri (root.resolve (packageConfigJsonPath)).writeAsString ('{'
360
+ ' "configVersion": ${_packageConfig .version },'
361
+ ' "packages": [ ${packagesJson .join (',' )} ]'
362
+ '}' );
363
+ }
364
+
325
365
await File .fromUri (root.resolve ('.packages' ))
326
366
.writeAsString ('$packagesContents ' );
327
367
}
0 commit comments