Last active
January 29, 2018 08:29
-
-
Save kbilsted/23bf7143a0197d365106ef3bf654f9e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
namespace CSharpCompression | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Length == 0) | |
{ | |
Console.WriteLine("Need arguments!\nUsage: Compress <path> [-v0|v1]"); | |
} | |
bool showFilesWhenProcessing = false; | |
bool printOnlyChangedFiles = false; | |
var startPath = args[0]; | |
if (args.Length == 2) | |
{ | |
showFilesWhenProcessing = args[1] == "-v0"; | |
printOnlyChangedFiles = args[1] == "-v1"; | |
} | |
int totalLinesReduced = 0; | |
foreach (var path in Directory.EnumerateFiles(startPath, "*.cs", SearchOption.AllDirectories)) | |
{ | |
var linesReduced = PrintAndCompress(showFilesWhenProcessing, printOnlyChangedFiles, path); | |
totalLinesReduced += linesReduced; | |
} | |
Console.WriteLine(); | |
Console.WriteLine($"Total lines reduced: {totalLinesReduced}"); | |
} | |
private static int PrintAndCompress(bool showFilesWhenProcessing, bool printOnlyChangedFiles, string path) | |
{ | |
if (showFilesWhenProcessing || printOnlyChangedFiles) | |
Console.Write($"{">>>",5} {path} "); | |
var linesReduced = CompressFile(path); | |
Console.SetCursorPosition(0, Console.CursorTop); | |
if (showFilesWhenProcessing || (linesReduced!=0&& printOnlyChangedFiles)) | |
{ | |
Console.WriteLine($"{linesReduced,5} {path}"); | |
} | |
return linesReduced; | |
} | |
private static int CompressFile(string path) | |
{ | |
var isUtf8Bom = IsUtf8Bom(path); | |
var enc = isUtf8Bom ? Encoding.UTF8 : Encoding.GetEncoding("iso-8859-1"); | |
var file = File.ReadAllText(path, enc); | |
var lines = file.Count(x => x == '\n'); | |
var newFile = CallCompressors(file); | |
if (newFile.Length != file.Length) | |
{ | |
File.WriteAllText(path, newFile, enc); | |
return lines - newFile.Count(x => x == '\n'); | |
} | |
return 0; | |
} | |
private static string CallCompressors(string file) | |
{ | |
file = Compressors.CompressProperties(file); | |
file = Compressors.CompressRegionVersionHistory(file); | |
return file; | |
} | |
private static bool IsUtf8Bom(string path) | |
{ | |
var b = File.ReadAllBytes(path); | |
if (b.Length < 3) | |
return false; | |
bool isUtf8Bom = true; | |
for (int i = 0; i < Encoding.UTF8.GetPreamble().Length; i++) | |
{ | |
if (Encoding.UTF8.GetPreamble()[i] != b[i]) | |
{ | |
isUtf8Bom = false; | |
} | |
} | |
return isUtf8Bom; | |
} | |
} | |
class Compressors | |
{ | |
private static RegexOptions options = RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.CultureInvariant; | |
private static RegexOptions optionsIgnoreCase = options | RegexOptions.IgnoreCase; | |
static readonly Regex GetSetRegEx = new Regex(@"\s*\{\s+get;\s+(?<modifier>protected |private )?set;\s+\}", options); | |
public static string CompressProperties(string file) | |
{ | |
var newFile = GetSetRegEx.Replace(file, @" { get; ${modifier}set; }"); | |
return newFile; | |
} | |
static readonly Regex VersionHistoryRegEx = new Regex(@"( |\t)*#region Version History( |\t)*((?>\s*//[^\n]*))*\s*#endregion( |\t)*(\r\n?|\n)+", optionsIgnoreCase); | |
public static string CompressRegionVersionHistory(string file) | |
{ | |
var newFile = VersionHistoryRegEx.Replace(file, ""); | |
return newFile; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment