Skip to content

Commit 931f06c

Browse files
committed
PackMan: Provide an overload of storeFetchedPackage that doesn't expose Location
This way we keep the internals of PackageManager within the PackageManager, instead of having external classes be aware of the paths we use.
1 parent 30badaa commit 931f06c

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

source/dub/dub.d

+2-16
Original file line numberDiff line numberDiff line change
@@ -845,20 +845,6 @@ class Dub {
845845

846846
logDebug("Acquiring package zip file");
847847

848-
NativePath dstpath = this.m_packageManager.getPackagePath(location, basePackageName, ver.toString());
849-
if (!dstpath.existsFile())
850-
mkdirRecurse(dstpath.toNativeString());
851-
// For libraries leaking their import path
852-
dstpath = dstpath ~ basePackageName;
853-
854-
import std.datetime : seconds;
855-
auto lock = lockFile(dstpath.toNativeString() ~ ".lock", 30.seconds); // possibly wait for other dub instance
856-
if (dstpath.existsFile())
857-
{
858-
m_packageManager.refresh(false);
859-
return m_packageManager.getPackage(packageId, ver, location);
860-
}
861-
862848
// repeat download on corrupted zips, see #1336
863849
foreach_reverse (i; 0..3)
864850
{
@@ -867,10 +853,10 @@ class Dub {
867853
auto path = getTempFile(basePackageName, ".zip");
868854
supplier.fetchPackage(path, basePackageName, Dependency(range), (options & FetchOptions.usePrerelease) != 0); // Q: continue on fail?
869855
scope(exit) std.file.remove(path.toNativeString());
870-
logDiagnostic("Placing to %s...", dstpath.toNativeString());
856+
logDiagnostic("Placing to %s...", location.toString());
871857

872858
try {
873-
return m_packageManager.storeFetchedPackage(path, pinfo, dstpath);
859+
return m_packageManager.store(path, location, basePackageName, ver);
874860
} catch (ZipException e) {
875861
logInfo("Failed to extract zip archive for %s %s...", packageId, ver);
876862
// rethrow the exception at the end of the loop

source/dub/packagemanager.d

+54-10
Original file line numberDiff line numberDiff line change
@@ -583,27 +583,71 @@ class PackageManager {
583583
throw new Exception(format("No override exists for %s %s", package_, src));
584584
}
585585

586-
/// Extracts the package supplied as a path to it's zip file to the
587-
/// destination and sets a version field in the package description.
586+
deprecated("Use `store(NativePath source, PlacementLocation dest, string name, Version vers)`")
588587
Package storeFetchedPackage(NativePath zip_file_path, Json package_info, NativePath destination)
589588
{
590-
import std.range : walkLength;
589+
return this.store_(zip_file_path, destination, package_info["name"].get!string,
590+
Version(package_info["version"].get!string));
591+
}
591592

592-
auto package_name = package_info["name"].get!string;
593-
auto package_version = package_info["version"].get!string;
593+
/**
594+
* Store a zip file stored at `src` into a managed location `destination`
595+
*
596+
* This will extracts the package supplied as (as a zip file) to the
597+
* `destination` and sets a version field in the package description.
598+
* In the future, we should aim not to alter the package description,
599+
* but this is done for backward compatibility.
600+
*
601+
* Params:
602+
* src = The path to the zip file containing the package
603+
* dest = At which `PlacementLocation` the package should be stored
604+
* name = Name of the package being stored
605+
* vers = Version of the package
606+
*
607+
* Returns:
608+
* The `Package` after it has been loaded.
609+
*
610+
* Throws:
611+
* If the package cannot be loaded / the zip is corrupted / the package
612+
* already exists, etc...
613+
*/
614+
Package store(NativePath src, PlacementLocation dest, string name, Version vers)
615+
{
616+
NativePath dstpath = this.getPackagePath(dest, name, vers.toString());
617+
if (!dstpath.existsFile())
618+
mkdirRecurse(dstpath.toNativeString());
619+
// For libraries leaking their import path
620+
dstpath = dstpath ~ name;
621+
622+
// possibly wait for other dub instance
623+
import core.time : seconds;
624+
auto lock = lockFile(dstpath.toNativeString() ~ ".lock", 30.seconds);
625+
if (dstpath.existsFile()) {
626+
this.refresh(false);
627+
return this.getPackage(name, vers, dest);
628+
}
629+
return this.store_(src, dstpath, name, vers);
630+
}
631+
632+
/// Backward-compatibility for deprecated overload, simplify once `storeFetchedPatch`
633+
/// is removed
634+
private Package store_(NativePath src, NativePath destination, string name, Version vers)
635+
{
636+
import std.range : walkLength;
594637

595638
logDebug("Placing package '%s' version '%s' to location '%s' from file '%s'",
596-
package_name, package_version, destination.toNativeString(), zip_file_path.toNativeString());
639+
name, vers, destination.toNativeString(), src.toNativeString());
597640

598641
if( existsFile(destination) ){
599-
throw new Exception(format("%s (%s) needs to be removed from '%s' prior placement.", package_name, package_version, destination));
642+
throw new Exception(format("%s (%s) needs to be removed from '%s' prior placement.",
643+
name, vers, destination));
600644
}
601645

602646
// open zip file
603647
ZipArchive archive;
604648
{
605-
logDebug("Opening file %s", zip_file_path);
606-
auto f = openFile(zip_file_path, FileMode.read);
649+
logDebug("Opening file %s", src);
650+
auto f = openFile(src, FileMode.read);
607651
scope(exit) f.close();
608652
archive = new ZipArchive(f.readAll());
609653
}
@@ -669,7 +713,7 @@ class PackageManager {
669713
logDebug("%s file(s) copied.", to!string(countFiles));
670714

671715
// overwrite dub.json (this one includes a version field)
672-
auto pack = Package.load(destination, NativePath.init, null, package_info["version"].get!string);
716+
auto pack = Package.load(destination, NativePath.init, null, vers.toString());
673717

674718
if (pack.recipePath.head != defaultPackageFilename)
675719
// Storeinfo saved a default file, this could be different to the file from the zip.

0 commit comments

Comments
 (0)