Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Visual Studio no longer supports macros, so the answer in the following question is only valid for previous releases:

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

Is there a way for Visual Studio 2012 / 2013 to report an error/warning when you build a solution that has missing files?

like image 431
Turch Avatar asked Dec 19 '13 19:12

Turch


2 Answers

I've modified Sergey's script to recursively handle folders.

Update: Added a message box popup for each project that lists missing files as suggested by Thomas Svensen in comments

Update 2: Now writes missing files to the output window and as an error in the error list. This doesn't cause a failed build, just a line in the error list.

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
like image 91
Turch Avatar answered Oct 27 '22 16:10

Turch


Based on VS macro code you referenced, I've created an extension for Visual Commander to report warning:

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
like image 39
Sergey Vlasov Avatar answered Oct 27 '22 15:10

Sergey Vlasov