Skip to content

Commit 7c3ed45

Browse files
committed
dub run <package> should search the registry on failure
1 parent edb53b0 commit 7c3ed45

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

changelog/dub-run.dd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
`dub run` will now automatically fetch a package if it's not found locally
2+
3+
Starting with this release, `dub run <mypackage>` makes sure that the package is available locally.
4+
This means that now a `dub fetch <mypackage>` is no longer required and all a user of a library needs to run your dub package is `dub run`:
5+
6+
$(CONSOLE
7+
> dub run gitcompatibledubpackage
8+
gitcompatibledubpackage wasn't found locally, but it's available online:
9+
---
10+
Description: Example of a DUB package also usable as git submodule. For DUB test suite.
11+
Version: 1.0.4
12+
---
13+
Do you want to fetch gitcompatibledubpackage? [Y/n]:
14+
)
15+
16+
An optional `--yes` (`-y`) flag is provided for non-interactive use:
17+
18+
$(CONSOLE
19+
> dub run --yes gitcompatibledubpackage
20+
gitcompatibledubpackage wasn't found locally, but it's available online:
21+
---
22+
Description: Example of a DUB package also usable as git submodule. For DUB test suite.
23+
Version: 1.0.4
24+
---
25+
Fetching gitcompatibledubpackage 1.0.4...
26+
Building package gitcompatibledubpackage in /home/seb/.dub/packages/gitcompatibledubpackage-1.0.4/gitcompatibledubpackage/
27+
Performing "debug" build using dmd for x86_64.
28+
gitcompatibledubpackage 1.0.4: building configuration "exe"...
29+
Linking...
30+
Running ../../.dub/packages/gitcompatibledubpackage-1.0.4/gitcompatibledubpackage/gitcompatibledubpackage
31+
Hello DUB
32+
)
33+
34+
If one wants to run a specific version of a package, it can be passed to `dub run` too:
35+
36+
$(CONSOLE
37+
> dub run gitcompatibledubpackage@1.0.3
38+
Fetching gitcompatibledubpackage 1.0.3...
39+
Building package gitcompatibledubpackage in /home/seb/.dub/packages/gitcompatibledubpackage-1.0.3/gitcompatibledubpackage/
40+
Performing "debug" build using dmd for x86_64.
41+
gitcompatibledubpackage 1.0.3: building configuration "exe"...
42+
Linking...
43+
Running ../../.dub/packages/gitcompatibledubpackage-1.0.3/gitcompatibledubpackage/gitcompatibledubpackage
44+
Hello DUB
45+
)

source/dub/commandline.d

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ class InitCommand : Command {
517517
free_args = free_args[1 .. $];
518518
}
519519

520-
string input(string caption, string default_value)
520+
static string input(string caption, string default_value)
521521
{
522522
writef("%s [%s]: ", caption, default_value);
523523
auto inp = readln();
@@ -829,6 +829,7 @@ class GenerateCommand : PackageBuildCommand {
829829
}
830830

831831
class BuildCommand : GenerateCommand {
832+
bool m_yes; // automatic yes to prompts;
832833
this()
833834
{
834835
this.name = "build";
@@ -848,12 +849,70 @@ class BuildCommand : GenerateCommand {
848849
args.getopt("f|force", &m_force, [
849850
"Forces a recompilation even if the target is up to date"
850851
]);
852+
args.getopt("y|yes", &m_yes, [
853+
`Automatic yes to prompts. Assume "yes" as answer to all prompts and run non-interactively.`
854+
]);
851855
super.prepare(args);
852856
m_generator = "build";
853857
}
854858

855859
override int execute(Dub dub, string[] free_args, string[] app_args)
856860
{
861+
// single package files don't need to be downloaded, they are on the disk.
862+
if (free_args.length < 1 || m_single)
863+
return super.execute(dub, free_args, app_args);
864+
865+
const package_parts = splitPackageName(free_args[0]);
866+
const package_name = package_parts.name;
867+
868+
static bool input(string caption, bool default_value = true) {
869+
writef("%s [%s]: ", caption, default_value ? "Y/n" : "y/N");
870+
auto inp = readln();
871+
string userInput = "y";
872+
if (inp.length > 1)
873+
userInput = inp[0 .. $ - 1].toLower;
874+
875+
switch (userInput) {
876+
case "no", "n", "0":
877+
return false;
878+
case "yes", "y", "1":
879+
default:
880+
return true;
881+
}
882+
}
883+
884+
Dependency dep;
885+
886+
if (package_parts.version_.length > 0) {
887+
// the user provided a version manually
888+
free_args[0] = package_name;
889+
dep = Dependency(package_parts.version_);
890+
} else {
891+
const pack = dub.packageManager.getFirstPackage(package_name);
892+
if (pack)
893+
return super.execute(dub, free_args, app_args);
894+
895+
// search for the package and filter versions for exact matches
896+
auto search = dub.searchPackages(package_name)
897+
.map!(tup => tup[1].find!(p => p.name == package_name))
898+
.filter!(ps => !ps.empty);
899+
if (search.empty)
900+
return 2;
901+
902+
const p = search.front.front;
903+
logInfo("%s wasn't found locally, but it's available online:", package_name);
904+
logInfo("---");
905+
logInfo("Description: %s", p.description);
906+
logInfo("Version: %s", p.version_);
907+
logInfo("---");
908+
909+
const answer = m_yes ? true : input("Do you want to fetch %s?".format(package_name));
910+
if (!answer)
911+
return 0;
912+
dep = Dependency(p.version_);
913+
}
914+
915+
dub.fetch(package_name, dep, dub.defaultPlacementLocation, FetchOptions.none);
857916
return super.execute(dub, free_args, app_args);
858917
}
859918
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
set -eu -o pipefail
4+
set -x
5+
$DUB remove --version="*" gitcompatibledubpackage || true
6+
7+
# check whether the interactive run mode works
8+
echo "y" | $DUB run gitcompatibledubpackage | grep "Hello DUB"
9+
$DUB remove gitcompatibledubpackage
10+
11+
! (echo "n" | $DUB run gitcompatibledubpackage | grep "Hello DUB")
12+
! $DUB remove gitcompatibledubpackage
13+
14+
# check -y
15+
$DUB run --yes gitcompatibledubpackage | grep "Hello DUB"
16+
$DUB remove gitcompatibledubpackage
17+
18+
# check --yes
19+
$DUB run -y gitcompatibledubpackage | grep "Hello DUB"
20+
$DUB remove gitcompatibledubpackage
21+
22+
# check supplying versions directly
23+
dub_log="$($DUB run gitcompatibledubpackage@1.0.3)"
24+
echo "$dub_log" | grep "Hello DUB"
25+
echo "$dub_log" | grep "Fetching.*1.0.3"
26+
$DUB remove gitcompatibledubpackage
27+
28+
# check supplying an invalid version
29+
(! $DUB run gitcompatibledubpackage@0.42.43) 2>&1 | \
30+
grep 'No package gitcompatibledubpackage was found matching the dependency 0[.]42[.]43'
31+
32+
! $DUB remove gitcompatibledubpackage

test/single-file-sdl-default-name

977 KB
Binary file not shown.

0 commit comments

Comments
 (0)