From 163668619f2ed64a1c7d9eac057d375760e96eef Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Mon, 8 Nov 2021 23:20:08 -0800 Subject: [PATCH 1/8] Add -AsNupkg and -IncludeXML params and test --- src/code/InstallHelper.cs | 64 +++++++++++++++++++---------------- src/code/SavePSResource.cs | 8 ++--- test/SavePSResource.Tests.ps1 | 24 +++++++++++++ 3 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index d7b74640c..e83f07c73 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -82,7 +82,7 @@ public void InstallPackages( 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}';", + "AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXML '{10}'", string.Join(",", names), versionRange != null ? versionRange.OriginalString : string.Empty, prerelease.ToString(), @@ -91,7 +91,9 @@ public void InstallPackages( quiet.ToString(), reinstall.ToString(), trustRepository.ToString(), - noClobber.ToString())); + noClobber.ToString(), + asNupkg.ToString(), + includeXML.ToString())); _versionRange = versionRange; _prerelease = prerelease; @@ -332,15 +334,7 @@ private List InstallPackage( globalPackagesFolder: tempInstallPath, logger: NullLogger.Instance, token: _cancellationToken).GetAwaiter().GetResult(); - - if (_asNupkg) // this is Save functionality - { - DirectoryInfo nupkgPath = new DirectoryInfo(((System.IO.FileStream)result.PackageStream).Name); - File.Copy(nupkgPath.FullName, Path.Combine(tempInstallPath, pkgIdentity.Id + pkgIdentity.Version + ".nupkg")); - - continue; - } - + // Create the package extraction context PackageExtractionContext packageExtractionContext = new PackageExtractionContext( packageSaveMode: PackageSaveMode.Nupkg, @@ -395,10 +389,8 @@ private List InstallPackage( _cmdletPassedIn.WriteVerbose(string.Format("Successfully able to download package from source to: '{0}'", tempInstallPath)); - // Prompt if module requires license acceptance (need to read info license acceptance info from the module manifest) // pkgIdentity.Version.Version gets the version without metadata or release labels. string newVersion = pkgIdentity.Version.ToNormalizedString(); - string normalizedVersionNoPrereleaseLabel = newVersion; if (pkgIdentity.Version.IsPrerelease) { @@ -414,6 +406,35 @@ private List InstallPackage( // Check if the package is a module or a script var isModule = File.Exists(modulePath); + string installPath; + if (_savePkg) + { + // For save the installation path is what is passed in via -Path + installPath = _pathsToInstallPkg.FirstOrDefault(); + + // If saving as nupkg simply copy the nupkg and move onto next iteration of loop + // asNupkg functionality only applies to Save-PSResource + if (_asNupkg) + { + var nupkgFile = pkgIdentity + ".nupkg"; + File.Copy(Path.Combine(tempDirNameVersion, nupkgFile), Path.Combine(installPath, nupkgFile)); + + _cmdletPassedIn.WriteVerbose(string.Format("'{0}' moved into file path '{1}'", nupkgFile, installPath)); + pkgsSuccessfullyInstalled.Add(p.Name); + + continue; + } + } + else + { + // PSModules: + /// ./Modules + /// ./Scripts + /// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list + installPath = isModule ? _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase)) + : _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase)); + } + if (isModule) { var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1"); @@ -451,22 +472,7 @@ private List InstallPackage( // Delete the extra nupkg related files that are not needed and not part of the module/script DeleteExtraneousFiles(tempInstallPath, pkgIdentity, tempDirNameVersion); - string installPath; - if (_savePkg) - { - // For save the installation path is what is passed in via -Path - installPath = _pathsToInstallPkg.FirstOrDefault(); - } - else { - // PSModules: - /// ./Modules - /// ./Scripts - /// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list - installPath = isModule ? _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase)) - : _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase)); - } - - if (_includeXML) + if (_includeXML && isModule) { CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isModule); } diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 9c8b292f7..86131f972 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -67,9 +67,8 @@ public sealed class SavePSResource : PSCmdlet [Parameter(ParameterSetName = InputObjectParameterSet)] public PSCredential Credential { get; set; } - /* /// - /// Saves as a .nupkg + /// Saves the resource as a .nupkg /// [Parameter()] public SwitchParameter AsNupkg { get; set; } @@ -79,7 +78,6 @@ public sealed class SavePSResource : PSCmdlet /// [Parameter()] public SwitchParameter IncludeXML { get; set; } - */ /// /// The destination where the resource is to be installed. Works for all resource types. @@ -242,8 +240,8 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p credential: Credential, noClobber: false, specifiedPath: _path, - asNupkg: false, - includeXML: false, + asNupkg: AsNupkg, + includeXML: IncludeXML, pathsToInstallPkg: new List { _path } ); } diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index 910597f7e..612cc08d6 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -188,6 +188,30 @@ Describe 'Test Save-PSResource for PSResources' { (Get-ChildItem -Path $pkgDir.FullName).Count | Should -Be 1 } + It "Save module as a nupkg" { + Save-PSResource -Name "TestModule" -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -AsNupkg + write-host $SaveDir + write-host + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "testmodule.1.3.0.nupkg" + $pkgDir | Should -Not -BeNullOrEmpty + } + + It "Save script as a nupkg" { + Save-PSResource -Name "TestTestScript" -Version "1.3.1" -Repository $TestGalleryName -Path $SaveDir -AsNupkg + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "testtestscript.1.3.1.nupkg" + $pkgDir | Should -Not -BeNullOrEmpty + } + + It "Save module and include XML metadata file" { + Save-PSResource -Name "TestModule" -Version "1.3.0" -Repository $TestGalleryName -Path $SaveDir -IncludeXML + $pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "TestModule" + $pkgDir | Should -Not -BeNullOrEmpty + $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName + $pkgDirVersion.Name | Should -Be "1.3.0" + $xmlFile = Get-ChildItem -Path $pkgDirVersion | Where-Object Name -eq "PSGetModuleInfo.xml" + $xmlFile | Should -Not -BeNullOrEmpty + } + <# # Tests should not write to module directory It "Save specific module resource by name if no -Path param is specifed" { From d73c6979e8507b641c5be6dd20a15daecdda34ac Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 9 Nov 2021 09:07:58 -0800 Subject: [PATCH 2/8] Fix bug with script xml --- 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 e83f07c73..dfb54c4d4 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -472,7 +472,7 @@ private List InstallPackage( // Delete the extra nupkg related files that are not needed and not part of the module/script DeleteExtraneousFiles(tempInstallPath, pkgIdentity, tempDirNameVersion); - if (_includeXML && isModule) + if (_includeXML) { CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isModule); } From 2d78a3cb839fbdc9adb6a6444c4ff44ef58139ff Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 9 Nov 2021 12:12:30 -0800 Subject: [PATCH 3/8] Update save test --- test/SavePSResource.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index 612cc08d6..351d0481e 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -208,7 +208,7 @@ Describe 'Test Save-PSResource for PSResources' { $pkgDir | Should -Not -BeNullOrEmpty $pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName $pkgDirVersion.Name | Should -Be "1.3.0" - $xmlFile = Get-ChildItem -Path $pkgDirVersion | Where-Object Name -eq "PSGetModuleInfo.xml" + $xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -eq "PSGetModuleInfo.xml" $xmlFile | Should -Not -BeNullOrEmpty } From 266d6ffc00f8463a77d5c5460764e443c00f4b4c Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 9 Nov 2021 12:34:58 -0800 Subject: [PATCH 4/8] Change nupkg file name to lower for linux search --- 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 dfb54c4d4..f566970b7 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -416,7 +416,7 @@ private List InstallPackage( // asNupkg functionality only applies to Save-PSResource if (_asNupkg) { - var nupkgFile = pkgIdentity + ".nupkg"; + var nupkgFile = pkgIdentity.ToString().ToLower() + ".nupkg"; File.Copy(Path.Combine(tempDirNameVersion, nupkgFile), Path.Combine(installPath, nupkgFile)); _cmdletPassedIn.WriteVerbose(string.Format("'{0}' moved into file path '{1}'", nupkgFile, installPath)); From 5f1c56d8c2dbbafd5d60f36fcb22fcf61f1ac478 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Mon, 29 Nov 2021 21:39:12 -0800 Subject: [PATCH 5/8] Incorporate code review suggestions --- src/code/InstallHelper.cs | 22 ++++++++++++---------- src/code/InstallPSResource.cs | 3 ++- src/code/SavePSResource.cs | 3 ++- src/code/UpdatePSResource.cs | 5 ++--- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 166884e19..a1c85043e 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -35,7 +35,6 @@ internal class InstallHelper : PSCmdlet private const string MsgInstallUntrustedPackage = "You are installing the modules from an untrusted repository. If you trust this repository, change its Trusted value by running the Set-PSResourceRepository cmdlet. Are you sure you want to install the PSresource from '{0}' ?"; private CancellationToken _cancellationToken; - private readonly bool _savePkg; private readonly PSCmdlet _cmdletPassedIn; private List _pathsToInstallPkg; private VersionRange _versionRange; @@ -49,18 +48,18 @@ internal class InstallHelper : PSCmdlet private string _specifiedPath; private bool _asNupkg; private bool _includeXML; - private bool _noClobber; + private bool _noClobber; + private bool _savePkg; List _pathsToSearch; #endregion #region Public methods - public InstallHelper(bool savePkg, PSCmdlet cmdletPassedIn) + public InstallHelper(PSCmdlet cmdletPassedIn) { CancellationTokenSource source = new CancellationTokenSource(); _cancellationToken = source.Token; - _savePkg = savePkg; _cmdletPassedIn = cmdletPassedIn; } @@ -80,10 +79,11 @@ public void InstallPackages( bool asNupkg, bool includeXML, bool skipDependencyCheck, + bool savePkg, 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}'", + "AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXML '{10}'; SavePackage '{11}'", string.Join(",", names), versionRange != null ? versionRange.OriginalString : string.Empty, prerelease.ToString(), @@ -94,7 +94,8 @@ public void InstallPackages( trustRepository.ToString(), noClobber.ToString(), asNupkg.ToString(), - includeXML.ToString())); + includeXML.ToString(), + savePkg.ToString())); _versionRange = versionRange; _prerelease = prerelease; @@ -108,6 +109,7 @@ public void InstallPackages( _specifiedPath = specifiedPath; _asNupkg = asNupkg; _includeXML = includeXML; + _savePkg = savePkg; _pathsToInstallPkg = pathsToInstallPkg; // Create list of installation paths to search. @@ -420,10 +422,10 @@ private List InstallPackage( if (_savePkg) { // For save the installation path is what is passed in via -Path - installPath = _pathsToInstallPkg.FirstOrDefault(); - - // If saving as nupkg simply copy the nupkg and move onto next iteration of loop - // asNupkg functionality only applies to Save-PSResource + installPath = _pathsToInstallPkg.FirstOrDefault(); + + // If saving as nupkg simply copy the nupkg and move onto next iteration of loop + // asNupkg functionality only applies to Save-PSResource if (_asNupkg) { var nupkgFile = pkgIdentity.ToString().ToLower() + ".nupkg"; diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 71a529901..7dcfbb835 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -136,7 +136,7 @@ protected override void BeginProcessing() _pathsToInstallPkg = Utils.GetAllInstallationPaths(this, Scope); - _installHelper = new InstallHelper(savePkg: false, cmdletPassedIn: this); + _installHelper = new InstallHelper(cmdletPassedIn: this); } protected override void ProcessRecord() @@ -257,6 +257,7 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ asNupkg: false, includeXML: true, skipDependencyCheck: SkipDependencyCheck, + savePkg: false, pathsToInstallPkg: _pathsToInstallPkg); } diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 32664a678..7a77bed42 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -144,7 +144,7 @@ protected override void BeginProcessing() _path = SessionState.Path.CurrentLocation.Path; } - _installHelper = new InstallHelper(savePkg: true, cmdletPassedIn: this); + _installHelper = new InstallHelper(cmdletPassedIn: this); } protected override void ProcessRecord() @@ -250,6 +250,7 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p asNupkg: AsNupkg, includeXML: IncludeXML, skipDependencyCheck: SkipDependencyCheck, + savePkg: false, pathsToInstallPkg: new List { _path } ); } diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index ba4bec2b6..d3d98513e 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -121,9 +121,7 @@ protected override void BeginProcessing() cancellationToken: _cancellationTokenSource.Token, cmdletPassedIn: this); - _installHelper = new InstallHelper( - savePkg: false, - cmdletPassedIn: this); + _installHelper = new InstallHelper(cmdletPassedIn: this); } protected override void ProcessRecord() @@ -174,6 +172,7 @@ protected override void ProcessRecord() asNupkg: false, includeXML: true, skipDependencyCheck: SkipDependencyCheck, + savePkg: false, pathsToInstallPkg: _pathsToInstallPkg); } From 4d8178268a203554b68acbc5b508588cf48c96a5 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 30 Nov 2021 09:14:42 -0800 Subject: [PATCH 6/8] Fix variable name for pkgInfo --- 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 a1c85043e..7f121f014 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -432,7 +432,7 @@ private List InstallPackage( File.Copy(Path.Combine(tempDirNameVersion, nupkgFile), Path.Combine(installPath, nupkgFile)); _cmdletPassedIn.WriteVerbose(string.Format("'{0}' moved into file path '{1}'", nupkgFile, installPath)); - pkgsSuccessfullyInstalled.Add(p.Name); + pkgsSuccessfullyInstalled.Add(pkgInfo.Name); continue; } From a5f31bac56323c79e9afd9133ba556edb5d48329 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 30 Nov 2021 13:47:28 -0800 Subject: [PATCH 7/8] Update 'savePkg' to true when calling from SavePSResource --- src/code/InstallHelper.cs | 2 +- src/code/SavePSResource.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 7f121f014..f3dde9286 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -85,7 +85,7 @@ public void InstallPackages( _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}'", string.Join(",", names), - versionRange != null ? versionRange.OriginalString : string.Empty, + versionRange != null ? (versionRange.OriginalString != null ? versionRange.OriginalString : string.Empty) : string.Empty, prerelease.ToString(), repository != null ? string.Join(",", repository) : string.Empty, acceptLicense.ToString(), diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 7a77bed42..75639ecbf 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -250,7 +250,7 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p asNupkg: AsNupkg, includeXML: IncludeXML, skipDependencyCheck: SkipDependencyCheck, - savePkg: false, + savePkg: true, pathsToInstallPkg: new List { _path } ); } From bb41b93a56cca3860b66d07e095998888d48e058 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 1 Dec 2021 11:43:22 -0800 Subject: [PATCH 8/8] Removed unused specifiedPath variable --- src/code/InstallHelper.cs | 3 --- src/code/InstallPSResource.cs | 1 - src/code/SavePSResource.cs | 1 - src/code/UpdatePSResource.cs | 1 - 4 files changed, 6 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index bb4be6bdb..8f5d45d8e 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -45,7 +45,6 @@ internal class InstallHelper : PSCmdlet private bool _force; private bool _trustRepository; private PSCredential _credential; - private string _specifiedPath; private bool _asNupkg; private bool _includeXML; private bool _noClobber; @@ -75,7 +74,6 @@ public List InstallPackages( bool trustRepository, bool noClobber, PSCredential credential, - string specifiedPath, bool asNupkg, bool includeXML, bool skipDependencyCheck, @@ -106,7 +104,6 @@ public List InstallPackages( _trustRepository = trustRepository || force; _noClobber = noClobber; _credential = credential; - _specifiedPath = specifiedPath; _asNupkg = asNupkg; _includeXML = includeXML; _savePkg = savePkg; diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 61e16dda8..f2fc2e8ca 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -260,7 +260,6 @@ private void ProcessInstallHelper(string[] pkgNames, bool pkgPrerelease, string[ trustRepository: TrustRepository, noClobber: NoClobber, credential: Credential, - specifiedPath: null, asNupkg: false, includeXML: true, skipDependencyCheck: SkipDependencyCheck, diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 662d14fdd..e9c456cd7 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -252,7 +252,6 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p trustRepository: TrustRepository, credential: Credential, noClobber: false, - specifiedPath: _path, asNupkg: AsNupkg, includeXML: IncludeXML, skipDependencyCheck: SkipDependencyCheck, diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index 88e35e8c6..dc947e084 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -174,7 +174,6 @@ protected override void ProcessRecord() trustRepository: TrustRepository, credential: Credential, noClobber: false, - specifiedPath: null, asNupkg: false, includeXML: true, skipDependencyCheck: SkipDependencyCheck,