Skip to content

Commit

Permalink
Move packageConfigFile() to entrypoint.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm committed Jan 29, 2024
1 parent 11a94ef commit 2b43fd4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 70 deletions.
60 changes: 55 additions & 5 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import 'language_version.dart';
import 'lock_file.dart';
import 'log.dart' as log;
import 'package.dart';
import 'package_config.dart' show PackageConfig;
import 'package_config.dart';
import 'package_graph.dart';
import 'package_name.dart';
Expand Down Expand Up @@ -287,20 +286,71 @@ class Entrypoint {

/// Writes the .dart_tool/package_config.json file
Future<void> writePackageConfigFile() async {
final entrypointName = isGlobal ? null : root.name;
ensureDir(p.dirname(packageConfigPath));
writeTextFile(
packageConfigPath,
await lockFile.packageConfigFile(
await _packageConfigFile(
cache,
entrypoint: entrypointName,
entrypointSdkConstraint:
root.pubspec.sdkConstraints[sdk.identifier]?.effectiveConstraint,
relativeFrom: isGlobal ? null : rootDir,
),
);
}

/// Returns the contents of the `.dart_tool/package_config` file generated
/// from this entrypoint based on [lockFile].
///
/// If [isGlobal] no entry will be created for [root].
Future<String> _packageConfigFile(
SystemCache cache, {
VersionConstraint? entrypointSdkConstraint,
}) async {
final entries = <PackageConfigEntry>[];
for (final name in ordered(lockFile.packages.keys)) {
final id = lockFile.packages[name]!;
final rootPath =
cache.getDirectory(id, relativeFrom: isGlobal ? null : rootDir);
Uri rootUri;
if (p.isRelative(rootPath)) {
// Relative paths are relative to the root project, we want them
// relative to the `.dart_tool/package_config.json` file.
rootUri = p.toUri(p.join('..', rootPath));
} else {
rootUri = p.toUri(rootPath);
}
final pubspec = await cache.describe(id);
entries.add(
PackageConfigEntry(
name: name,
rootUri: rootUri,
packageUri: p.toUri('lib/'),
languageVersion: pubspec.languageVersion,
),
);
}

if (!isGlobal) {
entries.add(
PackageConfigEntry(
name: root.name,
rootUri: p.toUri('../'),
packageUri: p.toUri('lib/'),
languageVersion: root.pubspec.languageVersion,
),
);
}

final packageConfig = PackageConfig(
configVersion: 2,
packages: entries,
generated: DateTime.now(),
generator: 'pub',
generatorVersion: sdk.version,
);

return '${JsonEncoder.withIndent(' ').convert(packageConfig.toJson())}\n';
}

/// Gets all dependencies of the [root] package.
///
/// Performs version resolution according to [SolveType].
Expand Down
65 changes: 0 additions & 65 deletions lib/src/lock_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';

import 'package:collection/collection.dart' hide mapMap;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
Expand All @@ -12,11 +10,8 @@ import 'package:source_span/source_span.dart';
import 'package:yaml/yaml.dart';

import 'io.dart';
import 'language_version.dart';
import 'package_config.dart';
import 'package_name.dart';
import 'pubspec.dart';
import 'sdk.dart' show sdk;
import 'system_cache.dart';
import 'utils.dart';

Expand Down Expand Up @@ -353,66 +348,6 @@ class LockFile {
);
}

/// Returns the contents of the `.dart_tool/package_config` file generated
/// from this lockfile.
///
/// If [entrypoint] is passed, an accompanying [entrypointSdkConstraint]
/// should be given, these identify the current package in which this file is
/// written. Passing `null` as [entrypointSdkConstraint] is correct if the
/// current package has no SDK constraint.
Future<String> packageConfigFile(
SystemCache cache, {
String? entrypoint,
VersionConstraint? entrypointSdkConstraint,
String? relativeFrom,
}) async {
final entries = <PackageConfigEntry>[];
for (final name in ordered(packages.keys)) {
final id = packages[name]!;
final rootPath = cache.getDirectory(id, relativeFrom: relativeFrom);
Uri rootUri;
if (p.isRelative(rootPath)) {
// Relative paths are relative to the root project, we want them
// relative to the `.dart_tool/package_config.json` file.
rootUri = p.toUri(p.join('..', rootPath));
} else {
rootUri = p.toUri(rootPath);
}
final pubspec = await cache.describe(id);
entries.add(
PackageConfigEntry(
name: name,
rootUri: rootUri,
packageUri: p.toUri('lib/'),
languageVersion: pubspec.languageVersion,
),
);
}

if (entrypoint != null) {
entries.add(
PackageConfigEntry(
name: entrypoint,
rootUri: p.toUri('../'),
packageUri: p.toUri('lib/'),
languageVersion: LanguageVersion.fromSdkConstraint(
entrypointSdkConstraint,
),
),
);
}

final packageConfig = PackageConfig(
configVersion: 2,
packages: entries,
generated: DateTime.now(),
generator: 'pub',
generatorVersion: sdk.version,
);

return '${JsonEncoder.withIndent(' ').convert(packageConfig.toJson())}\n';
}

/// Returns the serialized YAML text of the lock file.
///
/// [packageDir] is the containing directory of the root package, used to
Expand Down

0 comments on commit 2b43fd4

Please sign in to comment.