internal bool GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, bool minInclusive = true, bool maxInclusive = true, bool terminateFirstFound = false)
{
try
{
bool found = false;
var paths = InstalledPath.Where(Directory.Exists).ToArray();
// if directory does not exist then just return false
if (!paths.Any())
{
return false;
}
// look in the destination directory for directories that contain *.nupkg & .nuspec files.
var subdirs = paths.SelectMany(Directory.EnumerateDirectories);
foreach (var subdir in subdirs)
{
//reset the flag when we begin a folder
var isDup = false;
var nupkgs = Directory.EnumerateFiles(subdir, "*.nupkg", SearchOption.TopDirectoryOnly).Union(
Directory.EnumerateFiles(subdir, "*.nuspec", SearchOption.TopDirectoryOnly));
foreach (var pkgFile in nupkgs)
{
if (isDup)
{
continue;
}
//As the package name has to be in the installed file path, check if it is true
if (!String.IsNullOrWhiteSpace(name) && !NuGetProvider.IsNameMatch(name, pkgFile))
{
//not the right package
continue;
}
//unpack the package
var existFileName = Path.GetFileNameWithoutExtension(pkgFile);
var pkgItem = GetPackageByFilePath(pkgFile, existFileName);
if (pkgItem != null && pkgItem.IsInstalled)
{
//A user does not provide any package name in the commandeline, return them all
if (string.IsNullOrWhiteSpace(name))
{
isDup = true;
if (!YieldPackage(pkgItem, existFileName))
{
return found;
}
}
else
{
//check if the version matches
if (!string.IsNullOrWhiteSpace(requiredVersion))
{
if (pkgItem.Package.Version != new SemanticVersion(requiredVersion))
{
continue;
}
}
else
{
// if min and max version not matched
if (!MinAndMaxVersionMatched(pkgItem.Package.Version, minimumVersion, maximumVersion, minInclusive, maxInclusive))
{
continue;
}
if (terminateFirstFound)
{
// if we are searching for a name and terminatefirstfound is set to true, then returns here
return true;
}
}
//found the match
isDup = true;
YieldPackage(pkgItem, existFileName);
found = true;
} //end of else
} //end of if (pkgItem...)
} //end of foreach
}//end of foreach subfolder
return found;
}
catch (Exception ex)
{
ex.Dump(this);
return false;
}
}