Skip to content

Add -TemporaryPath parameter to Install-PSResource, Save-PSResource, and Update-PSResource #763

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 22, 2022
18 changes: 17 additions & 1 deletion help/Install-PSResource.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Installs resources from a registered repository.

```
Install-PSResource [-Name] <string[]> [-Version <string>] [-Prerelease] [-Repository <string[]>]
[-Credential <pscredential>] [-Scope <ScopeType>] [-TrustRepository] [-Reinstall] [-Quiet]
[-Credential <pscredential>] [-Scope <ScopeType>] [-TemporaryPath <string>] [-TrustRepository] [-Reinstall] [-Quiet]
[-AcceptLicense] [-NoClobber] [-SkipDependencyCheck] [-AuthenticodeCheck] [-PassThru] [-WhatIf]
[-Confirm] [<CommonParameters>]
```
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion help/Save-PSResource.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Saves resources (modules and scripts) from a registered repository onto the mach

```
Save-PSResource [-Name] <string[]> [-Version <string>] [-Prerelease] [-Repository <string[]>]
[-Credential <pscredential>] [-AsNupkg] [-IncludeXML] [-Path <string>] [-TrustRepository]
[-Credential <pscredential>] [-AsNupkg] [-IncludeXML] [-Path <string>] [-TemporaryPath <string>] [-TrustRepository]
[-PassThru] [-SkipDependencyCheck] [-AuthenticodeCheck] [-Quiet] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion help/Update-PSResource.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Downloads and installs the newest version of a package already installed on the

```
Update-PSResource [[-Name] <string[]>] [-Version <string>] [-Prerelease] [-Repository <string[]>]
[-Scope <ScopeType>] [-TrustRepository] [-Credential <pscredential>] [-Quiet] [-AcceptLicense]
[-Scope <ScopeType>] [-TemporaryPath <string>] [-TrustRepository] [-Credential <pscredential>] [-Quiet] [-AcceptLicense]
[-Force] [-PassThru] [-SkipDependencyCheck] [-AuthenticodeCheck] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class InstallHelper : PSCmdlet
private bool _savePkg;
List<string> _pathsToSearch;
List<string> _pkgNamesToInstall;
private string _tmpPath;

#endregion

Expand Down Expand Up @@ -82,10 +83,11 @@ public List<PSResourceInfo> InstallPackages(
bool skipDependencyCheck,
bool authenticodeCheck,
bool savePkg,
List<string> pathsToInstallPkg)
List<string> 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}'",
"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(),
Expand All @@ -97,7 +99,9 @@ public List<PSResourceInfo> InstallPackages(
noClobber.ToString(),
asNupkg.ToString(),
includeXml.ToString(),
savePkg.ToString()));
savePkg.ToString(),
tmpPath ?? string.Empty));


_versionRange = versionRange;
_prerelease = prerelease;
Expand All @@ -113,6 +117,7 @@ public List<PSResourceInfo> InstallPackages(
_includeXml = includeXml;
_savePkg = savePkg;
_pathsToInstallPkg = pathsToInstallPkg;
_tmpPath = tmpPath ?? Path.GetTempPath();

// Create list of installation paths to search.
_pathsToSearch = new List<string>();
Expand Down Expand Up @@ -327,7 +332,7 @@ private List<PSResourceInfo> 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
Expand Down
26 changes: 25 additions & 1 deletion src/code/InstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ class InstallPSResource : PSCmdlet
[Parameter]
public ScopeType Scope { get; set; }

/// <summary>
/// The destination where the resource is to be temporarily installed
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string TemporaryPath
{
get
{ return _tmpPath; }

set
{
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;

/// <summary>
/// Suppresses being prompted for untrusted sources.
/// </summary>
Expand Down Expand Up @@ -528,7 +551,8 @@ private void ProcessInstallHelper(string[] pkgNames, VersionRange pkgVersion, bo
skipDependencyCheck: SkipDependencyCheck,
authenticodeCheck: AuthenticodeCheck,
savePkg: false,
pathsToInstallPkg: _pathsToInstallPkg);
pathsToInstallPkg: _pathsToInstallPkg,
tmpPath: _tmpPath);

if (PassThru)
{
Expand Down
27 changes: 26 additions & 1 deletion src/code/SavePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ public string Path
}
private string _path;

/// <summary>
/// The destination where the resource is to be temporarily saved to.

/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string TemporaryPath
{
get
{ return _tmpPath; }

set
{
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;

/// <summary>
/// Suppresses being prompted for untrusted sources.
/// </summary>
Expand Down Expand Up @@ -266,7 +290,8 @@ private void ProcessSaveHelper(string[] pkgNames, bool pkgPrerelease, string[] p
skipDependencyCheck: SkipDependencyCheck,
authenticodeCheck: AuthenticodeCheck,
savePkg: true,
pathsToInstallPkg: new List<string> { _path });
pathsToInstallPkg: new List<string> { _path },
tmpPath: _tmpPath);

if (PassThru)
{
Expand Down
28 changes: 27 additions & 1 deletion src/code/UpdatePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,30 @@ public sealed class UpdatePSResource : PSCmdlet
[Parameter]
public ScopeType Scope { get; set; }

/// <summary>
/// The destination where the resource is to be temporarily installed to while updating.

/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string TemporaryPath
{
get
{ return _tmpPath; }

set
{
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;

/// <summary>
/// When specified, suppresses prompting for untrusted sources.
/// </summary>
Expand Down Expand Up @@ -187,7 +212,8 @@ protected override void ProcessRecord()
skipDependencyCheck: SkipDependencyCheck,
authenticodeCheck: AuthenticodeCheck,
savePkg: false,
pathsToInstallPkg: _pathsToInstallPkg);
pathsToInstallPkg: _pathsToInstallPkg,
tmpPath: _tmpPath);

if (PassThru)
{
Expand Down