public override Task <bool> InvokeAsync(string paramList) { if (File.Exists(paramList.Trim())) { try { var generator = new RazorGenerator(paramList.Trim(), string.Empty, new object()); generator.Init(); var result = generator.Render(); generator.OutPut(); OutputInformation("Entered string was: {0}", result); } catch (TemplateCompilationException exception) { OutputError(MRazorUtil.GetError(exception)); } catch (Exception ex) { OutputError("Error {0}", ex.Message); } } else { OutputError("Source file {0} not found", paramList); } return(Task.FromResult(true)); }
//private IRazorEngineService _engine; //private RazorFileTemplate _razorFileTemplate; //public CustomTemplateBase(IRazorEngineService engine, RazorFileTemplate razorFileTemplate) : base() //{ // _engine = engine; // _razorFileTemplate = razorFileTemplate; //} public string Partial(string partialName, object obj) { var path = partialName; if (this.ViewBag.InputFolder != null) { path = FileUtils.GetPartialPath(this.ViewBag.InputFolder, path); } if (File.Exists(path)) { try { //compile partial template var fileContent = FileUtils.ReadFileContent(path); var templateKey = Razor.GetKey(partialName); if (!Razor.IsTemplateCached(templateKey, null)) { var templateSource = new LoadedTemplateSource(fileContent); Razor.AddTemplate(templateKey, templateSource); Razor.Compile(templateKey, obj.GetType()); } Include(partialName, obj, null).WriteTo(this.CurrentWriter); return(string.Empty); } catch (TemplateCompilationException tex) { return(string.Format("Partial Render Error{0}\r\n{1}", path, MRazorUtil.GetError(tex))); } catch (Exception ex) { return(string.Format("Partial Render Error{0}\r\n{1}", path, ex.Message)); } } else { return("Partial file Not Found " + path); } }
public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress) { // SwitchDomainForRazorEngine(); byte[] resultBytes; try { var model = new RazorModel(); //set file name and namespace for model using model.DefaultNameSpace = wszDefaultNamespace; var info = new FileInfo(wszInputFilePath); if (info.Exists) { model.FileName = info.Name; } int iFound; uint itemId; ProjectItem item; VSDOCUMENTPRIORITY[] pdwPriority = new VSDOCUMENTPRIORITY[1]; // obtain a reference to the current project as an IVsProject type IVsProject vsProject = VsHelper.ToVsProject(project); // this locates, and returns a handle to our source file, as a ProjectItem vsProject.IsDocumentInProject(wszInputFilePath, out iFound, pdwPriority, out itemId); // if our source file was found in the project (which it should have been) if (iFound != 0 && itemId != 0) { IServiceProvider oleSp; vsProject.GetItemContext(itemId, out oleSp); if (oleSp != null) { ServiceProvider sp = new ServiceProvider(oleSp); // convert our handle to a ProjectItem item = sp.GetService(typeof(ProjectItem)) as ProjectItem; } else { throw new ApplicationException("Unable to retrieve Visual Studio ProjectItem"); } } else { throw new ApplicationException("Unable to retrieve Visual Studio ProjectItem"); } var generator = new RazorGenerator(wszInputFilePath, bstrInputFileContents, model); generator.Init(); //get extension from header file if (!string.IsNullOrEmpty(generator.RazorTemplate.OutPutExtension)) { _extenstion = generator.RazorTemplate.OutPutExtension; } //generate code var result = generator.Render(); resultBytes = Encoding.UTF8.GetBytes(result); int outputLength = resultBytes.Length; rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(outputLength); Marshal.Copy(resultBytes, 0, rgbOutputFileContents[0], outputLength); pcbOutput = (uint)outputLength; return(VSConstants.S_OK); } catch (TemplateCompilationException tex) { //Display error in result template foreach (var compilerError in tex.CompilerErrors) { pGenerateProgress.GeneratorError(0, 1, compilerError.ErrorText, (uint)compilerError.Line, (uint)compilerError.Column); } var message = MRazorUtil.GetError(tex); resultBytes = Encoding.UTF8.GetBytes(message); int outputLength = resultBytes.Length; rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(outputLength); Marshal.Copy(resultBytes, 0, rgbOutputFileContents[0], outputLength); pcbOutput = (uint)outputLength; return(VSConstants.S_FALSE);// Change to E_Fail will display error in error list } catch (Exception ex) { var messageBuilder = new StringBuilder(ex.Message); messageBuilder.AppendLine(); if (ex.Source != null) { messageBuilder.Append(ex.Source); } messageBuilder.Append(ex.StackTrace); if (ex.InnerException != null) { messageBuilder.AppendLine(); messageBuilder.Append(ex.InnerException.Message + ex.InnerException.StackTrace); } resultBytes = Encoding.UTF8.GetBytes(messageBuilder.ToString()); int outputLength = resultBytes.Length; rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(outputLength); Marshal.Copy(resultBytes, 0, rgbOutputFileContents[0], outputLength); pcbOutput = (uint)outputLength; return(VSConstants.S_FALSE);// Change to E_Fail will display error in error list } //finally //{ // //unload domain for unload dll loaded from InputDllFolder // if (_domain != null) AppDomain.Unload(_domain); //} }