visual studio 2013 - T4 reports Compiling transformation: Invalid token 'this' in class, struct -
attempting run t4 templates immutable object graph gives errors of
╔═══════╦═══╦══════════════════════════════════════════════════════════════════════════════════════════════════╦═════════════════════════════════════════════════════════╦═══╦════╦══════╗ ║ error ║ 5 ║ compiling transformation: invalid token 'this' in class, struct, or interface member declaration ║ c:\dev\immutableobjectgraph-master\2013\demo\message.tt ║ 1 ║ 1 ║ demo ║ ║ error ║ 6 ║ compiling transformation: method must have return type ║ c:\dev\immutableobjectgraph-master\2013\demo\message.tt ║ 1 ║ 6 ║ demo ║ ║ error ║ 7 ║ compiling transformation: type expected ║ c:\dev\immutableobjectgraph-master\2013\demo\message.tt ║ 1 ║ 12 ║ demo ║ ╚═══════╩═══╩══════════════════════════════════════════════════════════════════════════════════════════════════╩═════════════════════════════════════════════════════════╩═══╩════╩══════╝
the line reported line 1, , full set of t4 templates many hundreds of lines. how troubleshoot , fix issue?
you cannot have literals in t4 template after scriptlet.
change
<#@ template debug="true" language="c#" #> <#+ // scriptlet #> <-- empty line here
to
<#@ template debug="true" language="c#" #> <#+ // scriptlet #>
debugging
you can see c# being generated t4 engine calling preprocesstemplate
custom templating host.
i modified custom template host sample purpose:
using microsoft.visualstudio.texttemplating; using system; using system.codedom.compiler; using system.collections.generic; using system.io; using system.text; namespace customhost { class customcmdlinehost : itexttemplatingenginehost { public string templatefile { get; private set; } public string fileextension { get; private set; } public encoding fileencoding { get; private set; } public compilererrorcollection errors { get; private set; } public ilist<string> standardassemblyreferences { { return new[] { typeof(system.uri).assembly.location }; } } public ilist<string> standardimports { { return new string[] { "system" }; } } public customcmdlinehost(string file) { this.templatefile = file; this.fileencoding = encoding.utf8; this.fileextension = ".txt"; } public bool loadincludetext(string requestfilename, out string content, out string location) { content = location = string.empty; if (file.exists(requestfilename)) { content = file.readalltext(requestfilename); return true; } return false; } public object gethostoption(string optionname) { object returnobject; switch (optionname) { case "cacheassemblies": returnobject = true; break; default: returnobject = null; break; } return returnobject; } public string resolveassemblyreference(string assemblyreference) { return resolvepath(assemblyreference); } public type resolvedirectiveprocessor(string processorname) { throw new exception("directive processor not found"); } public string resolvepath(string filename) { if (file.exists(filename)) { return filename; } string candidate = path.combine(path.getdirectoryname(this.templatefile), filename); if (file.exists(candidate)) { return candidate; } return filename; } public string resolveparametervalue(string directiveid, string processorname, string parametername) { return string.empty; } public void setfileextension(string extension) { fileextension = extension; } public void setoutputencoding(system.text.encoding encoding, bool fromoutputdirective) { fileencoding = encoding; } public void logerrors(compilererrorcollection errors) { errors = errors; } public appdomain providetemplatingappdomain(string content) { return appdomain.createdomain("generation app domain"); } } class program { static void main(string[] args) { var templatefilename = args[0]; customcmdlinehost host = new customcmdlinehost(templatefilename); engine engine = new engine(); string language; string[] refs; var output = engine.preprocesstemplate( // input file file.readalltext(templatefilename), host, "testclass", "testnamespace", out language, out refs); string outputfilename = path.combine( path.getdirectoryname(templatefilename), templatefilename + ".generator.cs"); file.writealltext(outputfilename, output, host.fileencoding); foreach (compilererror error in host.errors) console.writeline(error.tostring()); console.readline(); } } }
examining transformer generated template showed lines following outside transformtext()
method. seemingly, literals in source templates came after scriptlet (<#+ #>
) placed in-fix generated generator class.
#line 1 "c:\dev\immutableobjectgraph-master\2013\demo\fruit.tt" this.write("\n");
removing newline characters @ end of each template file resolved issue.
Comments
Post a Comment