How can I prevent bad project references?

2019-05-11 07:02发布

We are using C# projects with TFS as source control and for the CI builds.

I keep finding that other developers are referencing assemblies from /Bin directories incorrectly when they should be using our /Libs folder (where we keep 3rd party assemblies)

What can I do as part of the solution build or CI build (we do also use powershell) to check and fail the build if anyone does this?

2条回答
劳资没心,怎么记你
2楼-- · 2019-05-11 07:25

Add a custom activity to your build process template. The activity's pseudo code should look like:

  • Execute before the compilation phase.
  • Loop all new changesets containing file extensions ending with *proj.
  • For all *proj files, search their contents for HintPath elements containing \Bin.
  • If results > 0, exit build with error, listing the policy failing projects.

To complete the solution also consider enforcing a custom check-in policy for the VS clients.

查看更多
Melony?
3楼-- · 2019-05-11 07:32

Since you are using PowerShell you might as well use it for this problem; the principle is straightforward: parse all csproj files and check if the HintPath doe not contain your Bin directory. In PowerShell that is something like (I've only just begun learning PS so there might be shorter ways):

# define path to bindir (or part of it) and main source dir
$binDir = path\to\Bin
$sourceDir = path\to\sourcefiles

# fix dots and slashes (or anything that cannot be used in regex
$binDirReg = $binDir -replace '\\', '\\' -replace '\.', '\.'

# parse
$res = Get-ChildItem -Recurse -Include *.csproj -Path $sourceDir |
       Select-String "HintPath>.*$binDirReg.*<"

if( $res )
{
  "ERROR somebody used Bin dir instead of Lib"
}
查看更多
登录 后发表回答