StingyJack / Scripty

Tools to let you use Roslyn-powered C# scripts for code generation
MIT License
0 stars 1 forks source link

update #load to attempt loading of files with namespace #3

Open StingyJack opened 7 years ago

StingyJack commented 7 years ago

Sometimes you want to use an existing class but its in a namespace and CSharpScript doesn't like that.

The script #loads could be evaluated for any files with namespaces and create temp copies without them and rewrite the script #load to use the stripped version.

Or a different #loadcls directive could be used to specifically request this behavior.

Or the referenced classes could be compiled into a temp assembly and the #load changed to an #r

Either way some collision or dependency detection and loading may need to be done.

StingyJack commented 7 years ago

using directive #loadAsR to attempt this

simple example

Script to exec (MyScript.csx)

#loadAsR ".\MyClass.cs"

var result = GimmeGimmeSomeLovin("got to have");

Output.WriteLine($"//{result}");

MyClass.cs

using System;
namespace MyNamespace
{
  using System.Text;
  internal class MyClass 
  {
     public string GimmeGimmeSomeLovin(string text)
     {
        return text + " good lovin";
     }
  }
}

the ouptut would be // got to have good lovin

this would be done by substituting a copy of MyClass.cs like this

using System;
//namespace MyNamespace
//{
  using System.Text;
//  internal class MyClass 
//  {
     public string GimmeGimmeSomeLovin(string text)
     {
        return text + " good lovin";
     }
//  }
//}

and replacing the #loadAsR ".\MyClass.cs" with #load ".\MyClass.cs.csx"

There is more work to do about simplification of usings and references in the #loadAsR target

StingyJack commented 7 years ago

using a custom directive requires adding a preprocessor to alter the script (or create a copy and substitute it) prior to creating options. This was a pain in the ass, and I found the SourceFileResolver could be swapped out to let us intercept calls to read file data and allow us to return alternative facts/content for certain files

I have this working with the attached files

ScriptToExecute.csx.txt ReferencedScript.csx.txt ReferencedClass.cs.txt ScriptToExecute.cs.output.txt