Skip to content

Commit e799a53

Browse files
authored
Merge pull request #763 from alyssa1303/temp-path-param
Add -TemporaryPath parameter to Install-PSResource, Save-PSResource, and Update-PSResource
2 parents e2ae648 + 806622f commit e799a53

File tree

7 files changed

+138
-10
lines changed

7 files changed

+138
-10
lines changed

help/Install-PSResource.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Installs resources from a registered repository.
1717

1818
```
1919
Install-PSResource [-Name] <string[]> [-Version <string>] [-Prerelease] [-Repository <string[]>]
20-
[-Credential <pscredential>] [-Scope <ScopeType>] [-TrustRepository] [-Reinstall] [-Quiet]
20+
[-Credential <pscredential>] [-Scope <ScopeType>] [-TemporaryPath <string>] [-TrustRepository] [-Reinstall] [-Quiet]
2121
[-AcceptLicense] [-NoClobber] [-SkipDependencyCheck] [-AuthenticodeCheck] [-PassThru] [-WhatIf]
2222
[-Confirm] [<CommonParameters>]
2323
```
@@ -224,6 +224,22 @@ Accept pipeline input: False
224224
Accept wildcard characters: False
225225
```
226226
227+
### -TemporaryPath
228+
229+
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.
230+
231+
```yaml
232+
Type: System.String
233+
Parameter Sets: (All)
234+
Aliases:
235+
236+
Required: False
237+
Position: Named
238+
Default value: None
239+
Accept pipeline input: False
240+
Accept wildcard characters: False
241+
```
242+
227243
### -Prerelease
228244
229245
When specified, includes prerelease versions in search results returned.

help/Save-PSResource.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Saves resources (modules and scripts) from a registered repository onto the mach
1717

1818
```
1919
Save-PSResource [-Name] <string[]> [-Version <string>] [-Prerelease] [-Repository <string[]>]
20-
[-Credential <pscredential>] [-AsNupkg] [-IncludeXML] [-Path <string>] [-TrustRepository]
20+
[-Credential <pscredential>] [-AsNupkg] [-IncludeXML] [-Path <string>] [-TemporaryPath <string>] [-TrustRepository]
2121
[-PassThru] [-SkipDependencyCheck] [-AuthenticodeCheck] [-Quiet] [-WhatIf] [-Confirm]
2222
[<CommonParameters>]
2323
```
@@ -207,6 +207,22 @@ Accept pipeline input: False
207207
Accept wildcard characters: False
208208
```
209209
210+
### -TemporaryPath
211+
212+
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.
213+
214+
```yaml
215+
Type: System.String
216+
Parameter Sets: (All)
217+
Aliases:
218+
219+
Required: False
220+
Position: Named
221+
Default value: None
222+
Accept pipeline input: False
223+
Accept wildcard characters: False
224+
```
225+
210226
### -Prerelease
211227
212228
When specified, includes prerelease versions in search results returned.

help/Update-PSResource.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Downloads and installs the newest version of a package already installed on the
1717

1818
```
1919
Update-PSResource [[-Name] <string[]>] [-Version <string>] [-Prerelease] [-Repository <string[]>]
20-
[-Scope <ScopeType>] [-TrustRepository] [-Credential <pscredential>] [-Quiet] [-AcceptLicense]
20+
[-Scope <ScopeType>] [-TemporaryPath <string>] [-TrustRepository] [-Credential <pscredential>] [-Quiet] [-AcceptLicense]
2121
[-Force] [-PassThru] [-SkipDependencyCheck] [-AuthenticodeCheck] [-WhatIf] [-Confirm]
2222
[<CommonParameters>]
2323
```
@@ -162,6 +162,22 @@ Accept pipeline input: False
162162
Accept wildcard characters: False
163163
```
164164

165+
### -TemporaryPath
166+
167+
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.
168+
169+
```yaml
170+
Type: System.String
171+
Parameter Sets: (All)
172+
Aliases:
173+
174+
Required: False
175+
Position: Named
176+
Default value: None
177+
Accept pipeline input: False
178+
Accept wildcard characters: False
179+
```
180+
165181
### -Prerelease
166182

167183
When specified, allows updating to a prerelease version.

src/code/InstallHelper.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ internal class InstallHelper : PSCmdlet
5353
private bool _savePkg;
5454
List<string> _pathsToSearch;
5555
List<string> _pkgNamesToInstall;
56+
private string _tmpPath;
5657

5758
#endregion
5859

@@ -82,10 +83,11 @@ public List<PSResourceInfo> InstallPackages(
8283
bool skipDependencyCheck,
8384
bool authenticodeCheck,
8485
bool savePkg,
85-
List<string> pathsToInstallPkg)
86+
List<string> pathsToInstallPkg,
87+
string tmpPath)
8688
{
8789
_cmdletPassedIn.WriteVerbose(string.Format("Parameters passed in >>> Name: '{0}'; Version: '{1}'; Prerelease: '{2}'; Repository: '{3}'; " +
88-
"AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXml '{10}'; SavePackage '{11}'",
90+
"AcceptLicense: '{4}'; Quiet: '{5}'; Reinstall: '{6}'; TrustRepository: '{7}'; NoClobber: '{8}'; AsNupkg: '{9}'; IncludeXml '{10}'; SavePackage '{11}'; TemporaryPath '{12}'",
8991
string.Join(",", names),
9092
versionRange != null ? (versionRange.OriginalString != null ? versionRange.OriginalString : string.Empty) : string.Empty,
9193
prerelease.ToString(),
@@ -97,7 +99,9 @@ public List<PSResourceInfo> InstallPackages(
9799
noClobber.ToString(),
98100
asNupkg.ToString(),
99101
includeXml.ToString(),
100-
savePkg.ToString()));
102+
savePkg.ToString(),
103+
tmpPath ?? string.Empty));
104+
101105

102106
_versionRange = versionRange;
103107
_prerelease = prerelease;
@@ -113,6 +117,7 @@ public List<PSResourceInfo> InstallPackages(
113117
_includeXml = includeXml;
114118
_savePkg = savePkg;
115119
_pathsToInstallPkg = pathsToInstallPkg;
120+
_tmpPath = tmpPath ?? Path.GetTempPath();
116121

117122
// Create list of installation paths to search.
118123
_pathsToSearch = new List<string>();
@@ -327,7 +332,7 @@ private List<PSResourceInfo> InstallPackage(
327332
foreach (PSResourceInfo pkg in pkgsToInstall)
328333
{
329334
currentInstalledPkgCount++;
330-
var tempInstallPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
335+
var tempInstallPath = Path.Combine(_tmpPath, Guid.NewGuid().ToString());
331336
try
332337
{
333338
// Create a temp directory to install to

src/code/InstallPSResource.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ class InstallPSResource : PSCmdlet
6767
[Parameter]
6868
public ScopeType Scope { get; set; }
6969

70+
/// <summary>
71+
/// The destination where the resource is to be temporarily installed
72+
/// </summary>
73+
[Parameter]
74+
[ValidateNotNullOrEmpty]
75+
public string TemporaryPath
76+
{
77+
get
78+
{ return _tmpPath; }
79+
80+
set
81+
{
82+
if (WildcardPattern.ContainsWildcardCharacters(value))
83+
{
84+
throw new PSArgumentException("Wildcard characters are not allowed in the temporary path.");
85+
}
86+
87+
// This will throw if path cannot be resolved
88+
_tmpPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
89+
}
90+
}
91+
private string _tmpPath;
92+
7093
/// <summary>
7194
/// Suppresses being prompted for untrusted sources.
7295
/// </summary>
@@ -528,7 +551,8 @@ private void ProcessInstallHelper(string[] pkgNames, VersionRange pkgVersion, bo
528551
skipDependencyCheck: SkipDependencyCheck,
529552
authenticodeCheck: AuthenticodeCheck,
530553
savePkg: false,
531-
pathsToInstallPkg: _pathsToInstallPkg);
554+
pathsToInstallPkg: _pathsToInstallPkg,
555+
tmpPath: _tmpPath);
532556

533557
if (PassThru)
534558
{

src/code/SavePSResource.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ public string Path
109109
}
110110
private string _path;
111111

112+
/// <summary>
113+
/// The destination where the resource is to be temporarily saved to.
114+
115+
/// </summary>
116+
[Parameter]
117+
[ValidateNotNullOrEmpty]
118+
public string TemporaryPath
119+
{
120+
get
121+
{ return _tmpPath; }
122+
123+
set
124+
{
125+
if (WildcardPattern.ContainsWildcardCharacters(value))
126+
{
127+
throw new PSArgumentException("Wildcard characters are not allowed in the temporary path.");
128+
}
129+
130+
// This will throw if path cannot be resolved
131+
_tmpPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
132+
}
133+
}
134+
private string _tmpPath;
135+
112136
/// <summary>
113137
/// Suppresses being prompted for untrusted sources.
114138
/// </summary>
@@ -266,7 +290,8 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p
266290
skipDependencyCheck: SkipDependencyCheck,
267291
authenticodeCheck: AuthenticodeCheck,
268292
savePkg: true,
269-
pathsToInstallPkg: new List<string> { _path });
293+
pathsToInstallPkg: new List<string> { _path },
294+
tmpPath: _tmpPath);
270295

271296
if (PassThru)
272297
{

src/code/UpdatePSResource.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NuGet.Versioning;
66
using System;
77
using System.Collections.Generic;
8+
using System.IO;
89
using System.Linq;
910
using System.Management.Automation;
1011
using System.Threading;
@@ -68,6 +69,30 @@ public sealed class UpdatePSResource : PSCmdlet
6869
[Parameter]
6970
public ScopeType Scope { get; set; }
7071

72+
/// <summary>
73+
/// The destination where the resource is to be temporarily installed to while updating.
74+
75+
/// </summary>
76+
[Parameter]
77+
[ValidateNotNullOrEmpty]
78+
public string TemporaryPath
79+
{
80+
get
81+
{ return _tmpPath; }
82+
83+
set
84+
{
85+
if (WildcardPattern.ContainsWildcardCharacters(value))
86+
{
87+
throw new PSArgumentException("Wildcard characters are not allowed in the temporary path.");
88+
}
89+
90+
// This will throw if path cannot be resolved
91+
_tmpPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
92+
}
93+
}
94+
private string _tmpPath;
95+
7196
/// <summary>
7297
/// When specified, suppresses prompting for untrusted sources.
7398
/// </summary>
@@ -187,7 +212,8 @@ protected override void ProcessRecord()
187212
skipDependencyCheck: SkipDependencyCheck,
188213
authenticodeCheck: AuthenticodeCheck,
189214
savePkg: false,
190-
pathsToInstallPkg: _pathsToInstallPkg);
215+
pathsToInstallPkg: _pathsToInstallPkg,
216+
tmpPath: _tmpPath);
191217

192218
if (PassThru)
193219
{

0 commit comments

Comments
 (0)