如果Visual Studio 2012/2013中的项目/解决方案中缺少文件,则报告错误/警告

时间:2013-12-19 19:12:48

标签: visual-studio visual-studio-2012 visual-studio-2013

Visual Studio no longer supports macros,因此以下问题的答案仅对以前的版本有效:

Report error/warning if missing files in project/solution in Visual Studio

在构建缺少文件的解决方案时,Visual Studio 2012/2013是否有办法报告错误/警告?

3 个答案:

答案 0 :(得分:5)

我已经修改了Sergey的脚本来递归处理文件夹。

更新:为每个项目添加了一个消息框弹出窗口,列出了Thomas Svensen在评论中建议的丢失文件

更新2:现在将丢失的文件写入输出窗口,并将错误列表中的错误写入。这不会导致构建失败,只会导致错误列表中的一行。

Imports EnvDTE
Imports EnvDTE80

Public Class E
    Implements VisualCommanderExt.IExtension

    Sub SetSite(DTE_ As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.IExtension.SetSite
        DTE = DTE_
        events = DTE.Events
        buildEvents = events.BuildEvents
        AddHandler buildEvents.OnBuildBegin, AddressOf OnBuildBegin
    End Sub

    Sub Close() Implements VisualCommanderExt.IExtension.Close
        RemoveHandler buildEvents.OnBuildBegin, AddressOf OnBuildBegin
    End Sub

    Private Sub OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction)
        For Each proj As Project In DTE.Solution.Projects
            Dim missingFiles As String = ""
            For Each item As ProjectItem In proj.ProjectItems
                missingFiles = missingFiles + CheckProjectItem(item, proj)
            Next
            If missingFiles.Length > 0 Then
                ' comment out to remove message box popup and only have missing files listed in output / error windows
                System.Windows.MessageBox.Show("Project " + proj.Name + " missing files:" + System.Environment.NewLine + missingFiles)
            End If
        Next
    End Sub

    Private Function CheckProjectItem(ByVal item As EnvDTE.ProjectItem, ByVal proj As EnvDTE.Project)
        Dim missingFiles As String = ""
        For Each chiltItem As ProjectItem In item.ProjectItems
            missingFiles = missingFiles + CheckProjectItem(chiltItem, proj)
        Next

        If (item.Kind = "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}") Then ' only check physical file items
            For i As Integer = 1 To item.FileCount
                Dim path As String = item.FileNames(i)
                If Not System.IO.File.Exists(item.FileNames(i)) Then
                    LogMissingFile(item.FileNames(i), proj.Name)
                    missingFiles = missingFiles + item.FileNames(i) + System.Environment.NewLine 
                End If
            Next
        End If
        Return missingFiles
    End Function

    Private Sub LogMissingFile(ByVal fileName As String, ByVal projectName As String)
        Dim ow As OutputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
        Dim build As OutputWindowPane = ow.OutputWindowPanes.Item("Build")
        Dim outputString As String = "!! Missing file: " + fileName  + " in project " + projectName + System.Environment.NewLine
        Dim errorString As String = "Missing file in project " + projectName + ": " + fileName
        ' write to ONLY the Output window
        ' build.OutputString(outputString)
        ' write to BOTH Output and Error window; vsTaskPriorityHigh will show as error, vsTaskPriorityMedium as warning, and vsTaskPriorityLow as message
        build.OutputTaskItemString(outputString, vsTaskPriority.vsTaskPriorityHigh, vsTaskCategories.vsTaskCategoryMisc, vsTaskIcon.vsTaskIconCompile, fileName, 0, errorString)
    End Sub

    private DTE As EnvDTE80.DTE2
    private events As EnvDTE.Events
    private buildEvents as EnvDTE.BuildEvents

End Class

答案 1 :(得分:3)

根据您引用的VS宏代码,我为Visual Commander创建了一个扩展报告警告:

Imports EnvDTE
Imports EnvDTE80

Public Class E
    Implements VisualCommanderExt.IExtension

    Sub SetSite(DTE_ As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.IExtension.SetSite
        DTE = DTE_
        events = DTE.Events
        buildEvents = events.BuildEvents
        AddHandler buildEvents.OnBuildBegin, AddressOf OnBuildBegin
    End Sub

    Sub Close() Implements VisualCommanderExt.IExtension.Close
        RemoveHandler buildEvents.OnBuildBegin, AddressOf OnBuildBegin
    End Sub

    Private Sub OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction)
        For Each proj As Project In DTE.Solution.Projects
            For Each item As ProjectItem In proj.ProjectItems
                If (item.Kind <> "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}") Then ' only check physical file items
                    Continue For
                End If

                For i As Integer = 1 To item.FileCount
                    Dim path As String = item.FileNames(i)
                    If Not System.IO.File.Exists(item.FileNames(i)) Then
                        WriteToBuildWindow("!! Missing file:" & item.FileNames(i) + " in project " + proj.Name)
                    End If
                Next
            Next
        Next
    End Sub

    Private Sub WriteToBuildWindow(ByVal text As String)
        Dim ow As OutputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
        Dim build As OutputWindowPane = ow.OutputWindowPanes.Item("Build")
        build.OutputString(text & System.Environment.NewLine)
    End Sub

    private DTE As EnvDTE80.DTE2
    private events As EnvDTE.Events
    private buildEvents as EnvDTE.BuildEvents

End Class

答案 2 :(得分:1)

关于在检测到丢失文件时破坏构建,我发现最接近的是使用以下内容取消构建(与OnBuildBegin事件一起):

DTE.ExecuteCommand("Build.Cancel")