1

We are attempting to migrate a large code-base from one UI library to another. The 2 libraries are conceptually quite similar with some naming differences in most cases. We would like to automate as much of this as possible.

We want to implement something similar to Unity's API updater tool(https://docs.unity3d.com/Manual/APIUpdater.html) which automatically replaces calls to obsolete APIs. Are there are source weaving tools/frameworks for C#? I know there's Fody (https://github.com/Fody/Fody) but it operates at the IL level, not at the source level.

BlueSilver
  • 1,052
  • 2
  • 10
  • 25
  • *operates at the IL level, not at the source level* I think they're all IL-levle tools. Aside from that ... you could use Roslyn to parse the code and transform it? – ta.speot.is Apr 04 '18 at 06:26
  • Is that the easiest option I've got? That sounds like a lot of work which I won't be able to justify spending time for. – BlueSilver Apr 04 '18 at 06:28
  • the tags are wrong: This is not source weaving, but rather source code transformation. Source weaving is when you want to add a preprocessing or postprocessing step for every compilation, what you want is a one-time transformation of the code. – Georg Apr 04 '18 at 08:10
  • It should be quite easy to do with Roslyn, which works on source code level just like you need. – Evk Apr 04 '18 at 09:09
  • @Georg Edited the title accordingly, couldn't find tags though. – BlueSilver Apr 05 '18 at 09:23
  • @Evk Yeah, it was nowhere as complicated I thought it would be - Roslyn seems quite simple. I will try it. – BlueSilver Apr 06 '18 at 12:03

2 Answers2

0

I would agree with @ta.speot.is from the comments, I guess Roslyn would be the best option here. The main advantages of Roslyn here are that it supports all (including the latest) features of C#, and that it does a semantic analysis, so you can actually know what method is meant and filter with that. The downside of Roslyn is that it is not really nice for code generation, because the data structures used by Roslyn are immutable, but unless your transformation is very complex, I guess this does not have to bother you much.

There is even an official example for using Roslyn for source code transformation, very close to what I believe you need: https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Transformation

Depending on the age of your code, CodeDOM could also be an alternative: Read the code as CodeDOM, transform the CodeDOM and generate code from it. However, CodeDOM was meant to support only the commonalities of all major .NET languages, so you will lose a lot if you do that. If your code uses things like lambda expressions or covariance, tuples or even coalesce operators (??), I think CodeDOM is not going to help you much. Also, CodeDOM does not perform any semantic analysis. So, my recommendation would still be Roslyn.

Georg
  • 5,626
  • 1
  • 23
  • 44
  • Thanks, that was super helpful! I took a look at Roslyn and it seems pretty straightforward. I think I'll go with that. – BlueSilver Apr 06 '18 at 12:02
0

There is another potential solution depending on the amount of work needed to implement it.

If your two libraries have little difference between them except for some nomenclature then potentially you want to write an adapter that will take one API set and be a pass through to the other, with some naming changes. Combine this idea with Unity and inheritance and this may be an inexpensive solution.

AviFarah
  • 327
  • 1
  • 10