From 371cdd5c7543f2e86a70ded270bd88465fdbaef3 Mon Sep 17 00:00:00 2001 From: dsx42 Date: Thu, 16 Jun 2022 23:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B1=8F=E8=94=BD=E9=94=99=E8=AF=AF=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UnattendTool.cmd | 2 +- UnattendTool.ps1 | 44 ++++++++++++++++++++++----- build.cmd | 2 +- build.ps1 | 77 ++++++++++++++++++++++++++++++++++++------------ product.json | 11 +++++++ 5 files changed, 107 insertions(+), 29 deletions(-) create mode 100644 product.json diff --git a/UnattendTool.cmd b/UnattendTool.cmd index 5d5bbb1..9d55361 100644 --- a/UnattendTool.cmd +++ b/UnattendTool.cmd @@ -1 +1 @@ -PowerShell -ExecutionPolicy RemoteSigned -File .\UnattendTool.ps1 -Interactive \ No newline at end of file +PowerShell -NoProfile -ExecutionPolicy RemoteSigned -File "%~dp0UnattendTool.ps1" -Interactive diff --git a/UnattendTool.ps1 b/UnattendTool.ps1 index b919788..0c6da9a 100644 --- a/UnattendTool.ps1 +++ b/UnattendTool.ps1 @@ -794,7 +794,7 @@ function ShowGetISOPath { $ISOFiles = [ordered]@{} $Index = 0; try { - Get-ChildItem -Path $Path -Include '*.iso' -Recurse -File | ForEach-Object { + Get-ChildItem -Path $Path -Include '*.iso' -Recurse -File -ErrorAction SilentlyContinue | ForEach-Object { $Index = $Index + 1 $ISOFiles.Add([System.String]$Index, $_.FullName) } @@ -954,7 +954,37 @@ function UpdateVentoyConfig { [System.IO.File]::WriteAllLines($VentoyConfigJsonPath, $JSONString, $Utf8NoBomEncoding) } -$VersionInfo = 'v2022.6.11' +function GetVertion { + $ProductJsonPath = "$PSScriptRoot\product.json" + + if (!(Test-Path -Path $ProductJsonPath -PathType Leaf)) { + Write-Warning -Message ("$ProductJsonPath 不存在") + [System.Environment]::Exit(0) + } + + $ProductInfo = $null + try { + $ProductInfo = Get-Content -Path $ProductJsonPath | ConvertFrom-Json + } + catch { + Write-Warning -Message ("$ProductJsonPath 解析失败") + [System.Environment]::Exit(0) + } + if (!$ProductInfo -or $ProductInfo -isNot [PSCustomObject]) { + Write-Warning -Message ("$ProductJsonPath 解析失败") + [System.Environment]::Exit(0) + } + + $Version = $ProductInfo.'version' + if (!$Version) { + Write-Warning -Message ("$ProductJsonPath 不存在 version 信息") + [System.Environment]::Exit(0) + } + + return $Version +} + +$VersionInfo = GetVertion if ($Version) { return $VersionInfo @@ -962,9 +992,9 @@ if ($Version) { Clear-Host $PSDefaultParameterValues['*:Encoding'] = 'utf8' -$Host.UI.RawUI.WindowTitle = 'Windows 应答文件生成' +$Host.UI.RawUI.WindowTitle = 'Windows 应答文件生成' Set-Location -Path $PSScriptRoot -Write-Host -Object "=====> Windows 系统自动安装应答文件生成 $VersionInfo <=====" +Write-Host -Object "=====> Windows 系统自动安装应答文件生成 v$VersionInfo <=====" Write-Host -Object '' $WipeDisk = $false @@ -1093,13 +1123,11 @@ else { $VentoyConfigParentPath = Join-Path -Path $ParentPath -ChildPath 'ventoy' if (!$(Test-Path -Path $VentoyConfigParentPath -PathType Container)) { - New-Item -Path $VentoyConfigParentPath -ItemType Directory -Force - Write-Host -Object '' + New-Item -Path $VentoyConfigParentPath -ItemType Directory -Force | Out-Null } $VentoyConfigScriptPath = Join-Path -Path $VentoyConfigParentPath -ChildPath 'script' if (!$(Test-Path -Path $VentoyConfigScriptPath -PathType Container)) { - New-Item -Path $VentoyConfigScriptPath -ItemType Directory -Force - Write-Host -Object '' + New-Item -Path $VentoyConfigScriptPath -ItemType Directory -Force | Out-Null } $ProductInfo = @{} diff --git a/build.cmd b/build.cmd index 93c8242..f2f934c 100644 --- a/build.cmd +++ b/build.cmd @@ -1 +1 @@ -PowerShell -ExecutionPolicy RemoteSigned -File .\build.ps1 \ No newline at end of file +PowerShell -NoProfile -ExecutionPolicy RemoteSigned -File "%~dp0build.ps1" diff --git a/build.ps1 b/build.ps1 index 105ca48..11c086b 100644 --- a/build.ps1 +++ b/build.ps1 @@ -1,33 +1,72 @@ -Set-Location -Path $PSScriptRoot +Clear-Host -$Version = .\UnattendTool.ps1 -Version -$Output = 'target' -$ProjectName = 'UnattendTool' +Set-Location -Path $PSScriptRoot + +$ProductJsonPath = "$PSScriptRoot\product.json" + +if (!(Test-Path -Path $ProductJsonPath -PathType Leaf)) { + Write-Warning -Message ("$ProductJsonPath 不存在") + [System.Environment]::Exit(0) +} + +$ProductInfo = $null +try { + $ProductInfo = Get-Content -Path $ProductJsonPath | ConvertFrom-Json +} +catch { + Write-Warning -Message ("$ProductJsonPath 解析失败") + [System.Environment]::Exit(0) +} +if (!$ProductInfo -or $ProductInfo -isNot [PSCustomObject]) { + Write-Warning -Message ("$ProductJsonPath 解析失败") + [System.Environment]::Exit(0) +} -$DestinationPath = ".\$Output\${ProjectName}_$Version.zip" +$Version = $ProductInfo.'version' +if (!$Version) { + Write-Warning -Message ("$ProductJsonPath 不存在 version 信息") + [System.Environment]::Exit(0) +} + +$ProjectName = $ProductInfo.'name' +if (!$ProjectName) { + Write-Warning -Message ("$ProductJsonPath 不存在 name 信息") + [System.Environment]::Exit(0) +} + +$Files = $ProductInfo.'files' +if (!$Files -or $Files -isNot [System.Array] -or $Files.Count -le 0) { + Write-Warning -Message ("$ProductJsonPath 不存在 files 信息") + [System.Environment]::Exit(0) +} + +$CopyFiles = @() +foreach ($File in $Files) { + $CopyFiles += "$PSScriptRoot\$File" +} -$Files = @( - '.\UnattendTool.cmd', - '.\UnattendTool.ps1', - '.\LICENSE', - '.\README.md' -) +$Output = 'target' +$OutputPath = "$PSScriptRoot\$Output" +$OutputProjectPath = "$OutputPath\${ProjectName}" +$OutputFileName = "${ProjectName}_v$Version" +$ZipFilePath = "$OutputPath\$OutputFileName.zip" +$Sha256FilePath = "$OutputPath\$OutputFileName.sha256" -if (Test-Path -Path ".\$Output" -PathType Container) { - Remove-Item -Path ".\$Output" -Recurse -Force +if (Test-Path -Path $OutputPath -PathType Container) { + Remove-Item -Path $OutputPath -Recurse -Force } -New-Item -Path ".\$Output\$ProjectName" -ItemType Directory -Force +New-Item -Path $OutputProjectPath -ItemType Directory -Force | Out-Null -Copy-Item -Path $Files -Destination ".\$Output\$ProjectName" -Force -Recurse +Copy-Item -Path $CopyFiles -Destination $OutputProjectPath -Force -Recurse -Compress-Archive -Path ".\$Output\$ProjectName" -DestinationPath $DestinationPath -Force +Compress-Archive -Path $OutputProjectPath -DestinationPath $ZipFilePath -Force -$Hash = Get-FileHash -Path $DestinationPath -Algorithm SHA256 +$Hash = Get-FileHash -Path $ZipFilePath -Algorithm SHA256 -$Checksum = $Hash.Hash + " ${ProjectName}_$Version.zip" +$Checksum = $Hash.Hash + " $OutputFileName.zip" -Add-Content -Path ".\$Output\${ProjectName}_$Version.sha256" -Value $Checksum +Add-Content -Path $Sha256FilePath -Value $Checksum Write-Host -Object '' Write-Host -Object ('Path: ' + $Hash.Path) diff --git a/product.json b/product.json new file mode 100644 index 0000000..1e503ae --- /dev/null +++ b/product.json @@ -0,0 +1,11 @@ +{ + "name": "UnattendTool", + "version": "2022.6.16", + "files": [ + "UnattendTool.cmd", + "UnattendTool.ps1", + "product.json", + "LICENSE", + "README.md" + ] +} \ No newline at end of file