Skip to content

Conversation

@StingyJack
Copy link
Contributor

This lets a .cs file be loaded and used as script. For me this solves some of the preprocessor directive hacking I was doing to reuse existing classes or get intellisense. (ideas gleaned from issue #36 )

There is a flowchart and some other docs that describe it more completely but the gist is that a derived SourceFileResolver is passed into the ScriptOptions, and it intercepts the calls to .cs files and uses SyntaxTree et.al. to provide a "namespace free" compiled version of the source that can be lifted into the script scope.

There are a few orphaned bits in the CsRewriter class that I left in because I think adding debugging support will require them.

I tried to not pollute this with the OutputBehavior PR. Please review and suggest any edits, corrections, or additions.

Example of the script with #load for .cs

#r ".\..\..\..\packages\NUnit.3.4.0\lib\net45\nunit.framework.dll"
#load "..\TestCs\ReferencedClass.cs"
#load "ReferencedScript.csx"

//Write using supplied ScriptContext
Output.WriteLine("namespace TestNamespace{class TestClass{public void TestMethod(){}}}");

//with the referenced assembly
var bag = new NUnit.Framework.Internal.PropertyBag();
Output.WriteLine($"// {bag}");

//With referenced script
Output.WriteLine($"// we have multiplied {Go4th(2, 3)}");

//Create instance from intercepted class
var rc1 = new ReferencedClass(Context);
Output.WriteLine($"// Emitting prop with backing field {rc1.PropertyWithBackingField}");
rc1.Owl($"// using the referenced class to output")


//Dave Glick - is this just template copyright stuff that was brought in or
// is this something else?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the two files with this copyright header were copied wholesale from a Microsoft example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these classes are included in the newer VS shell references. You may be able to remove them if you up the shell ref's.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - planning on continuing work on updating everything tomorrow so I'll take a look and see if they can be stripped.

@daveaglick daveaglick merged commit 995afca into daveaglick:develop Mar 13, 2017
@daveaglick
Copy link
Owner

Thanks. This pull request has been integrated manually.

@daveaglick
Copy link
Owner

Went ahead and merged this one, everything looks good. Unfortunately I'm having tons of other problems with the new Roslyn assemblies, so a release will have to wait a bit, but I'll get it out as soon as I can get everything working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants