Files
C4IT-F4SD-M42WebApi/F4SDM42WebApi/BuildM42Package.ps1

93 lines
3.5 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[string]$TemplateDir,
[Parameter(Mandatory = $true)]
[string]$AssembliesDir,
[Parameter(Mandatory = $true)]
[string]$OutputRoot,
[Parameter(Mandatory = $true)]
[string]$PackageName,
[Parameter(Mandatory = $true)]
[string]$PackageVersion
)
$ErrorActionPreference = 'Stop'
function Normalize-FullPath {
param([string]$Path)
return [System.IO.Path]::GetFullPath($Path)
}
function Normalize-TextFileLineEndings {
param([string]$Path)
$content = [System.IO.File]::ReadAllText($Path)
$content = [regex]::Replace($content, '(?<!\r)\n', "`r`n")
[System.IO.File]::WriteAllText($Path, $content, [System.Text.UTF8Encoding]::new($true))
}
$templateDirFull = Normalize-FullPath $TemplateDir
$assembliesDirFull = Normalize-FullPath $AssembliesDir
$outputRootFull = Normalize-FullPath $OutputRoot
$packageDir = Join-Path $outputRootFull "$PackageName v$PackageVersion"
$zipPath = "$packageDir.zip"
if (-not (Test-Path -LiteralPath (Join-Path $templateDirFull 'package.json'))) {
throw "Package template not found: $templateDirFull"
}
if (-not (Test-Path -LiteralPath (Join-Path $assembliesDirFull 'C4ITF4SDM42WebApi.dll'))) {
throw "Package assemblies not found: $assembliesDirFull"
}
if (Test-Path -LiteralPath $packageDir) {
Remove-Item -LiteralPath $packageDir -Recurse -Force
}
if (Test-Path -LiteralPath $zipPath) {
Remove-Item -LiteralPath $zipPath -Force
}
New-Item -ItemType Directory -Path $packageDir -Force | Out-Null
Copy-Item -Path (Join-Path $templateDirFull '*') -Destination $packageDir -Recurse -Force
Copy-Item -Path $assembliesDirFull -Destination (Join-Path $packageDir 'Assemblies') -Recurse -Force
$packageJsonPath = Join-Path $packageDir 'package.json'
$packageJson = [System.IO.File]::ReadAllText($packageJsonPath)
$packageJson = [regex]::Replace($packageJson, '"Version"\s*:\s*"[^"]+"', "`"Version`": `"$PackageVersion`"")
$packageJson = [regex]::Replace($packageJson, '"LastUpdatedDate"\s*:\s*"[^"]+"', "`"LastUpdatedDate`": `"$(Get-Date -Format 'yyyy-MM-ddTHH:mm:ss')`"")
[System.IO.File]::WriteAllText($packageJsonPath, $packageJson, [System.Text.UTF8Encoding]::new($true))
$configDataPath = Join-Path $packageDir 'install\1010_Config_Data\02-01-0080 C4IT_F4SDConfigurationType.dat'
if (-not (Test-Path -LiteralPath $configDataPath)) {
throw "Configuration data file not found: $configDataPath"
}
$configData = [System.IO.File]::ReadAllText($configDataPath)
$configData = [regex]::Replace(
$configData,
'(<C4IT_AddonConfigClassBase>[\s\S]*?<Version>)[^<]*(</Version>)',
"`${1}$PackageVersion`${2}",
[System.Text.RegularExpressions.RegexOptions]::Singleline
)
if ($configData -notmatch "<Version>$([regex]::Escape($PackageVersion))</Version>") {
throw "Could not update C4IT_F4SDConfigurationType Version to $PackageVersion in $configDataPath"
}
[System.IO.File]::WriteAllText($configDataPath, $configData, [System.Text.UTF8Encoding]::new($true))
$textFileExtensions = @('.json', '.xml', '.dat', '.type', '.class', '.config', '.host')
Get-ChildItem -LiteralPath $packageDir -Recurse -File |
Where-Object { $textFileExtensions -contains $_.Extension.ToLowerInvariant() } |
ForEach-Object { Normalize-TextFileLineEndings $_.FullName }
Compress-Archive -Path (Join-Path $packageDir '*') -DestinationPath $zipPath -Force
Write-Host "Matrix42 package folder: $packageDir"
Write-Host "Matrix42 package zip: $zipPath"