daveaglick / Scripty

Tools to let you use Roslyn-powered C# scripts for code generation
MIT License
621 stars 69 forks source link

I use Scripty to Default Interface Generation #113

Open ramoneeza opened 7 years ago

ramoneeza commented 7 years ago

Great Tool!!! I think you can include aditional classes as "Walker" per Interface/Classes/Attributes.

This is an example of my Default Interface Generation:

var compilation = Project.Analysis.GetCompilationAsync().Result;
var walker = new ClassWithInterfaceWalker() { Interface = "AutoImplement" };
foreach (var st in compilation.SyntaxTrees) walker.Visit(st.GetRoot());
var classes = walker.Classes.Where(s => s.AttrArguments.Length > 0).OrderBy(s => s.AttrArguments[0]);
var macros = new Dictionary<string, MacroRoslyn>();
foreach(var a in classes)
{
    var interf = a.GetStrippedArgument(0);
    var classname = a.ClassName;
    var fs = $"AutoImplement.{interf}.cs";
    var fe = fs+"macro";
    if (!macros.ContainsKey(interf))
    {
        var pathfe = System.IO.Path.Combine(, fe);
        if (!File.Exists(fe))
        {
            throw new Exception($"Macro no encontrada: {fe}");
        }
        macros.Add(interf, new MacroRoslyn(File.ReadAllLines(pathfe)));
    }
    var macro = macros[interf];
    macro.SetInstance(a.Class);
    var macroresult = new StringBuilder();
    macro.Execute(macroresult);
    Output[fs].Write(macroresult.ToString());
}