Skip to content

Instantly share code, notes, and snippets.

@sixlettervariables
Created August 7, 2015 22:36
Show Gist options
  • Save sixlettervariables/9ec32519e959fde8f662 to your computer and use it in GitHub Desktop.
Save sixlettervariables/9ec32519e959fde8f662 to your computer and use it in GitHub Desktop.
Remove PublicKeyToken's from your Code First migration RESX files
// <copyright file="RemovePublicKeyTokenProgram.cs">
// Copyright (c) 2015 Christopher A. Watford ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// </copyright>
//
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace RemovePublicKeyToken
{
static class RemovePublicKeyTokenProgram
{
/// <summary>
/// This is the value of your PublicKeyToken you'd like removed.
/// </summary>
const string OLD_TOKEN = "abc123";
static void Main(string[] args)
{
//
// 1. Search for all RESX files in the specified directory
//
var resxs = Directory.EnumerateFiles(args.FirstOrDefault() ?? ".", "*.resx");
foreach (var resx in resxs)
{
//
// 2. Open the RESX file and find the first <value> element
// with a parent <data> element that has a 'name="Target"' attribute.
//
var xdoc = XDocument.Load(resx);
var valueNode = xdoc.Descendants("value")
.Where(xelt => xelt.Parent.Attribute("name").Value == "Target")
.FirstOrDefault();
//
// 3. Decode the Base64 encoded GZIP
//
byte[] bytes = Convert.FromBase64String(valueNode.Value);
using (var ms = new MemoryStream(bytes))
using (var gunzip = new GZipStream(ms, CompressionMode.Decompress))
using (var output = new MemoryStream())
{
//
// 4. UNgzip the data into an output buffer
//
gunzip.CopyTo(output);
output.Position = 0;
using (var reader = new StreamReader(output, Encoding.UTF8))
{
//
// 5. Read the existing EDMX
//
var edmx = reader.ReadToEnd();
//
// 6. Replace the old PublicKeyToken with the 'null' token
//
var newEdmx = edmx.Replace("PublicKeyToken=" + OLD_TOKEN, "PublicKeyToken=null");
//
// 7. Save the updated EDMX to a local file for inspection
//
File.WriteAllText(Path.GetFileName(Path.ChangeExtension(resx, "edmx")), newEdmx);
//
// 8. Create the new UTF8 EDMX to put into the RESX
//
var inputBytes = Encoding.UTF8.GetPreamble().Concat(Encoding.UTF8.GetBytes(newEdmx)).ToArray();
using (var reInput = new MemoryStream())
{
//
// 9. Re-GZIP the EDMX
//
using (var gzip = new GZipStream(reInput, CompressionLevel.Fastest, true))
{
gzip.Write(inputBytes, 0, inputBytes.Length);
}
reInput.Position = 0;
//
// 10. Set the <value> node's text to our new Bas64 encoded EDMX
//
var newBase64 = Convert.ToBase64String(reInput.ToArray());
valueNode.SetValue(newBase64);
}
}
}
//
// 11. Save the updated RESX file to our local directory
//
xdoc.Save(Path.GetFileName(resx));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment