internal BindingCompilerOptimization(XamlPlatformInfo xamlPlatformInfo, ProjectInfo projectInfo, BindingCompilerSettings bindingCompilerSettings, IFileSystem fileSystem)
        {
            this.xamlPlatformInfo        = xamlPlatformInfo;
            this.projectInfo             = projectInfo;
            this.useSourceGenerator      = bindingCompilerSettings.UseSourceGenerator;
            this.bindingXamlPlatformInfo = new BindingXamlPlatformInfo(xamlPlatformInfo, bindingCompilerSettings);
            this.bindingTreeParser       = new BindingTreeParser(
                this.bindingXamlPlatformInfo,
                new BindingMarkupExtensionParser(new BindingPathParser(new BindingPathLexicalAnalyzer())),
                bindingCompilerSettings.OptInToOptimizations);
            var codeAnalyzer = new CodeAnalyzer(
                this.projectInfo.AssemblyName,
                this.projectInfo.Compiles,
                this.projectInfo.AssemblyReferences,
                fileSystem.File,
                new XamlTypeBaseTypeSourceCodeGenerator(
                    this.projectInfo.XDocumentProvider,
                    this.bindingXamlPlatformInfo.XClassName,
                    this.projectInfo.AssemblyName,
                    this.projectInfo.AssemblyReferences,
                    this.bindingXamlPlatformInfo.XamlTypeToSourceCodeNamespaces),
                !bindingCompilerSettings.UseSourceGenerator);

            this.typeResolver = new TypeResolver(codeAnalyzer);
            this.bindingOptimizationWriter = new BindingOptimizationWriter(this.projectInfo.IntermediateDirectory, xamlPlatformInfo, fileSystem);
            this.bindingContainerXamlModificationCollector = new BindingContainerXamlModificationCollector();
            this.bindingContainerCodeGenerator             = new BindingContainerCodeGenerator(
                new BindingPathCodeGenerator(
                    this.typeResolver,
                    this.bindingXamlPlatformInfo,
                    new ReadOnlyDependencyPropertyToNotificationEventResolver(codeAnalyzer, this.bindingXamlPlatformInfo.ReadOnlyDependencyPropertyNotificationEvents),
                    new BindingModeResolver(this.bindingXamlPlatformInfo.OneWayBindingProperties, codeAnalyzer, xamlPlatformInfo.XamlPlatform),
                    new TypeAssignmentCompatibilityAssessor(codeAnalyzer)),
                this.bindingXamlPlatformInfo,
                this.typeResolver);
        }
Exemplo n.º 2
0
        public async Task Visit_Then_BindingTreeToStringVisitorShouldOutputExpectedResult(string inputFilePath, string[] compiles, string expectedXamlDocumentPath, string[] expectedSourceCodePaths)
        {
            var xamlDocument = await LoadXDocument(inputFilePath).ConfigureAwait(false);

            var assemblyReferences = GetAssemblyReferences(xamlPlatformInfo.XamlPlatform);
            var codeAnalyzer       = new CodeAnalyzer(ContainingAssemblyName, compiles.Select(x =>
            {
                var compile = Substitute.For <IFileReference>();
                compile.Path.Returns(x);
                return(compile);
            }).ToList(),
                                                      assemblyReferences,
                                                      this.fileSystem.File,
                                                      null,
                                                      false);
            var typeResolver            = new TypeResolver(codeAnalyzer);
            var xamlTypeResolver        = XamlTypeResolver.FromXDocument(xamlDocument, ContainingAssemblyName, assemblyReferences, bindingXamlPlatformInfo.XamlTypeToSourceCodeNamespaces);
            var xamlElementNameResolver = new XamlElementNameResolver(xamlPlatformInfo.XamlNamespace);
            var bindingTreeParser       = new BindingTreeParser(
                bindingXamlPlatformInfo,
                new BindingMarkupExtensionParser(new BindingPathParser(new BindingPathLexicalAnalyzer())),
                false);
            var bindingTree = bindingTreeParser.Parse(xamlDocument.Root, Path.GetDirectoryName(inputFilePath), Path.GetFileNameWithoutExtension(inputFilePath), xamlTypeResolver, xamlElementNameResolver);

            var fileReference = Substitute.For <IFileReference>();

            fileReference.Id.Returns(inputFilePath);
            var generatorInfo = GeneratorInfo.Get(fileReference, ContainingAssemblyName, RootNamespace);
            var xamlModificationCollectionResult = this.bindingContainerXamlModificationCollector.Visit(
                bindingTree,
                new Xaml.Optimizations.Bindings.Internal.XamlModification.BindingContainer.Parameters(generatorInfo, xamlTypeResolver),
                new Xaml.Optimizations.Bindings.Internal.XamlModification.BindingContainer.Context(new XamlElementNameProvider(xamlElementNameResolver)));

            var testee = new BindingContainerCodeGenerator(
                new BindingPathCodeGenerator(
                    typeResolver,
                    this.bindingXamlPlatformInfo,
                    new ReadOnlyDependencyPropertyToNotificationEventResolver(
                        codeAnalyzer,
                        bindingXamlPlatformInfo.ReadOnlyDependencyPropertyNotificationEvents),
                    new BindingModeResolver(bindingXamlPlatformInfo.OneWayBindingProperties, codeAnalyzer, xamlPlatformInfo.XamlPlatform),
                    new TypeAssignmentCompatibilityAssessor(codeAnalyzer)),
                this.bindingXamlPlatformInfo, typeResolver);

            var result = testee.Visit(
                bindingTree,
                new Xaml.Optimizations.Bindings.Internal.CodeGenerators.BindingContainer.Parameters(in generatorInfo, xamlTypeResolver, xamlModificationCollectionResult.Value.BindingRootNodeTypes),
                new Context(new XamlElementNameProvider(xamlElementNameResolver), new BindingSourceProvider(typeResolver)));

            this.bindingOptimizationWriter.ApplyOptimizations(result.Value);
            this.bindingOptimizationWriter.ApplyOptimizations(xamlModificationCollectionResult.Value.XamlModificationInfos, this.bindingXamlPlatformInfo.SundewBindingsXamlNamespace);

            var sourceCodePairs = expectedSourceCodePaths.Zip(result.Value, (path, container) => new { FileName = Path.GetFileNameWithoutExtension(path), ExpectedContent = ReadResourceStreamToEnd(path), container.SourceCode });

            foreach (var sourceCodePair in sourceCodePairs)
            {
                sourceCodePair.SourceCode.Should().Be(sourceCodePair.ExpectedContent);
                this.fileSystem.File.Received(1).WriteAllText(Arg.Any <string>(), sourceCodePair.ExpectedContent);
            }

            xamlDocument.ToString().Trim().Should().Be((await LoadXDocument(expectedXamlDocumentPath).ConfigureAwait(false)).ToString().Trim());
        }