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
9 changes: 9 additions & 0 deletions changelog/dub-fetch-shortcut.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
`dub fetch` now supports `<package>@<version>` as a shortcut

`dub fetch <package>@<version>` is a shortcut for
`dub fetch <package> --version=<version>`:

$(CONSOLE
> dub fetch gitcompatibledubpackage@1.0.4
Fetching gitcompatibledubpackage 1.0.4...
)
46 changes: 36 additions & 10 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int runDubCommandLine(string[] args)
args[1] = args[1].setExtension(".d");
}
}

// special single-file package shebang syntax
if (args.length >= 2 && args[1].endsWith(".d")) {
args = args[0] ~ ["run", "-q", "--temp-build", "--single", args[1], "--"] ~ args[2 ..$];
Expand Down Expand Up @@ -1146,7 +1146,7 @@ class AddCommand : Command {
this()
{
this.name = "add";
this.argumentsPattern = "<package>[=<version-spec>] [<packages...>]";
this.argumentsPattern = "<package>[@<version-spec>] [<packages...>]";
this.description = "Adds dependencies to the package file.";
this.helpText = [
"Adds <packages> as dependencies.",
Expand Down Expand Up @@ -1261,7 +1261,7 @@ class FetchCommand : FetchRemoveCommand {
this()
{
this.name = "fetch";
this.argumentsPattern = "<name>";
this.argumentsPattern = "<name>[@<version-spec>]";
this.description = "Manually retrieves and caches a package";
this.helpText = [
"Note: Use \"dub add <dependency>\" if you just want to use a certain package as a dependency, you don't have to explicitly fetch packages.",
Expand Down Expand Up @@ -1298,7 +1298,10 @@ class FetchCommand : FetchRemoveCommand {
FetchOptions fetchOpts;
fetchOpts |= FetchOptions.forceBranchUpgrade;
if (m_version.length) dub.fetch(name, Dependency(m_version), location, fetchOpts);
else {
else if (name.canFind("@", "=")) {
const parts = name.splitPackageName;
dub.fetch(parts.name, Dependency(parts.version_), location, fetchOpts);
} else {
try {
dub.fetch(name, Dependency(">=0.0.0"), location, fetchOpts);
logInfo(
Expand Down Expand Up @@ -2139,15 +2142,14 @@ private void warnRenamed(string prev, string curr)
private bool addDependency(Dub dub, ref PackageRecipe recipe, string depspec)
{
Dependency dep;
// split <package>=<version-specifier>
auto parts = depspec.findSplit("=");
auto depname = parts[0];
if (!parts[1].empty)
dep = Dependency(parts[2]);
const parts = splitPackageName(depspec);
const depname = parts.name;
if (parts.version_)
dep = Dependency(parts.version_);
else
{
try {
auto ver = dub.getLatestVersion(depname);
const ver = dub.getLatestVersion(depname);
dep = ver.isBranch ? Dependency(ver) : Dependency("~>" ~ ver.toString());
} catch (Exception e) {
logError("Could not find package '%s'.", depname);
Expand All @@ -2159,3 +2161,27 @@ private bool addDependency(Dub dub, ref PackageRecipe recipe, string depspec)
logInfo("Adding dependency %s %s", depname, dep.versionSpec);
return true;
}

/* Split <package>=<version-specifier> and <package>@<version-specifier>
into `name` and `version_`. */
private auto splitPackageName(string packageName)
{
struct PackageAndVersion
{
string name;
string version_;
}

// split <package>=<version-specifier>
auto parts = packageName.split("=");
if (parts.length == 1) {
// support splitting <package>@<version-specified> too
parts = packageName.split("@");
}

PackageAndVersion p;
p.name = parts[0];
if (parts.length > 1)
p.version_ = parts[1];
return p;
}
2 changes: 1 addition & 1 deletion test/interactive-remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if [ -d $HOME/.dub/packages/dub-1.9.0/dub ] || [ -d $HOME/.dub/packages/dub-1.10
die $LINENO 'Failed to remove all version of dub'
fi
$DUB fetch dub --version=1.9.0 && [ -d $HOME/.dub/packages/dub-1.9.0/dub ]
$DUB fetch dub --version=1.10.0 && [ -d $HOME/.dub/packages/dub-1.10.0/dub ]
$DUB fetch dub@1.10.0 && [ -d $HOME/.dub/packages/dub-1.10.0/dub ]
# is non-interactive with --version=
$DUB remove dub --version=\*
if [ -d $HOME/.dub/packages/dub-1.9.0/dub ] || [ -d $HOME/.dub/packages/dub-1.10.0/dub ]; then
Expand Down
2 changes: 1 addition & 1 deletion test/issue1574-addcommand.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ grep -q '"gitcompatibledubpackage"\s*:\s*"~>1\.0\.4"' dub.json
$DUB add gitcompatibledubpackage=1.0.2 non-existing-issue1574-pkg='~>9.8.7' --skip-registry=all
grep -q '"gitcompatibledubpackage"\s*:\s*"1\.0\.2"' dub.json
grep -q '"non-existing-issue1574-pkg"\s*:\s*"~>9\.8\.7"' dub.json
if $DUB add foo=1.2.3 gitcompatibledubpackage='~>a.b.c' --skip-registry=all; then
if $DUB add foo@1.2.3 gitcompatibledubpackage='~>a.b.c' --skip-registry=all; then
die $LINENO 'Adding non-semver spec should error'
fi
if grep -q '"foo"' dub.json; then
Expand Down