private async Task HandleScriptErrors(Func <Task> doWork)
        {
            try
            {
                await doWork();

                if (_scriptState?.Exception != null)
                {
                    Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(_scriptState.Exception));
                }

                if (_scriptState?.ReturnValue != null)
                {
                    _globals.Print(_scriptState.ReturnValue);
                }
            }
            catch (CompilationErrorException e)
            {
                foreach (var diagnostic in e.Diagnostics)
                {
                    Console.WritePrettyError(diagnostic.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(e));
            }
        }
        protected virtual async Task Execute(string input, bool debugMode)
        {
            try
            {
                if (_scriptState == null)
                {
                    var sourceText = SourceText.From(input);
                    var context    = new ScriptContext(sourceText, CurrentDirectory, Enumerable.Empty <string>(), debugMode: debugMode);
                    await RunFirstScript(context);
                }
                else
                {
                    var lineRuntimeDependencies =
                        ScriptCompiler.RuntimeDependencyResolver.GetDependenciesFromCode(CurrentDirectory, input).ToArray();
                    var lineDependencies = lineRuntimeDependencies.SelectMany(rtd => rtd.Assemblies).Distinct();

                    var scriptMap = lineRuntimeDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts);
                    if (scriptMap.Count > 0)
                    {
                        _scriptOptions =
                            _scriptOptions.WithSourceResolver(
                                new NuGetSourceReferenceResolver(
                                    new SourceFileResolver(ImmutableArray <string> .Empty, CurrentDirectory), scriptMap));
                    }
                    foreach (var runtimeDependency in lineDependencies)
                    {
                        Logger.Verbose("Adding reference to a runtime dependency => " + runtimeDependency);
                        _scriptOptions = _scriptOptions.AddReferences(MetadataReference.CreateFromFile(runtimeDependency.Path));
                    }

                    _scriptState = await _scriptState.ContinueWithAsync(input, _scriptOptions, ex => true);
                }

                if (_scriptState?.Exception != null)
                {
                    Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(_scriptState.Exception));
                }

                if (_scriptState?.ReturnValue != null)
                {
                    _globals.Print(_scriptState.ReturnValue);
                }
            }
            catch (Exception e)
            {
                Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(e));
            }
        }
        public virtual ScriptEmitResult Emit <TReturn, THost>(ScriptContext context)
        {
            try
            {
                var compilationContext = _scriptCompiler.CreateCompilationContext <TReturn, THost>(context);
                var compilation        = compilationContext.Script.GetCompilation();

                var         peStream    = new MemoryStream();
                EmitOptions emitOptions = null;
                if (context.OptimizationLevel == Microsoft.CodeAnalysis.OptimizationLevel.Debug)
                {
                    emitOptions = new EmitOptions()
                                  .WithDebugInformationFormat(DebugInformationFormat.Embedded);
                }

                var result = compilation.Emit(peStream, options: emitOptions);

                if (result.Success)
                {
                    return(new ScriptEmitResult(peStream, compilation.DirectiveReferences));
                }

                return(ScriptEmitResult.Error(result.Diagnostics));
            }
            catch (CompilationErrorException e)
            {
                foreach (var diagnostic in e.Diagnostics)
                {
                    _scriptConsole.WritePrettyError(diagnostic.ToString());
                }

                throw;
            }
        }
Exemple #4
0
        private string CreateScriptAssembly(ScriptContext context, string tempProjectDirecory)
        {
            try
            {
                var emitResult = _scriptEmitter.Emit <int>(context, AssemblyName);
                if (!emitResult.Success)
                {
                    throw new CompilationErrorException("One or more errors occurred when emitting the assembly", emitResult.Diagnostics);
                }

                var assemblyPath = Path.Combine(tempProjectDirecory, $"{AssemblyName}.dll");
                using (var peFileStream = new FileStream(assemblyPath, FileMode.Create))
                    using (emitResult.PeStream)
                    {
                        emitResult.PeStream.WriteTo(peFileStream);
                    }

                var pdbPath = Path.Combine(tempProjectDirecory, $"{AssemblyName}.pdb");
                using (var pdbFileStream = new FileStream(pdbPath, FileMode.Create))
                    using (emitResult.PdbStream)
                    {
                        emitResult.PdbStream.WriteTo(pdbFileStream);
                    }

                foreach (var reference in emitResult.DirectiveReferences)
                {
                    if (reference.Display.EndsWith(".NuGet.dll"))
                    {
                        continue;
                    }
                    var refInfo         = new FileInfo(reference.Display);
                    var newAssemblyPath = Path.Combine(tempProjectDirecory, refInfo.Name);
                    File.Copy(refInfo.FullName, newAssemblyPath, true);
                }

                return(assemblyPath);
            }
            catch (CompilationErrorException ex)
            {
                _scriptConsole.WritePrettyError(ex.Message);
                foreach (var diagnostic in ex.Diagnostics)
                {
                    _scriptConsole.WritePrettyError(diagnostic.ToString());
                }
                throw;
            }
        }
Exemple #5
0
        public virtual Task <TReturn> Execute <TReturn, THost>(ScriptContext context, THost host)
        {
            try
            {
                var compilationContext = ScriptCompiler.CreateCompilationContext <TReturn, THost>(context);
                return(Execute(compilationContext, host));
            }
            catch (CompilationErrorException e)
            {
                foreach (var diagnostic in e.Diagnostics)
                {
                    ScriptConsole.WritePrettyError(diagnostic.ToString());
                }

                throw;
            }
        }
        protected virtual async Task Execute(string input, bool debugMode)
        {
            try
            {
                if (_scriptState == null)
                {
                    var sourceText = SourceText.From(input);
                    var context    = new ScriptContext(sourceText, CurrentDirectory, Enumerable.Empty <string>(), debugMode: debugMode);
                    await RunFirstScript(context);
                }
                else
                {
                    var lineDependencies = ScriptCompiler.RuntimeDependencyResolver.GetDependenciesFromCode(CurrentDirectory, input);

                    foreach (var runtimeDependency in lineDependencies)
                    {
                        Logger.Verbose("Adding reference to a runtime dependency => " + runtimeDependency);
                        _scriptOptions = _scriptOptions.AddReferences(MetadataReference.CreateFromFile(runtimeDependency.Path));
                    }

                    _scriptState = await _scriptState.ContinueWithAsync(input, _scriptOptions, ex => true);
                }

                if (_scriptState?.Exception != null)
                {
                    Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(_scriptState.Exception));
                }

                if (_scriptState?.ReturnValue != null)
                {
                    _globals.Print(_scriptState.ReturnValue);
                }
            }
            catch (Exception e)
            {
                Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(e));
            }
        }
        public virtual ScriptEmitResult Emit <TReturn>(ScriptContext context, string assemblyName = null)
        {
            try
            {
                var compilationContext = _scriptCompiler.CreateCompilationContext <TReturn, CommandLineScriptGlobals>(context);

                var compilation = compilationContext.Script.GetCompilation();
                if (!string.IsNullOrEmpty(assemblyName))
                {
                    var compilationOptions = compilationContext.Script.GetCompilation().Options
                                             .WithScriptClassName(assemblyName);
                    compilation = compilationContext.Script.GetCompilation()
                                  .WithOptions(compilationOptions)
                                  .WithAssemblyName(assemblyName);
                }

                var peStream  = new MemoryStream();
                var pdbStream = new MemoryStream();
                var result    = compilation.Emit(peStream, pdbStream: pdbStream, options: new EmitOptions().
                                                 WithDebugInformationFormat(DebugInformationFormat.PortablePdb));

                if (result.Success)
                {
                    return(new ScriptEmitResult(peStream, pdbStream, compilation.DirectiveReferences));
                }

                return(ScriptEmitResult.Error(result.Diagnostics));
            }
            catch (CompilationErrorException e)
            {
                foreach (var diagnostic in e.Diagnostics)
                {
                    _scriptConsole.WritePrettyError(diagnostic.ToString());
                }

                throw;
            }
        }