用t4模板和head.js进行css和js的版本控制

head.js  介绍 http://headjs.com/site/api/v1.00.html#load

原文http://www.cnblogs.com/wang2650/p/5102690.html

 

t4模板的使用 请自行搜索

  Custom.ttinclude 主要是获取项目的路径

jscssconfig.xml 配置文件,保存要进行版本控制的js和css文件, 包括文件的路径和文件的最后一次修改时间. 如果时间不写,默认是改写为当前时间.

Common.tt 模板文件 . 读取xml配置文件的内容,读取其中的文件列表,根据文件的最后修改日期和配置中的日期进行比较,生成文件版本号.最后保存文件列表中文件的修改日期到配置文件.

最后common.js的内容  为

head.load("/js/wxq.js?ver=20160105140146","/js/jquery-1.9.1.min.js?ver=20141023171022",function() {}); 

wxq.js也可以是css文件.  最后一个是回调函数.

 

模板文件 

1
Common.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<#@ template language="C#" hostspecific="True"#>
<#@include file="Custom.ttinclude"#>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Linq" #>
<#@ assembly name="System.Configuration" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ output extension=".js" #>
<#
 
   String  configFilePath=@"\js\jscssconfig.xml";
   XmlDocument doc = new XmlDocument();
   doc.Load(GetProjectPath()+configFilePath);
    XmlNodeList fileinfoNodeList = doc.SelectNodes("/FileList/FileInfo");
    if (fileinfoNodeList != null){#>head.load(<#
 
         foreach (XmlNode fileNode in fileinfoNodeList)
           {
           XmlElement xeFileInfo=(XmlElement)fileNode;
            if( string.IsNullOrEmpty( xeFileInfo.Attributes["updateDateTime"].Value))//如果找到
            {
             xeFileInfo.SetAttribute("updateDateTime", DateTime.Now.ToString());
  
            }
            else{
               FileInfo fi = new FileInfo(GetProjectPath()+ fileNode.InnerText.Replace(@"/",@"\"));
                if( fi.LastWriteTime> Convert.ToDateTime( xeFileInfo.Attributes["updateDateTime"].Value ))
                {
                 xeFileInfo.SetAttribute("updateDateTime", fi.LastWriteTime.ToString());
                }
                 
             }
             #>"<#=fileNode.InnerText#>?ver=<#= Convert.ToDateTime(xeFileInfo.Attributes["updateDateTime"].Value).ToString("yyyyMMddHHMMss")#>",<#}#>function() {}); <#
     }
 
   doc.Save (GetProjectPath()+configFilePath) ;
 
 #>

  

 

 

Custom.ttinclude 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<#@ template language="C#v3.5" debug="true" hostspecific="True" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Configuration" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Data.Common" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Windows.Forms" #>
<#+
   public EnvDTE.Project GetCurrentProject()  {
    IServiceProvider _ServiceProvider = (IServiceProvider)Host;
    if (_ServiceProvider == null)
        throw new Exception("Host property returned unexpected value (null)");
 
   EnvDTE.DTE dte = (EnvDTE.DTE)_ServiceProvider.GetService(typeof(EnvDTE.DTE));
    if (dte == null)throw new Exception("Unable to retrieve EnvDTE.DTE");
     
    Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
    if (activeSolutionProjects == null)
        throw new Exception("DTE.ActiveSolutionProjects returned null");
    EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
    if (dteProject == null)
        throw new Exception("DTE.ActiveSolutionProjects[0] returned null");
     
    return dteProject;
 
}
   public string GetProjectPath()
    {
        EnvDTE.Project project = GetCurrentProject();
        System.IO.FileInfo info = new System.IO.FileInfo(project.FullName);
         return info.Directory.FullName;
    }
#>

  

jscssconfig.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<FileList>
  <FileInfo updateDateTime="2016/1/5 14:40:46"><![CDATA[/js/wxq.js]]></FileInfo>
  <FileInfo updateDateTime="2014/10/23 17:59:22"><![CDATA[/js/jquery-1.9.1.min.js]]></FileInfo>
</FileList>

  

 

posted @   过错  阅读(759)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示