[IMPEX] add link support (#2815)

* rework impex

- rework impex
- add link logic

* add error handling
This commit is contained in:
MyDrift 2024-10-01 22:23:36 +02:00 committed by GitHub
parent 806cbd021a
commit e165388117
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,44 +19,59 @@ function Invoke-WPFImpex {
$Config = $null $Config = $null
) )
if ($type -eq "export") { function ConfigDialog {
$FileBrowser = New-Object System.Windows.Forms.SaveFileDialog if (!$Config) {
} switch ($type) {
if ($type -eq "import") { "export" { $FileBrowser = New-Object System.Windows.Forms.SaveFileDialog }
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog "import" { $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog }
} }
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$FileBrowser.Filter = "JSON Files (*.json)|*.json"
$FileBrowser.ShowDialog() | Out-Null
if (-not $Config) { if ($FileBrowser.FileName -eq "") {
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop') return $null
$FileBrowser.Filter = "JSON Files (*.json)|*.json" } else {
$FileBrowser.ShowDialog() | Out-Null return $FileBrowser.FileName
}
if($FileBrowser.FileName -eq "") {
return
} else { } else {
$Config = $FileBrowser.FileName return $Config
} }
} }
if ($type -eq "export") { switch ($type) {
$jsonFile = Get-WinUtilCheckBoxes -unCheck $false "export" {
$jsonFile | ConvertTo-Json | Out-File $FileBrowser.FileName -Force try {
$runscript = "iex ""& { `$(irm christitus.com/win) } -Config '$($FileBrowser.FileName)'""" $Config = ConfigDialog
$runscript | Set-Clipboard if ($Config) {
} $jsonFile = Get-WinUtilCheckBoxes -unCheck $false | ConvertTo-Json
if ($type -eq "import") { $jsonFile | Out-File $Config -Force
$jsonFile = Get-Content $Config | ConvertFrom-Json "iex ""& { `$(irm christitus.com/win) } -Config '$Config'""" | Set-Clipboard
$flattenedJson = @()
$jsonFile.PSObject.Properties | ForEach-Object {
$category = $_.Name
foreach ($checkboxName in $_.Value) {
if ($category -ne "Install") {
$flattenedJson += $checkboxName
} }
} 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: $_"
} }
} }
Invoke-WPFPresets -preset $flattenedJson -imported $true
} }
} }