visual studio 2013 - How to get a build to fail if project references are not as expected -
on project changed project references hack done. caused ci build failures , other waste. possible create msbuild task can @ project references solution wide check project references against known configuration, , build fail in case of unexpected changes. realise masks deeper problem waste generated last time happened considered unacceptable , i've been asked make sure doesn't happen again.
you use msbuild inline task. task search forbidden references inside files of type csproj , emit logs when finds them. fail build based on task output hasforbiddenreferences.
this approach usable if want execute task 1 time check csproj's. if want each project check itself, must alter task accept project's path , checking.
<?xml version="1.0" encoding="utf-8"?> <project toolsversion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <usingtask taskname="checkforbiddenreferences" taskfactory="codetaskfactory" assemblyfile="$(msbuildtoolspath)\microsoft.build.tasks.v4.0.dll" > <parametergroup> <solutiondir parametertype="system.string" required="true" /> <hasforbiddenreferences parametertype="system.boolean" output="true" /> </parametergroup> <task> <reference include="system.xml.dll"/> <using namespace="system"/> <using namespace="system.io"/> <using namespace="system.linq"/> <using namespace="system.xml.linq"/> <code type="fragment" language="cs"> <![cdata[ hasforbiddenreferences = false; // load forbidden references here var forbiddenreferences = new[] { "forbidden1", "forbidden2", "forbidden3" }; var files = directory.getfiles(solutiondir, "*.csproj", searchoption.alldirectories); xnamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003"; foreach (var f in files.where(f => { var doc = xdocument.parse(file.readalltext(f)); return doc .descendants(msbuild + "reference") .union(doc.descendants(msbuild + "projectreference")) .any(r => forbiddenreferences.any(fr => { var forbid = r.attribute("include").value.contains(fr); if (forbid) log.logmessage(string.format("forbidden reference {0} on project {1}", fr, f)); return forbid; })); })) { log.logmessage(string.format("file {0} contains forbidden references", f)); hasforbiddenreferences = true; } ]]> </code> </task> </usingtask> </project>
call task usual:
<target name="checkreferences" > <checkforbiddenreferences solutiondir="$(solutiondir)" > <output propertyname="hasforbiddenreferences" taskparameter="hasforbiddenreferences" /> </checkforbiddenreferences> <error condition="$(hasforbiddenreferences)" text="build failed due forbidden references"/> </target>
Comments
Post a Comment