Advertisement
Guest User

Untitled

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