Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#@ template debug="false" hostspecific="true" language="C#" #>
- <#@ assembly name="System.Core" #>
- <#@ assembly name="VSLangProj" #>
- <#@ assembly name="EnvDTE" #>
- <#@ import namespace="EnvDTE" #>
- <#@ import namespace="System" #>
- <#@ import namespace="System.Linq" #>
- <#@ import namespace="System.IO" #>
- <#@ import namespace="System.Text" #>
- <#@ import namespace="System.Collections.Generic" #>
- <#@ output extension=".ts" #>
- <#
- Action<ProjectItem, string, List<ProjectItem>> getAllFilesInternal = null;
- getAllFilesInternal = (item, mask, result) => {
- if (item.FileCount == 0)
- return;
- var fileName = item.FileNames[0];
- if (fileName.EndsWith(mask))
- {
- result.Add(item);
- }
- else
- {
- foreach (ProjectItem subItem in item.ProjectItems)
- {
- getAllFilesInternal(subItem, mask, result);
- }
- }
- };
- Action<ProjectItems, string, List<ProjectItem>> getAllFiles = (items, mask, result) => {
- result.Clear();
- foreach (ProjectItem item in items) {
- getAllFilesInternal(item, mask, result);
- }
- };
- Func<string, string, string> makeRelativePath = (String fromPath, String toPath) =>
- {
- if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
- if (String.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath");
- Uri fromUri = new Uri(fromPath);
- Uri toUri = new Uri(toPath);
- Uri relativeUri = fromUri.MakeRelativeUri(toUri);
- String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
- return relativePath.Replace('/', Path.DirectorySeparatorChar);
- };
- Dictionary<string, ProjectItem> classesInFiles = new Dictionary<string, ProjectItem>();
- Dictionary<string, string> hierarchy = new Dictionary<string, string>();
- Dictionary<ProjectItem, bool> emitted = new Dictionary<ProjectItem, bool>();
- Action<string> emitHierarchy = null;
- emitHierarchy = (className) => {
- if (!classesInFiles.ContainsKey(className))
- return;
- var pi = classesInFiles[className];
- if (emitted.ContainsKey(pi))
- {
- return;
- }
- if (hierarchy.ContainsKey(className)) {
- var parent = hierarchy[className];
- if (!String.IsNullOrEmpty(parent))
- emitHierarchy(parent);
- }
- #>
- ///<reference path="<#=makeRelativePath(Host.ResolvePath("") + "\\", pi.FileNames[0])#>"/>
- <#
- emitted.Add(pi, true);
- };
- Action<ProjectItem, string> writeReferences = null;
- writeReferences = (ProjectItem item, string mask) => {
- if (item.FileCount == 0)
- return;
- var fileName = item.FileNames[0];
- if (fileName.EndsWith(mask))
- {
- #>
- ///<reference path="<#=makeRelativePath(Host.ResolvePath("") + "\\", fileName)#>"/>
- <#
- } else {
- foreach (ProjectItem subItem in item.ProjectItems) {
- writeReferences(subItem, mask);
- }
- }
- };
- var projectName = new DirectoryInfo(Host.ResolvePath("")).Name;
- var serviceProvider = this.Host as IServiceProvider;
- var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
- var inheritancePattern = new System.Text.RegularExpressions.Regex("class ([^ <]*)( extends ([^ <]*))*");
- foreach (Project p in dte.Solution.Projects)
- {
- if (p.Name == projectName)
- {
- foreach (VSLangProj.Reference reference in ((VSLangProj.VSProject)p.Object).References)
- {
- if (reference.SourceProject == null)
- continue;
- foreach (ProjectItem item in reference.SourceProject.ProjectItems)
- {
- if (item.FileCount == 0)
- continue;
- writeReferences(item, "References.ts");
- }
- }
- List<ProjectItem> tsFiles = new List<ProjectItem>();
- getAllFiles(p.ProjectItems, ".ts", tsFiles);
- foreach (ProjectItem item in tsFiles)
- {
- if (item.FileNames[0].EndsWith(".d.ts"))
- continue;
- var file = System.IO.File.ReadAllText(item.FileNames[0]);
- var match = inheritancePattern.Match(file);
- if (match.Groups.Count > 0)
- {
- string className = match.Groups[1].Value;
- string parentName = null;
- if (match.Groups.Count > 2)
- {
- parentName = match.Groups[3].Value;
- }
- if (string.IsNullOrEmpty(className))
- continue;
- if (classesInFiles.ContainsKey(className)) {
- throw new Exception(className + " is duplicite");
- }
- classesInFiles.Add(className, item);
- hierarchy.Add(className, parentName);
- }
- }
- tsFiles = new List<ProjectItem>();
- getAllFiles(p.ProjectItems, ".ts", tsFiles);
- foreach (ProjectItem item in tsFiles)
- {
- if (item.FileCount > 0 && item.FileNames[0].EndsWith("References.ts"))
- continue;
- var fileName = item.FileNames[0];
- bool contains = false;
- foreach (var i in classesInFiles.Values) {
- if (i.FileCount == 0)
- continue;
- if (i.FileNames[0].Equals(fileName)) {
- contains = true;
- break;
- }
- }
- if (!contains)
- {
- if (fileName.EndsWith(".ts"))
- {
- #>
- ///<reference path="<#=makeRelativePath(Host.ResolvePath("") + "\\", fileName)#>"/>
- <#
- }
- }
- }
- foreach (string className in hierarchy.Keys)
- {
- emitHierarchy(className);
- }
- }
- }
- #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement