From b1cf7849b50a26bd3cec32f92d106fac7e30e863 Mon Sep 17 00:00:00 2001 From: kittholland Date: Tue, 6 Dec 2016 12:46:00 -0800 Subject: [PATCH] Rewrite New-TemporaryFile in C# Literal translation of New-TemporaryFile function to a C# cmdlet, other than moving logic from Begin to End. --- .../utility/NewTemporaryFileCommand.cs | 46 +++++++++++++++++++ .../Microsoft.PowerShell.Utility.psm1 | 31 ------------- .../Microsoft.PowerShell.Utility.psd1 | 4 +- .../Microsoft.PowerShell.Utility.psd1 | 5 +- .../Microsoft.PowerShell.Utility.psd1 | 5 +- ...crosoft.PowerShell.Commands.Utility.csproj | 3 ++ 6 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs new file mode 100644 index 00000000000..2ea91339037 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs @@ -0,0 +1,46 @@ + +using System; +using System.IO; +using System.Management.Automation; + +namespace Microsoft.PowerShell.Commands +{ + /// + /// The implementation of the "New-TemporaryFile" cmdlet + /// + [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=526726")] + [OutputType(typeof(System.IO.FileInfo))] + public class NewTemporaryFileCommand : Cmdlet + { + /// + /// returns a TemporaryFile + /// + protected override void EndProcessing() + { + string filePath = null; + string tempPath = System.Environment.GetEnvironmentVariable("TEMP"); + if (ShouldProcess(tempPath)) + { + try + { + filePath = Path.GetTempFileName(); + } + catch (Exception e) + { + WriteError( + new ErrorRecord( + e, + "NewTemporaryFileWriteError", + ErrorCategory.WriteError, + tempPath)); + return; + } + if (!string.IsNullOrEmpty(filePath)) + { + FileInfo file = new FileInfo(filePath); + WriteObject(file); + } + } + } + } +} diff --git a/src/Modules/Shared/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 b/src/Modules/Shared/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 index 7ab842bd1db..731053a2302 100644 --- a/src/Modules/Shared/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 +++ b/src/Modules/Shared/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 @@ -141,37 +141,6 @@ function Get-FileHash } } -<# This cmdlet is used to create a new temporary file in $env:temp #> -function New-TemporaryFile -{ - [CmdletBinding( - HelpURI='https://go.microsoft.com/fwlink/?LinkId=526726', - SupportsShouldProcess=$true)] - [OutputType([System.IO.FileInfo])] - Param() - - Begin - { - try - { - if($PSCmdlet.ShouldProcess($env:TEMP)) - { - $tempFilePath = [System.IO.Path]::GetTempFileName() - } - } - catch - { - $errorRecord = [System.Management.Automation.ErrorRecord]::new($_.Exception,"NewTemporaryFileWriteError", "WriteError", $env:TEMP) - Write-Error -ErrorRecord $errorRecord - return - } - - if($tempFilePath) - { - Get-Item $tempFilePath - } - } -} <############################################################################################ # Format-Hex cmdlet helps in displaying the Hexadecimal equivalent of the input data. ############################################################################################> diff --git a/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 b/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 index 6fd68da654c..00192551dd6 100644 --- a/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 +++ b/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 @@ -22,8 +22,8 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide", "Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", - "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime" -FunctionsToExport= "Get-FileHash", "New-TemporaryFile", "Format-Hex", "Import-PowerShellDataFile" + "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile" +FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile" AliasesToExport= "fhx" NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1" HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390787' diff --git a/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 b/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 index 57643dae87c..4959588d9d6 100644 --- a/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 +++ b/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 @@ -19,12 +19,11 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide", "Group-Object", "Sort-Object", "Get-Variable", "New-Variable", "Set-Variable", "Remove-Variable", "Clear-Variable", "Export-Clixml", "Import-Clixml", "ConvertTo-Xml", "Select-Xml", "Write-Debug", "Write-Verbose", "Write-Warning", "Write-Error", "Write-Information", "Write-Output", "Set-PSBreakpoint", - "Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack", + "Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime" -FunctionsToExport= "Get-FileHash", "New-TemporaryFile", "Format-Hex", "Import-PowerShellDataFile", - "ConvertFrom-SddlString" +FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString" AliasesToExport= "fhx" NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1" HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390787' diff --git a/src/Modules/Windows-Full/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 b/src/Modules/Windows-Full/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 index b7b5892c60a..5179fdcda34 100644 --- a/src/Modules/Windows-Full/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 +++ b/src/Modules/Windows-Full/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 @@ -24,9 +24,8 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack", "Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Show-Command", "Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", "Get-RunspaceDebug", "Wait-Debugger", - "ConvertFrom-String", "Convert-String" , "Get-Uptime" -FunctionsToExport= "Get-FileHash", "New-TemporaryFile", "Format-Hex", "Import-PowerShellDataFile", - "ConvertFrom-SddlString" + "ConvertFrom-String", "Convert-String" , "Get-Uptime", "New-TemporaryFile" +FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString" AliasesToExport= "CFS", "fhx" NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1" HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390787' diff --git a/src/vs-csproj/Microsoft.PowerShell.Commands.Utility.csproj b/src/vs-csproj/Microsoft.PowerShell.Commands.Utility.csproj index dd533298e73..3ab886baeb4 100644 --- a/src/vs-csproj/Microsoft.PowerShell.Commands.Utility.csproj +++ b/src/vs-csproj/Microsoft.PowerShell.Commands.Utility.csproj @@ -229,6 +229,9 @@ commands\utility\NewGuidCommand.cs + + commands\utility\NewTemporaryFileCommand.cs + commands\utility\NewTimeSpanCommand.cs