ppittle / pMixins

pMixins - Mixin framework for C#
http://pMixins.com
Apache License 2.0
23 stars 5 forks source link

Add sample of calling of codegeneration out of T4 template #37

Open dzmitry-lahoda opened 9 years ago

dzmitry-lahoda commented 9 years ago

If want to support MonoDevelop or SharpDevelop, may you provide sample how to run mixing engine out of .tt file?

I did such metaprograming to generate try/catch code with NRefactory, seems for mixins if may be OK.

ppittle commented 9 years ago

Hi asd-and-Rizzo!

Thank you for the comment, and apologies for the slow reply, I've been out for vacation.

I'm not sure I understand your post, can you expand? Are you looking to run pMixins as part of a T4 pipeline?

As for supporting MonoDevelop or SharpDevelop, this is unfortunately outside the scope for pMixins. I do not have enough familiarity with these IDEs to know what it would take to create a pMixins plugin targeting them. However, the code generation portion of pMixins is isolated from the host IDE such that, an instance of pMixinPartialCodeGenerator (which is responsible for running the code generation pipeline and returning the mixin code behind source code) can run without a dependency on Visual Studio. So if you wanted to create a port, it should be possible and I'd be happy to support you in this effort.

dzmitry-lahoda commented 9 years ago

pMixinPartialCodeGenerator seems good point. I want to use pMixing to work in all IDEs. I think it is possible just by running pMixing out of *.tt.

Once I used NRefractory to read cs files in project to generated try/catch wrappers during compilation when IDE called T4 (https://gitorious.org/asdandrizzo/clr/source/1fa0b825dbb9ce9e34e88e3a551dca052bb8b0d9:tools/MetaSharp/MetaSharp.Samples/ExceptionWrapper.tt).

Seems I can try check if pMixing work well out of T4. If I can write try/catch decorator with pMixins. I hope pMixins support such scenarios or at least provide good DSL to work with such.

ppittle commented 9 years ago

I'm not very familiar with the inner workings of T4, but if you look at CopaceticSoftware.pMixins.CodeGenerator.Tests.IntegrationTests, this has an example in the MainSetup method for using the pMixinPartialCodeGenerator directly:

var generator = new pMixinPartialCodeGenerator();

//load the ContextFactory using Ninject DI
var CodeGeneratorContextFactory = Kernel.Get<ICodeGeneratorContextFactory>();

//build the Generation Context
var CodeGenerationContext =
                    CodeGeneratorContextFactory
                        .GenerateContext(
                            new []{
                            new RawSourceFile
                            {
                                FileContents = SourceCode,
                                FileName = new FilePath(@"c:\testing\testFile.cs"),
                                ProjectFileName = ProjectFile
                            }})
                        .First();

//run the pMixins Code Generator
var response = generator.GeneratePartialCode(CodeGenerationContext);

//get the code behind file text
var generatedCode = response.GeneratedCodeSyntaxTree.GetText();

The most complicated part is loading the ICodeGeneratorContextFactory. pMixins needs to know a lot about the Solution in order to operate correctly. Essentially, it needs to know all of the referenced assemblies in case you want to add a mixin from an external assembly.

T4 in Visual Studio has access to the DTE object which pMixins can consume via the SolutionDTEReader. There is also a SolutionFileReader for reading a .sln file directly. However, I'm not sure how pMixins could get this information in other IDEs.

dzmitry-lahoda commented 9 years ago

Thanks. This seems enough information to try some things to work.

dzmitry-lahoda commented 9 years ago

Actually what I want step by step integration of pipeline like "Read Code -> Generate Code" so existing projects can migrate by small step.

Real situations I solved once times:

  1. We used Unity Interceptions to try/catch methods of several classes.
  2. Appears to slow for start up time (+200 milliseconds of MS Office addin is very bad)
  3. Not so good performance for methods that just check dictionary and returns int but called 1000 times on MS office start.

So I come up with solution to use NRefactory+T4 and it took me 2 days to replace runtime via compile time.

Mobile solution for .NET CF of Xamarin iOS could have similar issue. And if consider that Xamarin Studio with T4 is used, I need to dig way to use pMixing without special add-in.

E.g. starting with manual listing of files pMixing should now, and not dependency on whatever DI container.

Starting small to inject pMixin into dev pipeline can be rather important.