Skip to content

Commit f7f1891

Browse files
authored
Handle all examples in workspace with flag --example (#4679)
1 parent ef62cdf commit f7f1891

File tree

8 files changed

+196
-63
lines changed

8 files changed

+196
-63
lines changed

lib/src/command/add.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,14 @@ Specify multiple sdk packages with descriptors.''');
290290
precompile: !argResults.isDryRun && argResults.shouldPrecompile,
291291
);
292292

293-
if (!argResults.isDryRun &&
294-
argResults.example &&
295-
entrypoint.example != null) {
296-
await entrypoint.example!.acquireDependencies(
297-
SolveType.get,
298-
precompile: argResults.shouldPrecompile,
299-
summaryOnly: true,
300-
);
293+
if (!argResults.isDryRun && argResults.example) {
294+
for (final example in entrypoint.examples) {
295+
await example.acquireDependencies(
296+
SolveType.get,
297+
precompile: argResults.shouldPrecompile,
298+
summaryOnly: true,
299+
);
300+
}
301301
}
302302

303303
if (isOffline) {

lib/src/command/downgrade.dart

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,27 @@ class DowngradeCommand extends PubCommand {
8080
unlock: argResults.rest,
8181
dryRun: _dryRun,
8282
);
83-
final example = entrypoint.example;
84-
if (argResults.flag('example') && example != null) {
85-
await example.acquireDependencies(
86-
SolveType.get,
87-
unlock: argResults.rest,
88-
dryRun: _dryRun,
89-
summaryOnly: true,
90-
);
83+
if (_example) {
84+
for (final example in entrypoint.examples) {
85+
await example.acquireDependencies(
86+
SolveType.get,
87+
unlock: argResults.rest,
88+
dryRun: _dryRun,
89+
summaryOnly: true,
90+
);
91+
}
9192
}
9293

9394
if (_tighten) {
94-
if (_example && entrypoint.example != null) {
95-
log.warning(
96-
'Running `downgrade --tighten` only in `${entrypoint.workspaceRoot.dir}`. Run `$topLevelProgram pub upgrade --tighten --directory example/` separately.',
97-
);
95+
if (_example && entrypoint.examples.isNotEmpty) {
96+
for (final example in entrypoint.examples) {
97+
log.warning(
98+
'Running `downgrade --tighten` only in '
99+
'`${entrypoint.workspaceRoot.dir}`. '
100+
'Run `$topLevelProgram pub downgrade --tighten '
101+
'--directory ${example.workspaceRoot.presentationDir}` separately.',
102+
);
103+
}
98104
}
99105
final changes = entrypoint.tighten();
100106
entrypoint.applyChanges(changes, _dryRun);

lib/src/command/get.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ class GetCommand extends PubCommand {
8383
enforceLockfile: argResults.flag('enforce-lockfile'),
8484
);
8585

86-
final example = entrypoint.example;
87-
if ((argResults.flag('example')) && example != null) {
88-
await example.acquireDependencies(
89-
SolveType.get,
90-
dryRun: argResults.flag('dry-run'),
91-
precompile: argResults.flag('precompile'),
92-
summaryOnly: true,
93-
enforceLockfile: argResults.flag('enforce-lockfile'),
94-
);
86+
if (argResults.flag('example')) {
87+
for (final example in entrypoint.examples) {
88+
await example.acquireDependencies(
89+
SolveType.get,
90+
dryRun: argResults.flag('dry-run'),
91+
precompile: argResults.flag('precompile'),
92+
summaryOnly: true,
93+
enforceLockfile: argResults.flag('enforce-lockfile'),
94+
);
95+
}
9596
}
9697
}
9798
}

lib/src/command/remove.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ To remove a dependency override of a package prefix the package name with
9797
dryRun: isDryRun,
9898
);
9999

100-
final example = entrypoint.example;
101-
if (!isDryRun && argResults.flag('example') && example != null) {
102-
await example.acquireDependencies(
103-
SolveType.get,
104-
precompile: argResults.flag('precompile'),
105-
summaryOnly: true,
106-
);
100+
if (!isDryRun && argResults.flag('example')) {
101+
for (final example in entrypoint.examples) {
102+
await example.acquireDependencies(
103+
SolveType.get,
104+
precompile: argResults.flag('precompile'),
105+
summaryOnly: true,
106+
);
107+
}
107108
}
108109
}
109110

lib/src/command/upgrade.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,41 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
154154
}
155155

156156
if (_upgradeMajorVersions) {
157-
if (argResults.flag('example') && entrypoint.example != null) {
158-
log.warning(
159-
'Running `upgrade --major-versions` only in `${entrypoint.workspaceRoot.dir}`. Run `$topLevelProgram pub upgrade --major-versions --directory example/` separately.',
160-
);
157+
if (argResults.flag('example')) {
158+
for (final example in entrypoint.examples) {
159+
log.warning(
160+
'Running `upgrade --major-versions` only in '
161+
'`${entrypoint.workspaceRoot.dir}`. '
162+
'Run `$topLevelProgram pub upgrade --major-versions '
163+
'--directory ${example.workspaceRoot.presentationDir}` separately.',
164+
);
165+
}
161166
}
162167
await _runUpgradeMajorVersions();
163168
} else {
164169
await _runUpgrade(entrypoint);
165170
if (_tighten) {
166-
if (argResults.flag('example') && entrypoint.example != null) {
167-
log.warning(
168-
'Running `upgrade --tighten` only in `${entrypoint.workspaceRoot.dir}`. Run `$topLevelProgram pub upgrade --tighten --directory example/` separately.',
169-
);
171+
if (argResults.flag('example')) {
172+
for (final example in entrypoint.examples) {
173+
log.warning(
174+
'Running `upgrade --tighten` only in '
175+
'`${entrypoint.workspaceRoot.dir}`. '
176+
'Run `$topLevelProgram pub upgrade --tighten '
177+
'--directory ${example.workspaceRoot.presentationDir}` '
178+
'separately.',
179+
);
180+
}
170181
}
171182
final changes = entrypoint.tighten(
172183
packagesToUpgrade: await _packagesToUpgrade,
173184
);
174185
entrypoint.applyChanges(changes, _dryRun);
175186
}
176187
}
177-
if (argResults.flag('example') && entrypoint.example != null) {
178-
// Reload the entrypoint to ensure we pick up potential changes that has
179-
// been made.
180-
final exampleEntrypoint = Entrypoint(directory, cache).example!;
181-
await _runUpgrade(exampleEntrypoint, onlySummary: true);
188+
if (argResults.flag('example')) {
189+
for (final example in entrypoint.examples) {
190+
await _runUpgrade(example, onlySummary: true);
191+
}
182192
}
183193
}
184194

lib/src/entrypoint.dart

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ See $workspacesDocUrl for more information.''',
321321
Entrypoint._(
322322
this.workingDir,
323323
this._lockFile,
324-
this._example,
324+
this._examples,
325325
this._packageGraph,
326326
this.cache,
327327
this._packages,
@@ -349,10 +349,15 @@ See $workspacesDocUrl for more information.''',
349349
final newWorkPackage = newWorkspaceRoot.transitiveWorkspace.firstWhere(
350350
(package) => package.dir == workPackage.dir,
351351
);
352-
return Entrypoint._(workingDir, _lockFile, _example, _packageGraph, cache, (
353-
root: newWorkspaceRoot,
354-
work: newWorkPackage,
355-
), isCachedGlobal);
352+
return Entrypoint._(
353+
workingDir,
354+
_lockFile,
355+
_examples,
356+
_packageGraph,
357+
cache,
358+
(root: newWorkspaceRoot, work: newWorkPackage),
359+
isCachedGlobal,
360+
);
356361
}
357362

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

381-
/// Gets the [Entrypoint] package for the current working directory.
386+
/// Gets [Entrypoint]s for examples of any workspace packages.
382387
///
383-
/// This will be null if the example folder doesn't have a `pubspec.yaml`.
384-
Entrypoint? get example {
385-
if (_example != null) return _example;
386-
if (!fileExists(workspaceRoot.path('example', 'pubspec.yaml'))) {
387-
return null;
388+
/// Does not return examples that are already in the workspace
389+
///
390+
/// This will be empty if the example folder doesn't have a `pubspec.yaml`.
391+
List<Entrypoint> get examples {
392+
if (_examples case final List<Entrypoint> examples) return examples;
393+
final directoriesInWorkspace = <String>{};
394+
for (final package in workspaceRoot.transitiveWorkspace) {
395+
directoriesInWorkspace.add(p.canonicalize(package.dir));
396+
}
397+
final result = <Entrypoint>[];
398+
for (final package in workspaceRoot.transitiveWorkspace) {
399+
final examplePath = package.path('example');
400+
401+
if (!directoriesInWorkspace.contains(p.canonicalize(examplePath)) &&
402+
fileExists(p.join(examplePath, 'pubspec.yaml'))) {
403+
result.add(Entrypoint(examplePath, cache));
404+
}
388405
}
389-
return _example = Entrypoint(workspaceRoot.path('example'), cache);
406+
return _examples = result;
390407
}
391408

392-
Entrypoint? _example;
409+
List<Entrypoint>? _examples;
393410

394411
/// Writes the .dart_tool/package_config.json file and workspace references to
395412
/// it.

test/testdata/goldens/upgrade/example_warns_about_major_versions_test/pub upgrade --major-versions does not update major versions in example~.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Changed 1 dependency!
1212
Resolving dependencies in `./example`...
1313
Downloading packages...
1414
Got dependencies in `./example`.
15-
[STDERR] Running `upgrade --major-versions` only in `.`. Run `dart pub upgrade --major-versions --directory example/` separately.
15+
[STDERR] Running `upgrade --major-versions` only in `.`. Run `dart pub upgrade --major-versions --directory ./example` separately.
1616

1717
-------------------------------- END OF OUTPUT ---------------------------------
1818

test/workspace_test.dart

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,104 @@ b a${s}b$s
17251725
output: contains('+ bar'),
17261726
);
17271727
});
1728+
1729+
test('`--example` gets all (non-workspace) examples in workspace', () async {
1730+
final server = await servePackages();
1731+
server.serve('foo', '1.0.0');
1732+
server.serve('foo', '1.5.0');
1733+
1734+
await dir(appPath, [
1735+
libPubspec(
1736+
'myapp',
1737+
'1.2.3',
1738+
extras: {
1739+
'workspace': ['pkgs/a'],
1740+
},
1741+
sdk: '^3.5.0',
1742+
),
1743+
dir('pkgs', [
1744+
dir('a', [
1745+
libPubspec('a', '1.0.0', resolutionWorkspace: true),
1746+
dir('example', [libPubspec('example_b', '1.0.0')]),
1747+
]),
1748+
dir('b', [
1749+
libPubspec(
1750+
'b',
1751+
'1.0.0',
1752+
resolutionWorkspace: true,
1753+
extras: {
1754+
'workspace': ['example'],
1755+
},
1756+
),
1757+
dir('example', [libPubspec('example_b', '1.0.0')]),
1758+
]),
1759+
]),
1760+
]).create();
1761+
1762+
final s = p.separator;
1763+
1764+
await runPub(
1765+
args: ['get', '--example'],
1766+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
1767+
output: allOf(
1768+
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
1769+
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
1770+
),
1771+
);
1772+
1773+
await runPub(
1774+
args: ['upgrade', '--example'],
1775+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
1776+
output: allOf(
1777+
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
1778+
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
1779+
),
1780+
);
1781+
1782+
await runPub(
1783+
args: ['upgrade', '--example', '--tighten'],
1784+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
1785+
output: allOf(
1786+
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
1787+
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
1788+
),
1789+
error: contains(
1790+
'Running `upgrade --tighten` only in `.`. Run `dart pub upgrade --tighten --directory .${s}pkgs/a${s}example` separately.',
1791+
),
1792+
);
1793+
1794+
await runPub(
1795+
args: ['upgrade', '--example', '--major-versions'],
1796+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
1797+
output: allOf(
1798+
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
1799+
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.')),
1800+
),
1801+
error: contains(
1802+
'Running `upgrade --major-versions` only in `.`. Run `dart pub upgrade --major-versions --directory .${s}pkgs/a${s}example` separately.',
1803+
),
1804+
);
1805+
1806+
await runPub(
1807+
args: ['add', 'foo:^1.0.0', '--example'],
1808+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
1809+
output: allOf(
1810+
contains('+ foo 1.5.0'),
1811+
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
1812+
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.')),
1813+
),
1814+
);
1815+
1816+
await runPub(
1817+
args: ['downgrade', '--example'],
1818+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
1819+
output: allOf(
1820+
contains('< foo 1.0.0'),
1821+
contains('Got dependencies in `.${s}pkgs/a${s}example`.'),
1822+
isNot(contains('Got dependencies in `.${s}pkgs/b${s}example`.`.')),
1823+
),
1824+
);
1825+
});
17281826
}
17291827

17301828
final s = p.separator;

0 commit comments

Comments
 (0)