Centralize method for creating temporary directories

This commit is contained in:
Manuel Thalmann 2023-06-16 20:25:43 +02:00
parent ada22de1a8
commit 988e5bf689
2 changed files with 8 additions and 7 deletions

View file

@ -53,4 +53,10 @@ class Context {
-Wait ` -Wait `
-NoNewWindow; -NoNewWindow;
} }
[string] GetTempDirectory() {
$tempDir = Join-Path $([System.IO.Path]::GetTempPath()) $([System.IO.Path]::GetRandomFileName());
$null = New-Item -ItemType Directory $tempDir;
return $tempDir;
}
} }

View file

@ -4,16 +4,11 @@ $null = New-Module {
$softwareName = "PuTTY"; $softwareName = "PuTTY";
$configFile = "$($softwareName).reg"; $configFile = "$($softwareName).reg";
function New-TempDirectory() {
$tempDir = Join-Path $([System.IO.Path]::GetTempPath()) $([System.IO.Path]::GetRandomFileName());
$null = New-Item -ItemType Directory $tempDir;
return $tempDir;
}
function Invoke-BackupPuTTY([Context] $context) { function Invoke-BackupPuTTY([Context] $context) {
$tempDir = New-TempDirectory; $tempDir = $context.GetTempDirectory();
$fileName = Join-Path "$tempDir" $configFile; $fileName = Join-Path "$tempDir" $configFile;
& reg export "HKCU\Software\SimonTatham" "$fileName" /y; & reg export "HKCU\Software\SimonTatham" "$fileName" /y;
$context.Backup($tempDir, $context.SoftwareArchive($softwareName), @()); $context.Backup($tempDir, $context.SoftwareArchive($softwareName), @());
Remove-Item -Recurse $tempDir;
} }
} }