/// <summary>
        /// Compiles the specified collection of view models.
        /// </summary>
        private static BindingExpressionCompilationResult CompileViewModels(RoslynExpressionCompilerState state, IEnumerable <DataSourceWrapperInfo> models, String output, Boolean debug)
        {
            state.DeleteWorkingDirectory();

            var referencedAssemblies = GetDefaultReferencedAssemblies(state);

            var initialPassResult =
                PerformInitialCompilationPass(state, models, referencedAssemblies, debug);

            var fixupPassResult =
                PerformSyntaxTreeFixup(initialPassResult);

            if (fixupPassResult.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error).Any())
            {
                if (state.WriteErrorsToFile)
                {
                    WriteErrorsToWorkingDirectory(state, models, fixupPassResult);
                }

                return(BindingExpressionCompilationResult.CreateFailed(CompilerStrings.FailedFinalPass,
                                                                       CreateBindingExpressionCompilationErrors(state, models, fixupPassResult.GetDiagnostics())));
            }

            return(EmitCompilation(state, models, output, fixupPassResult));
        }
        /// <summary>
        /// Emits the specified compilation to either an in-memory stream or a file.
        /// </summary>
        private static BindingExpressionCompilationResult EmitCompilation(RoslynExpressionCompilerState state, IEnumerable <DataSourceWrapperInfo> models, String output, Compilation compilation)
        {
            var outputStream = default(Stream);

            try
            {
                outputStream = state.GenerateInMemory ? new MemoryStream() : (Stream)File.OpenWrite(output);

                var options = new EmitOptions(outputNameOverride: "Ultraviolet.Presentation.CompiledExpressions.dll",
                                              debugInformationFormat: DebugInformationFormat.PortablePdb, fileAlignment: 512, baseAddress: 0x11000000);
                var emitResult = compilation.Emit(outputStream, options: options);
                if (emitResult.Success)
                {
                    var assembly = state.GenerateInMemory ? Assembly.Load(((MemoryStream)outputStream).ToArray()) : null;
                    return(BindingExpressionCompilationResult.CreateSucceeded(assembly));
                }
                else
                {
                    return(BindingExpressionCompilationResult.CreateFailed(CompilerStrings.FailedEmit,
                                                                           CreateBindingExpressionCompilationErrors(state, models, emitResult.Diagnostics)));
                }
            }
            finally
            {
                if (outputStream != null)
                {
                    outputStream.Dispose();
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Compiles the specified collection of view models.
        /// </summary>
        private static BindingExpressionCompilationResult CompileViewModels(LegacyExpressionCompilerState state, IEnumerable <DataSourceWrapperInfo> models, String output, Boolean debug)
        {
            state.DeleteWorkingDirectory();

            var referencedAssemblies = GetDefaultReferencedAssemblies();

            var expressionVerificationResult =
                PerformExpressionVerificationCompilationPass(state, models, referencedAssemblies, debug);

            if (expressionVerificationResult.Errors.Cast <CompilerError>().Where(x => !x.IsWarning).Any())
            {
                if (state.WriteErrorsToFile)
                {
                    WriteErrorsToWorkingDirectory(state, models, expressionVerificationResult);
                }

                return(BindingExpressionCompilationResult.CreateFailed(CompilerStrings.FailedExpressionValidationPass,
                                                                       CreateBindingExpressionCompilationErrors(state, models, expressionVerificationResult.Errors)));
            }

            var setterEliminationPassResult =
                PerformSetterEliminationCompilationPass(state, models, referencedAssemblies, debug);

            var conversionFixupPassResult =
                PerformConversionFixupCompilationPass(state, models, referencedAssemblies, setterEliminationPassResult, debug);

            var finalPassResult =
                PerformFinalCompilationPass(state, state.GenerateInMemory ? null : output, models, referencedAssemblies, conversionFixupPassResult, debug);

            if (finalPassResult.Errors.Cast <CompilerError>().Where(x => !x.IsWarning).Any())
            {
                if (state.WriteErrorsToFile)
                {
                    WriteErrorsToWorkingDirectory(state, models, finalPassResult);
                }

                return(BindingExpressionCompilationResult.CreateFailed(CompilerStrings.FailedFinalPass,
                                                                       CreateBindingExpressionCompilationErrors(state, models, finalPassResult.Errors)));
            }

            return(BindingExpressionCompilationResult.CreateSucceeded(finalPassResult.CompiledAssembly));
        }