How can I prevent bad project references?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 09:19:11

问题


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?


回答1:


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.




回答2:


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"
}


来源:https://stackoverflow.com/questions/22289610/how-can-i-prevent-bad-project-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!