工作区生成

Open Folder 方案中的生成支持需要扩展程序为工作区提供索引文件上下文数据,以及要运行的生成操作。

下面是扩展需要的内容的概述。

生成文件上下文

  • 提供程序工厂
    • ExportFileContextProviderAttribute 属性,其中包含 supportedContextTypeGuids 所有适用的 string 常量 BuildContextTypes
    • 实现 IWorkspaceProviderFactory<IFileContextProvider>
    • 文件上下文提供程序
      • 为每个支持生成操作和配置返回一个FileContext

索引生成文件数据值

  • 提供程序工厂
    • ExportFileScannerAttribute 属性,用作 IReadOnlyCollection<FileDataValue> 支持的类型
    • 实现 IWorkspaceProviderFactory<IFileScanner>
  • 文件扫描程序打开 ScanContentAsync<T>
    • 当类型参数为时 FileScannerTypeConstants.FileDataValuesType 返回数据
    • 返回使用以下各项构造的每个配置的文件数据值:
      • typeBuildConfigurationContext.ContextTypeGuid
      • context 作为生成配置(例如 "Debug|x86""ret"null 不适用)。 此值 必须与 文件上下文中的配置匹配。

生成文件上下文操作

  • 提供程序工厂
    • ExportFileContextActionProvider 属性,其中包含 supportedContextTypeGuids 所有适用的 string 常量 BuildContextTypes
    • 实现 IWorkspaceProviderFactory<IFileContextActionProvider>
  • 操作提供程序打开 IFileContextActionProvider.GetActionsAsync
    • 返回与给定FileContext.ContextType值匹配的一个IFileContextAction
  • 文件上下文操作
    • IFileContextAction实现和IVsCommandItem
    • CommandGroup 属性返回 16537f6e-cb14-44da-b087-d1387ce3bf57
    • CommandId0x1000用于生成、0x1010重新生成或0x1020清理

注意

FileDataValue由于需要编制索引,因此打开工作区和扫描文件以获取完整生成功能的时间点之间会有一些时间。 由于以前没有缓存的索引,因此首次打开文件夹时会出现延迟。

从生成报告消息

该生成可以向用户显示信息、警告和错误消息,这两种方法之一。 最简单的方法是使用 IBuildMessageService 并提供一个 BuildMessage,如下所示:

using Microsoft.VisualStudio.Workspace;
using Microsoft.VisualStudio.Workspace.Build;

private static void OutputBuildMessage(IWorkspace workspace)
{
    IBuildMessageService buildMessageService = workspace.GetBuildMessageService();

    if (buildMessageService != null)
    {
      // Example error build message. See the documentation for BuildMessage for more information.
      var message = new BuildMessage()
      {
          Type = BuildMessage.TaskType.Error,
          Code = "MY1001",
          TaskText = "This is a sample error",
          ProjectFile = "buildfile.bld",
          File = "sourcefile.src"
          LogMessage = $"This is sample text that will only go to the Build output window pane.\n"

          // And any other properties to set
      };

      buildMessageService.ReportBuildMessages(new BuildMessage[] { message });
    }
}

BuildMessage.TypeBuildMessage.LogMessage 控制向用户显示信息的位置行为。 除生成具有给定详细信息的错误列表条目以外的None任何BuildMessage.TaskType值。 LogMessage将始终在“输出工具”窗口的“生成”窗格中输出。

或者,扩展可以直接与 “错误列表 ”或 “生成 ”窗格进行交互。 Visual Studio 2017 版本 15.7 之前的版本中存在 bug,其中 pszProjectUniqueName 忽略了参数 OutputTaskItemStringEx2

警告

可为参数提供任意基础实现的IProgress<IFileContextActionProgressUpdate>调用方IFileContextAction.ExecuteAsync。 从不直接调用 IProgress<IFileContextActionProgressUpdate>.Report(IFileContextActionProgressUpdate) 。 目前没有使用此参数的一般准则,但这些准则可能会更改。

tasks.vs.json 和 launch.vs.json

有关创作 tasks.vs.json 或 launch.vs.json 文件的信息,请参阅 自定义生成和调试任务