7

Is it possible to hook into the Roslyn build process during a Visual Studio/TFS build, and if yes, is it possible to get a hold of the Microsoft.CodeAnalysis.Solution/Microsoft.CodeAnalysis.Project instance being used by Roslyn during compilation?

dotarj
  • 418
  • 2
  • 8
  • It would be easier to help you if you'd say what you're trying to achieve. For example, you can add extra code diagnostics relatively easily. – Jon Skeet Nov 21 '14 at 21:06
  • @Jon: I want to do some reference validation (eg. projects that should not reference some other projects) on the projects being build, so I need to get a hold of the Microsoft.CodeAnalysis.Solution instance being build. – dotarj Nov 21 '14 at 21:16
  • 1
    Right. That sounds like it might be something better aimed at an MSBuild task than Roslyn... which doesn't mean to say there *isn't* a way of doing it in Roslyn, of course. – Jon Skeet Nov 21 '14 at 21:17
  • @Jon: Yes, thanks. I've added the msbuild and msbuild-task tags. – dotarj Nov 21 '14 at 21:23

1 Answers1

0

The way I see it, Jon is pretty much right in his comment. What I would suggest is to create an MSBuild task, which is your wanted hook into the build process.

Create an MSBuild project file (you've probably seen them already, it's those files that have the .targets extension). It looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="MyNamespace.MyReferenceValidationTask" AssemblyFile="MyPath\MyNamespace.dll"/>
    <Target 
        BeforeTargets="BeforeCompile"
        Name="ValidationTarget">
        <MyNamespace.MyReferenceValidationTask
            SolutionRoot="$(SolutionDir)" />
    </Target>
</Project>

The attribute in the "MyNamespace.MyReferenceValidationTask" tag "SolutionRoot" is your property in your task. All the macros that are available in Visual Studio are also available here. (see this post here: https://stackoverflow.com/a/1453023/978594)

What you do inside of the task is entirely up to you. You can, for example, load your solution file with Roslyn and thus have all the projects and their references and do your desired validation.

Community
  • 1
  • 1
Alex Endris
  • 444
  • 4
  • 16