/// <summary> /// Generates the contents of the given file extension. /// If null is returned, it means to preserve the existing file instead /// of generating a new one. /// </summary> /// <param name="fileExtension">One of the file extensions from the <see cref="GetEnumerator()"/> method.</param> /// <returns>The generated content for the given file extension, or null.</returns> private byte[] GenerateAllContent(string fileExtension) { System.Diagnostics.Debugger.Log(0, "", String.Format("Called 'GenerateAllContent'; fileExtension={0} ", fileExtension)); string inputFileContent; // For debugging purposes, it is better to be able to edit the .tt files without having to // recompile the solution to test changes. During release, use the embedded .tt files // instead. switch (fileExtension) { case "config": Diagnostics.DebugWrite("GenerateAllContent >> ConfigurationSectionDesignerSample template file={0}", Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSample.tt")); inputFileContent = File.ReadAllText(Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSample.tt")); break; case "xsd": inputFileContent = File.ReadAllText(Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSchema.tt")); break; // Preserve the diagram and cs files, don't write anything to them case "diagram": return(null); // Generate the code behind file! default: if (fileExtension == CodeFileExtension) { return(null); } else if (fileExtension == string.Format("{0}-gen", CodeFileExtension)) { System.Diagnostics.Debugger.Log(0, "", String.Format("fileExtension is of ?-gen. Running CodeFileGenerator.GenerateCode with InputFilePath={0}", InputFilePath)); return(CodeFileGenerator.GenerateCode(InputFilePath)); } else { throw new ApplicationException("Unhandled file content"); } } // Replace our input file name placeholder with the real input file name // so the text transformer knows which .csd file to work on. System.Diagnostics.Debugger.Log(0, "", String.Format("Replacing $inputFileName$ with real value before running TemplateGenerator.GenerateCode(InputFilePath={0},inputFileContent={1}); ", InputFilePath, "TOO LONG!")); inputFileContent = inputFileContent.Replace("$inputFileName$", InputFilePath); return(TemplateGenerator.GenerateCode(InputFilePath, inputFileContent)); }
private byte[] GenerateFromTextTemplate(string fileExtension) { string ttFilePath = ""; if (fileExtension == "config") { ttFilePath = Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSample.tt"); } else if (fileExtension == "xsd") { ttFilePath = Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSchema.tt"); } Diagnostics.DebugWrite("GenerateAllContent >> Generating from template file '{0}'.", ttFilePath); string inputFileContent = File.ReadAllText(ttFilePath); // Replace our input file name placeholder with the real input file name // so the text transformer knows which .csd file to work on. System.Diagnostics.Debugger.Log(0, "", String.Format("Replacing $inputFileName$ with real value before running TemplateGenerator.GenerateCode(InputFilePath={0},inputFileContent.Length={1}); ", InputFilePath, inputFileContent.Length)); inputFileContent = inputFileContent.Replace("$inputFileName$", InputFilePath); // TODO: CURRERROR: Templating service unavailable error at this point.. // It fails because BaseTemplatedCodeGenerator.TextTemplating property returns null. /* * protected ITextTemplating TextTemplating * { * get * { * ITextTemplating textTemplating = (ITextTemplating) null; * IVsHierarchy vsHierarchy = this.GetService(typeof (IVsHierarchy)) as IVsHierarchy; * if (vsHierarchy != null) * { * Microsoft.VisualStudio.OLE.Interop.IServiceProvider ppSP = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider) null; * if (!Microsoft.VisualStudio.TextTemplating.NativeMethods.Failed(vsHierarchy.GetSite(out ppSP)) && ppSP != null) * { * Guid guid = typeof (STextTemplating).GUID; * IntPtr ppvObject = IntPtr.Zero; * ErrorHandler.ThrowOnFailure(ppSP.QueryService(ref guid, ref guid, out ppvObject)); * if (ppvObject != IntPtr.Zero) * textTemplating = Marshal.GetObjectForIUnknown(ppvObject) as ITextTemplating; * } * } * return textTemplating; * } * }*/ return(TemplateGenerator.GenerateCode(InputFilePath, inputFileContent)); }
/// <summary> /// Generates the contents of the given file extension. /// If null is returned, it means to preserve the existing file instead /// of generating a new one. /// </summary> /// <param name="fileExtension">One of the file extensions from the <see cref="GetEnumerator()"/> method.</param> /// <returns>The generated content for the given file extension, or null.</returns> private byte[] GenerateAllContent(string fileExtension) { string inputFileContent; // For debugging purposes, it is better to be able to edit the .tt files without having to // recompile the solution to test changes. During release, use the embedded .tt files // instead. switch (fileExtension) { case "config": inputFileContent = File.ReadAllText(Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSample.tt")); break; case "xsd": inputFileContent = File.ReadAllText(Path.Combine(TextTemplateFolder, "ConfigurationSectionDesignerSchema.tt")); break; // Preserve the diagram and cs files, don't write anything to them case "diagram": return(null); default: if (fileExtension == CodeFileExtension) { return(null); } else if (fileExtension == string.Format("{0}-gen", CodeFileExtension)) { return(CodeFileGenerator.GenerateCode(InputFilePath)); } else { throw new ApplicationException("Unhandled file content"); } } // Replace our input file name placeholder with the real input file name // so the text transformer knows which .csd file to work on. inputFileContent = inputFileContent.Replace("$inputFileName$", InputFilePath); return(TemplateGenerator.GenerateCode(InputFilePath, inputFileContent)); }