From bc33abcc78b3c4c842dc4cfa3aaf387c147b0d7e Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Wed, 1 Dec 2021 16:02:53 -0500 Subject: [PATCH 01/15] add check to see if package expected to be installed was not --- src/code/InstallPSResource.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index bdbbbdc5f..0cc84e01d 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -5,6 +5,7 @@ using NuGet.Versioning; using System; using System.Collections.Generic; +using System.Linq; using System.Management.Automation; using Dbg = System.Diagnostics.Debug; @@ -264,14 +265,23 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ asNupkg: false, includeXML: true, skipDependencyCheck: SkipDependencyCheck, - pathsToInstallPkg: _pathsToInstallPkg); - - if (PassThru) - { - foreach (PSResourceInfo pkg in installedPkgs) - { - WriteObject(pkg); - } + pathsToInstallPkg: _pathsToInstallPkg); + + List actualPkgNames = installedPkgs.Select(x => x.Name).ToList(); + foreach(string expectedName in pkgNames) + { + if (!actualPkgNames.Contains(expectedName)) + { + WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose to see more information", expectedName)); + } + } + + if (PassThru) + { + foreach (PSResourceInfo pkg in installedPkgs) + { + WriteObject(pkg); + } } } From d647dbddd29a211ebe204ed2a99ed3a45071a6a4 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Wed, 1 Dec 2021 16:11:44 -0500 Subject: [PATCH 02/15] update warning message for install failing --- src/code/InstallPSResource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index a5536b5d4..d357dad86 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -272,7 +272,7 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ { if (!actualPkgNames.Contains(expectedPkgName)) { - WriteWarning(String.Format("Package '{0}' could not be installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); + WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); } } From 81cad1048e9aa8e193122b0fa237fdf1fb05c5c0 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 2 Dec 2021 11:35:37 -0500 Subject: [PATCH 03/15] rename variable --- src/code/InstallPSResource.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index d357dad86..2b5880ee5 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -267,10 +267,10 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ savePkg: false, pathsToInstallPkg: _pathsToInstallPkg); - List actualPkgNames = installedPkgs.Select(x => x.Name).ToList(); + List installedPkgNames = installedPkgs.Select(x => x.Name).ToList(); foreach(string expectedPkgName in pkgNames) { - if (!actualPkgNames.Contains(expectedPkgName)) + if (!installedPkgNames.Contains(expectedPkgName)) { WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); } From 2ba6799310e540cf43ced783ba8400d02f77e0bf Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 2 Dec 2021 14:07:02 -0500 Subject: [PATCH 04/15] have InstallPackages modify out errorRecords variable and write errors at cmdlet level --- src/code/InstallHelper.cs | 52 +++++++++++++++++++++++++++-------- src/code/InstallPSResource.cs | 14 ++++++++-- src/code/SavePSResource.cs | 8 +++++- src/code/UpdatePSResource.cs | 8 +++++- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 8f5d45d8e..afba22f7c 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -50,6 +50,7 @@ internal class InstallHelper : PSCmdlet private bool _noClobber; private bool _savePkg; List _pathsToSearch; + List _errorRecordsList; #endregion @@ -78,7 +79,8 @@ public List InstallPackages( bool includeXML, bool skipDependencyCheck, bool savePkg, - List pathsToInstallPkg) + List pathsToInstallPkg, + out List errorRecords) { _cmdletPassedIn.WriteVerbose(string.Format("Parameters passed in >>> Name: '{0}'; Version: '{1}'; Prerelease: '{2}'; Repository: '{3}'; " + "AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXML '{10}'; SavePackage '{11}'", @@ -108,6 +110,7 @@ public List InstallPackages( _includeXML = includeXML; _savePkg = savePkg; _pathsToInstallPkg = pathsToInstallPkg; + _errorRecordsList = new List(); // Create list of installation paths to search. _pathsToSearch = new List(); @@ -125,12 +128,15 @@ public List InstallPackages( } // Go through the repositories and see which is the first repository to have the pkg version available - return ProcessRepositories( + List installedPkgs = ProcessRepositories( packageNames: names, repository: repository, trustRepository: _trustRepository, credential: _credential, skipDependencyCheck: skipDependencyCheck); + + errorRecords = _errorRecordsList; + return installedPkgs; } #endregion @@ -488,11 +494,14 @@ private List InstallPackage( var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1"); if (!File.Exists(moduleManifest)) { - var message = String.Format("Module manifest file: {0} does not exist. This is not a valid PowerShell module.", moduleManifest); + var message = String.Format("{0} package could not be installed with error: Module manifest file: {1} does not exist. This is not a valid PowerShell module.", pkgIdentity.Id, moduleManifest); var ex = new ArgumentException(message); var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); _cmdletPassedIn.WriteError(psdataFileDoesNotExistError); + + // TODO: verify it works Anam + _errorRecordsList.Add(psdataFileDoesNotExistError); continue; } @@ -541,14 +550,25 @@ private List InstallPackage( } catch (Exception e) { - _cmdletPassedIn.WriteError( - new ErrorRecord( + var errRec = new ErrorRecord( new PSInvalidOperationException( message: $"Unable to successfully install package '{pkg.Name}': '{e.Message}'", innerException: e), "InstallPackageFailed", ErrorCategory.InvalidOperation, - _cmdletPassedIn)); + _cmdletPassedIn); + + // _cmdletPassedIn.WriteError( + // new ErrorRecord( + // new PSInvalidOperationException( + // message: $"Unable to successfully install package '{pkg.Name}': '{e.Message}'", + // innerException: e), + // "InstallPackageFailed", + // ErrorCategory.InvalidOperation, + // _cmdletPassedIn)); + _cmdletPassedIn.WriteError(errRec); + // TODO: change this to Package {0} could not be installed with error: at start?? I feel not. + _errorRecordsList.Add(errRec); } finally { @@ -560,6 +580,8 @@ private List InstallPackage( if (!TryDeleteDirectory(tempInstallPath, out ErrorRecord errorMsg)) { _cmdletPassedIn.WriteError(errorMsg); + // TODO: does this prevent installation? do I need to write Package {0} could not be installed with error: here? + _errorRecordsList.Add(errorMsg); } else { @@ -607,11 +629,13 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t if (!File.Exists(LicenseFilePath)) { - var exMessage = "License.txt not Found. License.txt must be provided when user license acceptance is required."; + var exMessage = String.Format("{0} package could not be installed with error: License.txt not found. License.txt must be provided when user license acceptance is required.", p.Name); var ex = new ArgumentException(exMessage); var acceptLicenseError = new ErrorRecord(ex, "LicenseTxtNotFound", ErrorCategory.ObjectNotFound, null); _cmdletPassedIn.WriteError(acceptLicenseError); + // TODO verify it works Anam + _errorRecordsList.Add(acceptLicenseError); success = false; } @@ -634,11 +658,13 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t // Check if user agreed to license terms, if they didn't then throw error, otherwise continue to install if (!_acceptLicense) { - var message = $"License Acceptance is required for module '{p.Name}'. Please specify '-AcceptLicense' to perform this operation."; + var message = String.Format("{0} package could not be installed with error: License Acceptance is required for module '{0}'. Please specify '-AcceptLicense' to perform this operation.", p.Name); var ex = new ArgumentException(message); var acceptLicenseError = new ErrorRecord(ex, "ForceAcceptLicense", ErrorCategory.InvalidArgument, null); _cmdletPassedIn.WriteError(acceptLicenseError); + // TODO verify it works Anam + _errorRecordsList.Add(acceptLicenseError); success = false; } } @@ -683,13 +709,15 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable) duplicateCmdlets.AddRange(duplicateCmds); var errMessage = string.Format( - "The following commands are already available on this system: '{0}'. This module '{1}' may override the existing commands. If you still want to install this module '{1}', remove the -NoClobber parameter.", + "{1} package could not be installed with error: The following commands are already available on this system: '{0}'. This module '{1}' may override the existing commands. If you still want to install this module '{1}', remove the -NoClobber parameter.", String.Join(", ", duplicateCmdlets), pkgName); var ex = new ArgumentException(errMessage); var noClobberError = new ErrorRecord(ex, "CommandAlreadyExists", ErrorCategory.ResourceExists, null); _cmdletPassedIn.WriteError(noClobberError); + // TODO verify it works Anam + _errorRecordsList.Add(noClobberError); foundClobber = true; return foundClobber; @@ -712,10 +740,12 @@ private void CreateMetadataXMLFile(string dirNameVersion, string installPath, PS // Write all metadata into metadataXMLPath if (!pkg.TryWrite(metadataXMLPath, out string error)) { - var message = string.Format("Error parsing metadata into XML: '{0}'", error); + var message = string.Format("{0} package could not be installed with error: Error parsing metadata into XML: '{1}'", pkg.Name, error); var ex = new ArgumentException(message); var ErrorParsingMetadata = new ErrorRecord(ex, "ErrorParsingMetadata", ErrorCategory.ParserError, null); - WriteError(ErrorParsingMetadata); + _cmdletPassedIn.WriteError(ErrorParsingMetadata); + // TODO verify it works Anam + _errorRecordsList.Add(ErrorParsingMetadata); } } diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 2b5880ee5..8181e754e 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -265,17 +265,27 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ includeXML: true, skipDependencyCheck: SkipDependencyCheck, savePkg: false, - pathsToInstallPkg: _pathsToInstallPkg); + pathsToInstallPkg: _pathsToInstallPkg, + out List errorRecords); List installedPkgNames = installedPkgs.Select(x => x.Name).ToList(); foreach(string expectedPkgName in pkgNames) { if (!installedPkgNames.Contains(expectedPkgName)) { - WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); + // WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); + var message = String.Format("Package {0} could not be installed with error: resource could not be found in any registered repositories", expectedPkgName); + var ex = new ArgumentException(message); + var ResourceNotFoundError = new ErrorRecord(ex, "resourceNotFoundError", ErrorCategory.ObjectNotFound, null); + errorRecords.Add(ResourceNotFoundError); } } + foreach (ErrorRecord error in errorRecords) + { + WriteError(error); + } + if (PassThru) { foreach (PSResourceInfo pkg in installedPkgs) diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index e9c456cd7..da0118d51 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -256,7 +256,13 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p includeXML: IncludeXML, skipDependencyCheck: SkipDependencyCheck, savePkg: true, - pathsToInstallPkg: new List { _path } ); + pathsToInstallPkg: new List { _path }, + errorRecords: out List errorRecords); + + foreach (ErrorRecord error in errorRecords) + { + WriteError(error); + } if (PassThru) { diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index dc947e084..75e69014a 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -178,7 +178,13 @@ protected override void ProcessRecord() includeXML: true, skipDependencyCheck: SkipDependencyCheck, savePkg: false, - pathsToInstallPkg: _pathsToInstallPkg); + pathsToInstallPkg: _pathsToInstallPkg, + errorRecords: out List errorRecords); + + foreach(ErrorRecord error in errorRecords) + { + WriteError(error); + } if (PassThru) { From 6d21d4b37b4267ea70343274f1c04ec75c6730d9 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 2 Dec 2021 19:04:40 -0500 Subject: [PATCH 05/15] Refactor install failing silently when package does not exist to not overlap with reinstall case --- src/code/InstallHelper.cs | 65 +++++++++++++++++------------------ src/code/InstallPSResource.cs | 39 ++++++++++----------- src/code/SavePSResource.cs | 8 +---- src/code/UpdatePSResource.cs | 8 +---- 4 files changed, 52 insertions(+), 68 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index afba22f7c..eebe29d0a 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -50,7 +50,6 @@ internal class InstallHelper : PSCmdlet private bool _noClobber; private bool _savePkg; List _pathsToSearch; - List _errorRecordsList; #endregion @@ -79,8 +78,7 @@ public List InstallPackages( bool includeXML, bool skipDependencyCheck, bool savePkg, - List pathsToInstallPkg, - out List errorRecords) + List pathsToInstallPkg) { _cmdletPassedIn.WriteVerbose(string.Format("Parameters passed in >>> Name: '{0}'; Version: '{1}'; Prerelease: '{2}'; Repository: '{3}'; " + "AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXML '{10}'; SavePackage '{11}'", @@ -110,7 +108,6 @@ public List InstallPackages( _includeXML = includeXML; _savePkg = savePkg; _pathsToInstallPkg = pathsToInstallPkg; - _errorRecordsList = new List(); // Create list of installation paths to search. _pathsToSearch = new List(); @@ -135,7 +132,6 @@ public List InstallPackages( credential: _credential, skipDependencyCheck: skipDependencyCheck); - errorRecords = _errorRecordsList; return installedPkgs; } @@ -158,6 +154,7 @@ private List ProcessRepositories( var findHelper = new FindHelper(_cancellationToken, _cmdletPassedIn); List allPkgsInstalled = new List(); + foreach (var repo in listOfRepositories) { // If no more packages to install, then return @@ -223,11 +220,23 @@ private List ProcessRepositories( if (!_reinstall) { // Removes all of the names that are already installed from the list of names to search for - pkgsFromRepoToInstall = FilterByInstalledPkgs(pkgsFromRepoToInstall); + _cmdletPassedIn.WriteVerbose("made it to reinstall not selected condition being true"); + foreach(PSResourceInfo p in pkgsFromRepoToInstall) + { + _cmdletPassedIn.WriteVerbose("pkgs left before !reinstall removing: " + p.Name); + } + pkgsFromRepoToInstall = FilterByInstalledPkgs(pkgsFromRepoToInstall, ref pkgNamesToInstall); + foreach(PSResourceInfo p in pkgsFromRepoToInstall) + { + _cmdletPassedIn.WriteVerbose("pkgs left after !reinstall removing: " + p.Name); + } + // this now contains valid packages from Find minus those which would require reinstall + // TODO I think: also remove names of packages which are already installed from PkgNamesToInstall here! } if (!pkgsFromRepoToInstall.Any()) { + _cmdletPassedIn.WriteVerbose("nothing to install left so return"); continue; } @@ -246,11 +255,21 @@ private List ProcessRepositories( allPkgsInstalled.AddRange(pkgsInstalled); } + _cmdletPassedIn.WriteVerbose("names left in pkgNamesToInstall:" + String.Join(", ", pkgNamesToInstall)); + // TODO: iterate through pkgNamesToInstall left and write error here tbh + foreach (string pkgName in pkgNamesToInstall) + { + var message = String.Format("Package {0} could not be installed as it was not found in any registered repositories", pkgName); + var ex = new ArgumentException(message); + var ResourceNotFoundError = new ErrorRecord(ex, "resourceNotFoundError", ErrorCategory.ObjectNotFound, null); + _cmdletPassedIn.WriteError(ResourceNotFoundError); + } + return allPkgsInstalled; } // Check if any of the pkg versions are already installed, if they are we'll remove them from the list of packages to install - private IEnumerable FilterByInstalledPkgs(IEnumerable packages) + private IEnumerable FilterByInstalledPkgs(IEnumerable packages, ref List pkgNamesToInstall) { // Create list of installation paths to search. List _pathsToSearch = new List(); @@ -292,6 +311,7 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable InstallPackage( var ex = new ArgumentException(message); var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); _cmdletPassedIn.WriteError(psdataFileDoesNotExistError); - - // TODO: verify it works Anam - _errorRecordsList.Add(psdataFileDoesNotExistError); continue; } @@ -550,25 +567,14 @@ private List InstallPackage( } catch (Exception e) { - var errRec = new ErrorRecord( + _cmdletPassedIn.WriteError( + new ErrorRecord( new PSInvalidOperationException( message: $"Unable to successfully install package '{pkg.Name}': '{e.Message}'", innerException: e), "InstallPackageFailed", ErrorCategory.InvalidOperation, - _cmdletPassedIn); - - // _cmdletPassedIn.WriteError( - // new ErrorRecord( - // new PSInvalidOperationException( - // message: $"Unable to successfully install package '{pkg.Name}': '{e.Message}'", - // innerException: e), - // "InstallPackageFailed", - // ErrorCategory.InvalidOperation, - // _cmdletPassedIn)); - _cmdletPassedIn.WriteError(errRec); - // TODO: change this to Package {0} could not be installed with error: at start?? I feel not. - _errorRecordsList.Add(errRec); + _cmdletPassedIn)); } finally { @@ -580,8 +586,6 @@ private List InstallPackage( if (!TryDeleteDirectory(tempInstallPath, out ErrorRecord errorMsg)) { _cmdletPassedIn.WriteError(errorMsg); - // TODO: does this prevent installation? do I need to write Package {0} could not be installed with error: here? - _errorRecordsList.Add(errorMsg); } else { @@ -634,8 +638,6 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var acceptLicenseError = new ErrorRecord(ex, "LicenseTxtNotFound", ErrorCategory.ObjectNotFound, null); _cmdletPassedIn.WriteError(acceptLicenseError); - // TODO verify it works Anam - _errorRecordsList.Add(acceptLicenseError); success = false; } @@ -663,8 +665,6 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var acceptLicenseError = new ErrorRecord(ex, "ForceAcceptLicense", ErrorCategory.InvalidArgument, null); _cmdletPassedIn.WriteError(acceptLicenseError); - // TODO verify it works Anam - _errorRecordsList.Add(acceptLicenseError); success = false; } } @@ -716,8 +716,6 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable) var noClobberError = new ErrorRecord(ex, "CommandAlreadyExists", ErrorCategory.ResourceExists, null); _cmdletPassedIn.WriteError(noClobberError); - // TODO verify it works Anam - _errorRecordsList.Add(noClobberError); foundClobber = true; return foundClobber; @@ -743,9 +741,8 @@ private void CreateMetadataXMLFile(string dirNameVersion, string installPath, PS var message = string.Format("{0} package could not be installed with error: Error parsing metadata into XML: '{1}'", pkg.Name, error); var ex = new ArgumentException(message); var ErrorParsingMetadata = new ErrorRecord(ex, "ErrorParsingMetadata", ErrorCategory.ParserError, null); + _cmdletPassedIn.WriteError(ErrorParsingMetadata); - // TODO verify it works Anam - _errorRecordsList.Add(ErrorParsingMetadata); } } diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 8181e754e..c1c8494ef 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -265,26 +265,25 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ includeXML: true, skipDependencyCheck: SkipDependencyCheck, savePkg: false, - pathsToInstallPkg: _pathsToInstallPkg, - out List errorRecords); - - List installedPkgNames = installedPkgs.Select(x => x.Name).ToList(); - foreach(string expectedPkgName in pkgNames) - { - if (!installedPkgNames.Contains(expectedPkgName)) - { - // WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); - var message = String.Format("Package {0} could not be installed with error: resource could not be found in any registered repositories", expectedPkgName); - var ex = new ArgumentException(message); - var ResourceNotFoundError = new ErrorRecord(ex, "resourceNotFoundError", ErrorCategory.ObjectNotFound, null); - errorRecords.Add(ResourceNotFoundError); - } - } - - foreach (ErrorRecord error in errorRecords) - { - WriteError(error); - } + pathsToInstallPkg: _pathsToInstallPkg); + + // List installedPkgNames = installedPkgs.Select(x => x.Name).ToList(); + // foreach(string expectedPkgName in pkgNames) + // { + // if (!installedPkgNames.Contains(expectedPkgName)) + // { + // // WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); + // var message = String.Format("Package {0} could not be installed with error: resource could not be found in any registered repositories", expectedPkgName); + // var ex = new ArgumentException(message); + // var ResourceNotFoundError = new ErrorRecord(ex, "resourceNotFoundError", ErrorCategory.ObjectNotFound, null); + // errorRecords.Add(ResourceNotFoundError); + // } + // } + + // foreach (ErrorRecord error in errorRecords) + // { + // WriteError(error); + // } if (PassThru) { diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index da0118d51..c6ed9981c 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -256,13 +256,7 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p includeXML: IncludeXML, skipDependencyCheck: SkipDependencyCheck, savePkg: true, - pathsToInstallPkg: new List { _path }, - errorRecords: out List errorRecords); - - foreach (ErrorRecord error in errorRecords) - { - WriteError(error); - } + pathsToInstallPkg: new List { _path }); if (PassThru) { diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index 75e69014a..dc947e084 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -178,13 +178,7 @@ protected override void ProcessRecord() includeXML: true, skipDependencyCheck: SkipDependencyCheck, savePkg: false, - pathsToInstallPkg: _pathsToInstallPkg, - errorRecords: out List errorRecords); - - foreach(ErrorRecord error in errorRecords) - { - WriteError(error); - } + pathsToInstallPkg: _pathsToInstallPkg); if (PassThru) { From f0eb9f471bb13d6676e35041103d5d9519447248 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 2 Dec 2021 20:22:14 -0500 Subject: [PATCH 06/15] fix some case insensitive bugs with removing pkg names from list --- src/code/InstallHelper.cs | 7 ++++--- test/InstallPSResource.Tests.ps1 | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index eebe29d0a..c2102f5a9 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -249,7 +249,8 @@ private List ProcessRepositories( foreach (PSResourceInfo pkg in pkgsInstalled) { - pkgNamesToInstall.Remove(pkg.Name); + // pkgNamesToInstall.Remove(pkg.Name); + pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } allPkgsInstalled.AddRange(pkgsInstalled); @@ -261,7 +262,7 @@ private List ProcessRepositories( { var message = String.Format("Package {0} could not be installed as it was not found in any registered repositories", pkgName); var ex = new ArgumentException(message); - var ResourceNotFoundError = new ErrorRecord(ex, "resourceNotFoundError", ErrorCategory.ObjectNotFound, null); + var ResourceNotFoundError = new ErrorRecord(ex, "ResourceNotFoundError", ErrorCategory.ObjectNotFound, null); _cmdletPassedIn.WriteError(ResourceNotFoundError); } @@ -311,7 +312,7 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } return filteredPackages.Values.ToArray(); diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index b0812c636..b51dd257e 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -36,7 +36,7 @@ Describe 'Test Install-PSResource for Module' { } It "Install specific module resource by name" { - Install-PSResource -Name "TestModule" -Repository $TestGalleryName + Install-PSResource -Name "TestModule" -Repository $TestGalleryName $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" $pkg.Version | Should -Be "1.3.0" @@ -57,9 +57,11 @@ Describe 'Test Install-PSResource for Module' { } It "Should not install resource given nonexistant name" { - Install-PSResource -Name NonExistantModule -Repository $TestGalleryName + Install-PSResource -Name "NonExistantModule" -Repository $TestGalleryName -ErrorVariable err -ErrorAction SilentlyContinue $pkg = Get-Module "NonExistantModule" -ListAvailable $pkg.Name | Should -BeNullOrEmpty + $err.Count | Should -Not -Be 0 + $err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFoundError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } # Do some version testing, but Find-PSResource should be doing thorough testing From 0c8d98797e7c953cfee8d3974b5d80cf18e552d5 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 15:52:14 -0500 Subject: [PATCH 07/15] try using NuGet version to detect invalid versions according to NuGetVersion syntax --- src/code/InstallHelper.cs | 49 ++++++++++++++++------------------- src/code/InstallPSResource.cs | 33 ++++++++++------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index c2102f5a9..decc5082b 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -50,6 +50,7 @@ internal class InstallHelper : PSCmdlet private bool _noClobber; private bool _savePkg; List _pathsToSearch; + List _pckgNamesToInstall; #endregion @@ -111,6 +112,7 @@ public List InstallPackages( // Create list of installation paths to search. _pathsToSearch = new List(); + _pckgNamesToInstall = names.ToList(); // _pathsToInstallPkg will only contain the paths specified within the -Scope param (if applicable) // _pathsToSearch will contain all resource package subdirectories within _pathsToInstallPkg path locations @@ -148,7 +150,7 @@ private List ProcessRepositories( bool skipDependencyCheck) { var listOfRepositories = RepositorySettings.Read(repository, out string[] _); - List pkgNamesToInstall = packageNames.ToList(); + // List pkgNamesToInstall = packageNames.ToList(); var yesToAll = false; var noToAll = false; @@ -158,7 +160,7 @@ private List ProcessRepositories( foreach (var repo in listOfRepositories) { // If no more packages to install, then return - if (!pkgNamesToInstall.Any()) return allPkgsInstalled; + if (!_pckgNamesToInstall.Any()) return allPkgsInstalled; string repoName = repo.Name; _cmdletPassedIn.WriteVerbose(string.Format("Attempting to search for packages in '{0}'", repoName)); @@ -190,7 +192,7 @@ private List ProcessRepositories( // Finds parent packages and dependencies IEnumerable pkgsFromRepoToInstall = findHelper.FindByResourceName( - name: pkgNamesToInstall.ToArray(), + name: _pckgNamesToInstall.ToArray(), type: ResourceType.None, version: _versionRange != null ? _versionRange.OriginalString : null, prerelease: _prerelease, @@ -219,19 +221,7 @@ private List ProcessRepositories( // Check to see if the pkgs (including dependencies) are already installed (ie the pkg is installed and the version satisfies the version range provided via param) if (!_reinstall) { - // Removes all of the names that are already installed from the list of names to search for - _cmdletPassedIn.WriteVerbose("made it to reinstall not selected condition being true"); - foreach(PSResourceInfo p in pkgsFromRepoToInstall) - { - _cmdletPassedIn.WriteVerbose("pkgs left before !reinstall removing: " + p.Name); - } - pkgsFromRepoToInstall = FilterByInstalledPkgs(pkgsFromRepoToInstall, ref pkgNamesToInstall); - foreach(PSResourceInfo p in pkgsFromRepoToInstall) - { - _cmdletPassedIn.WriteVerbose("pkgs left after !reinstall removing: " + p.Name); - } - // this now contains valid packages from Find minus those which would require reinstall - // TODO I think: also remove names of packages which are already installed from PkgNamesToInstall here! + pkgsFromRepoToInstall = FilterByInstalledPkgs(pkgsFromRepoToInstall); } if (!pkgsFromRepoToInstall.Any()) @@ -242,23 +232,20 @@ private List ProcessRepositories( List pkgsInstalled = InstallPackage( pkgsFromRepoToInstall, - pkgNamesToInstall, + // _pckgNamesToInstall, repo.Url.AbsoluteUri, credential, isLocalRepo); foreach (PSResourceInfo pkg in pkgsInstalled) { - // pkgNamesToInstall.Remove(pkg.Name); - pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } allPkgsInstalled.AddRange(pkgsInstalled); } - _cmdletPassedIn.WriteVerbose("names left in pkgNamesToInstall:" + String.Join(", ", pkgNamesToInstall)); - // TODO: iterate through pkgNamesToInstall left and write error here tbh - foreach (string pkgName in pkgNamesToInstall) + foreach (string pkgName in _pckgNamesToInstall) { var message = String.Format("Package {0} could not be installed as it was not found in any registered repositories", pkgName); var ex = new ArgumentException(message); @@ -270,7 +257,7 @@ private List ProcessRepositories( } // Check if any of the pkg versions are already installed, if they are we'll remove them from the list of packages to install - private IEnumerable FilterByInstalledPkgs(IEnumerable packages, ref List pkgNamesToInstall) + private IEnumerable FilterByInstalledPkgs(IEnumerable packages) { // Create list of installation paths to search. List _pathsToSearch = new List(); @@ -312,7 +299,7 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } return filteredPackages.Values.ToArray(); @@ -320,7 +307,7 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable InstallPackage( IEnumerable pkgsToInstall, // those found to be required to be installed (includes Dependency packages as well) - List pkgNamesToInstall, // those requested by the user to be installed + // List pkgNamesToInstall, // those requested by the user to be installed string repoUrl, PSCredential credential, bool isLocalRepo) @@ -357,7 +344,7 @@ private List InstallPackage( string activity = ""; string statusDescription = ""; - if (pkgNamesToInstall.ToList().Contains(pkg.Name, StringComparer.InvariantCultureIgnoreCase)) + if (_pckgNamesToInstall.Contains(pkg.Name, StringComparer.InvariantCultureIgnoreCase)) { // Installing parent package (one whose name was passed in to install) activityId = 0; @@ -387,10 +374,14 @@ private List InstallPackage( if (!NuGetVersion.TryParse(createFullVersion, out NuGetVersion pkgVersion)) { + // TODO: should this be WriteError (cause once one bad version is specified that name isn't going to be searched/installed) + // i.e can't do Install-PSResource ValidPkg -Version "2-0-0-0","2.0.0.0" _cmdletPassedIn.WriteVerbose(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion", pkg.Name, pkg.Version.ToString())); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } + _cmdletPassedIn.WriteVerbose("Parsed NuGet version is: " + pkgVersion.ToFullString()); var pkgIdentity = new PackageIdentity(pkg.Name, pkgVersion); var cacheContext = new SourceCacheContext(); @@ -520,6 +511,7 @@ private List InstallPackage( var ex = new ArgumentException(message); var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); _cmdletPassedIn.WriteError(psdataFileDoesNotExistError); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } @@ -576,6 +568,7 @@ private List InstallPackage( "InstallPackageFailed", ErrorCategory.InvalidOperation, _cmdletPassedIn)); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } finally { @@ -639,6 +632,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var acceptLicenseError = new ErrorRecord(ex, "LicenseTxtNotFound", ErrorCategory.ObjectNotFound, null); _cmdletPassedIn.WriteError(acceptLicenseError); + _pckgNamesToInstall.RemoveAll(x => x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)); success = false; } @@ -666,6 +660,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var acceptLicenseError = new ErrorRecord(ex, "ForceAcceptLicense", ErrorCategory.InvalidArgument, null); _cmdletPassedIn.WriteError(acceptLicenseError); + _pckgNamesToInstall.RemoveAll(x => x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)); success = false; } } @@ -717,6 +712,7 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable) var noClobberError = new ErrorRecord(ex, "CommandAlreadyExists", ErrorCategory.ResourceExists, null); _cmdletPassedIn.WriteError(noClobberError); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkgName, StringComparison.InvariantCultureIgnoreCase)); foundClobber = true; return foundClobber; @@ -743,6 +739,7 @@ private void CreateMetadataXMLFile(string dirNameVersion, string installPath, PS var ex = new ArgumentException(message); var ErrorParsingMetadata = new ErrorRecord(ex, "ErrorParsingMetadata", ErrorCategory.ParserError, null); + _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); _cmdletPassedIn.WriteError(ErrorParsingMetadata); } } diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index c1c8494ef..0da0192f1 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -158,7 +158,14 @@ protected override void ProcessRecord() { _versionRange = VersionRange.All; } - else if (!Utils.TryParseVersionOrVersionRange(Version, out _versionRange)) + // else if (!Utils.TryParseVersionOrVersionRange(Version, out _versionRange)) + // { + // var exMessage = "Argument for -Version parameter is not in the proper format."; + // var ex = new ArgumentException(exMessage); + // var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null); + // ThrowTerminatingError(IncorrectVersionFormat); + // } + else if (!VersionRange.TryParse(Version, out _versionRange)) { var exMessage = "Argument for -Version parameter is not in the proper format."; var ex = new ArgumentException(exMessage); @@ -166,6 +173,12 @@ protected override void ProcessRecord() ThrowTerminatingError(IncorrectVersionFormat); } + if (_versionRange == null) + { + WriteVerbose("versionRange null!"); + } + WriteVerbose("NuGet version range: " + _versionRange.ToString()); + ProcessInstallHelper( pkgNames: Name, pkgPrerelease: Prerelease, @@ -267,24 +280,6 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ savePkg: false, pathsToInstallPkg: _pathsToInstallPkg); - // List installedPkgNames = installedPkgs.Select(x => x.Name).ToList(); - // foreach(string expectedPkgName in pkgNames) - // { - // if (!installedPkgNames.Contains(expectedPkgName)) - // { - // // WriteWarning(String.Format("Package '{0}' was not installed. Please run the cmdlet with -Verbose for more information", expectedPkgName)); - // var message = String.Format("Package {0} could not be installed with error: resource could not be found in any registered repositories", expectedPkgName); - // var ex = new ArgumentException(message); - // var ResourceNotFoundError = new ErrorRecord(ex, "resourceNotFoundError", ErrorCategory.ObjectNotFound, null); - // errorRecords.Add(ResourceNotFoundError); - // } - // } - - // foreach (ErrorRecord error in errorRecords) - // { - // WriteError(error); - // } - if (PassThru) { foreach (PSResourceInfo pkg in installedPkgs) From ecbc5f12132ee6e42810ca563688b79cb506c1c4 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 17:09:22 -0500 Subject: [PATCH 08/15] update error message for package not being found --- src/code/InstallHelper.cs | 4 +++- src/code/InstallPSResource.cs | 18 +++++++++--------- test/InstallPSResource.Tests.ps1 | 4 +++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index decc5082b..0fe3f6247 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -247,7 +247,9 @@ private List ProcessRepositories( foreach (string pkgName in _pckgNamesToInstall) { - var message = String.Format("Package {0} could not be installed as it was not found in any registered repositories", pkgName); + var message = String.Format("Package '{0}' with requested version range {1} could not be installed as it was not found in any registered repositories", + pkgName, + _versionRange.ToString()); var ex = new ArgumentException(message); var ResourceNotFoundError = new ErrorRecord(ex, "ResourceNotFoundError", ErrorCategory.ObjectNotFound, null); _cmdletPassedIn.WriteError(ResourceNotFoundError); diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 0da0192f1..d03b40db6 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -158,27 +158,27 @@ protected override void ProcessRecord() { _versionRange = VersionRange.All; } - // else if (!Utils.TryParseVersionOrVersionRange(Version, out _versionRange)) - // { - // var exMessage = "Argument for -Version parameter is not in the proper format."; - // var ex = new ArgumentException(exMessage); - // var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null); - // ThrowTerminatingError(IncorrectVersionFormat); - // } - else if (!VersionRange.TryParse(Version, out _versionRange)) + else if (!Utils.TryParseVersionOrVersionRange(Version, out _versionRange)) { var exMessage = "Argument for -Version parameter is not in the proper format."; var ex = new ArgumentException(exMessage); var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null); ThrowTerminatingError(IncorrectVersionFormat); } + // else if (!VersionRange.TryParse(Version, out _versionRange)) + // { + // var exMessage = "Argument for -Version parameter is not in the proper format."; + // var ex = new ArgumentException(exMessage); + // var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null); + // ThrowTerminatingError(IncorrectVersionFormat); + // } if (_versionRange == null) { WriteVerbose("versionRange null!"); } WriteVerbose("NuGet version range: " + _versionRange.ToString()); - + ProcessInstallHelper( pkgNames: Name, pkgPrerelease: Prerelease, diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index b51dd257e..dad59071b 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -99,7 +99,9 @@ Describe 'Test Install-PSResource for Module' { ) { param($Version, $Description) - Install-PSResource -Name "TestModule" -Version $Version -Repository $TestGalleryName + Install-PSResource -Name "TestModule" -Version $Version -Repository $TestGalleryName -ErrorAction SilentlyContinue + $Error[0].FullyQualifiedErrorId | Should -be "ResourceNotFoundError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + $res = Get-Module "TestModule" -ListAvailable $res | Should -BeNullOrEmpty } From 4f3a18d5566f9de17b33a4e4f7fe5a1821b26fed Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 17:20:44 -0500 Subject: [PATCH 09/15] clean up code --- src/code/InstallHelper.cs | 35 +++++++++++++++-------------------- src/code/InstallPSResource.cs | 13 ------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 0fe3f6247..1ec4091f3 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -50,7 +50,7 @@ internal class InstallHelper : PSCmdlet private bool _noClobber; private bool _savePkg; List _pathsToSearch; - List _pckgNamesToInstall; + List _pkgNamesToInstall; #endregion @@ -112,7 +112,7 @@ public List InstallPackages( // Create list of installation paths to search. _pathsToSearch = new List(); - _pckgNamesToInstall = names.ToList(); + _pkgNamesToInstall = names.ToList(); // _pathsToInstallPkg will only contain the paths specified within the -Scope param (if applicable) // _pathsToSearch will contain all resource package subdirectories within _pathsToInstallPkg path locations @@ -128,7 +128,6 @@ public List InstallPackages( // Go through the repositories and see which is the first repository to have the pkg version available List installedPkgs = ProcessRepositories( - packageNames: names, repository: repository, trustRepository: _trustRepository, credential: _credential, @@ -143,14 +142,12 @@ public List InstallPackages( // This method calls iterates through repositories (by priority order) to search for the pkgs to install private List ProcessRepositories( - string[] packageNames, string[] repository, bool trustRepository, PSCredential credential, bool skipDependencyCheck) { var listOfRepositories = RepositorySettings.Read(repository, out string[] _); - // List pkgNamesToInstall = packageNames.ToList(); var yesToAll = false; var noToAll = false; @@ -160,7 +157,7 @@ private List ProcessRepositories( foreach (var repo in listOfRepositories) { // If no more packages to install, then return - if (!_pckgNamesToInstall.Any()) return allPkgsInstalled; + if (!_pkgNamesToInstall.Any()) return allPkgsInstalled; string repoName = repo.Name; _cmdletPassedIn.WriteVerbose(string.Format("Attempting to search for packages in '{0}'", repoName)); @@ -192,7 +189,7 @@ private List ProcessRepositories( // Finds parent packages and dependencies IEnumerable pkgsFromRepoToInstall = findHelper.FindByResourceName( - name: _pckgNamesToInstall.ToArray(), + name: _pkgNamesToInstall.ToArray(), type: ResourceType.None, version: _versionRange != null ? _versionRange.OriginalString : null, prerelease: _prerelease, @@ -232,20 +229,19 @@ private List ProcessRepositories( List pkgsInstalled = InstallPackage( pkgsFromRepoToInstall, - // _pckgNamesToInstall, repo.Url.AbsoluteUri, credential, isLocalRepo); foreach (PSResourceInfo pkg in pkgsInstalled) { - _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } allPkgsInstalled.AddRange(pkgsInstalled); } - foreach (string pkgName in _pckgNamesToInstall) + foreach (string pkgName in _pkgNamesToInstall) { var message = String.Format("Package '{0}' with requested version range {1} could not be installed as it was not found in any registered repositories", pkgName, @@ -301,7 +297,7 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } return filteredPackages.Values.ToArray(); @@ -309,7 +305,6 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable InstallPackage( IEnumerable pkgsToInstall, // those found to be required to be installed (includes Dependency packages as well) - // List pkgNamesToInstall, // those requested by the user to be installed string repoUrl, PSCredential credential, bool isLocalRepo) @@ -346,7 +341,7 @@ private List InstallPackage( string activity = ""; string statusDescription = ""; - if (_pckgNamesToInstall.Contains(pkg.Name, StringComparer.InvariantCultureIgnoreCase)) + if (_pkgNamesToInstall.Contains(pkg.Name, StringComparer.InvariantCultureIgnoreCase)) { // Installing parent package (one whose name was passed in to install) activityId = 0; @@ -380,7 +375,7 @@ private List InstallPackage( // i.e can't do Install-PSResource ValidPkg -Version "2-0-0-0","2.0.0.0" _cmdletPassedIn.WriteVerbose(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion", pkg.Name, pkg.Version.ToString())); - _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } _cmdletPassedIn.WriteVerbose("Parsed NuGet version is: " + pkgVersion.ToFullString()); @@ -513,7 +508,7 @@ private List InstallPackage( var ex = new ArgumentException(message); var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); _cmdletPassedIn.WriteError(psdataFileDoesNotExistError); - _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } @@ -570,7 +565,7 @@ private List InstallPackage( "InstallPackageFailed", ErrorCategory.InvalidOperation, _cmdletPassedIn)); - _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } finally { @@ -634,7 +629,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var acceptLicenseError = new ErrorRecord(ex, "LicenseTxtNotFound", ErrorCategory.ObjectNotFound, null); _cmdletPassedIn.WriteError(acceptLicenseError); - _pckgNamesToInstall.RemoveAll(x => x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)); success = false; } @@ -662,7 +657,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t var acceptLicenseError = new ErrorRecord(ex, "ForceAcceptLicense", ErrorCategory.InvalidArgument, null); _cmdletPassedIn.WriteError(acceptLicenseError); - _pckgNamesToInstall.RemoveAll(x => x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase)); success = false; } } @@ -714,7 +709,7 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable) var noClobberError = new ErrorRecord(ex, "CommandAlreadyExists", ErrorCategory.ResourceExists, null); _cmdletPassedIn.WriteError(noClobberError); - _pckgNamesToInstall.RemoveAll(x => x.Equals(pkgName, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkgName, StringComparison.InvariantCultureIgnoreCase)); foundClobber = true; return foundClobber; @@ -741,7 +736,7 @@ private void CreateMetadataXMLFile(string dirNameVersion, string installPath, PS var ex = new ArgumentException(message); var ErrorParsingMetadata = new ErrorRecord(ex, "ErrorParsingMetadata", ErrorCategory.ParserError, null); - _pckgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); _cmdletPassedIn.WriteError(ErrorParsingMetadata); } } diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index d03b40db6..c0a3bb51d 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -165,19 +165,6 @@ protected override void ProcessRecord() var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null); ThrowTerminatingError(IncorrectVersionFormat); } - // else if (!VersionRange.TryParse(Version, out _versionRange)) - // { - // var exMessage = "Argument for -Version parameter is not in the proper format."; - // var ex = new ArgumentException(exMessage); - // var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null); - // ThrowTerminatingError(IncorrectVersionFormat); - // } - - if (_versionRange == null) - { - WriteVerbose("versionRange null!"); - } - WriteVerbose("NuGet version range: " + _versionRange.ToString()); ProcessInstallHelper( pkgNames: Name, From 4ebf928ac66bfb6e6235b36e491a29992ac37bde Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 17:42:49 -0500 Subject: [PATCH 10/15] code clean up --- src/code/InstallHelper.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index aa392cf9b..f843dff3a 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -127,13 +127,11 @@ public List InstallPackages( } // Go through the repositories and see which is the first repository to have the pkg version available - List installedPkgs = ProcessRepositories( + return ProcessRepositories( repository: repository, trustRepository: _trustRepository, credential: _credential, skipDependencyCheck: skipDependencyCheck); - - return installedPkgs; } #endregion From ac54d7a84dc5debc8476b1248548c1a62a1ce4a2 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 18:12:44 -0500 Subject: [PATCH 11/15] change certain verbose message to error --- src/code/InstallHelper.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index f843dff3a..1b52c2392 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -221,7 +221,6 @@ private List ProcessRepositories( if (!pkgsFromRepoToInstall.Any()) { - _cmdletPassedIn.WriteVerbose("nothing to install left so return"); continue; } @@ -373,10 +372,12 @@ private List InstallPackage( if (!NuGetVersion.TryParse(createFullVersion, out NuGetVersion pkgVersion)) { - // TODO: should this be WriteError (cause once one bad version is specified that name isn't going to be searched/installed) - // i.e can't do Install-PSResource ValidPkg -Version "2-0-0-0","2.0.0.0" - _cmdletPassedIn.WriteVerbose(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion", - pkg.Name, pkg.Version.ToString())); + var message = String.Format("{0} package could not be installed with error: could not parse package '{0}' version '{1} into a NuGetVersion", + pkg.Name, + pkg.Version.ToString()); + var ex = new ArgumentException(message); + var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); + _cmdletPassedIn.WriteError(psdataFileDoesNotExistError); _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } From 3ed6affa59c086e2eca98aea70e4e2a268a56b77 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 18:15:40 -0500 Subject: [PATCH 12/15] rename error --- src/code/InstallHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 1b52c2392..24ccb8ac6 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -376,8 +376,8 @@ private List InstallPackage( pkg.Name, pkg.Version.ToString()); var ex = new ArgumentException(message); - var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); - _cmdletPassedIn.WriteError(psdataFileDoesNotExistError); + var packageIdentityVersionParseError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null); + _cmdletPassedIn.WriteError(packageIdentityVersionParseError); _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } From e02cede6aa9edb787b792570b56cd13adeaf0ca0 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 18:16:25 -0500 Subject: [PATCH 13/15] remove verbose message --- src/code/InstallHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 24ccb8ac6..0aab6af9f 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -381,7 +381,7 @@ private List InstallPackage( _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); continue; } - _cmdletPassedIn.WriteVerbose("Parsed NuGet version is: " + pkgVersion.ToFullString()); + var pkgIdentity = new PackageIdentity(pkg.Name, pkgVersion); var cacheContext = new SourceCacheContext(); From e1e5d65653089e83133698ee263759b8731e5817 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 3 Dec 2021 18:18:04 -0500 Subject: [PATCH 14/15] code clean up --- src/code/InstallHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 0aab6af9f..14b576b84 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -739,8 +739,8 @@ private void CreateMetadataXMLFile(string dirNameVersion, string installPath, PS var ex = new ArgumentException(message); var ErrorParsingMetadata = new ErrorRecord(ex, "ErrorParsingMetadata", ErrorCategory.ParserError, null); - _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); _cmdletPassedIn.WriteError(ErrorParsingMetadata); + _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } } From 8c32d8abea7e446a64027ff9c42055adfd6b7b98 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 6 Dec 2021 16:53:55 -0500 Subject: [PATCH 15/15] update comment --- src/code/InstallHelper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 14b576b84..7ed9d1916 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -238,6 +238,7 @@ private List ProcessRepositories( allPkgsInstalled.AddRange(pkgsInstalled); } + // At this only package names left were those which could not be found in registered repositories foreach (string pkgName in _pkgNamesToInstall) { var message = String.Format("Package '{0}' with requested version range {1} could not be installed as it was not found in any registered repositories",