private async Task <IJavaScriptCompilation> CompileAndTraceAsync(LogTargets logTargets, bool throwOnCompilationError = true, bool suppressCompilationSummary = true)
        {
            try
            {
                IJavaScriptCompilation compilation = await _compilationService.GetFunctionCompilationAsync(Metadata);

                if (compilation.SupportsDiagnostics)
                {
                    ImmutableArray <Diagnostic> diagnostics = compilation.GetDiagnostics();
                    TraceCompilationDiagnostics(diagnostics, logTargets);

                    if (!suppressCompilationSummary)
                    {
                        bool compilationSucceeded = !diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error);

                        TraceOnPrimaryHost(string.Format(CultureInfo.InvariantCulture, "Compilation {0}.", compilationSucceeded ? "succeeded" : "failed"), TraceLevel.Info);
                    }
                }

                return(compilation);
            }
            catch (CompilationErrorException ex)
            {
                TraceOnPrimaryHost("Function compilation error", TraceLevel.Error);
                TraceCompilationDiagnostics(ex.Diagnostics, logTargets);

                if (throwOnCompilationError)
                {
                    throw;
                }
            }

            return(null);
        }
示例#2
0
        private async Task ReloadScriptAsync()
        {
            // Reset cached function
            _functionLoader.Reset();
            _compilerErrorCount = 0;
            LogOnPrimaryHost(string.Format(CultureInfo.InvariantCulture, "Script for function '{0}' changed. Reloading.", Metadata.Name), LogLevel.Information);

            ImmutableArray <Diagnostic> compilationResult = ImmutableArray <Diagnostic> .Empty;
            FunctionSignature           signature         = null;

            try
            {
                IDotNetCompilation compilation = await _compilationService.GetFunctionCompilationAsync(Metadata);

                compilationResult = compilation.GetDiagnostics();

                // TODO: Invoking this without the assembly is not supported by all compilations
                signature         = compilation.GetEntryPointSignature(_functionEntryPointResolver, null);
                compilationResult = ValidateFunctionBindingArguments(signature, _triggerInputName, _inputBindings, _outputBindings, compilationResult.ToBuilder());
            }
            catch (CompilationErrorException exc)
            {
                compilationResult = AddFunctionDiagnostics(compilationResult.AddRange(exc.Diagnostics));
            }

            TraceCompilationDiagnostics(compilationResult, LogTargets.User);

            bool compilationSucceeded = !compilationResult.Any(d => d.Severity == DiagnosticSeverity.Error);

            LogOnPrimaryHost(string.Format(CultureInfo.InvariantCulture, "Compilation {0}.", compilationSucceeded ? "succeeded" : "failed"), LogLevel.Information);

            // If the compilation succeeded, AND:
            //  - We haven't cached a function (failed to compile on load), OR
            //  - We're referencing local function types (i.e. POCOs defined in the function) AND Our our function signature has changed
            // Restart our host.
            if (compilationSucceeded &&
                (_functionSignature == null ||
                 (_functionSignature.HasLocalTypeReference || !_functionSignature.Equals(signature))))
            {
                await Host.RestartAsync().ConfigureAwait(false);
            }
        }