t4模板生成 _references.js 文件

t4模板功能:
  指定目录,自动生成 _references.js 文件

相关文件说明:
  _references.js 实现js智能提示。

注意事项:
  hostspecific="true"
  
<#@ template debug="false" hostspecific="true" language="C#" #>
  projectDir=Host.ResolveAssemblyReference("$(ProjectDir)");

源码:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".js" #>
<#
    projectDir=Host.ResolveAssemblyReference("$(ProjectDir)");
    searchPaths=new string[]{"Scripts","Content\\ExtJs","Views"};
    foreach(var person in this.GetJavascriptList()) {
    #>
/// <reference path="<#= person #>" />
<#    }    #>

<#+ 
    string projectDir;
    string[] searchPaths;
    // 开始
    public List<string> GetJavascriptList()
    {
        List<string> fileList=new List<string>();

        foreach(var i in searchPaths){
            GetFileList(Path.Combine(projectDir,i),ref fileList);
        }
        return fileList;
    }

    // 取单个目录Js文件列表
    private void GetFileList(string path, ref List<string> fileList){
        var files=Directory.GetFiles(path);
        var query=from i in files.Where(t=>validFile(t))
            select formatPath(i);
        foreach(var i in query){
            fileList.Add(i);
        }

        var folders=Directory.GetDirectories(path);
        foreach(var fo in folders){
            GetFileList(fo,ref fileList);
        }
    }

    // 过滤文件
    private bool validFile(string filePath){
        var vaild=Path.GetExtension(filePath)==".js" && !filePath.Contains("_references.js");
        return vaild;
    }

    // 格式化输出路径
    private string formatPath(string filePath){
        return filePath.ToLower()
            .Replace(projectDir,"~")
            .Replace("\\","/");
    }

#>

 

 

 

posted @ 2014-06-21 23:11  眼镜兄  阅读(551)  评论(0编辑  收藏  举报