Skip to content

Instantly share code, notes, and snippets.

@matejskubic
Created August 31, 2017 14:26
Show Gist options
  • Save matejskubic/be22a072468d235c9bc4bc40f542cf6a to your computer and use it in GitHub Desktop.
Save matejskubic/be22a072468d235c9bc4bc40f542cf6a to your computer and use it in GitHub Desktop.
Implement Interface by T4 Text Template Transformation
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#@ output extension=".cs" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
EnvDTE.DTE dte = serviceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
Array projects = dte.ActiveSolutionProjects as Array;
EnvDTE.Project p = projects.GetValue(0) as EnvDTE.Project;
EnvDTE.CodeElement cmIntegration = p.CodeModel.CodeElements.OfType<EnvDTE.CodeElement>().Single(f => f.Name == "Integration");
IEnumerable<CodeClass> classes = FindClasses(p.CodeModel.CodeElements, "NapakaSporocilo", "KodaInSporocilo");
IEnumerable<IGrouping<string, CodeClass>> classesByNamespace = classes.GroupBy(f => f.Namespace.Name);
foreach (IGrouping<string, CodeClass> nsGroup in classesByNamespace)
{
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace <#= nsGroup.Key #>
{<#
foreach (CodeClass cc in nsGroup)
{
switch (cc.Name)
{
case "NapakaSporocilo":
#>
partial class <#= cc.Name #> : INapakaSporocilo
{
public IKodaInSporocilo[] INapake
{
get { return Napake; }
}
}
<#
break;
case "KodaInSporocilo":
#>
partial class <#= cc.Name #> : IKodaInSporocilo
{
}
<#
break;
}
}
#>
}
<#
}
#>
<#+
private IEnumerable<CodeClass> FindClasses(CodeElements elements, params string[] classNames)
{
foreach (CodeElement element in elements)
{
//WriteLine("{0}, {1} {2}",element.FullName, element.Kind, element is CodeClass);
if (!element.FullName.StartsWith("Integration"))
{
continue;
}
if(element is CodeClass)
{
CodeClass c = element as CodeClass;
if (c != null)
{
//WriteLine("ž1{0}, {1} {2}", c.FullName, c.Access, element is CodeClass2);
if (Array.IndexOf(classNames, c.Name) >= 0)
{
yield return c;
}
foreach (var cc in FindClasses(c.Members, classNames))
{
yield return cc;
}
}
}
if (element is CodeNamespace)
{
CodeNamespace ns = element as CodeNamespace;
if (ns != null)
{
foreach (var cc in FindClasses(ns.Members, classNames))
{
yield return cc;
}
}
}
}
yield break;
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment