clariuslabs / TransformOnBuild

Transform Text Templates On Build
Apache License 2.0
48 stars 17 forks source link

Could not find file '...\T4Toolbox.tt' #37

Closed mperrenoud closed 8 years ago

mperrenoud commented 8 years ago

I have a template that is used to generate POCO's from the database. It's included below. When I execute the custom tool from inside Visual Studio all is well. However, when I execute it with the build through this package I get an error:

...\TableTemplate.tt(9,4): error : There was an error loading the include file 'T4Toolbox.tt'. The transformation will not be run. The following Exception was thrown:

System.IO.FileNotFoundException: Could not find file '...\T4Toolbox.tt'. (TaskId:14)

File name: '...\T4Toolbox.tt' (TaskId:14)

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) (TaskId:14)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) (TaskId:14)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) (TaskId:14)

at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) (TaskId:14)

at System.IO.StreamReader..ctor(String path) (TaskId:14)

at Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost.ReadFileContent(String fileName) (TaskId:14)

at Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost.LoadIncludeText(String requestFileName, String& content, String& location) (TaskId:14)

at Microsoft.VisualStudio.TextTemplating.Engine.ProcessIncludeDirective(Directive directive, ITextTemplatingEngineHost host, VisitedFiles includedFiles) (TaskId:14)

TableTemplate.tt

<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>

<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>

<#@ include file="T4Toolbox.tt" #>

<#+
public class TableTemplate : Template
{
    Table tbl;
    string classNamespace;
    bool shouldBaseClientModel;

    public TableTemplate(Table tbl, string classNamespace, bool shouldBaseClientModel)
    {
        this.tbl = tbl;
        this.classNamespace = classNamespace;
        this.shouldBaseClientModel = shouldBaseClientModel;
    }

    public override string TransformText()
    {
#>
using System;

namespace <#= classNamespace #>
{
    /// <summary>
    /// Represents the <#= tbl.Name #> table.
    /// NOTE: This class is generated from a T4 template - you should not modify it manually.
    /// </summary>
    public partial class <#= tbl.Name #><#+ if (shouldBaseClientModel) { #> : BaseClientModel<#+ } #>

    {
<#+
        // Keep count so we don't whitespace the last property/column
        int columnCount = tbl.Columns.Count;
        int i = 0;

        // Iterate all columns
        foreach (Column col in tbl.Columns)
        {
            i++;
            string propertyType = GetNetDataType(col.DataType.Name);

            // If we can't map it, skip it
            if (string.IsNullOrWhiteSpace(propertyType))
            {
                continue;
            }

            // Handle nullable columns by making the type nullable
            if (col.Nullable && propertyType != "string")
            {
                propertyType += "?";
            }
#>
        public <#= propertyType #> <#= col.Name #> { get; set; }
<#+
            // Do we insert the space?
            if (i != columnCount)
            {
#>

<#+
            }
        }
#>
    }
}
<#+
        return this.GenerationEnvironment.ToString();
    }

    private static string GetNetDataType(string sqlDataTypeName)
    {
        switch (sqlDataTypeName.ToLower())
        {
            case "bigint":
                return "Int64";
            case "binary":
            case "image":
            case "varbinary":
                return "byte[]";
            case "bit":
                return "bool";
            case "char":
                return "char";
            case "datetime":
            case "smalldatetime":
                return "DateTime";
            case "decimal":
            case "money":
            case "numeric":
                return "decimal";
            case "float":
                return "double";
            case "int":
                return "int";
            case "nchar":
            case "nvarchar":
            case "text":
            case "varchar":
            case "xml":
                return "string";
            case "real":
                return "single";
            case "smallint":
                return "Int16";
            case "tinyint":
                return "byte";
            case "uniqueidentifier":
                return "Guid";

            default:
                return null;
        }
    }
}
#>
mperrenoud commented 8 years ago

Looks like based on this SO post, it won't matter if I get this running because I can't get at the Host. I need the Host so that I can add these files to the project nicely. Oh well.