Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NuGet v3 populate dependencies #1778

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 22 additions & 23 deletions src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
if (repository != null)
{
// Write error and disregard repository entries containing wildcards.
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries:false, out string[] errorMsgs, out _repositoryNameContainsWildcard);
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries: false, out string[] errorMsgs, out _repositoryNameContainsWildcard);
foreach (string error in errorMsgs)
{
_cmdletPassedIn.WriteError(new ErrorRecord(
Expand Down Expand Up @@ -156,7 +156,7 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
if (repositoriesToSearch != null && repositoriesToSearch.Count == 0)
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
new PSArgumentException("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
"RepositoryNameIsNotResolved",
ErrorCategory.InvalidArgument,
this));
Expand Down Expand Up @@ -222,7 +222,8 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
bool shouldReportErrorForEachRepo = !suppressErrors && !_repositoryNameContainsWildcard;
foreach (PSResourceInfo currentPkg in SearchByNames(currentServer, currentResponseUtil, currentRepository, shouldReportErrorForEachRepo))
{
if (currentPkg == null) {
if (currentPkg == null)
{
_cmdletPassedIn.WriteDebug("No packages returned from server");
continue;
}
Expand All @@ -244,7 +245,7 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
{
// Scenarios: Find-PSResource -Name "pkg" -> write error only if pkg wasn't found in any registered repositories
// Scenarios: Find-PSResource -Name "pkg" -Repository *Gallery -> write error if only if pkg wasn't found in any matching repositories.
foreach(string pkgName in pkgsDiscovered)
foreach (string pkgName in pkgsDiscovered)
{
var msg = repository == null ? $"Package '{pkgName}' could not be found in any registered repositories." :
$"Package '{pkgName}' could not be found in registered repositories: '{string.Join(", ", repositoryNamesToSearch)}'.";
Expand All @@ -256,7 +257,7 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
this));
}
}
}
}

public IEnumerable<PSCommandResourceInfo> FindByCommandOrDscResource(
bool isSearchingForCommands,
Expand All @@ -283,12 +284,12 @@ public IEnumerable<PSCommandResourceInfo> FindByCommandOrDscResource(
// Error out if repository array of names to be searched contains wildcards.
if (repository != null)
{
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries:false, out string[] errorMsgs, out _repositoryNameContainsWildcard);
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries: false, out string[] errorMsgs, out _repositoryNameContainsWildcard);

if (string.Equals(repository[0], "*"))
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("-Repository parameter does not support entry '*' with -CommandName and -DSCResourceName parameters."),
new PSArgumentException("-Repository parameter does not support entry '*' with -CommandName and -DSCResourceName parameters."),
"RepositoryDoesNotSupportWildcardEntryWithCmdOrDSCName",
ErrorCategory.InvalidArgument,
this));
Expand Down Expand Up @@ -337,7 +338,7 @@ public IEnumerable<PSCommandResourceInfo> FindByCommandOrDscResource(
if (repositoriesToSearch != null && repositoriesToSearch.Count == 0)
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
new PSArgumentException("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
"RepositoryNameIsNotResolved",
ErrorCategory.InvalidArgument,
this));
Expand Down Expand Up @@ -487,12 +488,12 @@ public IEnumerable<PSResourceInfo> FindByTag(

if (repository != null)
{
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries:false, out string[] errorMsgs, out _repositoryNameContainsWildcard);
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries: false, out string[] errorMsgs, out _repositoryNameContainsWildcard);

if (string.Equals(repository[0], "*"))
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("-Repository parameter does not support entry '*' with -Tag parameter."),
new PSArgumentException("-Repository parameter does not support entry '*' with -Tag parameter."),
"RepositoryDoesNotSupportWildcardEntryWithTag",
ErrorCategory.InvalidArgument,
this));
Expand Down Expand Up @@ -541,7 +542,7 @@ public IEnumerable<PSResourceInfo> FindByTag(
if (repositoriesToSearch != null && repositoriesToSearch.Count == 0)
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
new PSArgumentException("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
"RepositoryNameIsNotResolved",
ErrorCategory.InvalidArgument,
this));
Expand Down Expand Up @@ -635,7 +636,7 @@ public IEnumerable<PSResourceInfo> FindByTag(
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Tags '{String.Join(", ", _tag)}' could not be found" , currentResult.exception),
new ResourceNotFoundException($"Tags '{String.Join(", ", _tag)}' could not be found", currentResult.exception),
"FindTagConvertToPSResourceFailure",
ErrorCategory.InvalidResult,
this);
Expand Down Expand Up @@ -729,7 +730,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
}
}
}
else if(pkgName.Contains("*"))
else if (pkgName.Contains("*"))
{
// Example: Find-PSResource -Name "Az*"
// Example: Find-PSResource -Name "Az*" -Tag "Storage"
Expand Down Expand Up @@ -1000,12 +1001,6 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
// After retrieving all packages find their dependencies
if (_includeDependencies)
{
if (currentServer.Repository.ApiVersion == PSRepositoryInfo.APIVersion.V3)
{
_cmdletPassedIn.WriteWarning("Installing dependencies is not currently supported for V3 server protocol repositories. The package will be installed without installing dependencies.");
yield break;
}

foreach (PSResourceInfo currentPkg in parentPkgs)
{
_cmdletPassedIn.WriteDebug($"Finding dependency packages for '{currentPkg.Name}'");
Expand Down Expand Up @@ -1102,7 +1097,8 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
{
_cmdletPassedIn.WriteVerbose(errRecord.Exception.Message);
}
else {
else
{
_cmdletPassedIn.WriteError(errRecord);
}
yield return null;
Expand Down Expand Up @@ -1164,7 +1160,8 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
{
_cmdletPassedIn.WriteVerbose(errRecord.Exception.Message);
}
else {
else
{
_cmdletPassedIn.WriteError(errRecord);
}
yield return null;
Expand Down Expand Up @@ -1199,7 +1196,8 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
// Check to see if version falls within version range
PSResourceInfo foundDep = currentResult.returnedObject;
string depVersionStr = $"{foundDep.Version}";
if (foundDep.IsPrerelease) {
if (foundDep.IsPrerelease)
{
depVersionStr += $"-{foundDep.Prerelease}";
}

Expand All @@ -1222,7 +1220,8 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
yield return depRes;
}
}
else {
else
{
List<string> pkgVersions = _packagesFound[depPkg.Name] as List<string>;
// _packagesFound has depPkg.name in it, but the version is not the same
if (!pkgVersions.Contains(FormatPkgVersionString(depPkg)))
Expand Down
54 changes: 26 additions & 28 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private List<PSResourceInfo> ProcessRepositories(
if (repository != null && repository.Length != 0)
{
// Write error and disregard repository entries containing wildcards.
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries:false, out string[] errorMsgs, out _);
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries: false, out string[] errorMsgs, out _);
foreach (string error in errorMsgs)
{
_cmdletPassedIn.WriteError(new ErrorRecord(
Expand Down Expand Up @@ -231,7 +231,7 @@ private List<PSResourceInfo> ProcessRepositories(
if (repositoriesToSearch != null && repositoriesToSearch.Count == 0)
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
new PSArgumentException("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
"RepositoryNameIsNotResolved",
ErrorCategory.InvalidArgument,
_cmdletPassedIn));
Expand Down Expand Up @@ -305,7 +305,8 @@ private List<PSResourceInfo> ProcessRepositories(
bool installDepsForRepo = skipDependencyCheck;

// If no more packages to install, then return
if (_pkgNamesToInstall.Count == 0) {
if (_pkgNamesToInstall.Count == 0)
{
return allPkgsInstalled;
}

Expand All @@ -329,14 +330,9 @@ private List<PSResourceInfo> ProcessRepositories(
}

repositoryNamesToSearch.Add(repoName);
if ((currentRepository.ApiVersion == PSRepositoryInfo.APIVersion.V3) && (!installDepsForRepo))
{
_cmdletPassedIn.WriteWarning("Installing dependencies is not currently supported for V3 server protocol repositories. The package will be installed without installing dependencies.");
installDepsForRepo = true;
}

var installedPkgs = InstallPackages(_pkgNamesToInstall.ToArray(), currentRepository, currentServer, currentResponseUtil, scope, skipDependencyCheck, findHelper);
foreach (var pkg in installedPkgs)
List<PSResourceInfo> installedPkgs = InstallPackages(_pkgNamesToInstall.ToArray(), currentRepository, currentServer, currentResponseUtil, scope, skipDependencyCheck, findHelper);
foreach (PSResourceInfo pkg in installedPkgs)
{
_pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase));
}
Expand Down Expand Up @@ -535,8 +531,10 @@ private void MoveFilesIntoInstallPath(
File.Delete(Path.Combine(finalModuleVersionDir, pkgInfo.Name + PSScriptFileExt));
}
}
else {
if (_includeXml) {
else
{
if (_includeXml)
{
_cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, scriptXML)));
Utils.MoveFiles(Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, scriptXML));
}
Expand Down Expand Up @@ -614,11 +612,6 @@ private List<PSResourceInfo> InstallPackages(

if (!skipDependencyCheck)
{
if (currentServer.Repository.ApiVersion == PSRepositoryInfo.APIVersion.V3)
{
_cmdletPassedIn.WriteWarning("Installing dependencies is not currently supported for V3 server protocol repositories. The package will be installed without installing dependencies.");
}

// Get the dependencies from the installed package.
if (parentPkgObj.Dependencies.Length > 0)
{
Expand Down Expand Up @@ -748,7 +741,7 @@ private Hashtable BeginPackageInstall(
return packagesHash;
}

break;
break;

case VersionType.SpecificVersion:
string nugetVersionString = specificVersion.ToNormalizedString(); // 3.0.17-beta
Expand Down Expand Up @@ -802,7 +795,9 @@ private Hashtable BeginPackageInstall(

break;
}
} else {
}
else
{
pkgToInstall = currentResult.returnedObject;

break;
Expand All @@ -816,11 +811,13 @@ private Hashtable BeginPackageInstall(

pkgToInstall.RepositorySourceLocation = repository.Uri.ToString();
pkgToInstall.AdditionalMetadata.TryGetValue("NormalizedVersion", out string pkgVersion);
if (pkgVersion == null) {
if (pkgVersion == null)
{
// Not all NuGet providers (e.g. Artifactory, possibly others) send NormalizedVersion in NuGet package responses.
// If they don't, we need to manually construct the combined version+prerelease from pkgToInstall.Version and the prerelease string.
pkgVersion = pkgToInstall.Version.ToString();
if (!String.IsNullOrEmpty(pkgToInstall.Prerelease)) {
if (!String.IsNullOrEmpty(pkgToInstall.Prerelease))
{
pkgVersion += $"-{pkgToInstall.Prerelease}";
}
}
Expand Down Expand Up @@ -913,11 +910,11 @@ private string CreateInstallationTempPath()
try
{
var dir = Directory.CreateDirectory(tempInstallPath); // should check it gets created properly
// To delete file attributes from the existing ones get the current file attributes first and use AND (&) operator
// with a mask (bitwise complement of desired attributes combination).
// TODO: check the attributes and if it's read only then set it
// attribute may be inherited from the parent
// TODO: are there Linux accommodations we need to consider here?
// To delete file attributes from the existing ones get the current file attributes first and use AND (&) operator
// with a mask (bitwise complement of desired attributes combination).
// TODO: check the attributes and if it's read only then set it
// attribute may be inherited from the parent
// TODO: are there Linux accommodations we need to consider here?
dir.Attributes &= ~FileAttributes.ReadOnly;
}
catch (Exception e)
Expand Down Expand Up @@ -995,7 +992,8 @@ private bool TryInstallToTempPath(
bool isModule = File.Exists(moduleManifest);
bool isScript = File.Exists(scriptPath);

if (!isModule && !isScript) {
if (!isModule && !isScript)
{
scriptPath = "";
}

Expand Down Expand Up @@ -1425,7 +1423,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t
"ForceAcceptLicense",
ErrorCategory.InvalidArgument,
_cmdletPassedIn);

return false;
}
}
Expand Down
Loading