Rewrite New-TemporaryFile in C# (#2786)

Literal translation of New-TemporaryFile function to a C# cmdlet,
other than moving logic from Begin to End.
This commit is contained in:
Kitt Holland
2016-12-06 16:22:07 -08:00
committed by Jason Shirk
parent 9aa693dfb3
commit e568fda5c3
6 changed files with 55 additions and 39 deletions
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// The implementation of the "New-TemporaryFile" cmdlet
/// </summary>
[Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=526726")]
[OutputType(typeof(System.IO.FileInfo))]
public class NewTemporaryFileCommand : Cmdlet
{
/// <summary>
/// returns a TemporaryFile
/// </summary>
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);
}
}
}
}
}
@@ -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.
############################################################################################>
@@ -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'
@@ -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'
@@ -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'
@@ -229,6 +229,9 @@
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\commands\utility\NewGuidCommand.cs">
<Link>commands\utility\NewGuidCommand.cs</Link>
</Compile>
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\commands\utility\NewTemporaryFileCommand.cs">
<Link>commands\utility\NewTemporaryFileCommand.cs</Link>
</Compile>
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\commands\utility\NewTimeSpanCommand.cs">
<Link>commands\utility\NewTimeSpanCommand.cs</Link>
</Compile>