Skip to content

Instantly share code, notes, and snippets.

@zaripych
Last active June 14, 2017 15:44
Show Gist options
  • Save zaripych/f4350f2509a2859d9fe8df0ecbe8c4b0 to your computer and use it in GitHub Desktop.
Save zaripych/f4350f2509a2859d9fe8df0ecbe8c4b0 to your computer and use it in GitHub Desktop.
# This script saves an XD transform file into MS Deploy Package with additional validations
# FOR MORE INFO ABOUT XDT TRANSFORMS READ HERE
# https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx
[CmdletBinding()]
Param(
<#
.SYNOPSIS Path to the file contents of which should be added to the package, contents of this file is the XD transform
#>
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[ValidateNotNullOrEmpty()]
[string]$filePath,
<#
.SYNOPSIS Transform target file name, to determine location of the transform file in the package (e.g. Web.config)
#>
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$transformTargetFileName,
<#
.SYNOPSIS Transform file name, to determine if it already exists (e.g. Web.Release.config)
#>
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$transformFileName,
<#
.SYNOPSIS Defines whether to overwrite the existing transform file if it exists
#>
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[bool]$overwrite = $false,
<#
.SYNOPSIS Target directory to look for the deploy package
#>
[Parameter(Mandatory=$false)]
[ValidateScript({Test-Path $_})]
[ValidateNotNullOrEmpty()]
[string]$targetDir = ".",
<#
.SYNOPSIS Package file name
#>
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$zipFileName = "*.zip"
)
$transformContents = (Get-Content $filePath) -join "`r`n"
$overwriteTransformFile = $overwrite
# Path to the MS Deploy ZIP package with web site contents
$zipPath = (Get-ChildItem -Path "$targetDir\**\$zipFileName" -Recurse).FullName
if (!($zipPath)) {
throw "MS Deploy Package not found"
}
if (($zipPath -is [system.array]) -and $zipPath.Length -gt 1) {
throw "Multiple packages found at: " + $zipPath
}
# Location of the transformation target in the the zip package
# NOTE: PackageTmp corresponds to the root directory for our web site which has complicated base directory path, hence the `.*` regex
$transformTargetLocator = ".*/PackageTmp/$([regex]::Escape($transformTargetFileName))".Replace("/", "(/|\\)")
$transformLocator = ".*/PackageTmp/$([regex]::Escape($transformFileName))".Replace("/", "(/|\\)")
# Open zip and find the transformation target
Add-Type -assembly System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::Open($zipPath, "Update")
try {
$transformLocation = ($zip.Entries | Where-Object { $_.FullName -match $transformLocator }).FullName
$transformTargetLocation = ($zip.Entries | Where-Object { $_.FullName -match $transformTargetLocator }).FullName
if (!($transformTargetLocation)) {
Write-Warning "XDT Transform target at '$transformTargetLocator' not found, skipping transform file generation"
return
}
if ($transformLocation -and (-not $overwriteTransformFile)) {
throw "Transform file at '$transformLocation' already exists, either change the transform file name, ensure that it doesn't exist, " +
"set `$overwriteTransformFile to `$true or disable this VSTS step"
}
$transformParentDirectory = [System.IO.Path]::GetDirectoryName($transformTargetLocation).Replace("\", "/")
$transformFilePath = $transformParentDirectory + "/$transformFileName"
$action = ""
$transformFile = if ($transformLocation) {
$action = "existing file"
$zip.GetEntry($transformLocation)
} else {
$action = "new file"
$zip.CreateEntry($transformFilePath)
}
# Update the contents of the transform file
$newTransformFile = [System.IO.StreamWriter]($transformFile).Open()
try {
$newTransformFile.BaseStream.SetLength(0)
$newTransformFile.Write($transformContents)
$newTransformFile.Flush()
} finally {
$newTransformFile.Close()
}
$reader = [System.IO.StreamReader]($transformFile).Open()
try {
$transformContents = $reader.ReadToEnd()
} finally {
$reader.Close()
}
Write-Host "Zip file '$zipPath' updated with $action '$transformFileName' at '$transformParentDirectory' now having following contents:"
Write-Output $transformContents
} finally {
$zip.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment