Skip to content

Commit 4fdef60

Browse files
authored
Implement -DestinationPath parameter for Publish-PSResource (#506)
1 parent ab834e1 commit 4fdef60

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

src/code/PublishPSResource.cs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public sealed class PublishPSResource : PSCmdlet
4848
/// Specifies the path to the resource that you want to publish. This parameter accepts the path to the folder that contains the resource.
4949
/// Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
5050
/// </summary>
51-
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "PathParameterSet")]
51+
[Parameter()]
5252
[ValidateNotNullOrEmpty]
5353
public string Path
5454
{
@@ -74,32 +74,47 @@ public string Path
7474
}
7575
}
7676
private string _path;
77-
77+
7878
/// <summary>
79-
/// Specifies a path to one or more locations. Unlike the Path parameter, the value of the LiteralPath parameter is used exactly as entered.
80-
/// No characters are interpreted as wildcards. If the path includes escape characters, enclose them in single quotation marks.
81-
/// Single quotation marks tell PowerShell not to interpret any characters as escape sequences.
79+
/// Specifies the path to where the resource (as a nupkg) should be saved to. This parameter can be used in conjunction with the
80+
/// -Repository parameter to publish to a repository and also save the exact same package to the local file system.
8281
/// </summary>
83-
[Parameter(Mandatory = true, ParameterSetName = "PathLiteralParameterSet")]
82+
[Parameter()]
8483
[ValidateNotNullOrEmpty]
85-
public string LiteralPath
84+
public string DestinationPath
8685
{
8786
get
88-
{ return _literalPath; }
87+
{ return _destinationPath; }
8988

9089
set
9190
{
92-
if (Directory.Exists(value))
91+
string resolvedPath = string.Empty;
92+
if (!string.IsNullOrEmpty(value))
9393
{
94-
_literalPath = value;
94+
resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path;
9595
}
96-
else if (File.Exists(value) && value.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
96+
97+
if (Directory.Exists(resolvedPath))
9798
{
98-
_literalPath = value;
99+
_destinationPath = resolvedPath;
100+
}
101+
else {
102+
// try to create the path
103+
try
104+
{
105+
Directory.CreateDirectory(value);
106+
}
107+
catch (Exception e)
108+
{
109+
var exMessage = string.Format("Destination path does not exist and cannot be created: {0}", e.Message);
110+
var ex = new ArgumentException(exMessage);
111+
var InvalidDestinationPath = new ErrorRecord(ex, "InvalidDestinationPath", ErrorCategory.InvalidArgument, null);
112+
ThrowTerminatingError(InvalidDestinationPath);
113+
}
99114
}
100115
}
101116
}
102-
private string _literalPath;
117+
private string _destinationPath;
103118

104119
/// <summary>
105120
/// Specifies a user account that has rights to a specific repository (used for finding dependencies).
@@ -173,8 +188,6 @@ protected override void ProcessRecord()
173188
FileInfo moduleFileInfo;
174189
Hashtable parsedMetadataHash = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
175190

176-
// _path has been resolved, literal path does not need to be resolved
177-
_path = string.IsNullOrEmpty(_path) ? _literalPath : _path;
178191
// Returns the name of the file or the name of the directory, depending on path
179192
var pkgFileOrDir = new DirectoryInfo(_path);
180193
bool isScript = _path.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase);
@@ -374,7 +387,29 @@ protected override void ProcessRecord()
374387
return;
375388
}
376389

390+
// If -DestinationPath is specified then also publish the .nupkg there
391+
if (!string.IsNullOrWhiteSpace(_destinationPath))
392+
{
393+
try
394+
{
395+
var nupkgName = _pkgName + "." + _pkgVersion.ToNormalizedString() + ".nupkg";
396+
Utils.MoveFiles(System.IO.Path.Combine(outputNupkgDir, nupkgName), System.IO.Path.Combine(_destinationPath, nupkgName));
397+
}
398+
catch (Exception e) {
399+
var message = string.Format("Error moving .nupkg into destination path '{0}' due to: '{1}'.", _destinationPath, e.Message);
400+
401+
var ex = new ArgumentException(message);
402+
var ErrorMovingNupkg = new ErrorRecord(ex, "ErrorMovingNupkg", ErrorCategory.NotSpecified, null);
403+
WriteError(ErrorMovingNupkg);
404+
405+
// exit process record
406+
return;
407+
}
408+
}
409+
410+
// This call does not throw any exceptions, but it will write unsuccessful responses to the console
377411
PushNupkg(outputNupkgDir, repository.Name, repositoryUrl);
412+
378413
}
379414
finally {
380415
WriteVerbose(string.Format("Deleting temporary directory '{0}'", outputDir));

test/PublishPSResource.Tests.ps1

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ Describe "Test Publish-PSResource" {
7474
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
7575
}
7676

77-
It "Publish a module with -LiteralPath" {
78-
$version = "1.0.0"
79-
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
80-
81-
Publish-PSResource -LiteralPath $script:PublishModuleBase
82-
83-
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
84-
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
85-
}
86-
8777
<# Temporarily comment this test out until Find Helper is complete and code within PublishPSResource is uncommented
8878
It "Publish a module with dependencies" {
8979
# Create dependency module
@@ -271,4 +261,21 @@ Describe "Test Publish-PSResource" {
271261

272262
$Error[0].FullyQualifiedErrorId | Should -be "403Error,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource"
273263
}
264+
265+
It "Publish a module with -Path -Repository and -DestinationPath" {
266+
$version = "1.0.0"
267+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
268+
269+
$tmpPath = Join-Path -Path $TestDrive -ChildPath "testtmppath"
270+
New-Item $tmpPath -Itemtype directory -Force
271+
272+
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2 -DestinationPath $tmpPath
273+
274+
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
275+
276+
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
277+
278+
$expectedPath = Join-Path -Path $tmpPath -ChildPath "$script:PublishModuleName.$version.nupkg"
279+
(Get-ChildItem $tmpPath).FullName | Should -Be $expectedPath
280+
}
274281
}

0 commit comments

Comments
 (0)