Skip to content

Instantly share code, notes, and snippets.

@maxkoryukov
Created August 16, 2016 04:28
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 maxkoryukov/20ba5df3f1453d003efd69783d777657 to your computer and use it in GitHub Desktop.
Save maxkoryukov/20ba5df3f1453d003efd69783d777657 to your computer and use it in GitHub Desktop.
CSharp Remove Comments
using System;
using System.IO;
using CSharpMinifier;
namespace MinifySolution
{
class Program
{
static void Main(string[] args)
{
var mini = new DirMinifier();
// put your directory
mini.Minify(@"C:\tmp\src");
}
}
public class DirMinifier
{
public bool Inplace { get; set; }
public void Minify(string root)
{
var minifierOptions = new MinifierOptions
{
// COULD BREAK YOUR BUILD:
LocalVarsCompressing = false,
MembersCompressing = false,
TypesCompressing = false,
MiscCompressing = false,
ConsoleApp = false,
NamespacesRemoving = false,
ToStringMethodsRemoving = false,
PublicCompressing = false,
EnumToIntConversion = false,
// SAFE TO CHANGE:
RegionsRemoving = true,
LineLength = 120,
SpacesRemoving = false
CommentsRemoving = true,
};
var m = new Minifier(minifierOptions);
var dir = new DirectoryInfo(this.Root);
var files = dir.GetFiles("*.cs", SearchOption.AllDirectories);
foreach (var f in files)
{
this.MinifyFile(f, m);
}
}
private string MiniName(FileInfo f)
{
if (this.Inplace)
return f.FullName;
var ext = f.Extension;
var old = f.FullName;
var newname = old.Remove(old.Length - ext.Length, ext.Length) + ".min.cs";
return newname;
}
protected void MinifyFile(FileInfo f, Minifier minifier)
{
var content = "";
using (var sr = f.OpenText())
{
content = sr.ReadToEnd();
}
var status = "[?]";
var result = "";
try
{
result = minifier.MinifyFromString(content);
status = "[+]";
}
catch
{
result = content;
status = "[!]";
}
var newname = this.MiniName(f);
using (var sw = new StreamWriter(newname))
{
sw.Write(result);
}
using (var st = new StreamWriter(@"minify-status.log", true))
{
st.WriteLine("{1} : [{0}]", f.FullName, status);
Console.WriteLine("{1} : [{0}]", f.FullName, status);
}
}
*/
}
}
@maxkoryukov
Copy link
Author

Program

This is simple program, which utilizes the https://github.com/KvanTTT/CSharp-Minifier library. You need just CSharpMinifier.dll, without GUI. Just create new console project, compile and run.

The program will remove all comments from *.cs files in C:\tmp\src directory

@maxkoryukov
Copy link
Author

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