Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm committed Oct 7, 2024
2 parents 9976f6f + a7fbdb2 commit 1bb1a0c
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ updates:
- package-ecosystem: "pub"
directory: "/"
schedule:
interval: "monthly"
interval: "weekly"

- package-ecosystem: github-actions
directory: /
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
matrix:
sdk: [dev]
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: ${{ matrix.sdk }}
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
sdk: [dev]
shard: [0, 1, 2, 3, 4, 5, 6]
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: ${{ matrix.sdk }}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ For example:

@override
String get docUrl => 'https://dart.dev/tools/pub/cmd/pub-add';
@override
bool get isOffline => argResults.flag('offline');

AddCommand() {
argParser.addFlag(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/command/deps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class DepsCommand extends PubCommand {
// before. Clients should opt to consume directDependencies and
// devDependencies separately instead.
'dependencies': (isRoot
? currentPackage.dependencies
: currentPackage.immediateDependencies)
? currentPackage.immediateDependencies
: currentPackage.dependencies)
.keys
.toList(),
'directDependencies': currentPackage.dependencies.keys.toList(),
Expand Down
43 changes: 34 additions & 9 deletions lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class UpgradeCommand extends PubCommand {
negatable: false,
);

argParser.addFlag(
'unlock-transitive',
help: 'Also upgrades the transitive dependencies '
'of the listed [dependencies]',
negatable: false,
);

argParser.addFlag(
'major-versions',
help: 'Upgrades packages to their latest resolvable versions, '
Expand Down Expand Up @@ -101,11 +108,27 @@ class UpgradeCommand extends PubCommand {

bool get _precompile => argResults.flag('precompile');

late final Future<List<String>> _packagesToUpgrade =
_computePackagesToUpgrade();

/// List of package names to upgrade, if empty then upgrade all packages.
///
/// This allows the user to specify list of names that they want the
/// upgrade command to affect.
List<String> get _packagesToUpgrade => argResults.rest;
Future<List<String>> _computePackagesToUpgrade() async {
if (argResults.flag('unlock-transitive')) {
final graph = await entrypoint.packageGraph;
return argResults.rest
.expand(
(package) =>
graph.transitiveDependencies(package).map((p) => p.name),
)
.toSet()
.toList();
} else {
return argResults.rest;
}
}

bool get _upgradeNullSafety =>
argResults.flag('nullsafety') || argResults.flag('null-safety');
Expand Down Expand Up @@ -142,7 +165,7 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
);
}
final changes =
entrypoint.tighten(packagesToUpgrade: _packagesToUpgrade);
entrypoint.tighten(packagesToUpgrade: await _packagesToUpgrade);
entrypoint.applyChanges(changes, _dryRun);
}
}
Expand All @@ -157,7 +180,7 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
Future<void> _runUpgrade(Entrypoint e, {bool onlySummary = false}) async {
await e.acquireDependencies(
SolveType.upgrade,
unlock: _packagesToUpgrade,
unlock: await _packagesToUpgrade,
dryRun: _dryRun,
precompile: _precompile,
summaryOnly: onlySummary,
Expand All @@ -171,7 +194,7 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
/// given.
///
/// This assumes that `--major-versions` was passed.
List<String> _directDependenciesToUpgrade() {
Future<List<String>> _directDependenciesToUpgrade() async {
assert(_upgradeMajorVersions);

final directDeps = {
Expand All @@ -180,12 +203,13 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
...package.devDependencies.keys,
],
}.toList();
final packagesToUpgrade = await _packagesToUpgrade;
final toUpgrade =
_packagesToUpgrade.isEmpty ? directDeps : _packagesToUpgrade;
packagesToUpgrade.isEmpty ? directDeps : packagesToUpgrade;

// Check that all package names in upgradeOnly are direct-dependencies
final notInDeps = toUpgrade.where((n) => !directDeps.contains(n));
if (toUpgrade.any(notInDeps.contains)) {
if (argResults.rest.any(notInDeps.contains)) {
usageException('''
Dependencies specified in `$topLevelProgram pub upgrade --major-versions <dependencies>` must
be direct 'dependencies' or 'dev_dependencies', following packages are not:
Expand All @@ -198,7 +222,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
}

Future<void> _runUpgradeMajorVersions() async {
final toUpgrade = _directDependenciesToUpgrade();
final toUpgrade = await _directDependenciesToUpgrade();
// Solve [resolvablePubspec] in-memory and consolidate the resolved
// versions of the packages into a map for quick searching.
final resolvedPackages = <String, PackageId>{};
Expand Down Expand Up @@ -267,7 +291,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
}),
);
changes = entrypoint.tighten(
packagesToUpgrade: _packagesToUpgrade,
packagesToUpgrade: await _packagesToUpgrade,
existingChanges: changes,
packageVersions: solveResult.packages,
);
Expand All @@ -279,7 +303,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
// But without a specific package we want to get as many non-major updates
// as possible (SolveType.upgrade).
final solveType =
_packagesToUpgrade.isEmpty ? SolveType.upgrade : SolveType.get;
(await _packagesToUpgrade).isEmpty ? SolveType.upgrade : SolveType.get;

entrypoint.applyChanges(changes, _dryRun);
await entrypoint.withUpdatedRootPubspecs({
Expand All @@ -290,6 +314,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
solveType,
dryRun: _dryRun,
precompile: !_dryRun && _precompile,
unlock: await _packagesToUpgrade,
);

// If any of the packages to upgrade are dependency overrides, then we
Expand Down
6 changes: 3 additions & 3 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ See $workspacesDocUrl for more information.''',
/// pubspec.yaml and all dependencies downloaded.
Future<void> acquireDependencies(
SolveType type, {
Iterable<String>? unlock,
Iterable<String> unlock = const [],
bool dryRun = false,
bool precompile = false,
bool summaryOnly = false,
Expand Down Expand Up @@ -547,7 +547,7 @@ Try running `$topLevelProgram pub get` to create `$lockFilePath`.''');
cache,
workspaceRoot,
lockFile: lockFile,
unlock: unlock ?? [],
unlock: unlock,
);
});
} on SolveFailure catch (e) {
Expand All @@ -557,7 +557,7 @@ Try running `$topLevelProgram pub get` to create `$lockFilePath`.''');
this,
type,
e.incompatibility,
unlock ?? [],
unlock,
cache,
),
);
Expand Down
16 changes: 14 additions & 2 deletions lib/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class Pubspec extends PubspecBase {
'`workspace` and `resolution` requires at least language version '
'${LanguageVersion.firstVersionWithWorkspaces}',
workspaceNode.span,
hint: '''
Consider updating the SDK constraint to:
environment:
sdk: '^${sdk.version}'
''',
);
}
if (workspaceNode == null || workspaceNode.value == null) return <String>[];
Expand Down Expand Up @@ -106,6 +112,12 @@ class Pubspec extends PubspecBase {
'`workspace` and `resolution` requires at least language version '
'${LanguageVersion.firstVersionWithWorkspaces}',
resolutionNode.span,
hint: '''
Consider updating the SDK constraint to:
environment:
sdk: '^${sdk.version}'
''',
);
}
return switch (resolutionNode?.value) {
Expand Down Expand Up @@ -726,8 +738,8 @@ T _wrapFormatException<T>(
}

/// Throws a [SourceSpanApplicationException] with the given message.
Never _error(String message, SourceSpan? span) {
throw SourceSpanApplicationException(message, span);
Never _error(String message, SourceSpan? span, {String? hint}) {
throw SourceSpanApplicationException(message, span, hint: hint);
}

enum _FileType {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
tar: ^2.0.0
typed_data: ^1.3.2
yaml: ^3.1.2
yaml_edit: ^2.1.1
yaml_edit: ^2.2.1

dev_dependencies:
checks: ^0.3.0
Expand Down
10 changes: 10 additions & 0 deletions test/add/common/add_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1148,4 +1148,14 @@ dependency_overrides:

await pubAdd(args: ['foo'], output: contains('+ foo 1.0.0'));
});

test('`--offline` works', () async {
final server = await servePackages();
server.serve('foo', '1.0.0');
await runPub(args: ['cache', 'add', 'foo', '--version', '1.0.0']);

await d.appDir().create();
server.serve('foo', '2.0.0');
await pubAdd(args: ['foo', '--offline']);
});
}
4 changes: 3 additions & 1 deletion test/deps_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ void main() {
"dependencies": [
"normal",
"overridden",
"from_path"
"from_path",
"unittest",
"override_only"
],
"directDependencies": [
"normal",
Expand Down
26 changes: 25 additions & 1 deletion test/pubspec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void main() {
String contents,
void Function(Pubspec) fn, {
String? expectedContains,
String? hintContains,
Description? containingDescription,
}) {
var expectation = const TypeMatcher<SourceSpanApplicationException>();
Expand All @@ -34,6 +35,13 @@ void main() {
contains(expectedContains),
);
}
if (hintContains != null) {
expectation = expectation.having(
(error) => error.hint,
'hint',
contains(hintContains),
);
}

final pubspec = Pubspec.parse(
contents,
Expand Down Expand Up @@ -384,6 +392,14 @@ environment:
workspace: ['a', 'b', 'c']
''',
(p) => p.workspace,
expectedContains: '`workspace` and `resolution` '
'requires at least language version 3.5',
hintContains: '''
Consider updating the SDK constraint to:
environment:
sdk: '^${Platform.version.split(' ').first}'
''',
);
// but no error if you don't look at it.
expect(
Expand All @@ -406,9 +422,17 @@ resolution: workspace
'''
environment:
sdk: ^1.2.3
resolution: local
resolution: workspace
''',
(p) => p.resolution,
expectedContains: '`workspace` and `resolution` '
'requires at least language version 3.5',
hintContains: '''
Consider updating the SDK constraint to:
environment:
sdk: '^${Platform.version.split(' ').first}'
''',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ $ pub deps --json
"version": "0.0.0",
"kind": "root",
"source": "root",
"dependencies": [],
"dependencies": [
"foo"
],
"directDependencies": [],
"devDependencies": [
"foo"
Expand Down
15 changes: 8 additions & 7 deletions test/testdata/goldens/help_test/pub upgrade --help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ $ pub upgrade --help
Upgrade the current package's dependencies to latest versions.

Usage: pub upgrade [dependencies...]
-h, --help Print this usage information.
--[no-]offline Use cached packages instead of accessing the network.
-n, --dry-run Report what dependencies would change but don't change any.
--[no-]precompile Precompile executables in immediate dependencies.
--tighten Updates lower bounds in pubspec.yaml to match the resolved version.
--major-versions Upgrades packages to their latest resolvable versions, and updates pubspec.yaml.
-C, --directory=<dir> Run this in the directory <dir>.
-h, --help Print this usage information.
--[no-]offline Use cached packages instead of accessing the network.
-n, --dry-run Report what dependencies would change but don't change any.
--[no-]precompile Precompile executables in immediate dependencies.
--tighten Updates lower bounds in pubspec.yaml to match the resolved version.
--unlock-transitive Also upgrades the transitive dependencies of the listed [dependencies]
--major-versions Upgrades packages to their latest resolvable versions, and updates pubspec.yaml.
-C, --directory=<dir> Run this in the directory <dir>.

Run "pub help" to see global options.
See https://dart.dev/tools/pub/cmd/pub-upgrade for detailed documentation.
Expand Down
Loading

0 comments on commit 1bb1a0c

Please sign in to comment.