private static void BuildDocumentWithPlugin(BuildJsonConfig config, TemplateManager manager, string baseDirectory, string outputDirectory, string applicationBaseDirectory, string pluginDirectory, string templateDirectory) { AppDomain builderDomain = null; try { var pluginConfig = Path.Combine(pluginDirectory, "docfx.plugins.config"); Logger.LogInfo($"Plug-in directory: {pluginDirectory}, configuration file: {pluginConfig}"); AppDomainSetup setup = new AppDomainSetup { ApplicationBase = applicationBaseDirectory, PrivateBinPath = string.Join(";", applicationBaseDirectory, pluginDirectory), ConfigurationFile = pluginConfig }; builderDomain = AppDomain.CreateDomain("document builder domain", null, setup); builderDomain.UnhandledException += (s, e) => { }; var wrapper = new DocumentBuilderWrapper(config, manager, baseDirectory, outputDirectory, pluginDirectory, new CrossAppDomainListener(), templateDirectory); builderDomain.DoCallBack(wrapper.BuildDocument); } finally { if (builderDomain != null) { AppDomain.Unload(builderDomain); } } }
private void BuildDocument(string baseDirectory, string outputDirectory) { var pluginBaseFolder = AppDomain.CurrentDomain.BaseDirectory; var pluginFolderName = "plugins_" + Path.GetRandomFileName(); var pluginFilePath = Path.Combine(pluginBaseFolder, pluginFolderName); var defaultPluginFolderPath = Path.Combine(pluginBaseFolder, "plugins"); if (Directory.Exists(pluginFilePath)) { throw new PluginDirectoryAlreadyExistsException(pluginFilePath); } bool created = false; try { created = _templateManager.TryExportTemplateFiles(pluginFilePath, @"^plugins/.*"); if (created) { BuildDocumentWithPlugin(Config, _templateManager, baseDirectory, outputDirectory, pluginBaseFolder, Path.Combine(pluginFilePath, "plugins")); } else { if (Directory.Exists(defaultPluginFolderPath)) { BuildDocumentWithPlugin(Config, _templateManager, baseDirectory, outputDirectory, pluginBaseFolder, defaultPluginFolderPath); } else { DocumentBuilderWrapper.BuildDocument(Config, _templateManager, baseDirectory, outputDirectory, null); } } } finally { if (created) { Logger.LogInfo($"Cleaning up temporary plugin folder \"{pluginFilePath}\""); } try { if (Directory.Exists(pluginFilePath)) { Directory.Delete(pluginFilePath, true); } } catch (Exception e) { Logger.LogWarning($"Error occurs when cleaning up temporary plugin folder \"{pluginFilePath}\", please clean it up manually: {e.Message}"); } } }