Is it possible to modify source code before compilation using Roslyn within MSBuild task on CI server? I've succeeded to do what I want in VS but I wonder if it is possible outside VS. Currently I'm looking at Workspace APIs and Compiler APIs and they seem to be the right tool to achieve that, but I'm still not sure is it possible at all? In particular I'm concerned about returning changes that I've done to MSBuild back to allow it to continue its job.
This is definitely a scenario that we are thinking of. Today there are a couple of problems that make it a bit difficult:
In the future, we'd like to provide a "Create a workspace from a csc/vbc command line string", which would make this a lot easier.
Take a look at Hooking into the compiler (csc.exe or vbc.exe) itself and Problem with using Roslyn in a MS Build Task for some previous discussion on this.
regarding the question on Problem with using Roslyn in a MS Build Task,
public class MyTask : Task
changing Task to AppDomainIsolatedTask is a quick fix. I don't have msdn account so I'll just post it here. Hope it can help.
I have not personally been able to test this, (so if you will, treat this "answer" as a comment), but reading stuff about Roslyn, especially this blog entry, it looks like it should be possible (look for the "Rewrite" paragraph.
Apparently, you can construct a syntax tree from the source (not too surprising for a compiler I guess), then modify this using APIs, and finally write it back out to a string/file or whatever.
Also see MSDN about Roslyn syntax trees conceptually, and this walkthrough about "Syntax Transformation".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With