Advertisement
Guest User

Untitled

a guest
May 16th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. <#@ template debug="false" hostspecific="true" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ assembly name="VSLangProj" #>
  4. <#@ assembly name="EnvDTE" #>
  5. <#@ import namespace="EnvDTE" #>
  6. <#@ import namespace="System" #>
  7. <#@ import namespace="System.Linq" #>
  8. <#@ import namespace="System.IO" #>
  9. <#@ import namespace="System.Text" #>
  10. <#@ import namespace="System.Collections.Generic" #>
  11. <#@ output extension=".ts" #>
  12. <#
  13.  
  14.     Action<ProjectItem, string, List<ProjectItem>> getAllFilesInternal = null;
  15.     getAllFilesInternal = (item, mask, result) => {
  16.         if (item.FileCount == 0)
  17.             return;
  18.  
  19.         var fileName = item.FileNames[0];
  20.         if (fileName.EndsWith(mask))
  21.         {
  22.             result.Add(item);
  23.         }
  24.         else
  25.         {
  26.             foreach (ProjectItem subItem in item.ProjectItems)
  27.             {
  28.                 getAllFilesInternal(subItem, mask, result);
  29.             }
  30.         }      
  31.     };
  32.  
  33.     Action<ProjectItems, string, List<ProjectItem>> getAllFiles = (items, mask, result) => {
  34.         result.Clear();
  35.         foreach (ProjectItem item in items) {
  36.             getAllFilesInternal(item, mask, result);
  37.         }
  38.     };
  39.  
  40.    
  41.    
  42.     Func<string, string, string> makeRelativePath = (String fromPath, String toPath) =>
  43.     {
  44.         if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
  45.         if (String.IsNullOrEmpty(toPath))   throw new ArgumentNullException("toPath");
  46.  
  47.         Uri fromUri = new Uri(fromPath);
  48.         Uri toUri = new Uri(toPath);
  49.  
  50.         Uri relativeUri = fromUri.MakeRelativeUri(toUri);
  51.         String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
  52.  
  53.         return relativePath.Replace('/', Path.DirectorySeparatorChar);
  54.     };
  55.  
  56.     Dictionary<string, ProjectItem> classesInFiles = new Dictionary<string, ProjectItem>();
  57.     Dictionary<string, string> hierarchy = new Dictionary<string, string>();
  58.     Dictionary<ProjectItem, bool> emitted = new Dictionary<ProjectItem, bool>();
  59.  
  60.     Action<string> emitHierarchy = null;
  61.     emitHierarchy = (className) => {
  62.         if (!classesInFiles.ContainsKey(className))
  63.             return;
  64.         var pi = classesInFiles[className];
  65.         if (emitted.ContainsKey(pi))
  66.         {
  67.             return;
  68.         }
  69.         if (hierarchy.ContainsKey(className)) {
  70.             var parent = hierarchy[className];
  71.             if (!String.IsNullOrEmpty(parent))
  72.                 emitHierarchy(parent);
  73.         }
  74. #>
  75. ///<reference path="<#=makeRelativePath(Host.ResolvePath("") + "\\", pi.FileNames[0])#>"/>
  76. <#
  77.  
  78.         emitted.Add(pi, true);
  79.     };
  80.  
  81.     Action<ProjectItem, string> writeReferences = null;
  82.     writeReferences = (ProjectItem item, string mask) => {
  83.         if (item.FileCount == 0)
  84.             return;
  85.  
  86.             var fileName = item.FileNames[0];
  87.             if (fileName.EndsWith(mask))
  88.             {
  89. #>
  90. ///<reference path="<#=makeRelativePath(Host.ResolvePath("") + "\\", fileName)#>"/>
  91. <#
  92.             } else {
  93.                 foreach (ProjectItem subItem in item.ProjectItems)  {
  94.                     writeReferences(subItem, mask);
  95.                 }
  96.             }
  97.     };
  98.    
  99.     var projectName = new DirectoryInfo(Host.ResolvePath("")).Name;
  100.  
  101.     var serviceProvider = this.Host as IServiceProvider;
  102.     var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
  103.  
  104.     var inheritancePattern = new System.Text.RegularExpressions.Regex("class ([^ <]*)( extends ([^ <]*))*");
  105.  
  106.     foreach (Project p in dte.Solution.Projects)
  107.     {
  108.         if (p.Name == projectName)
  109.         {
  110.        
  111.             foreach (VSLangProj.Reference reference in ((VSLangProj.VSProject)p.Object).References)
  112.             {
  113.                 if (reference.SourceProject == null)
  114.                     continue;
  115.                
  116.                 foreach (ProjectItem item in reference.SourceProject.ProjectItems)
  117.                 {
  118.                     if (item.FileCount == 0)
  119.                         continue;
  120.        
  121.                     writeReferences(item, "References.ts");
  122.                 }
  123.             }
  124.  
  125.  
  126.             List<ProjectItem> tsFiles = new List<ProjectItem>();
  127.             getAllFiles(p.ProjectItems, ".ts", tsFiles);
  128.  
  129.             foreach (ProjectItem item in tsFiles)
  130.             {
  131.                 if (item.FileNames[0].EndsWith(".d.ts"))
  132.                     continue;
  133.                 var file = System.IO.File.ReadAllText(item.FileNames[0]);
  134.                 var match = inheritancePattern.Match(file);
  135.        
  136.                 if (match.Groups.Count > 0)
  137.                 {
  138.                     string className = match.Groups[1].Value;
  139.                     string parentName = null;
  140.                     if (match.Groups.Count > 2)
  141.                     {
  142.                         parentName = match.Groups[3].Value;
  143.                     }
  144.                     if (string.IsNullOrEmpty(className))
  145.                         continue;
  146.                    
  147.                     if (classesInFiles.ContainsKey(className)) {
  148.                         throw new Exception(className + " is duplicite");
  149.                     }
  150.                     classesInFiles.Add(className, item);
  151.                     hierarchy.Add(className, parentName);
  152.                
  153.                 }
  154.             }
  155.  
  156.  
  157.             tsFiles = new List<ProjectItem>();
  158.             getAllFiles(p.ProjectItems, ".ts", tsFiles);
  159.  
  160.             foreach (ProjectItem item in tsFiles)
  161.             {
  162.                 if (item.FileCount > 0 && item.FileNames[0].EndsWith("References.ts"))
  163.                     continue;
  164.  
  165.                 var fileName = item.FileNames[0];
  166.  
  167.                 bool contains = false;
  168.                 foreach (var i in classesInFiles.Values) {
  169.                     if (i.FileCount == 0)
  170.                         continue;
  171.                     if (i.FileNames[0].Equals(fileName)) {
  172.                         contains = true;
  173.                         break;
  174.                     }
  175.                 }
  176.                 if (!contains)
  177.                 {
  178.                     if (fileName.EndsWith(".ts"))
  179.                     {
  180. #>
  181. ///<reference path="<#=makeRelativePath(Host.ResolvePath("") + "\\", fileName)#>"/>
  182. <#
  183.                     }
  184.                 }
  185.                
  186.             }
  187.  
  188.             foreach (string className in hierarchy.Keys)
  189.             {
  190.                 emitHierarchy(className);
  191.             }
  192.  
  193.  
  194.  
  195.  
  196.         }
  197.     }
  198. #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement