protected override string ProcessTemplate(string inputFileName, string inputFileContent, ITextTemplating processor, IVsHierarchy hierarchy) { ThreadHelper.ThrowIfNotOnUIThread(); try { TextTemplatingCallback callback = new TextTemplatingCallback(this); processor.BeginErrorSession(); string templateCode = processor.PreprocessTemplate(inputFileName, inputFileContent, callback, TEMPLATE_CLASS, TEMPLATE_NAMESPACE, out string[] references); if (processor.EndErrorSession() || callback.ErrorLogged) { return(ERROR_OUTPUT); } DetectExtensionDirective(inputFileContent); _encoding = callback.OutputEncoding; references = ProcessReferences(references, inputFileName).ToArray(); string output = TextTemplatingHelper.ExecuteTemplate(inputFileName, templateCode, references, out TemplateError[] errors); GenerateErrors(errors); if (output == null) { return(ERROR_OUTPUT); } else { return(output); } } catch (Exception e) { GenerateError(false, $"Something went wrong processing the template '{inputFileName}': {e}"); return(ERROR_OUTPUT); } }
public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress) { if (bstrInputFileContents == null) { throw new ArgumentException(bstrInputFileContents); } Status.Clear(); var originalFile = GetOriginalFile(wszInputFilePath); PromptToRefreshEntities(); if (context == null) { DTE2 dte = Package.GetGlobalService(typeof(SDTE)) as EnvDTE80.DTE2; settings = ConfigurationFile.ReadFromJsonFile(dte); Login m = new Login(dte, settings); m.ShowDialog(); context = m.Context; ConfigurationFile.WriteToJsonFile(dte, m.settings); m = null; if (context == null) { // TODO pGenerateProgress.GeneratorError(1, (uint)1, "Code generation for CRM Template aborted", uint.MaxValue, uint.MaxValue); if (originalFile == null) { SaveOutputContent(rgbOutputFileContents, out pcbOutput, ""); } else { // http://social.msdn.microsoft.com/Forums/vstudio/en-US/d8d72da3-ddb9-4811-b5da-2a167bbcffed/ivssinglefilegenerator-cancel-code-generation // I don't think a login failure would be considered a invalid model, so we'll restore what was there SaveOutputContent(rgbOutputFileContents, out pcbOutput, System.IO.File.ReadAllText(originalFile)); } return(VSConstants.S_OK); } } Status.Update("Generating code from template... "); ITextTemplating t4 = Package.GetGlobalService(typeof(STextTemplating)) as ITextTemplating; ITextTemplatingSessionHost sessionHost = t4 as ITextTemplatingSessionHost; context.Namespace = wszDefaultNamespace; sessionHost.Session = sessionHost.CreateSession(); sessionHost.Session["Context"] = context; Callback cb = new Callback(); t4.BeginErrorSession(); string content = t4.ProcessTemplate(wszInputFilePath, bstrInputFileContents, cb); t4.EndErrorSession(); // If there was an output directive in the TemplateFile, then cb.SetFileExtension() will have been called. if (!string.IsNullOrWhiteSpace(cb.FileExtension)) { extension = cb.FileExtension; } Status.Update("Writing code to disk... "); SaveOutputContent(rgbOutputFileContents, out pcbOutput, content); // Append any error/warning to output window foreach (var err in cb.ErrorMessages) { // The templating system (eg t4.ProcessTemplate) will automatically add error/warning to the ErrorList Status.Update("[" + (err.Warning == true ? "WARN" : "ERROR") + "] " + err.Message + " " + err.Line + "," + err.Column); } if (cb.ErrorMessages.Any(em => em.Warning == false)) { Configuration.Instance.DTE.ExecuteCommand("View.ErrorList"); } else { Status.Update("Done!"); } return(VSConstants.S_OK); }
protected override byte[] GenerateCode(string inputFileName, string inputFileContent) { if (this.ShowWarningDialog()) { return(new byte[0]); } base.SetWaitCursor(); this.errors = false; this.encodingSetFromOutputDirective = false; this.outputEncoding = null; ITextTemplating textTemplating = this.TextTemplating; string s = string.Empty; CallContext.LogicalSetData("NamespaceHint", base.FileNamespace); try { if (textTemplating == null) { throw new InvalidOperationException(Resources.TextTemplatingUnavailable); } textTemplating.BeginErrorSession(); IVsHierarchy service = base.GetService(typeof(IVsHierarchy)) as IVsHierarchy; s = this.ProcessTemplate(inputFileName, inputFileContent, textTemplating, service); this.errors |= textTemplating.EndErrorSession(); MarkProjectForTextTemplating(service); } finally { CallContext.FreeNamedDataSlot("NamespaceHint"); } if (this.errors) { IVsErrorList errorList = base.ErrorList; if (errorList != null) { try { errorList.BringToFront(); errorList.ForceShowErrors(); } catch { } } } if (this.outputEncoding == null) { this.outputEncoding = EncodingHelper.GetEncoding(inputFileName); } if (this.outputEncoding == null) { this.outputEncoding = Encoding.UTF8; } byte[] bytes = this.outputEncoding.GetBytes(s); byte[] preamble = this.outputEncoding.GetPreamble(); if ((preamble != null) && (preamble.Length > 0)) { bool flag2 = false; if (bytes.Length >= preamble.Length) { flag2 = true; for (int i = 0; i < preamble.Length; i++) { if (preamble[i] != bytes[i]) { flag2 = false; break; } } } if (!flag2) { byte[] array = new byte[preamble.Length + bytes.Length]; preamble.CopyTo(array, 0); bytes.CopyTo(array, preamble.Length); bytes = array; } } return(bytes); }
protected override byte[] GenerateCode(string inputFileName, string inputFileContent) { string result = ""; ThreadHelper.ThrowIfNotOnUIThread(); bool errors = false; if ((Package?.IsTemplateInProcess ?? false) && Package?.ProcessResults != null) { callback = Package.TextTemplatingCallback.DeepCopy(); result = Package.ProcessResults; Package.ProcessResults = null; } else { if (!Package.ShowSecurityWarningDialog()) { return(Array.Empty <byte>()); } base.SetWaitCursor(); callback = new TextTemplatingCallback(); ITextTemplating processor = GetTextTemplating(); if (processor == null) { throw new InvalidOperationException(SubSonicCoreErrors.TextTemplatingUnavailable); } processor.BeginErrorSession(); if (GetService(typeof(IVsHierarchy)) is IVsHierarchy hierarchy) { result = ProcessTemplate(inputFileName, inputFileContent, processor, hierarchy); errors |= callback.Errors.HasErrors; errors |= processor.EndErrorSession(); MarkProjectForTextTemplating(hierarchy); } } if (errors) { if (ErrorList is IVsErrorList vsErrorList) { try { vsErrorList.BringToFront(); vsErrorList.ForceShowErrors(); } catch { } } } callback.SetOutputEncoding(EncodingHelper.GetEncoding(inputFileName) ?? Encoding.UTF8, false); result = result?.TrimStart(Environment.NewLine.ToCharArray()) ?? ERROR_OUTPUT; Encoding encoding = callback.GetOutputEncoding(); byte[] bytes = encoding.GetBytes(result); byte[] preamble = encoding.GetPreamble(); if ((preamble != null) && (preamble.Length != 0)) { bool success = false; if (bytes.Length >= preamble.Length) { success = true; for (int i = 0; i < preamble.Length; i++) { if (preamble[i] != bytes[i]) { success = false; break; } } } if (!success) { byte[] array = new byte[preamble.Length + bytes.Length]; preamble.CopyTo(array, 0); bytes.CopyTo(array, preamble.Length); bytes = array; } } return(bytes); }