Advertisement
Guest User

Untitled

a guest
Dec 21st, 2015
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6.  
  7. namespace UnityEditor.T4Templating
  8. {
  9.     public sealed class T4TemplatePostProcessor : AssetPostprocessor
  10.     {
  11.         [MenuItem("Assets/Force T4 Detection", priority=1000)]
  12.         public static void OnGeneratedCSProjectFiles()
  13.         {
  14.             string[] Starts = { "<Compile Include=\"" , "<None Include=\"" };
  15.             const string End = "\" />";
  16.             string[] GeneratedLines = {
  17. @"<AutoGen>True</AutoGen>",
  18. @"<DesignTime>True</DesignTime>",
  19. @"<DependentUpon>{0}</DependentUpon>",
  20.                                       };
  21.             string[] TemplateLines = {
  22. "<None Include=\"{0}\">",
  23. "<Generator>TextTemplatingFileGenerator</Generator>",
  24. "<LastGenOutput>{1}</LastGenOutput>",
  25. "</None>",
  26.                                      };
  27.             const string ItemGroupEnd = "  </ItemGroup>";
  28.             List<string> TemplatesFiles = null;
  29.             List<string> TemplateTargets = null;
  30.             bool AnyTemplates = false;
  31.             bool AnyModifications = false;
  32.             string projectDirectory = Directory.GetCurrentDirectory();
  33.             const string searchPattern = "Assembly-CSharp*-vs.csproj";
  34.             const SearchOption searchOption = SearchOption.TopDirectoryOnly;
  35.             string[] projectFiles;
  36.             try
  37.             {
  38.                 projectFiles = Directory.GetFiles(projectDirectory, searchPattern, SearchOption.TopDirectoryOnly);
  39.             }
  40.             catch(System.Exception e )
  41.             {
  42.                 UnityEngine.Debug.LogException(e);
  43.                 UnityEngine.Debug.LogError(projectDirectory + " " + searchPattern + " " + searchOption );
  44.                 return;
  45.             }
  46.             foreach (var file in projectFiles)
  47.             {
  48.                 string tempProjectFile = FileUtil.GetUniqueTempPathInProject() + " " + Path.GetFileName(file);
  49.                 using (var reader = new StreamReader(file))
  50.                 using ( var writer = new StreamWriter(tempProjectFile) )
  51.                 {
  52.                     string line;
  53.                     while ( (line = reader.ReadLine()) != null )
  54.                     {
  55.                         if( line.EndsWith(End) )
  56.                         {
  57.                             bool ActedOn = false;
  58.                             foreach( var Start in Starts )
  59.                             {
  60.                                 int startIndex = line.IndexOf(Start);
  61.                                 if( startIndex != -1 )
  62.                                 {
  63.                                     startIndex += Start.Length;
  64.  
  65.                                     int lengthFromIndex = line.Length - ( startIndex + End.Length );
  66.                                     // there should be no quotes in here..
  67.                                     if (line.IndexOf('\"', startIndex, lengthFromIndex) == -1)
  68.                                     {
  69.                                         string path = line.Substring(startIndex, lengthFromIndex);
  70.  
  71.                                         string withTT = Path.Combine(
  72.                                             Path.GetDirectoryName(path),
  73.                                             Path.GetFileNameWithoutExtension(path) + ".tt" );
  74.  
  75.                                         if (File.Exists(withTT))
  76.                                         {
  77.                                             string line_stripped_exec = line.Remove(line.Length - 2, 1);
  78.  
  79.                                             int starterPos = Start.IndexOf('<') + 1;
  80.                                             string manual_exec =
  81.                                                 string.Concat(
  82.                                                     Start.Remove(starterPos),
  83.                                                     "/",
  84.                                                     Start.Substring(starterPos, Start.IndexOf(' ', starterPos) - starterPos),
  85.                                                     ">");
  86.  
  87.                                             object ttFileName = Path.GetFileName(withTT);
  88.  
  89.                                             writer.WriteLine(line_stripped_exec);
  90.  
  91.                                             AnyModifications = true;
  92.  
  93.                                             foreach (var generatedLineInsert in GeneratedLines)
  94.                                             {
  95.                                                 writer.WriteLine(generatedLineInsert, ttFileName);
  96.                                             }
  97.                                             writer.WriteLine(manual_exec);
  98.  
  99.                                             if (!AnyTemplates)
  100.                                             {
  101.                                                 TemplatesFiles = new List<string>();
  102.                                                 TemplateTargets = new List<string>();
  103.                                                 AnyTemplates = true;
  104.                                             }
  105.                                             TemplatesFiles.Add(withTT);
  106.                                             TemplateTargets.Add(Path.GetFileName(path));
  107.                                             ActedOn = true;
  108.                                             break;
  109.                                         }
  110.                                     }
  111.                                 }
  112.                             }
  113.                             if (ActedOn)
  114.                                 continue;
  115.                         }
  116.                         else if (AnyTemplates && line == ItemGroupEnd)
  117.                         {
  118.                             object[] args = new object[2];
  119.  
  120.                             for (int c = TemplatesFiles.Count, i = 0; i < c; i++)
  121.                             {
  122.                                 args[0] = TemplatesFiles[i];
  123.                                 args[1] = TemplateTargets[i];
  124.  
  125.                                 foreach (var templateLine in TemplateLines)
  126.                                     writer.WriteLine(templateLine, args);
  127.                                 AnyModifications = true;
  128.                             }
  129.                             AnyTemplates = false;
  130.                         }
  131.                         writer.WriteLine(line);
  132.                     }
  133.                 }
  134.                 if (AnyModifications)
  135.                 {
  136.                     File.Copy(tempProjectFile, file, true);
  137.                 }
  138.                 File.Delete(tempProjectFile);
  139.             }
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement