@@ -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 ) ) ;
0 commit comments