public void Compile(SourceFilePath path, string outputPath, PluginManager pluginManager) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseFile(path.AbsolutePath); string assemblyName = "GatsbyPlugin_" + Path.GetFileNameWithoutExtension(path.AbsolutePath); var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); var compilation = CSharpCompilation.Create(assemblyName) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "mscorlib.dll"))) .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "System.dll"))) .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "System.Core.dll"))) .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "Microsoft.CSharp.dll"))) .AddReferences(new MetadataFileReference(Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "RazorBurn.dll"))) .AddReferences(new MetadataFileReference(Assembly.GetEntryAssembly().Location)) .AddSyntaxTrees(syntaxTree); using (FileStream stream = new FileStream(outputPath, FileMode.Create)) { var result = compilation.Emit(stream); if (!result.Success) { string message = string.Join(Environment.NewLine, result.Diagnostics); throw new GatsbyException(string.Format("Failed to compile plugin {0}:\n{1}", path.AbsolutePath, message)); } } Assembly assembly = Assembly.LoadFile(Path.GetFullPath(outputPath)); this.assemblies.Add(new AssemblyData() { Assembly = assembly, Path = outputPath }); foreach (Type type in assembly.GetTypes()) pluginManager.Register(type); }
public Post RenderPost(Config config, SourceFilePath path, Site site) { Post post = null; try { post = this.compiler.Compile<Post>(File.ReadAllText(path.AbsolutePath)); post.Run(config, this.markdownTransformer, path.RelativePath, this, site); } catch (RazorCompilationException ex) { throw new GatsbyException(string.Format("Failed while compiling post {0}:\n\t{1}", path.AbsolutePath, string.Join("\n\t", ex.Errors))); } return post; }
public List<Generator> RenderPaginator(Config config, SourceFilePath path, Site site) { List<Generator> pagintors = new List<Generator>(); RazorTemplateFactory<Generator> factory = null; try { factory = this.compiler.CompileFactory<Generator>(File.ReadAllText(path.AbsolutePath)); } catch (RazorCompilationException ex) { throw new GatsbyException(string.Format("Failed while compiling paginator {0}:\n\t{1}", path.AbsolutePath, string.Join("\n\t", ex.Errors))); } int pageNumber = 1; while (true) { var template = factory.Create(); template.PageNumber = pageNumber; template.Run(config, this.markdownTransformer, path.RelativePath, this, site); pagintors.Add(template); if (template.GeneratorFinished) break; pageNumber++; } return pagintors; }
private static void Scan(List<SourceFilePath> pages, List<SourceFilePath> staticFiles, string path, string basePath, Config config) { var excludeFileNames = config.ExcludePatterns.SelectMany(x => Directory.EnumerateFileSystemEntries(path, x, SearchOption.TopDirectoryOnly)); var fileNames = Directory.EnumerateFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Where(x => !excludeFileNames.Contains(x)); foreach (var fileName in fileNames) { string file = Path.GetFileName(fileName); // Ignore anything that starts with an "_". if (file.StartsWith("_")) continue; if (Directory.Exists(fileName)) { Scan(pages, staticFiles, fileName, basePath, config); } else { SourceFilePath sourceFilePath = new SourceFilePath() { AbsolutePath = Path.GetFullPath(fileName), RelativePath = CalculateRelativePath(fileName, basePath) }; if (config.PageFileNameExtensions.Contains(Path.GetExtension(file).Substring(1))) { pages.Add(sourceFilePath); } else { staticFiles.Add(sourceFilePath); } } } }