winutil/functions/public/Invoke-WPFImpex.ps1
MyDrift e165388117
[IMPEX] add link support (#2815)
* rework impex

- rework impex
- add link logic

* add error handling
2024-10-01 15:23:36 -05:00

78 lines
2.5 KiB
PowerShell

function Invoke-WPFImpex {
<#
.SYNOPSIS
Handles importing and exporting of the checkboxes checked for the tweaks section
.PARAMETER type
Indicates whether to 'import' or 'export'
.PARAMETER checkbox
The checkbox to export to a file or apply the imported file to
.EXAMPLE
Invoke-WPFImpex -type "export"
#>
param(
$type,
$Config = $null
)
function ConfigDialog {
if (!$Config) {
switch ($type) {
"export" { $FileBrowser = New-Object System.Windows.Forms.SaveFileDialog }
"import" { $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog }
}
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$FileBrowser.Filter = "JSON Files (*.json)|*.json"
$FileBrowser.ShowDialog() | Out-Null
if ($FileBrowser.FileName -eq "") {
return $null
} else {
return $FileBrowser.FileName
}
} else {
return $Config
}
}
switch ($type) {
"export" {
try {
$Config = ConfigDialog
if ($Config) {
$jsonFile = Get-WinUtilCheckBoxes -unCheck $false | ConvertTo-Json
$jsonFile | Out-File $Config -Force
"iex ""& { `$(irm christitus.com/win) } -Config '$Config'""" | Set-Clipboard
}
} catch {
Write-Error "An error occurred while exporting: $_"
}
}
"import" {
try {
$Config = ConfigDialog
if ($Config) {
try {
if ($Config -match '^https?://') {
$jsonFile = (Invoke-WebRequest "$Config").Content | ConvertFrom-Json
} else {
$jsonFile = Get-Content $Config | ConvertFrom-Json
}
} catch {
Write-Error "Failed to load the JSON file from the specified path or URL: $_"
return
}
$flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" }).ForEach({ $_.Value })
Invoke-WPFPresets -preset $flattenedJson -imported $true
}
} catch {
Write-Error "An error occurred while importing: $_"
}
}
}
}