Skip to content
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
16 changes: 8 additions & 8 deletions lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ Specify multiple sdk packages with descriptors.''');
precompile: !argResults.isDryRun && argResults.shouldPrecompile,
);

if (!argResults.isDryRun &&
argResults.example &&
entrypoint.example != null) {
await entrypoint.example!.acquireDependencies(
SolveType.get,
precompile: argResults.shouldPrecompile,
summaryOnly: true,
);
if (!argResults.isDryRun && argResults.example) {
for (final example in entrypoint.examples) {
await example.acquireDependencies(
SolveType.get,
precompile: argResults.shouldPrecompile,
summaryOnly: true,
);
}
}

if (isOffline) {
Expand Down
30 changes: 18 additions & 12 deletions lib/src/command/downgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,27 @@ class DowngradeCommand extends PubCommand {
unlock: argResults.rest,
dryRun: _dryRun,
);
final example = entrypoint.example;
if (argResults.flag('example') && example != null) {
await example.acquireDependencies(
SolveType.get,
unlock: argResults.rest,
dryRun: _dryRun,
summaryOnly: true,
);
if (_example) {
for (final example in entrypoint.examples) {
await example.acquireDependencies(
SolveType.get,
unlock: argResults.rest,
dryRun: _dryRun,
summaryOnly: true,
);
}
}

if (_tighten) {
if (_example && entrypoint.example != null) {
log.warning(
'Running `downgrade --tighten` only in `${entrypoint.workspaceRoot.dir}`. Run `$topLevelProgram pub upgrade --tighten --directory example/` separately.',
);
if (_example && entrypoint.examples.isNotEmpty) {
for (final example in entrypoint.examples) {
log.warning(
'Running `downgrade --tighten` only in '
'`${entrypoint.workspaceRoot.dir}`. '
'Run `$topLevelProgram pub downgrade --tighten '
'--directory ${example.workspaceRoot.presentationDir}` separately.',
);
}
}
final changes = entrypoint.tighten();
entrypoint.applyChanges(changes, _dryRun);
Expand Down
19 changes: 10 additions & 9 deletions lib/src/command/get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ class GetCommand extends PubCommand {
enforceLockfile: argResults.flag('enforce-lockfile'),
);

final example = entrypoint.example;
if ((argResults.flag('example')) && example != null) {
await example.acquireDependencies(
SolveType.get,
dryRun: argResults.flag('dry-run'),
precompile: argResults.flag('precompile'),
summaryOnly: true,
enforceLockfile: argResults.flag('enforce-lockfile'),
);
if (argResults.flag('example')) {
for (final example in entrypoint.examples) {
await example.acquireDependencies(
SolveType.get,
dryRun: argResults.flag('dry-run'),
precompile: argResults.flag('precompile'),
summaryOnly: true,
enforceLockfile: argResults.flag('enforce-lockfile'),
);
}
}
}
}
15 changes: 8 additions & 7 deletions lib/src/command/remove.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ To remove a dependency override of a package prefix the package name with
dryRun: isDryRun,
);

final example = entrypoint.example;
if (!isDryRun && argResults.flag('example') && example != null) {
await example.acquireDependencies(
SolveType.get,
precompile: argResults.flag('precompile'),
summaryOnly: true,
);
if (!isDryRun && argResults.flag('example')) {
for (final example in entrypoint.examples) {
await example.acquireDependencies(
SolveType.get,
precompile: argResults.flag('precompile'),
summaryOnly: true,
);
}
}
}

Expand Down
36 changes: 23 additions & 13 deletions lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,41 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
}

if (_upgradeMajorVersions) {
if (argResults.flag('example') && entrypoint.example != null) {
log.warning(
'Running `upgrade --major-versions` only in `${entrypoint.workspaceRoot.dir}`. Run `$topLevelProgram pub upgrade --major-versions --directory example/` separately.',
);
if (argResults.flag('example')) {
for (final example in entrypoint.examples) {
log.warning(
'Running `upgrade --major-versions` only in '
'`${entrypoint.workspaceRoot.dir}`. '
'Run `$topLevelProgram pub upgrade --major-versions '
'--directory ${example.workspaceRoot.presentationDir}` separately.',
);
}
}
await _runUpgradeMajorVersions();
} else {
await _runUpgrade(entrypoint);
if (_tighten) {
if (argResults.flag('example') && entrypoint.example != null) {
log.warning(
'Running `upgrade --tighten` only in `${entrypoint.workspaceRoot.dir}`. Run `$topLevelProgram pub upgrade --tighten --directory example/` separately.',
);
if (argResults.flag('example')) {
for (final example in entrypoint.examples) {
log.warning(
'Running `upgrade --tighten` only in '
'`${entrypoint.workspaceRoot.dir}`. '
'Run `$topLevelProgram pub upgrade --tighten '
'--directory ${example.workspaceRoot.presentationDir}` '
'separately.',
);
}
}
final changes = entrypoint.tighten(
packagesToUpgrade: await _packagesToUpgrade,
);
entrypoint.applyChanges(changes, _dryRun);
}
}
if (argResults.flag('example') && entrypoint.example != null) {
// Reload the entrypoint to ensure we pick up potential changes that has
// been made.
final exampleEntrypoint = Entrypoint(directory, cache).example!;
await _runUpgrade(exampleEntrypoint, onlySummary: true);
if (argResults.flag('example')) {
for (final example in entrypoint.examples) {
await _runUpgrade(example, onlySummary: true);
}
}
}

Expand Down
43 changes: 30 additions & 13 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ See $workspacesDocUrl for more information.''',
Entrypoint._(
this.workingDir,
this._lockFile,
this._example,
this._examples,
this._packageGraph,
this.cache,
this._packages,
Expand Down Expand Up @@ -349,10 +349,15 @@ See $workspacesDocUrl for more information.''',
final newWorkPackage = newWorkspaceRoot.transitiveWorkspace.firstWhere(
(package) => package.dir == workPackage.dir,
);
return Entrypoint._(workingDir, _lockFile, _example, _packageGraph, cache, (
root: newWorkspaceRoot,
work: newWorkPackage,
), isCachedGlobal);
return Entrypoint._(
workingDir,
_lockFile,
_examples,
_packageGraph,
cache,
(root: newWorkspaceRoot, work: newWorkPackage),
isCachedGlobal,
);
}

/// Creates an entrypoint at the same location, that will use [pubspec] for
Expand All @@ -378,18 +383,30 @@ See $workspacesDocUrl for more information.''',
}
}

/// Gets the [Entrypoint] package for the current working directory.
/// Gets [Entrypoint]s for examples of any workspace packages.
///
/// This will be null if the example folder doesn't have a `pubspec.yaml`.
Entrypoint? get example {
if (_example != null) return _example;
if (!fileExists(workspaceRoot.path('example', 'pubspec.yaml'))) {
return null;
/// Does not return examples that are already in the workspace
///
/// This will be empty if the example folder doesn't have a `pubspec.yaml`.
List<Entrypoint> get examples {
if (_examples case final List<Entrypoint> examples) return examples;
final directoriesInWorkspace = <String>{};
for (final package in workspaceRoot.transitiveWorkspace) {
directoriesInWorkspace.add(p.canonicalize(package.dir));
}
final result = <Entrypoint>[];
for (final package in workspaceRoot.transitiveWorkspace) {
final examplePath = package.path('example');

if (!directoriesInWorkspace.contains(p.canonicalize(examplePath)) &&
fileExists(p.join(examplePath, 'pubspec.yaml'))) {
result.add(Entrypoint(examplePath, cache));
}
}
return _example = Entrypoint(workspaceRoot.path('example'), cache);
return _examples = result;
}

Entrypoint? _example;
List<Entrypoint>? _examples;

/// Writes the .dart_tool/package_config.json file and workspace references to
/// it.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,4 @@ packages:
source: hosted
version: "2.2.2"
sdks:
dart: ">=3.8.0 <4.0.0"
dart: ">=3.9.0 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Changed 1 dependency!
Resolving dependencies in `./example`...
Downloading packages...
Got dependencies in `./example`.
[STDERR] Running `upgrade --major-versions` only in `.`. Run `dart pub upgrade --major-versions --directory example/` separately.
[STDERR] Running `upgrade --major-versions` only in `.`. Run `dart pub upgrade --major-versions --directory ./example` separately.

-------------------------------- END OF OUTPUT ---------------------------------

Expand Down
98 changes: 98 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,104 @@ b a${s}b$s
output: contains('+ bar'),
);
});

test('`--example` gets all (non-workspace) examples in workspace', () async {
final server = await servePackages();
server.serve('foo', '1.0.0');
server.serve('foo', '1.5.0');

await dir(appPath, [
libPubspec(
'myapp',
'1.2.3',
extras: {
'workspace': ['pkgs/a'],
},
sdk: '^3.5.0',
),
dir('pkgs', [
dir('a', [
libPubspec('a', '1.0.0', resolutionWorkspace: true),
dir('example', [libPubspec('example_b', '1.0.0')]),
]),
dir('b', [
libPubspec(
'b',
'1.0.0',
resolutionWorkspace: true,
extras: {
'workspace': ['example'],
},
),
dir('example', [libPubspec('example_b', '1.0.0')]),
]),
]),
]).create();

final s = p.separator;

await runPub(
args: ['get', '--example'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: allOf(
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
),
);

await runPub(
args: ['upgrade', '--example'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: allOf(
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
),
);

await runPub(
args: ['upgrade', '--example', '--tighten'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: allOf(
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
),
error: contains(
'Running `upgrade --tighten` only in `.`. Run `dart pub upgrade --tighten --directory .${s}pkgs/a${s}example` separately.',
),
);

await runPub(
args: ['upgrade', '--example', '--major-versions'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: allOf(
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.')),
),
error: contains(
'Running `upgrade --major-versions` only in `.`. Run `dart pub upgrade --major-versions --directory .${s}pkgs/a${s}example` separately.',
),
);

await runPub(
args: ['add', 'foo:^1.0.0', '--example'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: allOf(
contains('+ foo 1.5.0'),
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.')),
),
);

await runPub(
args: ['downgrade', '--example'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: allOf(
contains('< foo 1.0.0'),
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
),
);
});
}

final s = p.separator;
Loading