Skip to content

How to run a script using CompilationDependencyResolver? #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dfkeenan opened this issue Feb 22, 2020 · 2 comments
Closed

How to run a script using CompilationDependencyResolver? #515

dfkeenan opened this issue Feb 22, 2020 · 2 comments

Comments

@dfkeenan
Copy link

Hi,

I am trying to using the CompilationDependencyResolver for my own script runner but the scripts don't compile due to it not knowing what System.Object or System.Void is.

I naively tried:

Dotnet.Script.DependencyModel.Logging.LogFactory logFactory = (t) => (l, m, e) => Log.WriteLine(m);
dependencyResolver = new CompilationDependencyResolver(logFactory);
//...
using var file = File.Open(scriptFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var loader = new InteractiveAssemblyLoader();
var dependencies = dependencyResolver.GetDependencies(path, new[] { scriptFileName }, true, "netcoreapp3.1");

var assemblyReferences = dependencies
                            .SelectMany(d => d.AssemblyPaths)
                            .ToArray();

var scriptOptions = ScriptOptions.Default
    .WithFilePath(scriptFileName)
    .WithMetadataResolver(new NuGetMetadataReferenceResolver(ScriptOptions.Default.MetadataResolver))
    .WithReferences(assemblyReferences)
    //.WithReferences(AssemblyReferences) - I was manually adding assemblies in the past
    .WithEmitDebugInformation(enableDebugging);

var script = CSharpScript.Create<SkiaPadScript>(file, scriptOptions, assemblyLoader:loader);

ScriptState<SkiaPadScript> result = await script.RunAsync();

My application targets netcoreapp3.1.

The script I am testing with is:

#! "netcoreapp3.1"
#r "nuget: SkiaPad.Scripting,1.0.0"
#r "nuget: Newtonsoft.Json,12.0.3"

using System;
using SkiaSharp;
using SkiaPad.Scripting;
using SkiaPad.Scripting.Mathematics;
using Newtonsoft.Json;
var paint = new SKPaint();

paint.Color = SKColors.Purple;

public void Start(ScriptContext context, SKCanvas canvas, SKRect bounds)
{    
    Console.WriteLine(JsonConvert.SerializeObject(paint));
}

public void Draw(ScriptContext context, SKCanvas canvas, SKRect bounds)
{
    canvas.Clear(SKColors.Yellow);
    
}

public void Dispose()
{

}

Any help would be greatly appreciated.

Thanks,
Daniel

@dfkeenan dfkeenan changed the title How to run a script using CompilationDependencyResolver How to run a script using CompilationDependencyResolver? Feb 22, 2020
@dfkeenan
Copy link
Author

I am not sure if it is the correct way to do it. But doing this has made it work.

var loaded = AppDomain.CurrentDomain.GetAssemblies()
    .Where(a => !a.IsDynamic)
    .Select(a => a.Location)
    .ToDictionary(l => Path.GetFileName(l));

var dependencies = dependencyResolver.GetDependencies(path, new[] { scriptFileName }, true, "netcoreapp3.1");

var assemblyReferences = dependencies
                            .SelectMany(d => d.AssemblyPaths)
                            .Select(l => loaded.TryGetValue(Path.GetFileName(l), out var e) ? e : l)
                            .ToArray();

@seesharper
Copy link
Collaborator

Compilation dependencies are used by OmniSharp to provide language services like Intellisense. It is the runtime dependencies you need to compile and run a script

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

No branches or pull requests

2 participants