Skip to content

Commit 101aa44

Browse files
committed
Add a --[no]-packages-dir flag.
This replaces the hidden --no-package-symlinks flag. Closes #1340 [email protected] Review URL: https://codereview.chromium.org//2250643003 .
1 parent 0244390 commit 101aa44

File tree

7 files changed

+72
-60
lines changed

7 files changed

+72
-60
lines changed

lib/src/command.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ abstract class PubCommand extends Command {
3737
Entrypoint get entrypoint {
3838
// Lazy load it.
3939
if (_entrypoint == null) {
40-
_entrypoint = new Entrypoint('.', cache,
41-
packageSymlinks: globalResults['package-symlinks']);
40+
_entrypoint = new Entrypoint('.', cache);
4241
}
4342
return _entrypoint;
4443
}

lib/src/command/downgrade.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ class DowngradeCommand extends PubCommand {
2424

2525
argParser.addFlag('dry-run', abbr: 'n', negatable: false,
2626
help: "Report what dependencies would change but don't change any.");
27+
28+
argParser.addFlag('packages-dir',
29+
negatable: true, defaultsTo: true,
30+
help: "Generate a packages/ directory when installing packages.");
2731
}
2832

2933
Future run() async {
3034
var dryRun = argResults['dry-run'];
3135
await entrypoint.acquireDependencies(SolveType.DOWNGRADE,
32-
useLatest: argResults.rest, dryRun: dryRun);
36+
useLatest: argResults.rest,
37+
dryRun: dryRun,
38+
packagesDir: argResults['packages-dir']);
39+
3340
if (isOffline) {
3441
log.warning("Warning: Downgrading when offline may not update you to "
3542
"the oldest versions of your dependencies.");

lib/src/command/get.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ class GetCommand extends PubCommand {
2525

2626
argParser.addFlag('precompile', defaultsTo: true,
2727
help: "Precompile executables and transformed dependencies.");
28+
29+
argParser.addFlag('packages-dir',
30+
negatable: true, defaultsTo: true,
31+
help: "Generate a packages/ directory when installing packages.");
2832
}
2933

3034
Future run() {
3135
return entrypoint.acquireDependencies(SolveType.GET,
3236
dryRun: argResults['dry-run'],
33-
precompile: argResults['precompile']);
37+
precompile: argResults['precompile'],
38+
packagesDir: argResults['packages-dir']);
3439
}
3540
}

lib/src/command/upgrade.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ class UpgradeCommand extends PubCommand {
2828

2929
argParser.addFlag('precompile', defaultsTo: true,
3030
help: "Precompile executables and transformed dependencies.");
31+
32+
argParser.addFlag('packages-dir',
33+
negatable: true, defaultsTo: true,
34+
help: "Generate a packages/ directory when installing packages.");
3135
}
3236

3337
Future run() async {
3438
await entrypoint.acquireDependencies(SolveType.UPGRADE,
3539
useLatest: argResults.rest,
3640
dryRun: argResults['dry-run'],
37-
precompile: argResults['precompile']);
41+
precompile: argResults['precompile'],
42+
packagesDir: argResults['packages-dir']);
3843

3944
if (isOffline) {
4045
log.warning("Warning: Upgrading when offline may not update you to the "

lib/src/command_runner.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ class PubCommandRunner extends CommandRunner {
6060
negatable: false, help: 'Execute commands with prejudice.');
6161
argParser.addFlag('sparkle', hide: !isAprilFools,
6262
negatable: false, help: 'A more sparkly experience.');
63-
argParser.addFlag('package-symlinks',
64-
negatable: true, defaultsTo: true, hide: true,
65-
help: "Generate packages/ directories when installing packages.");
6663

6764
addCommand(new BuildCommand());
6865
addCommand(new CacheCommand());

lib/src/entrypoint.dart

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ class Entrypoint {
7171
/// the network.
7272
final SystemCache cache;
7373

74-
/// Whether to create and symlink a "packages" directory containing links to
75-
/// the installed packages.
76-
final bool _packageSymlinks;
77-
7874
/// Whether this entrypoint is in memory only, as opposed to representing a
7975
/// real directory on disk.
8076
final bool _inMemory;
@@ -119,7 +115,7 @@ class Entrypoint {
119115
PackageGraph _packageGraph;
120116

121117
/// The path to the entrypoint's "packages" directory.
122-
String get packagesDir => root.path('packages');
118+
String get packagesPath => root.path('packages');
123119

124120
/// The path to the entrypoint's ".packages" file.
125121
String get packagesFile => root.path('.packages');
@@ -141,29 +137,21 @@ class Entrypoint {
141137
String get _snapshotPath => root.path('.pub', 'bin');
142138

143139
/// Loads the entrypoint from a package at [rootDir].
144-
///
145-
/// If [packageSymlinks] is `true`, this will create a "packages" directory
146-
/// with symlinks to the installed packages. This directory will be symlinked
147-
/// into any directory that might contain an entrypoint.
148-
Entrypoint(String rootDir, SystemCache cache, {bool packageSymlinks: true,
149-
this.isGlobal: false})
140+
Entrypoint(String rootDir, SystemCache cache, {this.isGlobal: false})
150141
: root = new Package.load(null, rootDir, cache.sources),
151142
cache = cache,
152-
_packageSymlinks = packageSymlinks,
153143
_inMemory = false;
154144

155145
/// Creates an entrypoint given package and lockfile objects.
156146
Entrypoint.inMemory(this.root, this._lockFile, this.cache,
157147
{this.isGlobal: false})
158-
: _packageSymlinks = false,
159-
_inMemory = true;
148+
: _inMemory = true;
160149

161150
/// Creates an entrypoint given a package and a [solveResult], from which the
162151
/// package graph and lockfile will be computed.
163152
Entrypoint.fromSolveResult(this.root, this.cache, SolveResult solveResult,
164153
{this.isGlobal: false})
165-
: _packageSymlinks = false,
166-
_inMemory = true {
154+
: _inMemory = true {
167155
_packageGraph = new PackageGraph.fromSolveResult(this, solveResult);
168156
_lockFile = _packageGraph.lockFile;
169157
}
@@ -186,9 +174,14 @@ class Entrypoint {
186174
/// If [precompile] is `true` (the default), this snapshots dependencies'
187175
/// executables and runs transformers on transformed dependencies.
188176
///
177+
/// If [packagesDir] is `true`, this will create "packages" directory with
178+
/// symlinks to the installed packages. This directory will be symlinked into
179+
/// any directory that might contain an entrypoint.
180+
///
189181
/// Updates [lockFile] and [packageRoot] accordingly.
190182
Future acquireDependencies(SolveType type, {List<String> useLatest,
191-
bool dryRun: false, bool precompile: true}) async {
183+
bool dryRun: false, bool precompile: true, bool packagesDir: false})
184+
async {
192185
var result = await resolveVersions(type, cache, root,
193186
lockFile: lockFile, useLatest: useLatest);
194187
if (!result.succeeded) throw result.error;
@@ -201,17 +194,18 @@ class Entrypoint {
201194
}
202195

203196
// Install the packages and maybe link them into the entrypoint.
204-
if (_packageSymlinks) {
205-
cleanDir(packagesDir);
197+
if (packagesDir) {
198+
cleanDir(packagesPath);
206199
} else {
207-
deleteEntry(packagesDir);
200+
deleteEntry(packagesPath);
208201
}
209202

210-
await Future.wait(result.packages.map(_get));
203+
await Future.wait(result.packages
204+
.map((id) => _get(id, packagesDir: packagesDir)));
211205
_saveLockFile(result);
212206

213-
if (_packageSymlinks) _linkSelf();
214-
_linkOrDeleteSecondaryPackageDirs();
207+
if (packagesDir) _linkSelf();
208+
_linkOrDeleteSecondaryPackageDirs(packagesDir: packagesDir);
215209

216210
result.summarizeChanges(type, dryRun: dryRun);
217211

@@ -449,18 +443,18 @@ class Entrypoint {
449443
/// This automatically downloads the package to the system-wide cache as well
450444
/// if it requires network access to retrieve (specifically, if the package's
451445
/// source is a [CachedSource]).
452-
Future _get(PackageId id) async {
446+
Future _get(PackageId id, {bool packagesDir: false}) async {
453447
if (id.isRoot) return;
454448

455449
var source = cache.source(id.source);
456-
if (!_packageSymlinks) {
450+
if (!packagesDir) {
457451
if (source is CachedSource) await source.downloadToSystemCache(id);
458452
return;
459453
}
460454

461-
var packageDir = p.join(packagesDir, id.name);
462-
if (entryExists(packageDir)) deleteEntry(packageDir);
463-
await source.get(id, packageDir);
455+
var packagePath = p.join(packagesPath, id.name);
456+
if (entryExists(packagePath)) deleteEntry(packagePath);
457+
await source.get(id, packagePath);
464458
}
465459

466460
/// Throws a [DataError] if the `.packages` file doesn't exist or if it's
@@ -654,42 +648,47 @@ class Entrypoint {
654648
/// Creates a self-referential symlink in the `packages` directory that allows
655649
/// a package to import its own files using `package:`.
656650
void _linkSelf() {
657-
var linkPath = p.join(packagesDir, root.name);
651+
var linkPath = p.join(packagesPath, root.name);
658652
// Create the symlink if it doesn't exist.
659653
if (entryExists(linkPath)) return;
660-
ensureDir(packagesDir);
654+
ensureDir(packagesPath);
661655
createPackageSymlink(root.name, root.dir, linkPath,
662656
isSelfLink: true, relative: true);
663657
}
664658

665-
/// If [packageSymlinks] is true, add "packages" directories to the whitelist
666-
/// of directories that may contain Dart entrypoints.
659+
/// If [packagesDir] is true, add "packages" directories to the whitelist of
660+
/// directories that may contain Dart entrypoints.
667661
///
668662
/// Otherwise, delete any "packages" directories in the whitelist of
669663
/// directories that may contain Dart entrypoints.
670-
void _linkOrDeleteSecondaryPackageDirs() {
664+
void _linkOrDeleteSecondaryPackageDirs({bool packagesDir: false}) {
671665
// Only the main "bin" directory gets a "packages" directory, not its
672666
// subdirectories.
673667
var binDir = root.path('bin');
674-
if (dirExists(binDir)) _linkOrDeleteSecondaryPackageDir(binDir);
668+
if (dirExists(binDir)) {
669+
_linkOrDeleteSecondaryPackageDir(binDir, packagesDir: packagesDir);
670+
}
675671

676672
// The others get "packages" directories in subdirectories too.
677673
for (var dir in ['benchmark', 'example', 'test', 'tool', 'web']) {
678-
_linkOrDeleteSecondaryPackageDirsRecursively(root.path(dir));
674+
_linkOrDeleteSecondaryPackageDirsRecursively(root.path(dir),
675+
packagesDir: packagesDir);
679676
}
680677
}
681678

682-
/// If [packageSymlinks] is true, creates a symlink to the "packages"
683-
/// directory in [dir] and all its subdirectories.
679+
/// If [packagesDir] is true, creates a symlink to the "packages" directory in
680+
/// [dir] and all its subdirectories.
684681
///
685682
/// Otherwise, deletes any "packages" directories in [dir] and all its
686683
/// subdirectories.
687-
void _linkOrDeleteSecondaryPackageDirsRecursively(String dir) {
684+
void _linkOrDeleteSecondaryPackageDirsRecursively(String dir,
685+
{bool packagesDir: false}) {
688686
if (!dirExists(dir)) return;
689-
_linkOrDeleteSecondaryPackageDir(dir);
690-
_listDirWithoutPackages(dir)
691-
.where(dirExists)
692-
.forEach(_linkOrDeleteSecondaryPackageDir);
687+
_linkOrDeleteSecondaryPackageDir(dir, packagesDir: packagesDir);
688+
for (var subdir in _listDirWithoutPackages(dir)) {
689+
if (!dirExists(subdir)) continue;
690+
_linkOrDeleteSecondaryPackageDir(subdir, packagesDir: packagesDir);
691+
}
693692
}
694693

695694
// TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed.
@@ -705,13 +704,13 @@ class Entrypoint {
705704
});
706705
}
707706

708-
/// If [packageSymlinks] is true, creates a symlink to the "packages"
709-
/// directory in [dir].
707+
/// If [packagesDir] is true, creates a symlink to the "packages" directory in
708+
/// [dir].
710709
///
711710
/// Otherwise, deletes a "packages" directories in [dir] if one exists.
712-
void _linkOrDeleteSecondaryPackageDir(String dir) {
711+
void _linkOrDeleteSecondaryPackageDir(String dir, {bool packagesDir: false}) {
713712
var symlink = p.join(dir, 'packages');
714713
if (entryExists(symlink)) deleteEntry(symlink);
715-
if (_packageSymlinks) createSymlink(packagesDir, symlink, relative: true);
714+
if (packagesDir) createSymlink(packagesPath, symlink, relative: true);
716715
}
717716
}

test/no_package_symlinks_test.dart renamed to test/no_packages_dir_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'test_pub.dart';
99

1010
main() {
1111
forBothPubGetAndUpgrade((command) {
12-
group("with --no-package-symlinks", () {
12+
group("with --no-packages-dir", () {
1313
integration("installs hosted dependencies to the cache", () {
1414
servePackages((builder) {
1515
builder.serve("foo", "1.0.0");
@@ -18,7 +18,7 @@ main() {
1818

1919
d.appDir({"foo": "any", "bar": "any"}).create();
2020

21-
pubCommand(command, args: ["--no-package-symlinks"]);
21+
pubCommand(command, args: ["--no-packages-dir"]);
2222

2323
d.nothing("$appPath/packages").validate();
2424

@@ -42,7 +42,7 @@ main() {
4242

4343
d.appDir({"foo": {"git": "../foo.git"}}).create();
4444

45-
pubCommand(command, args: ["--no-package-symlinks"]);
45+
pubCommand(command, args: ["--no-packages-dir"]);
4646

4747
d.nothing("$appPath/packages").validate();
4848

@@ -66,7 +66,7 @@ main() {
6666
})
6767
]).create();
6868

69-
pubCommand(command, args: ["--no-package-symlinks"]);
69+
pubCommand(command, args: ["--no-packages-dir"]);
7070

7171
d.nothing("$appPath/packages").validate();
7272
d.matcherFile("$appPath/pubspec.lock", contains("foo"));
@@ -81,7 +81,7 @@ main() {
8181
d.dir("web/subdir/packages")
8282
]).create();
8383

84-
pubCommand(command, args: ["--no-package-symlinks"]);
84+
pubCommand(command, args: ["--no-packages-dir"]);
8585

8686
d.dir(appPath, [
8787
d.nothing("packages"),
@@ -100,7 +100,7 @@ main() {
100100
d.dir("lib/packages")
101101
]).create();
102102

103-
pubCommand(command, args: ["--no-package-symlinks"]);
103+
pubCommand(command, args: ["--no-packages-dir"]);
104104

105105
d.dir(appPath, [
106106
d.nothing("packages"),

0 commit comments

Comments
 (0)