zoukankan      html  css  js  c++  java
  • 用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文件.  最后一个是回调函数.

    模板文件 

    Common.tt
    <#@ 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=@"jsjscssconfig.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 文件

    <#@ 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

    <?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>
    

      

  • 相关阅读:
    表的转置 行转列: DECODE(Oracle) 和 CASE WHEN 的异同点
    Sql中EXISTS与IN的使用及效率
    Oracle学习之start with...connect by子句的用法
    Java复制、移动和删除文件
    简单的实现微信获取openid
    SQL语句中LEFT JOIN、JOIN、INNER JOIN、RIGHT JOIN的区别?
    java 基础最全网站
    SpringBoot(十一)过滤器和拦截器
    做项目遇到的问题集锦
    使用Java实现二叉树的添加,删除,获取以及遍历
  • 原文地址:https://www.cnblogs.com/wang2650/p/5102690.html
Copyright © 2011-2022 走看看