private static byte[] GetOptimizedBytes(string path) { Environment.CurrentDirectory = Path.GetDirectoryName(path) ?? throw new ArgumentException("Path not found"); string asmNameExt = Path.GetFileName(path); string asmName = Path.GetFileNameWithoutExtension(asmNameExt); string asmNameMerged = $"{asmName}.merged{Path.GetExtension(asmNameExt)}"; string asmNameMinified = $"{asmName}.minified{Path.GetExtension(asmNameExt)}"; Assembly asm = Assembly.LoadFrom(path); ILRepack repack = new ILRepack(new RepackOptions(new[] { "/internalize", $"/out:{asmNameMerged}", asmNameExt }.Concat(GetDependentFilesPass(asm, Environment.CurrentDirectory)))); repack.Repack(); ILStrip optimizer = new ILStrip(asmNameMerged); optimizer.MakeInternal(); optimizer.ScanUsedClasses(); optimizer.ScanUnusedClasses(); optimizer.CleanupUnusedClasses(); optimizer.CleanupUnusedResources(); optimizer.CleanupUnusedReferences(); optimizer.Save(asmNameMinified); optimizer.Dispose(); File.Delete(asmNameMerged); byte[] result = File.ReadAllBytes(asmNameMinified); File.Delete(asmNameMinified); return(result); }
public AssemblyAsserts(ILStrip strip) { using (MemoryStream ms = new MemoryStream()) { strip.Save(ms); ms.Position = 0; definition = AssemblyDefinition.ReadAssembly(ms); } foreach (TypeDefinition type in definition.MainModule.Types) { types.Add(type.FullName, type); foreach (TypeDefinition nestedType in type.NestedTypes) { types.Add(nestedType.FullName, nestedType); } } string targetName = $"{definition.Name.Name}.g.resources"; EmbeddedResource wpfRoot = null; foreach (Resource resource in definition.MainModule.Resources) { if (resource.Name == targetName) { wpfRoot = resource as EmbeddedResource; } if (wpfRoot != null) { break; } } if (wpfRoot == null) { return; } using (Stream stream = wpfRoot.GetResourceStream()) { using (ResourceReader reader = new ResourceReader(stream)) foreach (DictionaryEntry entry in reader) { resources.Add((string)entry.Key); } } }
static int Main(string[] args) { CommandLineOptions options = new CommandLineOptions(); CommandLineParser <CommandLineOptions> parser = new CommandLineParser <CommandLineOptions>(options); parser.AssignmentSyntax = true; parser.WriteUsageOnError = true; if (!parser.Parse(args)) { #if DEBUG Console.ReadKey(); #endif return(1); } if (!options.Silent) { Console.WriteLine("Reading: " + options.InputFilename); } using (ILStrip ilStrip = new ILStrip(options.InputFilename)) { if (!options.Silent) { ilStrip.Logger = new CommandLineLogger(); } foreach (string s in options.EntryPoints) { ilStrip.EntryPoints.Add(s); } foreach (string s in options.ResourceExclusions) { ilStrip.UnusedResourceExclusions.Add(s); } foreach (string s in options.WpfResourceExclusions) { ilStrip.UnusedWpfResourceExclusions.Add(s); } ilStrip.RemoveUnknownResources = options.RemoveUnknownResources; ilStrip.ScanUsedClasses(); ilStrip.ScanUnusedClasses(); ilStrip.CleanupUnusedClasses(); ilStrip.CleanupUnusedReferences(); ilStrip.CleanupUnusedResources(); if (options.HideApi) { foreach (string s in options.HideExclusions) { ilStrip.MakeInternalExclusions.Add(s); } ilStrip.MakeInternal(); } if (!options.Silent) { Console.WriteLine("Writing: " + options.OutputFilename); } ilStrip.Save(options.OutputFilename); } #if DEBUG Console.ReadKey(); #endif return(0); }
static int Main(string[] args) { CommandLineOptions options = new CommandLineOptions(); CommandLineParser parser = new CommandLineParser(options); parser.Parse(); if (parser.HasErrors) { Console.WriteLine(parser.UsageInfo.ToString(78, true)); #if DEBUG Console.ReadKey(); #endif return(1); } if (options.DoHelp) { Console.WriteLine(parser.UsageInfo.ToString()); #if DEBUG Console.ReadKey(); #endif return(0); } if (!options.Silent) { Console.WriteLine("BrokenEvent.ILStrip.CLI, (C)2017, Broken Event"); } if (!options.Silent) { Console.WriteLine("Reading: " + options.InputFilename); } ILStrip ilStrip = new ILStrip(options.InputFilename); if (!options.Silent) { ilStrip.Logger = new CommandLineLogger(); } foreach (string s in options.EntryPoints) { ilStrip.EntryPoints.Add(s); } ilStrip.ScanUsedClasses(); ilStrip.ScanUnusedClasses(); if (options.CleanUnusedClasses) { ilStrip.CleanupUnusedClasses(); } if (options.CleanUnusedReferences) { ilStrip.CleanupUnusedReferences(); } if (options.CleanUnusedResources) { ilStrip.CleanupUnusedResources(); } if (options.HideApi) { foreach (string s in options.HideExclusions) { ilStrip.MakeInternalExclusions.Add(s); } ilStrip.MakeNotPublic(); } if (!options.Silent) { Console.WriteLine("Writing: " + options.OutputFilename); } ilStrip.Save(options.OutputFilename); #if DEBUG Console.ReadKey(); #endif return(0); }