0

We are working on several ASP.NET MVC C# projects within Visual Studio 2015 and Team Foundation Server 2013. Sometimes the NuGet upgrade process is a mess and some of the replaced files (mostly *.png, *.gif, *.ttf) have not been checked-in properly. What we have figured out so far: the check-in process gets into trouble, if directories should be removed and created in one step. You have to check-in twice. The problem is, if you don’t know about this and one of our developers retrieves the latest source, there are missing files. Visual Studio indicates that with a warning icon in Solution Explorer.

My question is: Is it possible to validate, if every file is present during gated-checkin or nightly-build on TFS which is linked in the csproj file? At least there should be a warning during build.

Hint: This is only a problem with files, which are not compiled (*.cs files) or do not have the setting “copy during build into output directory”. This happens e.g. with JS-files, which are bundled.

Final solution:

Write-Host "Check availability for all referenced files in all projects ..."

function Check-Files($directory, $files){
    if (!$directory.EndsWith("/")) { $directory = "$($directory)/" }
    ForEach($file in $files){
        if($file){
            Write-Host " Referenced file $($directory)$file"
            if(-not (Test-Path "$($directory)$($file)")){
                throw [System.IO.FileNotFoundException] "$($directory)$($file) not found."
            }
        }
    }
}

function CheckProjectFile($csprojFile){
    [xml]$projectContent = Get-Content $csprojFile
    Write-Host "Checking project: $($csprojFile) ..."
    $directory = Split-Path $csprojFile
    ForEach($itemGroup in $projectContent.Project.ItemGroup){
      Check-Files -files $itemGroup.Reference.HintPath -dir $directory
      Check-Files -files $itemGroup.Compile.Include -dir $directory
      Check-Files -files $itemGroup.None.Include -dir $directory
      Check-Files -files $itemGroup.Content.Include -dir $directory
      Check-Files -files $itemGroup.TypeScriptCompile.Include -dir $directory
      Check-Files -files $itemGroup.ProjectReference.Include -dir $directory
    }
}

$csprojFiles = Get-ChildItem -Path ./ -Recurse *.csproj | Select-Object -Property FullName
ForEach($file in $csprojFiles){
    CheckProjectFile($file.FullName)
}

I added the script file to my Team Project on TFS, changed my build definition, added the script directory to my "Source Settings" and included the script into "Pre-build script path". Done!

SiMiDev
  • 3
  • 4
  • Do you mean Nuget upgrade will replace your image files? Are these image files restored by Nuget? Do you check in the whole solution or single file? – Cece Dong - MSFT Aug 25 '16 at 03:50
  • The NuGet stuff was just an example for a reason of this problem. The questions is at the point “My question is …” ;-) – SiMiDev Aug 29 '16 at 15:25

2 Answers2

1

I created a small piece of powershell that you can execute as a step before building the solution.

function Check-Files($files){
  ForEach($file in  $files){
    if($file){
      Write-Host "looking for $file"
      if(-not (Test-Path $file)){
        throw [System.IO.FileNotFoundException] "$file not found."
      }
    }
  }
}

[xml]$projectContent = Get-Content ./your.csproj
ForEach($itemGroup in $projectContent.Project.ItemGroup){
  Check-Files -files $itemGroup.Reference.HintPath
  Check-Files -files $itemGroup.Compile.Include
  Check-Files -files $itemGroup.None.Include
  Check-Files -files $itemGroup.Content.Include
  Check-Files -files $itemGroup.TypeScriptCompile.Include
  Check-Files -files $itemGroup.ProjectReference.Include
}

Hope this helps you. Kind regards Jan

  • Hi @Jan. That was the right track. I adapted your script template a little bit. Now it scans automatically for *.csproj files. Each file is then checked for missing files. It works great. Thanks a lot! I will update my question with the new script file. – SiMiDev Sep 08 '16 at 19:57
0

First of all, make sure you check in the whole solution/project every time. In this way, all the edit files in this solution/ project will list in the Included pending changes. If you only check in a single file, then other edits will list in Excluded changes and won't be checked in.

Also you can use check-in policy to prevent check-in without a review. Here's an existing check-in policy that requires code review before check-in:

https://visualstudiogallery.msdn.microsoft.com/c476b708-77a8-4065-b9d0-919ab688f078

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • That was not my question?! I know how to check in. I know check-in policies. I just want to know, if a build controller could check, if every file mentioned in the csproj-files is on board ... – SiMiDev Aug 29 '16 at 15:27
  • The build controller performs some lightweight tasks, such as determining the name of the build, creating the label in version control, logging notes, and reporting status from the build. It can't validate whether all your files are checked in. You need either check in the whole solution/project or use check-in policy to make sure all files are checked in. – Cece Dong - MSFT Aug 30 '16 at 06:05
  • That’s right. The build controller or its agent performs only basic tasks. MSBuild does the job and MSBuild detects missing files (C# files or files with a configured destination in output folder). Unfortunately MSBuild does not check the other files, which have no need to be compiled or copied. We think it is not possible to use a “Check-in Policy” to solve our problem … I am sorry, but maybe my English is not good enough to point out our initial problem. – SiMiDev Sep 05 '16 at 05:38