Skip to content

Commit 980fc94

Browse files
evanweible-wfjonasfj
authored andcommitted
Add support for global activating package from a custom pub URL (#2041)
* Add support for global activating package from custom pub URL * Rename CL option to `--hosted-url`. * dartfmt
1 parent 605cafb commit 980fc94

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

lib/src/command/global_activate.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class GlobalActivateCommand extends PubCommand {
3838
argParser.addFlag("overwrite",
3939
negatable: false,
4040
help: "Overwrite executables from other packages with the same name.");
41+
42+
argParser.addOption("hosted-url",
43+
abbr: "u",
44+
help:
45+
"A custom pub server URL for the package. Only applies when using the `hosted` source.");
4146
}
4247

4348
Future run() {
@@ -67,6 +72,7 @@ class GlobalActivateCommand extends PubCommand {
6772
}
6873

6974
var overwrite = argResults["overwrite"];
75+
var hostedUrl = argResults["hosted-url"];
7076
Iterable<String> args = argResults.rest;
7177

7278
readArg([String error]) {
@@ -106,7 +112,7 @@ class GlobalActivateCommand extends PubCommand {
106112

107113
validateNoExtraArgs();
108114
return globals.activateHosted(package, constraint, executables,
109-
features: features, overwriteBinStubs: overwrite);
115+
features: features, overwriteBinStubs: overwrite, url: hostedUrl);
110116

111117
case "path":
112118
if (features.isNotEmpty) {

lib/src/global_packages.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,18 @@ class GlobalPackages {
113113
/// if [overwriteBinStubs] is `true`, any binstubs that collide with
114114
/// existing binstubs in other packages will be overwritten by this one's.
115115
/// Otherwise, the previous ones will be preserved.
116+
///
117+
/// [url] is an optional custom pub server URL. If not null, the package to be
118+
/// activated will be fetched from this URL instead of the default pub URL.
116119
Future activateHosted(
117120
String name, VersionConstraint constraint, List<String> executables,
118-
{Map<String, FeatureDependency> features, bool overwriteBinStubs}) async {
121+
{Map<String, FeatureDependency> features,
122+
bool overwriteBinStubs,
123+
String url}) async {
119124
_describeActive(name);
120125
await _installInCache(
121126
cache.hosted.source
122-
.refFor(name)
127+
.refFor(name, url: url)
123128
.withConstraint(constraint)
124129
.withFeatures(features ?? const {}),
125130
executables,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
7+
import '../../test_pub.dart';
8+
9+
main() {
10+
test('activating a package from a custom pub server', () async {
11+
// The default pub server (i.e. pub.dartlang.org).
12+
await servePackages((builder) {
13+
builder.serve("baz", "1.0.0");
14+
});
15+
16+
// The custom pub server.
17+
final customServer = await PackageServer.start((builder) {
18+
Map<String, dynamic> hostedDep(String name, String constraint) => {
19+
"hosted": {
20+
"url": builder.serverUrl,
21+
"name": name,
22+
},
23+
"version": constraint,
24+
};
25+
builder.serve("foo", "1.0.0", deps: {"bar": hostedDep("bar", "any")});
26+
builder.serve("bar", "1.0.0", deps: {"baz": "any"});
27+
});
28+
29+
await runPub(
30+
args: ["global", "activate", "foo", "-u", customServer.url],
31+
output: allOf([
32+
contains("Downloading bar 1.0.0..."),
33+
contains("Downloading baz 1.0.0..."),
34+
contains("Downloading foo 1.0.0..."),
35+
contains("Activated foo 1.0.0")
36+
]));
37+
});
38+
}

test/package_server.dart

+13-5
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class PackageServer {
5858
/// package to serve.
5959
///
6060
/// This is preserved so that additional packages can be added.
61-
final _builder = PackageServerBuilder._();
61+
PackageServerBuilder _builder;
6262

63-
/// A future that will complete to the port used for the server.
63+
/// The port used for the server.
6464
int get port => _inner.port;
6565

66-
/// A future that will complete to the URL for the server.
66+
/// The URL for the server.
6767
String get url => 'http://localhost:$port';
6868

6969
/// Creates an HTTP server that replicates the structure of pub.dartlang.org.
@@ -81,7 +81,9 @@ class PackageServer {
8181
return server;
8282
}
8383

84-
PackageServer._(this._inner);
84+
PackageServer._(this._inner) {
85+
_builder = PackageServerBuilder._(this);
86+
}
8587

8688
/// Add to the current set of packages that are being served.
8789
void add(void callback(PackageServerBuilder builder)) {
@@ -135,7 +137,13 @@ class PackageServerBuilder {
135137
/// A map from package names to a list of concrete packages to serve.
136138
final _packages = <String, List<_ServedPackage>>{};
137139

138-
PackageServerBuilder._();
140+
/// The package server that this builder is associated with.
141+
final PackageServer _server;
142+
143+
/// The URL for the server that this builder is associated with.
144+
String get serverUrl => _server.url;
145+
146+
PackageServerBuilder._(this._server);
139147

140148
/// Specifies that a package named [name] with [version] should be served.
141149
///

0 commit comments

Comments
 (0)