Added MultipleFiles.ttinclude.

This commit is contained in:
Igor Tkachev 2017-08-31 21:24:19 -04:00
Родитель 629a307c4b
Коммит 7d090ebba6
8 изменённых файлов: 217 добавлений и 79 удалений

Просмотреть файл

@ -22,6 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Templates", "Templates", "{
Templates\LinqToDB.SqlServer.ttinclude = Templates\LinqToDB.SqlServer.ttinclude
Templates\LinqToDB.Sybase.ttinclude = Templates\LinqToDB.Sybase.ttinclude
Templates\LinqToDB.ttinclude = Templates\LinqToDB.ttinclude
Templates\MultipleFiles.ttinclude = Templates\MultipleFiles.ttinclude
Templates\NotifyPropertyChanged.ttinclude = Templates\NotifyPropertyChanged.ttinclude
Templates\ObsoleteAttributes.ttinclude = Templates\ObsoleteAttributes.ttinclude
Templates\PluralizationService.ttinclude = Templates\PluralizationService.ttinclude

Просмотреть файл

@ -0,0 +1,70 @@
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="EnvDTE" #>
<#+
DTE _dte;
DTE DTE => _dte ?? (_dte = (DTE)((IServiceProvider)Host).GetService(typeof(DTE)));
ProjectItem _templateProjectItem;
ProjectItem TemplateProjectItem => _templateProjectItem ?? (_templateProjectItem = DTE.Solution.FindProjectItem(Host.TemplateFile));
readonly Dictionary<string,int> _fileNames = new Dictionary<string,int>();
Func<string,string,bool> CompareContent = (s1,s2) => s1 == s2;
void SaveOutput(string fileName, int fileType = 1)
{
var dir = Path.GetDirectoryName(Host.TemplateFile);
var output = Path.Combine(dir, fileName);
var newContent = GenerationEnvironment.ToString();
var oldContent = File.Exists(output) ? File.ReadAllText(output) : "";
if (!CompareContent(newContent, oldContent))
{
if (DTE.SourceControl != null && DTE.SourceControl.IsItemUnderSCC(output) && !DTE.SourceControl.IsItemCheckedOut(output))
DTE.SourceControl.CheckOutItem(output);
File.WriteAllText(output, newContent);
}
GenerationEnvironment.Length = 0;
_fileNames.Add(output, fileType);
}
void SyncProject()
{
var keepFileNames = _fileNames.ToDictionary(f => f.Key);
var projectFiles = new Dictionary<string,ProjectItem>();
var templateFileName = TemplateProjectItem.FileNames[0];
var originalFilePrefix = Path.GetFileNameWithoutExtension(templateFileName) + ".";
foreach (ProjectItem projectItem in TemplateProjectItem.ProjectItems)
{
projectFiles.Add(projectItem.FileNames[0], projectItem);
}
foreach (var pair in projectFiles)
{
if (!keepFileNames.ContainsKey(pair.Key))
if (!(Path.GetFileNameWithoutExtension(pair.Key) + ".").StartsWith(originalFilePrefix))
//if (pair.Key != templateFileName)
pair.Value.Delete();
}
// Add missing files to the project.
//
foreach (var fileName in keepFileNames)
{
if (!projectFiles.ContainsKey(fileName.Value.Key))
if (File.Exists(fileName.Value.Key))
{
var newItem = TemplateProjectItem.ProjectItems.AddFromFile(fileName.Value.Key);
newItem.Properties.Item("BuildAction").Value = fileName.Value.Value;
}
}
}
#>

Просмотреть файл

@ -0,0 +1,6 @@
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------

Просмотреть файл

@ -0,0 +1,29 @@
<#@ template language="C#" debug="True" hostSpecific="True" #>
<#@ output extension=".generated.cs" #>
<#@ include file="..\..\Templates\MultipleFiles.ttinclude" #>
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------
<#
SaveOutput("aaa.cs", 1);
#>
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------
<#
SaveOutput("bbb.txt", 2);
SyncProject();
#>
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------

Просмотреть файл

@ -41,6 +41,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="aaa.cs">
<DependentUpon>MultipleFiles.tt</DependentUpon>
</Compile>
<Compile Include="EditableModelTest.generated.cs">
<DependentUpon>EditableModelTest.tt</DependentUpon>
<AutoGen>True</AutoGen>
@ -52,6 +55,11 @@
<DesignTime>True</DesignTime>
<DependentUpon>ModelTest.tt</DependentUpon>
</Compile>
<Compile Include="MultipleFiles.generated.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>MultipleFiles.tt</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@ -67,6 +75,17 @@
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<None Include="MultipleFiles.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>MultipleFiles.generated.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="bbb.txt">
<DependentUpon>MultipleFiles.tt</DependentUpon>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

7
Tests/Tests/aaa.cs Normal file
Просмотреть файл

@ -0,0 +1,7 @@
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------

6
Tests/Tests/bbb.txt Normal file
Просмотреть файл

@ -0,0 +1,6 @@
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------