Skip to content

Instantly share code, notes, and snippets.

@jammykam
Created September 20, 2017 22:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jammykam/8b2ae5129417a52608bbbc9d406c0956 to your computer and use it in GitHub Desktop.
Save jammykam/8b2ae5129417a52608bbbc9d406c0956 to your computer and use it in GitHub Desktop.
Extract DLLs from TDS Update Packages
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Comcast.ExtractPostProcessDllFromPackage
{
class Program
{
class Parameters
{
public string SourcePackageFilename { get; set; }
public string TargetPackageFolder { get; set; }
}
static void Main(string[] args)
{
try
{
Parameters parameters = GetParameters(args);
string tempFolder = CreateTempFolder();
ExtractPacakgeZipFile(parameters, tempFolder);
ExtractAndRemovePostProcessingDll(parameters, tempFolder);
CreateNewUpdatePacakge(parameters, tempFolder);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Exception {0}({1}):\n{2}", ex.Message, ex.GetType().Name, ex.StackTrace));
Environment.Exit(100);
}
}
/// <summary>
/// Creates the new update pacakge
/// </summary>
/// <param name="parameters"></param>
/// <param name="tempFolder"></param>
private static void CreateNewUpdatePacakge(Parameters parameters, string tempFolder)
{
string newUpdatePackageName = Path.Combine(parameters.TargetPackageFolder, Path.GetFileName(parameters.SourcePackageFilename));
if (File.Exists(newUpdatePackageName))
{
File.Delete(newUpdatePackageName);
}
Console.WriteLine("Creating package " + newUpdatePackageName);
using (ZipFile zf = new ZipFile(newUpdatePackageName))
{
zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
using (FileStream fs = File.Open(Path.Combine(tempFolder, "package.zip"), FileMode.Open, FileAccess.Read))
{
zf.AddEntry("package.zip", fs);
zf.Save();
}
}
}
/// <summary>
/// Extracts the PackageInstallPostProcessor.dll and removes to from the zip file
/// </summary>
/// <param name="parameters"></param>
/// <param name="tempFolder"></param>
private static void ExtractAndRemovePostProcessingDll(Parameters parameters, string tempFolder)
{
if (!Directory.Exists(parameters.TargetPackageFolder))
{
Directory.CreateDirectory(parameters.TargetPackageFolder);
}
using (ZipFile zf = ZipFile.Read(Path.Combine(tempFolder, "package.zip")))
{
foreach (ZipEntry ze in zf)
{
if (ze.FileName == "addedfiles/bin/HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll")
{
string dllPath = Path.Combine(parameters.TargetPackageFolder, "HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll");
Console.WriteLine("Extracting file to " + dllPath);
using (FileStream fs = File.Open(dllPath, FileMode.Create, FileAccess.Write))
{
ze.Extract(fs);
}
}
}
Console.WriteLine("Removing post process dll");
zf.RemoveEntries(new string[]
{
"addedfiles/bin/HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll",
"properties/addedfiles/bin/HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll"
});
zf.Save();
}
}
/// <summary>
/// Extracts the Package.zip file
/// </summary>
/// <param name="parameters"></param>
/// <param name="tempFolder"></param>
private static void ExtractPacakgeZipFile(Parameters parameters, string tempFolder)
{
using (ZipFile zf = ZipFile.Read(parameters.SourcePackageFilename))
{
ZipEntry packageZipFile = (from ze in zf
where ze.FileName == "package.zip"
select ze).FirstOrDefault();
if (packageZipFile == null)
{
Console.WriteLine("Could not find package.zip in the update package");
}
packageZipFile.Extract(tempFolder);
}
}
/// <summary>
/// Creates the temp folder that holds the temp Package.zip file
/// </summary>
/// <returns></returns>
private static string CreateTempFolder()
{
string tempFolder = Path.Combine(Path.GetTempPath(), "ExtractPostProcessDllFromPackage");
Console.WriteLine("Using temp folder " + tempFolder);
if (Directory.Exists(tempFolder))
{
Console.WriteLine("Cleaning temp folder");
Directory.Delete(tempFolder, true);
}
Directory.CreateDirectory(tempFolder);
return tempFolder;
}
/// <summary>
/// Parses the loads the command line parameters
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
private static Parameters GetParameters(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Invalid Parameters\nParameters: [Path to Souce Package file] [Target Package Destination folder]");
Environment.Exit(0);
}
return new Parameters
{
SourcePackageFilename = args[0],
TargetPackageFolder = args[1]
};
}
}
}
@marcelgruber
Copy link

In case anyone is wondering what this does exactly, I discussed this with JammyKam.

Me:

Is this intended to remove the HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll file from update packages in the event that we have already deployed it elsewhere and to prevent an overwrite of the DLL resulting in a server restart?

JammyKam:

Yes, exactly, as part of a build or deploy process. It was posted by someone on slack, they got it from Hedgehog support.

In my case, this DLL was always included in our update packages even though we managed the deployment of that file externally. As far as I could tell, it was overwriting the DLL which caused a recycle of the app pool. According to JammyKam it's not supposed to restart the app pool, but it did in both his case and my case.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment