Skip to content

Commit

Permalink
fetch: allow to fetch dependencies as well
Browse files Browse the repository at this point in the history
Dependency resolution and retrieval is copied from the
dub build command.
  • Loading branch information
Panke committed Mar 20, 2020
1 parent 186ebaf commit 55ab80a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
41 changes: 36 additions & 5 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,9 @@ class FetchCommand : FetchRemoveCommand {
override void prepare(scope CommandArgs args)
{
super.prepare(args);
args.getopt("shallow", &m_shallow, [

Check warning on line 1458 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1458

Added line #L1458 was not covered by tests
"do not fetch missing dependencies"
]);
}

override int execute(Dub dub, string[] free_args, string[] app_args)
Expand All @@ -1465,29 +1468,57 @@ class FetchCommand : FetchRemoveCommand {
auto location = dub.defaultPlacementLocation;

auto name = free_args[0];
Package pack = fetchMainTarget(dub, location, name);
if (pack && !m_shallow)

Check warning on line 1472 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1471-L1472

Added lines #L1471 - L1472 were not covered by tests
{
NativePath registryPath = (){

Check warning on line 1474 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1474

Added line #L1474 was not covered by tests
import dub.project : PlacementLocation;
final switch (location) {
case PlacementLocation.system:
return PackageManager.repositoryPath(dub.specialDirs.systemSettings, location);
case PlacementLocation.user:
return PackageManager.repositoryPath(dub.specialDirs.localRepository, location);
case PlacementLocation.local:
return PackageManager.repositoryPath(dub.rootPath, location);

Check warning on line 1482 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1476-L1482

Added lines #L1476 - L1482 were not covered by tests
}
}();
dub.packageManager.customCachePaths([registryPath]);
dub.loadPackage(pack.path);

Check warning on line 1486 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1485-L1486

Added lines #L1485 - L1486 were not covered by tests
// needs to be after loadPackage, bc otherwise it has no effect
dub.packageManager.disableDefaultSearchPaths(true);
dub.project.reinit();
dub.upgrade(UpgradeOptions.select);

Check warning on line 1490 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1488-L1490

Added lines #L1488 - L1490 were not covered by tests
}
return 0;

Check warning on line 1492 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1492

Added line #L1492 was not covered by tests
}

Package fetchMainTarget(Dub dub, PlacementLocation location, string name)
{
FetchOptions fetchOpts;
fetchOpts |= FetchOptions.forceBranchUpgrade;
if (m_version.length) dub.fetch(name, Dependency(m_version), location, fetchOpts);
if (m_version.length)
return dub.fetch(name, Dependency(m_version), location, fetchOpts);

Check warning on line 1500 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1499-L1500

Added lines #L1499 - L1500 were not covered by tests
else if (name.canFind("@", "=")) {
const parts = name.splitPackageName;
dub.fetch(parts.name, Dependency(parts.version_), location, fetchOpts);
return dub.fetch(parts.name, Dependency(parts.version_), location, fetchOpts);

Check warning on line 1503 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1503

Added line #L1503 was not covered by tests
} else {
try {
dub.fetch(name, Dependency(">=0.0.0"), location, fetchOpts);
Package pack = dub.fetch(name, Dependency(">=0.0.0"), location, fetchOpts);

Check warning on line 1506 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1506

Added line #L1506 was not covered by tests
logInfo(
"Please note that you need to use `dub run <pkgname>` " ~
"or add it to dependencies of your package to actually use/run it. " ~
"dub does not do actual installation of packages outside of its own ecosystem.");
return pack;

Check warning on line 1511 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1511

Added line #L1511 was not covered by tests
}
catch(Exception e){
logInfo("Getting a release version failed: %s", e.msg);
logInfo("Retry with ~master...");
dub.fetch(name, Dependency("~master"), location, fetchOpts);
return dub.fetch(name, Dependency("~master"), location, fetchOpts);

Check warning on line 1516 in source/dub/commandline.d

View check run for this annotation

Codecov / codecov/patch

source/dub/commandline.d#L1516

Added line #L1516 was not covered by tests
}
}
return 0;
}
private:
bool m_shallow;
}

class InstallCommand : FetchCommand {
Expand Down
2 changes: 2 additions & 0 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ class Dub {

@property inout(PackageManager) packageManager() inout { return m_packageManager; }

@property SpecialDirs specialDirs() const { return m_dirs; }

Check warning on line 319 in source/dub/dub.d

View check run for this annotation

Codecov / codecov/patch

source/dub/dub.d#L319

Added line #L319 was not covered by tests

@property inout(Project) project() inout { return m_project; }

/** Returns the default compiler binary to use for building D code.
Expand Down
21 changes: 18 additions & 3 deletions source/dub/packagemanager.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,28 @@ class PackageManager {
this(NativePath package_path, NativePath user_path, NativePath system_path, bool refresh_packages = true)
{
m_repositories = [
Repository(package_path ~ ".dub/packages/"),
Repository(user_path ~ "packages/"),
Repository(system_path ~ "packages/")];
Repository(repositoryPath(package_path, PlacementLocation.local)),
Repository(repositoryPath(user_path, PlacementLocation.user)),
Repository(repositoryPath(system_path, PlacementLocation.system)),
];

if (refresh_packages) refresh(true);
}

import dub.project : PlacementLocation;
/* Maps a placement location and path the corresponding repository path */
static NativePath repositoryPath(NativePath base, PlacementLocation location)
{
with (PlacementLocation) final switch (location)
{
case local:
return base ~ ".dub/packages";
case user:
case system:
return base ~ "packages/";
}
}

/** Gets/sets the list of paths to search for local packages.
*/
@property void searchPath(NativePath[] paths)
Expand Down

0 comments on commit 55ab80a

Please sign in to comment.