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;
}
}
}
#>

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

@ -1,18 +1,18 @@
<#
{
var beforeGenerateLinqToDBModel = BeforeGenerateLinqToDBModel;
var afterGenerateLinqToDBModel = AfterGenerateLinqToDBModel;
var obsoleteTables = new List<Tuple<string,string,string>>();
BeforeGenerateLinqToDBModel = () =>
{
beforeGenerateLinqToDBModel();
foreach (var table in Tables.Values)
{
var idx = table.Description.IndexOf("[Obsolete");
<#
{
var beforeGenerateLinqToDBModel = BeforeGenerateLinqToDBModel;
var afterGenerateLinqToDBModel = AfterGenerateLinqToDBModel;
var obsoleteTables = new List<Tuple<string,string,string>>();
BeforeGenerateLinqToDBModel = () =>
{
beforeGenerateLinqToDBModel();
foreach (var table in Tables.Values)
{
var idx = table.Description.IndexOf("[Obsolete");
if (idx >= 0)
{
var idx2 = table.Description.IndexOf(']', idx);
@ -24,77 +24,77 @@
var info = Tuple.Create(table.Schema, table.Name, text);
if (obsoleteTables.All(a => a != info))
obsoleteTables.Add(info);
table.Attributes.Add(attr);
obsoleteTables.Add(info);
table.Attributes.Add(attr);
table.Description = table.Description.Substring(0, idx) + table.Description.Substring(idx2 + 1);
}
}
foreach (var c in table.Columns.Values)
{
idx = c.Description.IndexOf("[Obsolete");
if (idx >= 0)
{
}
foreach (var c in table.Columns.Values)
{
idx = c.Description.IndexOf("[Obsolete");
if (idx >= 0)
{
var idx2 = c.Description.IndexOf(']', idx);
if (idx2 > idx)
{
var attr = new Attribute(c.Description.Substring(idx + 1, idx2 - idx - 1));
c.Attributes.Add(attr);
c.Attributes.Add(attr);
c.Description = c.Description.Substring(0, idx) + c.Description.Substring(idx2 + 1);
}
}
}
}
};
AfterGenerateLinqToDBModel = () =>
{
foreach (var tableInfo in obsoleteTables)
{
var schema = tableInfo.Item1;
var name = tableInfo.Item2;
var text = tableInfo.Item3;
var obsoleteAttr = new Attribute(text);
foreach (var cm in GetTreeNodes(Model)
.OfType<MemberBase>()
.Where(t => t.Type != null)
.Where(t => t.Type == name || t.Type.Contains("<" + name + ">")))
{
// check schema
if (cm.Parent != null && cm.Parent.Parent != null)
{
var parent = cm.Parent.Parent;
if (parent is Table)
{
var table = (Table)parent;
if (schema == table.Schema)
if (cm.Attributes.All(a => a.Name != text))
cm.Attributes.Add(obsoleteAttr);
}
else if (parent is Class)
{
var cls = (Class)parent;
bool parentClassIncludesSchemaName = cls.Name.Equals(schema + "Schema", StringComparison.InvariantCultureIgnoreCase);
bool classIsForDefaultSchema = cls.Name == DataContextName;
bool isExtensionMethod = cls.Parent is Namespace || cls.Name == "TableExtensions";
if (classIsForDefaultSchema || parentClassIncludesSchemaName || isExtensionMethod)
if (cm.Attributes.All(a => a.Name != text))
cm.Attributes.Add(obsoleteAttr);
}
}
}
}
afterGenerateLinqToDBModel();
};
}
#>
}
}
}
};
AfterGenerateLinqToDBModel = () =>
{
foreach (var tableInfo in obsoleteTables)
{
var schema = tableInfo.Item1;
var name = tableInfo.Item2;
var text = tableInfo.Item3;
var obsoleteAttr = new Attribute(text);
foreach (var cm in GetTreeNodes(Model)
.OfType<MemberBase>()
.Where(t => t.Type != null)
.Where(t => t.Type == name || t.Type.Contains("<" + name + ">")))
{
// check schema
if (cm.Parent != null && cm.Parent.Parent != null)
{
var parent = cm.Parent.Parent;
if (parent is Table)
{
var table = (Table)parent;
if (schema == table.Schema)
if (cm.Attributes.All(a => a.Name != text))
cm.Attributes.Add(obsoleteAttr);
}
else if (parent is Class)
{
var cls = (Class)parent;
bool parentClassIncludesSchemaName = cls.Name.Equals(schema + "Schema", StringComparison.InvariantCultureIgnoreCase);
bool classIsForDefaultSchema = cls.Name == DataContextName;
bool isExtensionMethod = cls.Parent is Namespace || cls.Name == "TableExtensions";
if (classIsForDefaultSchema || parentClassIncludesSchemaName || isExtensionMethod)
if (cm.Attributes.All(a => a.Name != text))
cm.Attributes.Add(obsoleteAttr);
}
}
}
}
afterGenerateLinqToDBModel();
};
}
#>

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

@ -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>
//---------------------------------------------------------------------------------------------------