Skip to content

Reland "Add flag controlling creation of .packages file." #3413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class AddCommand extends PubCommand {
help: 'Build executables in immediate dependencies.');
argParser.addOption('directory',
abbr: 'C', help: 'Run this in the directory <dir>.', valueHelp: 'dir');
argParser.addFlag('legacy-packages-file',
help: 'Generate the legacy ".packages" file', negatable: false);
}

@override
Expand Down Expand Up @@ -165,7 +167,8 @@ class AddCommand extends PubCommand {
.acquireDependencies(SolveType.get,
dryRun: true,
precompile: argResults['precompile'],
analytics: analytics);
analytics: analytics,
generateDotPackages: false);
} else {
/// Update the `pubspec.yaml` before calling [acquireDependencies] to
/// ensure that the modification timestamp on `pubspec.lock` and
Expand All @@ -180,6 +183,7 @@ class AddCommand extends PubCommand {
SolveType.get,
precompile: argResults['precompile'],
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);

if (argResults['example'] && entrypoint.example != null) {
Expand All @@ -188,6 +192,7 @@ class AddCommand extends PubCommand {
precompile: argResults['precompile'],
onlyReportSuccessOrFailure: true,
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/command/downgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class DowngradeCommand extends PubCommand {

argParser.addOption('directory',
abbr: 'C', help: 'Run this in the directory<dir>.', valueHelp: 'dir');
argParser.addFlag('legacy-packages-file',
help: 'Generate the legacy ".packages" file', negatable: false);
}

@override
Expand All @@ -57,6 +59,7 @@ class DowngradeCommand extends PubCommand {
unlock: argResults.rest,
dryRun: dryRun,
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);
var example = entrypoint.example;
if (argResults['example'] && example != null) {
Expand All @@ -66,6 +69,7 @@ class DowngradeCommand extends PubCommand {
dryRun: dryRun,
onlyReportSuccessOrFailure: true,
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);
}

Expand Down
17 changes: 12 additions & 5 deletions lib/src/command/get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class GetCommand extends PubCommand {

argParser.addFlag('packages-dir', hide: true);

argParser.addFlag('legacy-packages-file',
help: 'Generate the legacy ".packages" file', negatable: false);

argParser.addFlag(
'example',
help: 'Also run in `example/` (if it exists).',
Expand All @@ -53,16 +56,20 @@ class GetCommand extends PubCommand {
SolveType.get,
dryRun: argResults['dry-run'],
precompile: argResults['precompile'],
generateDotPackages: argResults['legacy-packages-file'],
analytics: analytics,
);

var example = entrypoint.example;
if (argResults['example'] && example != null) {
await example.acquireDependencies(SolveType.get,
dryRun: argResults['dry-run'],
precompile: argResults['precompile'],
onlyReportSuccessOrFailure: true,
analytics: analytics);
await example.acquireDependencies(
SolveType.get,
dryRun: argResults['dry-run'],
precompile: argResults['precompile'],
generateDotPackages: argResults['legacy-packages-file'],
analytics: analytics,
onlyReportSuccessOrFailure: true,
);
}
}
}
8 changes: 7 additions & 1 deletion lib/src/command/remove.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class RemoveCommand extends PubCommand {

argParser.addOption('directory',
abbr: 'C', help: 'Run this in the directory<dir>.', valueHelp: 'dir');

argParser.addFlag('legacy-packages-file',
help: 'Generate the legacy ".packages" file', negatable: false);
}

@override
Expand All @@ -69,7 +72,8 @@ class RemoveCommand extends PubCommand {
.acquireDependencies(SolveType.get,
precompile: argResults['precompile'],
dryRun: true,
analytics: null);
analytics: null,
generateDotPackages: false);
} else {
/// Update the pubspec.
_writeRemovalToPubspec(packages);
Expand All @@ -81,6 +85,7 @@ class RemoveCommand extends PubCommand {
SolveType.get,
precompile: argResults['precompile'],
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);

var example = entrypoint.example;
Expand All @@ -90,6 +95,7 @@ class RemoveCommand extends PubCommand {
precompile: argResults['precompile'],
onlyReportSuccessOrFailure: true,
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class UpgradeCommand extends PubCommand {

argParser.addFlag('packages-dir', hide: true);

argParser.addFlag('legacy-packages-file',
help: 'Generate the legacy ".packages" file', negatable: false);

argParser.addFlag(
'major-versions',
help: 'Upgrades packages to their latest resolvable versions, '
Expand All @@ -80,6 +83,8 @@ class UpgradeCommand extends PubCommand {

bool get _precompile => argResults['precompile'];

bool get _packagesFile => argResults['legacy-packages-file'];

bool get _upgradeNullSafety =>
argResults['nullsafety'] || argResults['null-safety'];

Expand Down Expand Up @@ -126,6 +131,7 @@ class UpgradeCommand extends PubCommand {
dryRun: _dryRun,
precompile: _precompile,
onlyReportSuccessOrFailure: onlySummary,
generateDotPackages: _packagesFile,
analytics: analytics,
);
_showOfflineWarning();
Expand Down Expand Up @@ -237,6 +243,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
dryRun: true,
precompile: _precompile,
analytics: null, // No analytics for dry-run
generateDotPackages: false,
);
} else {
if (changes.isNotEmpty) {
Expand All @@ -249,6 +256,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
SolveType.get,
precompile: _precompile,
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);
}

Expand Down Expand Up @@ -333,6 +341,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
dryRun: true,
precompile: _precompile,
analytics: null,
generateDotPackages: false,
);
} else {
if (changes.isNotEmpty) {
Expand All @@ -345,6 +354,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
SolveType.upgrade,
precompile: _precompile,
analytics: analytics,
generateDotPackages: argResults['legacy-packages-file'],
);
}

Expand Down
19 changes: 12 additions & 7 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,17 @@ class Entrypoint {
Entrypoint? _example;

/// Writes .packages and .dart_tool/package_config.json
Future<void> writePackagesFiles() async {
Future<void> writePackagesFiles({bool generateDotPackages = false}) async {
final entrypointName = isGlobal ? null : root.name;
writeTextFile(
packagesFile,
lockFile.packagesFile(cache,
entrypoint: entrypointName,
relativeFrom: isGlobal ? null : root.dir));
if (generateDotPackages) {
writeTextFile(
packagesFile,
lockFile.packagesFile(cache,
entrypoint: entrypointName,
relativeFrom: isGlobal ? null : root.dir));
} else {
tryDeleteEntry(packagesFile);
}
ensureDir(p.dirname(packageConfigFile));
writeTextFile(
packageConfigFile,
Expand Down Expand Up @@ -268,6 +272,7 @@ class Entrypoint {
Iterable<String>? unlock,
bool dryRun = false,
bool precompile = false,
required bool generateDotPackages,
required PubAnalytics? analytics,
bool onlyReportSuccessOrFailure = false,
}) async {
Expand Down Expand Up @@ -341,7 +346,7 @@ class Entrypoint {
/// have to reload and reparse all the pubspecs.
_packageGraph = PackageGraph.fromSolveResult(this, result);

await writePackagesFiles();
await writePackagesFiles(generateDotPackages: generateDotPackages);

try {
if (precompile) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ Future<DartExecutableWithPackageConfig> getExecutableForCommand(
() => entrypoint.acquireDependencies(
SolveType.get,
analytics: analytics,
generateDotPackages: false,
),
);
} on ApplicationException catch (e) {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ class GlobalPackages {
var entrypoint = Entrypoint(path, cache);

// Get the package's dependencies.
await entrypoint.acquireDependencies(SolveType.get, analytics: analytics);
await entrypoint.acquireDependencies(
SolveType.get,
analytics: analytics,
generateDotPackages: false,
);
var name = entrypoint.root.name;
_describeActive(name, cache);

Expand Down
18 changes: 11 additions & 7 deletions lib/src/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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:pub_semver/pub_semver.dart';

import 'language_version.dart';
Expand Down Expand Up @@ -160,8 +162,6 @@ class PackageConfigEntry {
/// Given as `<major>.<minor>` version, similar to the `// @dart = X.Y`
/// comment. This is derived from the lower-bound on the Dart SDK requirement
/// in the `pubspec.yaml` for the given package.
///
/// `null` if not given.
LanguageVersion? languageVersion;

/// Additional properties not in the specification for the
Expand All @@ -173,10 +173,8 @@ class PackageConfigEntry {
required this.rootUri,
this.packageUri,
this.languageVersion,
this.additionalProperties,
}) {
additionalProperties ??= {};
}
this.additionalProperties = const {},
});

/// Create [PackageConfigEntry] from JSON [data].
///
Expand Down Expand Up @@ -249,7 +247,13 @@ class PackageConfigEntry {
Map<String, Object?> toJson() => {
'name': name,
'rootUri': rootUri.toString(),
if (packageUri != null) 'packageUri': packageUri?.toString(),
if (packageUri != null) 'packageUri': packageUri.toString(),
if (languageVersion != null) 'languageVersion': '$languageVersion',
}..addAll(additionalProperties ?? {});

@override
String toString() {
// TODO: implement toString
return JsonEncoder.withIndent(' ').convert(toJson());
}
}
35 changes: 26 additions & 9 deletions test/add/common/add_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void main() {
await pubAdd(args: ['foo:1.2.3']);

await d.cacheDir({'foo': '1.2.3'}).validate();
await d.appPackagesFile({'foo': '1.2.3'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.3'),
]).validate();
await d.appDir({'foo': '1.2.3'}).validate();
});

Expand All @@ -62,8 +64,11 @@ void main() {

await d.cacheDir(
{'foo': '1.2.3', 'bar': '1.1.0', 'baz': '2.5.3'}).validate();
await d.appPackagesFile(
{'foo': '1.2.3', 'bar': '1.1.0', 'baz': '2.5.3'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.3'),
d.packageConfigEntry(name: 'bar', version: '1.1.0'),
d.packageConfigEntry(name: 'baz', version: '2.5.3'),
]).validate();
await d
.appDir({'foo': '1.2.3', 'bar': '1.1.0', 'baz': '2.5.3'}).validate();
});
Expand All @@ -90,7 +95,9 @@ void main() {
await pubAdd(args: ['foo:1.2.3']);

await d.cacheDir({'foo': '1.2.3'}).validate();
await d.appPackagesFile({'foo': '1.2.3'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.3'),
]).validate();

await d.dir(appPath, [
d.pubspec({
Expand Down Expand Up @@ -136,7 +143,9 @@ void main() {
await pubAdd(args: ['foo:1.2.3']);

await d.cacheDir({'foo': '1.2.3'}).validate();
await d.appPackagesFile({'foo': '1.2.3'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.3'),
]).validate();
await d.appDir({'foo': '1.2.3'}).validate();
});

Expand Down Expand Up @@ -217,7 +226,9 @@ environment:
'adding it to dependencies instead.'));

await d.cacheDir({'foo': '1.2.3'}).validate();
await d.appPackagesFile({'foo': '1.2.3'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.3'),
]).validate();

await d.dir(appPath, [
d.pubspec({
Expand All @@ -244,7 +255,9 @@ environment:
await pubAdd(args: ['foo']);

await d.cacheDir({'foo': '1.2.2'}).validate();
await d.appPackagesFile({'foo': '1.2.2'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.2'),
]).validate();
await d.dir(appPath, [
d.pubspec({
'name': 'myapp',
Expand Down Expand Up @@ -456,7 +469,9 @@ environment:

await pubAdd(args: ['--dev', 'foo:1.2.3']);

await d.appPackagesFile({'foo': '1.2.3'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.3'),
]).validate();

await d.dir(appPath, [
d.pubspec({
Expand Down Expand Up @@ -566,7 +581,9 @@ environment:
await pubAdd(args: ['foo', '--dev']);

await d.cacheDir({'foo': '1.2.2'}).validate();
await d.appPackagesFile({'foo': '1.2.2'}).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(name: 'foo', version: '1.2.2'),
]).validate();
await d.dir(appPath, [
d.pubspec({
'name': 'myapp',
Expand Down
Loading