Skip to content

Instantly share code, notes, and snippets.

@tumtumtum
Created July 3, 2015 15:43
Show Gist options
  • Save tumtumtum/0afed0f962517f0eb02e to your computer and use it in GitHub Desktop.
Save tumtumtum/0afed0f962517f0eb02e to your computer and use it in GitHub Desktop.
Repackage cspkg file with replacement web.config
function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
Add-Type -Assembly System.IO.Compression
$archive = [System.IO.Compression.ZipFile]::Open($zipfilename, [System.IO.Compression.ZipArchiveMode]::Create);
try
{
$files = [System.IO.Directory]::GetFiles($sourceDir, "*.*", [System.IO.SearchOption]::AllDirectories)
foreach ($filename in $files)
{
$relativePath = $filename.Substring($sourcedir.Length + 1).Replace("\", "/");
echo "Creating zip entry for $relativePath"
$entry = $archive.CreateEntry($relativePath, [System.IO.Compression.CompressionLevel]::Optimal);
$destination = $entry.Open();
try
{
try
{
$source = [System.IO.File]::OpenRead($filename);
$source.CopyTo($destination);
}
finally
{
$source.Dispose();
}
}
finally
{
$destination.Dispose();
}
}
}
finally
{
$archive.Dispose();
}
}
function UnzipFiles( $zipfilename, $dest )
{
Add-Type -Assembly System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfilename, $dest)
}
$cspkg = Get-ChildItem *.cspkg
rm -Recurse -ErrorAction SilentlyContinue "cspkg.temp"
md -Force "cspkg.temp"
UnzipFiles ($cspkg.FullName) ((Get-Item -Path ".\" -Verbose).FullName + "\cspkg.temp")
$doc = [xml](Get-Content -Path "cspkg.temp\package.xml")
$ns = new-object system.xml.xmlnamespacemanager($doc.NameTable)
$ns.addnamespace("i", $doc.documentelement.namespaceuri)
$node = $doc.SelectNodes("//i:FilePath[text() = '\approot\Web.config']", $ns)
$node = $node.ParentNode
$path = "cspkg.temp/" + $node.filedescription.datacontentreference
echo "Copying Web.config to $path"
copy Web.config $path
rm -force $cspkg.FullName
ZipFiles $cspkg.FullName ((Get-Item -Path ".\" -Verbose).FullName + "\cspkg.temp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment