Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncansmart/bfd4cd82f9aa41454ffe to your computer and use it in GitHub Desktop.
Save duncansmart/bfd4cd82f9aa41454ffe to your computer and use it in GitHub Desktop.
Creates a single source file version of DotNetZip
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace ZipReducedOneFile
{
class Program
{
static void Main()
{
var files = Directory.EnumerateFiles(@"C:\Temp\DotNetZip\src\Zip", "*.cs")
.Concat(Directory.EnumerateFiles(@"C:\Temp\DotNetZip\src\BZip2", "*.cs"))
.Concat(Directory.EnumerateFiles(@"C:\Temp\DotNetZip\src\Zlib", "*.cs"))
.Concat(Directory.EnumerateFiles(@"C:\Temp\DotNetZip\src\CommonSrc", "CRC32.cs"));
var lines = from file in files
from line in File.ReadAllLines(file)
select line;
var usingRegex = new Regex(@"^using (.+?);");
var usings = (from line in lines
let match = usingRegex.Match(line)
let ns = string.Join("", match.Groups[1].Value.Where(ch => !char.IsWhiteSpace(ch)))
where match.Success
select new { line, ns })
// distinct-by ns
.GroupBy(x => x.ns).Select(x => x.First().line);
var remainder = (from line in lines
where !usingRegex.IsMatch(line)
select line);
File.WriteAllLines(
@"C:\Temp\DotNetZip\src\ZipReducedOneFile\DotNetZip.cs",
usings.Concat(remainder));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment