/
TextTemplate.tt
103 lines (99 loc) · 3.58 KB
/
TextTemplate.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
<#
string fieldPrefix = "_element";
string projectPath = Host.ResolveAssemblyReference("$(ProjectDir)");
foreach (string file in Directory.EnumerateFiles(projectPath, "*.Fields.cs"))
{
string pageName = file.Split(new char[] { '\\' }).Last().Split(new char[] { '.' })[0];
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace TextTemplate
{
public partial class <#=pageName#>
{
<#
IEnumerable<string> lines = File.ReadLines(file).Select(line => line.Trim()).Where(line => line.StartsWith("private") && line.Contains(" _"));
foreach(string line in lines)
{
string fieldName = line.Split(new char[] { ' ' })[2].Replace(";", string.Empty);
char firstChar = fieldName.Replace(fieldPrefix, string.Empty).ToCharArray()[0];
string firstCharToUpper = firstChar.ToString().ToUpper();
string propertyName = firstCharToUpper + fieldName.Substring(fieldPrefix.Length + 1);
#>
public string <#=propertyName#>
{
get
{
return <#=fieldName#>;
}
set
{
<#=fieldName#> = value;
}
}
<#
}
#>
}
}
<#
// End of file.
SaveOutput(pageName + ".GenProp.cs");
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.ComponentModel.DataAnnotations;
namespace TextTemplate.Models
{
public partial class <#=pageName#>Model
{
<#
lines = File.ReadLines(file).Select(line => line.Trim()).Where(line => line.StartsWith("private") && line.Contains(" _"));
foreach(string line in lines)
{
string fieldName = line.Split(new char[] { ' ' })[2].Replace(";", string.Empty);
char firstChar = fieldName.Replace(fieldPrefix, string.Empty).ToCharArray()[0];
string firstCharToUpper = firstChar.ToString().ToUpper();
string propertyName = firstCharToUpper + fieldName.Substring(fieldPrefix.Length + 1);
if (pageName == "LoginPage" && propertyName == "UserName") Write(" [Required(ErrorMessage = \"UserName is required.\")]\r\n");
#>
public string <#=propertyName#> { get; set; }
<#
}
#>
}
}
<#
// End of file.
SaveOutput("Models\\" + pageName + "Model.GenProp.cs");
}
#>
<#+
private void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>