<%@ CodeTemplate Language="C#" TargetLanguage="Text" Debug="True" Description="Template description here." %>
<%@ Property Name="Tables" Type="SchemaExplorer.TableSchemaCollection" Category="Source" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Design" %>
<%
// This is done so that we can make some assumptions about what each table
// can contain.
Tables = TableUtilities.FilterTables(Tables);
%>
<% if(!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); %>
<%
//Load Sub Template
CodeTemplate ModelObjectTemplate = GetCodeTemplate("NewTemplate10.cst");
%>
<%
string ModelDirectory = OutputDirectory + "\\Build\\";
![](/Images/OutliningIndicators/None.gif)
if(!Directory.Exists(ModelDirectory)) Directory.CreateDirectory(ModelDirectory);
%>
![](/Images/OutliningIndicators/None.gif)
Create completed!
<%
//Render Files
for (int i = 0; i < Tables.Count; i++)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
RenderFileUsingProperties(ModelDirectory + "CS_"+Tables[i].Name + ".txt",true,ref ModelObjectTemplate,Tables[i]);
}
%>
<script runat="template">
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
Output#region Output
private string _outputDirectory = String.Empty;
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
[Category("General")]
[Description("The directory to output the results to.")]
[DefaultValue("")]
public string OutputDirectory
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_outputDirectory.Length == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return @"F:\";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _outputDirectory;
}
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
_outputDirectory = value;
}
}
#endregion
![](/Images/OutliningIndicators/ContractedBlock.gif)
Get Template#region Get Template
public CodeTemplate GetCodeTemplate(string TemplateName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//CodeTemplate template=null;
CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+TemplateName);
compiler.CodeTemplateInfo.ToString();
compiler.Compile();
if (compiler.Errors.Count == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return compiler.CreateInstance();
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Text.StringBuilder errorMessage = new System.Text.StringBuilder();
for (int i = 0; i < compiler.Errors.Count; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
errorMessage.Append(compiler.Errors[i].ToString()).Append("\n");
}
throw new ApplicationException(errorMessage.ToString());
}
return null;
}
#endregion
![](/Images/OutliningIndicators/ContractedBlock.gif)
Inner Classes#region Inner Classes
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
TableUtilities Class#region TableUtilities Class
public class TableUtilities
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static bool SkipTable(TableSchema table)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (table.PrimaryKey == null || table.Columns.Count < 2);
}
public static TableSchemaCollection FilterTables(TableSchemaCollection tables)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TableSchemaCollection filtered = new TableSchemaCollection();
foreach(TableSchema table in tables)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(!SkipTable(table)) filtered.Add(table);
}
return filtered;
}
}
#endregion
#endregion
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
Render File#region Render File
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// Copies all properties to a template
/// Renders the file
/// NB. the overrite property
public void RenderFileUsingProperties(string fullPath, bool overrite, ref CodeTemplate template)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( overrite || (!File.Exists(fullPath)) )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CopyPropertiesTo(template);
template.RenderToFile(fullPath, overrite);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// Copies all properties to a template
/// Renders the file
/// NB. the overrite property
/// Set Property SourceTable
public void RenderFileUsingProperties(string fullPath, bool overrite, ref CodeTemplate template,TableSchema SourceTable)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( overrite || (!File.Exists(fullPath)) )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CopyPropertiesTo(template);
template.SetProperty("SourceTable",SourceTable);
template.RenderToFile(fullPath, overrite);
}
}
#endregion
</script>
posted on
2007-05-25 22:05
过江
阅读(
947)
评论()
编辑
收藏
举报