如何以编程方式在Visual Studio 2017中运行静态代码分析?

时间:2018-05-25 15:02:41

标签: visual-studio envdte static-code-analysis roslyn-code-analysis

我正在研究本地化遗留应用程序的解决方案。我使用EnvDte编写了一个Visual Studio插件,可以自动设置" Localizable"将解决方案中的每个表单标记为true,这是在表单设计器上提取资源的关键步骤。我现在正在尝试处理以编程方式设置的任何文本,触发Globalization (CA13##)警告的文本​​。

designer.Visible = true;
var host = (IDesignerHost)designer.Object;
var provider = TypeDescriptor.GetProvider(host.RootComponent);
var typeDescriptor = provider.GetExtendedTypeDescriptor(host.RootComponent);
if (typeDescriptor == null)
    continue;

var propCollection = typeDescriptor.GetProperties();
var propDesc = propCollection["Localizable"];
if (propDesc != null && host.RootComponent != null &&
    (bool?)propDesc.GetValue(host.RootComponent) != true)
{
    try
    {
        propDesc.SetValue(host.RootComponent, true);
    }
    catch (Exception ex)
    {
        // log the error
    }

    // save changes
}

我已经能够使用以下命令从菜单中手动运行它: Analyze - >运行代码分析 - >在解决方案上获取问题列表,但我想使用另一个运行并提取结果的加载项自动执行此步骤。

是否有任何资源指向访问构建警告或代码分析的结果?

是否有任何使用EnvDte或Roslyn的解决方案?

1 个答案:

答案 0 :(得分:0)

好的,我已经设法收集足够的信息来整理加载项。简而言之,您使用_dte.ExecuteCommand()

  1. 初始化命令:

    // nothing to see here...
    _package = package ?? throw new ArgumentNullException(nameof(package));
    
    var commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
    if (commandService == null)
        return;
    _dte = (DTE2)ServiceProvider.GetService(typeof(DTE));
    var menuCommandId = new CommandID(CommandSet, CommandId);
    var menuItem = new MenuCommand(MenuItemCallback, menuCommandId);
    commandService.AddCommand(menuItem);
    
    _events = _dte.Events.BuildEvents;
    // since static code analysis is a sort of build you need to hook into OnBuildDone
    _events.OnBuildDone += OnBuildDone;
    
  2. 触发分析

    private void MenuItemCallback(object sender, EventArgs e)
    {
        _dte.ExecuteCommand("Build.RunCodeAnalysisonSolution");
    }
    
  3. 在OnBuildDone事件中提取错误

    private void OnBuildDone(vsBuildScope scope, vsBuildAction action)
    {
        Dispatcher.CurrentDispatcher.InvokeAsync(new Action(() =>
        {
            _dte.ExecuteCommand("View.ErrorList", " ");
            var errors = _dte.ToolWindows.ErrorList.ErrorItems;
            for (int i = 1; i <= errors.Count; i++)
            {
                ErrorItem error = errors.Item(i);
                var code = error.Collection.Item(1);
                var item = new
                {
                    error.Column,
                    error.Description,
                    error.ErrorLevel,
                    error.FileName,
                    error.Line,
                    error.Project
                };
                error.Navigate(); // you can navigate to the error if you wanted to.
            }
        });
    }