Skip to content

Instantly share code, notes, and snippets.

@codingawayy
Last active January 11, 2024 16:00
Show Gist options
  • Save codingawayy/bfa87f0aeda1204091fe to your computer and use it in GitHub Desktop.
Save codingawayy/bfa87f0aeda1204091fe to your computer and use it in GitHub Desktop.
T4 template to generate list of files that are part of the project. This template is useful for generating list of css/js resources to be put in bundle. The value comes from the fact that files which are not part of the project (e.g. - excluded files) will not actually be included in the resulting list.
// This is an auto-generated file.
<#@ template language="C#" hostSpecific="true" debug="True" #>
<#@ output extension="cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDte" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System" #>
<#
/***************************************************************************************************
T4 template to generate list of files that are part of the project. This template is useful for
generating list of css/js resources to be put in bundle. The value comes from the fact that files
which are not part of the project (e.g. - excluded files) will not actually be included in the
resulting list.
(https://gist.github.com/niaher/bfa87f0aeda1204091fe)
***************************************************************************************************/
var visualStudio = (this.Host as IServiceProvider).GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
var project = visualStudio.Solution.FindProjectItem(this.Host.TemplateFile).ContainingProject as EnvDTE.Project;
string root = Host.ResolvePath("");
var projectItems = GetProjectItemsRecursively(project.ProjectItems);
#>
public static class Resources
{
public static string[] AppJavascript = {
<# ListFiles("/Scripts/App", "js", projectItems); #>
};
public static string[] AppCss = {
<# ListFiles("/Scripts/App", "css", projectItems); #>
<# ListFiles("/Styles", "css", projectItems); #>
};
}
<#+
public void ListFiles(string path, string fileType, List<string> projectItems)
{
var root = Host.ResolvePath("");
var fileNames = Directory.EnumerateFiles(root + path, "*." + fileType, SearchOption.AllDirectories)
.Where(f => !f.EndsWith("intellisense.js"))
.Select(f => f.Replace("\\", "/"))
.Where(f => projectItems.Any(p => p.Equals(f, StringComparison.OrdinalIgnoreCase)))
.Select(f => "~" + f.Substring(root.Length))
.OrderBy(f => f)
.ToList();
foreach(string fileName in fileNames)
{
WriteLine(string.Format("\t\t\"{0}\",", fileName));
}
}
public List<string> GetProjectItemsRecursively(EnvDTE.ProjectItems items)
{
var ret = new List<string>();
if (items == null) return ret;
foreach(EnvDTE.ProjectItem item in items)
{
string result = item.FileNames[1].Replace("\\", "/");
// If not folder.
if (result[result.Length - 1] != '\\')
{
ret.Add(result);
}
ret.AddRange(GetProjectItemsRecursively(item.ProjectItems));
}
return ret;
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment