コード例 #1
0
        public void ParseDirectives()
        {
            var scriptSource = BuildSimpleValidScriptSource();

            var result = InterceptDirectiveResolver.ParseDirectives(scriptSource.FilePath);

            Assert.IsNotNull(result);
        }
コード例 #2
0
        public void ConstructionParamsTest()
        {
            var searchPaths = new List <string> {
                CsTestHelpers.ProjectFilePath, CsTestHelpers.GetTestFileSubFolder()
            };
            var basePath = AppContext.BaseDirectory;

            var idr = new InterceptDirectiveResolver(searchPaths.ToImmutableArray(), basePath);

            Assert.IsNotNull(idr);
        }
コード例 #3
0
ファイル: ScriptEngine.cs プロジェクト: xiaoxiongnpu/Scripty
        public async Task <ScriptResult> Evaluate(ScriptSource source)
        {
            var resolver        = new InterceptDirectiveResolver();
            var assembliesToRef = new List <Assembly>
            {
                typeof(object).Assembly,                             //mscorlib
                typeof(Microsoft.CodeAnalysis.Project).Assembly,     // Microsoft.CodeAnalysis.Workspaces
                typeof(Microsoft.Build.Evaluation.Project).Assembly, // Microsoft.Build
                typeof(ScriptEngine).Assembly                        // Scripty.Core
            };

            var namepspaces = new List <string>
            {
                "System",
                "Scripty.Core",
                "Scripty.Core.Output",
                "Scripty.Core.ProjectTree"
            };

            var options = ScriptOptions.Default
                          .WithFilePath(source.FilePath)
                          .WithReferences(assembliesToRef)
                          .WithImports(namepspaces)
                          .WithSourceResolver(resolver);

            using (ScriptContext context = GetContext(source.FilePath))
            {
                try
                {
                    await CSharpScript.EvaluateAsync(source.Code, options, context);

                    foreach (var outputFile in context.Output.OutputFiles)
                    {
                        (outputFile as OutputFile).Close();

                        if (outputFile.FormatterEnabled)
                        {
                            var document = ProjectRoot.Analysis.AddDocument(outputFile.FilePath, File.ReadAllText(outputFile.FilePath));

                            var resultDocument = await Formatter.FormatAsync(
                                document,
                                outputFile.FormatterOptions.Apply(ProjectRoot.Workspace.Options)
                                );

                            var resultContent = await resultDocument.GetTextAsync();

                            File.WriteAllText(outputFile.FilePath, resultContent.ToString());
                        }
                    }
                }
                catch (CompilationErrorException compilationError)
                {
                    return(new ScriptResult(context.Output.OutputFiles,
                                            compilationError.Diagnostics
                                            .Select(x => new ScriptError
                    {
                        Message = x.GetMessage(),
                        Line = x.Location.GetLineSpan().StartLinePosition.Line,
                        Column = x.Location.GetLineSpan().StartLinePosition.Character
                    })
                                            .ToList()));
                }
                catch (AggregateException aggregateException)
                {
                    return(new ScriptResult(context.Output.OutputFiles,
                                            aggregateException.InnerExceptions
                                            .Select(x => new ScriptError
                    {
                        Message = x.ToString()
                    }).ToList()));
                }
                catch (Exception ex)
                {
                    return(new ScriptResult(context.Output.OutputFiles,
                                            new[]
                    {
                        new ScriptError
                        {
                            Message = ex.ToString()
                        }
                    }));
                }
                return(new ScriptResult(context.Output.OutputFiles));
            }
        }
コード例 #4
0
        public void ConstructionDefaultTest()
        {
            var idr = new InterceptDirectiveResolver();

            Assert.IsNotNull(idr);
        }