From 38acaa1d22e63c0b029daad4602bfb171e690b41 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Mon, 15 Aug 2022 19:16:12 -0400 Subject: [PATCH 1/9] add temporary path parameter to save --- src/code/InstallHelper.cs | 5 ++++- src/code/SavePSResource.cs | 29 ++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 75f6d54f2..0db4da8c8 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -53,6 +53,7 @@ internal class InstallHelper : PSCmdlet private bool _savePkg; List _pathsToSearch; List _pkgNamesToInstall; + private string _tmpPath; #endregion @@ -82,7 +83,8 @@ public List InstallPackages( bool skipDependencyCheck, bool authenticodeCheck, bool savePkg, - List pathsToInstallPkg) + List pathsToInstallPkg, + string tmpPath) { _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}'", @@ -113,6 +115,7 @@ public List InstallPackages( _includeXml = includeXml; _savePkg = savePkg; _pathsToInstallPkg = pathsToInstallPkg; + _tmpPath = tmpPath; // Create list of installation paths to search. _pathsToSearch = new List(); diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 3f2b2ce26..db1cffced 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -109,6 +109,32 @@ public string Path } private string _path; + public string TemporaryPath + { + get + { return _tmpPath; } + + set + { + string resolvedPath; + if (string.IsNullOrEmpty(value)) + { + // If the user does not specify a path to save to, use the user's default temporary directory + resolvedPath = System.IO.Path.GetTempPath(); + } + else { + resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; + } + + // Path where resource is saved must be a directory + if (Directory.Exists(resolvedPath)) + { + _tmpPath = resolvedPath; + } + } + } + private string _tmpPath; + /// /// Suppresses being prompted for untrusted sources. /// @@ -266,7 +292,8 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p skipDependencyCheck: SkipDependencyCheck, authenticodeCheck: AuthenticodeCheck, savePkg: true, - pathsToInstallPkg: new List { _path }); + pathsToInstallPkg: new List { _path }, + tmpPath: _tmpPath); if (PassThru) { From a0370c36a72fa24528e8fd8938aa4bda02314b2d Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Tue, 16 Aug 2022 11:54:58 -0400 Subject: [PATCH 2/9] add TemporaryPath parameter to Install and Update --- src/code/InstallHelper.cs | 9 +++++---- src/code/InstallPSResource.cs | 28 +++++++++++++++++++++++++++- src/code/SavePSResource.cs | 23 +++++++++++------------ src/code/UpdatePSResource.cs | 29 ++++++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 0db4da8c8..b1ca33ee8 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -87,7 +87,7 @@ public List InstallPackages( string tmpPath) { _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}'", + "AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXml '{10}'; SavePackage '{11}'; TemporaryPath '{12}'", string.Join(",", names), versionRange != null ? (versionRange.OriginalString != null ? versionRange.OriginalString : string.Empty) : string.Empty, prerelease.ToString(), @@ -99,7 +99,8 @@ public List InstallPackages( noClobber.ToString(), asNupkg.ToString(), includeXml.ToString(), - savePkg.ToString())); + savePkg.ToString(), + tmpPath != null ? tmpPath.ToString() : string.Empty)); _versionRange = versionRange; _prerelease = prerelease; @@ -115,7 +116,7 @@ public List InstallPackages( _includeXml = includeXml; _savePkg = savePkg; _pathsToInstallPkg = pathsToInstallPkg; - _tmpPath = tmpPath; + _tmpPath = tmpPath ?? Path.GetTempPath(); // Create list of installation paths to search. _pathsToSearch = new List(); @@ -330,7 +331,7 @@ private List InstallPackage( foreach (PSResourceInfo pkg in pkgsToInstall) { currentInstalledPkgCount++; - var tempInstallPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + var tempInstallPath = Path.Combine(_tmpPath, Guid.NewGuid().ToString()); try { // Create a temp directory to install to diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 4149a2ba5..25746c606 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -67,6 +67,31 @@ class InstallPSResource : PSCmdlet [Parameter] public ScopeType Scope { get; set; } + /// + /// The destination where the resource is to be temporarily installed + /// + [Parameter] + public string TemporaryPath + { + get + { return _tmpPath; } + + set + { + if (!string.IsNullOrEmpty(value)) + { + string resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; + + // Path where resource is temporarily saved must be a directory + if (Directory.Exists(resolvedPath)) + { + _tmpPath = resolvedPath; + } + } + } + } + private string _tmpPath; + /// /// Suppresses being prompted for untrusted sources. /// @@ -528,7 +553,8 @@ private void ProcessInstallHelper(string[] pkgNames, VersionRange pkgVersion, bo skipDependencyCheck: SkipDependencyCheck, authenticodeCheck: AuthenticodeCheck, savePkg: false, - pathsToInstallPkg: _pathsToInstallPkg); + pathsToInstallPkg: _pathsToInstallPkg, + tmpPath: _tmpPath); if (PassThru) { diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index db1cffced..7b863614d 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -109,6 +109,10 @@ public string Path } private string _path; + /// + /// The destination where the resource is to be temporarily installed + /// + [Parameter] public string TemporaryPath { get @@ -116,20 +120,15 @@ public string TemporaryPath set { - string resolvedPath; - if (string.IsNullOrEmpty(value)) + if (!string.IsNullOrEmpty(value)) { - // If the user does not specify a path to save to, use the user's default temporary directory - resolvedPath = System.IO.Path.GetTempPath(); - } - else { - resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; - } + string resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; - // Path where resource is saved must be a directory - if (Directory.Exists(resolvedPath)) - { - _tmpPath = resolvedPath; + // Path where resource is temporarily saved must be a directory + if (Directory.Exists(resolvedPath)) + { + _tmpPath = resolvedPath; + } } } } diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index bce9ed295..327dab49f 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -5,6 +5,7 @@ using NuGet.Versioning; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Management.Automation; using System.Threading; @@ -68,6 +69,31 @@ public sealed class UpdatePSResource : PSCmdlet [Parameter] public ScopeType Scope { get; set; } + /// + /// The destination where the resource is to be temporarily installed + /// + [Parameter] + public string TemporaryPath + { + get + { return _tmpPath; } + + set + { + if (!string.IsNullOrEmpty(value)) + { + string resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; + + // Path where resource is temporarily saved must be a directory + if (Directory.Exists(resolvedPath)) + { + _tmpPath = resolvedPath; + } + } + } + } + private string _tmpPath; + /// /// When specified, suppresses prompting for untrusted sources. /// @@ -187,7 +213,8 @@ protected override void ProcessRecord() skipDependencyCheck: SkipDependencyCheck, authenticodeCheck: AuthenticodeCheck, savePkg: false, - pathsToInstallPkg: _pathsToInstallPkg); + pathsToInstallPkg: _pathsToInstallPkg, + tmpPath: _tmpPath); if (PassThru) { From 865969509c7c079286780cce77e21325afd295d6 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Tue, 16 Aug 2022 12:38:22 -0400 Subject: [PATCH 3/9] add parameter attribute --- src/code/InstallPSResource.cs | 2 ++ src/code/SavePSResource.cs | 1 + src/code/UpdatePSResource.cs | 1 + 3 files changed, 4 insertions(+) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 25746c606..1ff4bb836 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -65,12 +65,14 @@ class InstallPSResource : PSCmdlet /// Specifies the scope of installation. /// [Parameter] + [ValidateNotNullOrEmpty] public ScopeType Scope { get; set; } /// /// The destination where the resource is to be temporarily installed /// [Parameter] + [ValidateNotNullOrEmpty] public string TemporaryPath { get diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 7b863614d..de588af86 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -113,6 +113,7 @@ public string Path /// The destination where the resource is to be temporarily installed /// [Parameter] + [ValidateNotNullOrEmpty] public string TemporaryPath { get diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index 327dab49f..05c4b5def 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -73,6 +73,7 @@ public sealed class UpdatePSResource : PSCmdlet /// The destination where the resource is to be temporarily installed /// [Parameter] + [ValidateNotNullOrEmpty] public string TemporaryPath { get From 374af5dafe2861dc950623046bd3c57695d41b57 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Tue, 16 Aug 2022 13:55:15 -0400 Subject: [PATCH 4/9] revert unintendded change --- src/code/InstallPSResource.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index 1ff4bb836..d646a8c58 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -65,7 +65,6 @@ class InstallPSResource : PSCmdlet /// Specifies the scope of installation. /// [Parameter] - [ValidateNotNullOrEmpty] public ScopeType Scope { get; set; } /// From 5f2cf6cf764e04b0ad935781570f8ee3c8d07ecd Mon Sep 17 00:00:00 2001 From: Alyssa Vu <49544763+alyssa1303@users.noreply.github.com> Date: Wed, 17 Aug 2022 11:19:35 -0400 Subject: [PATCH 5/9] Change wording Co-authored-by: Anam Navied --- src/code/SavePSResource.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index de588af86..e0b6c19c7 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -110,7 +110,8 @@ public string Path private string _path; /// - /// The destination where the resource is to be temporarily installed + /// The destination where the resource is to be temporarily saved to. + /// [Parameter] [ValidateNotNullOrEmpty] From aadaed3180cfdd5d56792f0961f1a27f3c4efb74 Mon Sep 17 00:00:00 2001 From: Alyssa Vu <49544763+alyssa1303@users.noreply.github.com> Date: Wed, 17 Aug 2022 11:19:50 -0400 Subject: [PATCH 6/9] Change wording Co-authored-by: Anam Navied --- src/code/UpdatePSResource.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index 05c4b5def..27a62eaa1 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -70,7 +70,8 @@ public sealed class UpdatePSResource : PSCmdlet public ScopeType Scope { get; set; } /// - /// The destination where the resource is to be temporarily installed + /// The destination where the resource is to be temporarily installed to while updating. + /// [Parameter] [ValidateNotNullOrEmpty] From 01c791d41699e354e790c10687ed3af483dc3e65 Mon Sep 17 00:00:00 2001 From: Alyssa Vu <49544763+alyssa1303@users.noreply.github.com> Date: Wed, 17 Aug 2022 14:19:35 -0400 Subject: [PATCH 7/9] remove toString() Co-authored-by: Paul Higinbotham --- src/code/InstallHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index b1ca33ee8..dfa0c66cb 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -100,7 +100,8 @@ public List InstallPackages( asNupkg.ToString(), includeXml.ToString(), savePkg.ToString(), - tmpPath != null ? tmpPath.ToString() : string.Empty)); + tmpPath ?? string.Empty)); + _versionRange = versionRange; _prerelease = prerelease; From 74faf4a441e9133dd081cf3a0a485fd4ecad3aac Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Wed, 17 Aug 2022 18:25:51 -0400 Subject: [PATCH 8/9] throw error for wildcard usage and unresolved path --- src/code/InstallPSResource.cs | 17 +++++++---------- src/code/SavePSResource.cs | 17 +++++++---------- src/code/UpdatePSResource.cs | 17 +++++++---------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index d646a8c58..b27b48093 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -79,16 +79,13 @@ public string TemporaryPath set { - if (!string.IsNullOrEmpty(value)) - { - string resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; - - // Path where resource is temporarily saved must be a directory - if (Directory.Exists(resolvedPath)) - { - _tmpPath = resolvedPath; - } - } + if (WildcardPattern.ContainsWildcardCharacters(value)) + { + throw new PSArgumentException("Wildcard characters are not allowed in the temporary path."); + } + + // This will throw if path cannot be resolved + _tmpPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; } } private string _tmpPath; diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index e0b6c19c7..9c92a261f 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -122,16 +122,13 @@ public string TemporaryPath set { - if (!string.IsNullOrEmpty(value)) - { - string resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; - - // Path where resource is temporarily saved must be a directory - if (Directory.Exists(resolvedPath)) - { - _tmpPath = resolvedPath; - } - } + if (WildcardPattern.ContainsWildcardCharacters(value)) + { + throw new PSArgumentException("Wildcard characters are not allowed in the temporary path."); + } + + // This will throw if path cannot be resolved + _tmpPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; } } private string _tmpPath; diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index 27a62eaa1..c670c0afe 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -82,16 +82,13 @@ public string TemporaryPath set { - if (!string.IsNullOrEmpty(value)) - { - string resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; - - // Path where resource is temporarily saved must be a directory - if (Directory.Exists(resolvedPath)) - { - _tmpPath = resolvedPath; - } - } + if (WildcardPattern.ContainsWildcardCharacters(value)) + { + throw new PSArgumentException("Wildcard characters are not allowed in the temporary path."); + } + + // This will throw if path cannot be resolved + _tmpPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; } } private string _tmpPath; From 806622fef9c6b2aa54035c3f09697d0891f5e265 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Wed, 17 Aug 2022 18:45:08 -0400 Subject: [PATCH 9/9] update .md files to add a new parameter --- help/Install-PSResource.md | 18 +++++++++++++++++- help/Save-PSResource.md | 18 +++++++++++++++++- help/Update-PSResource.md | 18 +++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/help/Install-PSResource.md b/help/Install-PSResource.md index 28eb45d6f..eb12f1aae 100644 --- a/help/Install-PSResource.md +++ b/help/Install-PSResource.md @@ -17,7 +17,7 @@ Installs resources from a registered repository. ``` Install-PSResource [-Name] [-Version ] [-Prerelease] [-Repository ] - [-Credential ] [-Scope ] [-TrustRepository] [-Reinstall] [-Quiet] + [-Credential ] [-Scope ] [-TemporaryPath ] [-TrustRepository] [-Reinstall] [-Quiet] [-AcceptLicense] [-NoClobber] [-SkipDependencyCheck] [-AuthenticodeCheck] [-PassThru] [-WhatIf] [-Confirm] [] ``` @@ -224,6 +224,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -TemporaryPath + +Specifies the path to temporarily install the resource before actual installation. If no temporary path is provided, the resource is temporarily installed in the current user's temporary folder. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Prerelease When specified, includes prerelease versions in search results returned. diff --git a/help/Save-PSResource.md b/help/Save-PSResource.md index 2029fb6d4..e98cf87b3 100644 --- a/help/Save-PSResource.md +++ b/help/Save-PSResource.md @@ -17,7 +17,7 @@ Saves resources (modules and scripts) from a registered repository onto the mach ``` Save-PSResource [-Name] [-Version ] [-Prerelease] [-Repository ] - [-Credential ] [-AsNupkg] [-IncludeXML] [-Path ] [-TrustRepository] + [-Credential ] [-AsNupkg] [-IncludeXML] [-Path ] [-TemporaryPath ] [-TrustRepository] [-PassThru] [-SkipDependencyCheck] [-AuthenticodeCheck] [-Quiet] [-WhatIf] [-Confirm] [] ``` @@ -207,6 +207,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -TemporaryPath + +Specifies the path to temporarily install the resource before saving. If no temporary path is provided, the resource is temporarily installed in the current user's temporary folder. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Prerelease When specified, includes prerelease versions in search results returned. diff --git a/help/Update-PSResource.md b/help/Update-PSResource.md index bdfa732ad..d527d3611 100644 --- a/help/Update-PSResource.md +++ b/help/Update-PSResource.md @@ -17,7 +17,7 @@ Downloads and installs the newest version of a package already installed on the ``` Update-PSResource [[-Name] ] [-Version ] [-Prerelease] [-Repository ] - [-Scope ] [-TrustRepository] [-Credential ] [-Quiet] [-AcceptLicense] + [-Scope ] [-TemporaryPath ] [-TrustRepository] [-Credential ] [-Quiet] [-AcceptLicense] [-Force] [-PassThru] [-SkipDependencyCheck] [-AuthenticodeCheck] [-WhatIf] [-Confirm] [] ``` @@ -162,6 +162,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -TemporaryPath + +Specifies the path to temporarily install the resource before actual installatoin. If no temporary path is provided, the resource is temporarily installed in the current user's temporary folder. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Prerelease When specified, allows updating to a prerelease version.