Exemplo n.º 1
0
        public async Task Should_Generate_FindControl_Refs_From_Avalonia_Markup_File(
            string expectation,
            string markup,
            bool devToolsMode)
        {
            var excluded    = devToolsMode ? null : "Avalonia.Diagnostics";
            var compilation =
                View.CreateAvaloniaCompilation(excluded)
                .WithCustomTextBox();

            var types         = new RoslynTypeSystem(compilation);
            var classResolver = new XamlXViewResolver(
                types,
                MiniCompiler.CreateDefault(
                    new RoslynTypeSystem(compilation),
                    MiniCompiler.AvaloniaXmlnsDefinitionAttribute));

            var xaml = await View.Load(markup);

            var classInfo    = classResolver.ResolveView(xaml);
            var nameResolver = new XamlXNameResolver();
            var names        = nameResolver.ResolveNames(classInfo.Xaml);

            var generator = new InitializeComponentCodeGenerator(types);

            var code = generator
                       .GenerateCode("SampleView", "Sample.App", classInfo.XamlType, names)
                       .Replace("\r", string.Empty);

            var expected = await InitializeComponentCode.Load(expectation);


            CSharpSyntaxTree.ParseText(code);
            Assert.Equal(expected.Replace("\r", string.Empty), code);
        }
Exemplo n.º 2
0
 public XamlXViewResolver(
     RoslynTypeSystem typeSystem,
     MiniCompiler compiler,
     bool checkTypeValidity        = false,
     Action <string> onTypeInvalid = null)
 {
     _checkTypeValidity = checkTypeValidity;
     _onTypeInvalid     = onTypeInvalid;
     _typeSystem        = typeSystem;
     _compiler          = compiler;
 }
Exemplo n.º 3
0
    public static MiniCompiler CreateDefault(RoslynTypeSystem typeSystem, params string[] additionalTypes)
    {
        var mappings = new XamlLanguageTypeMappings(typeSystem);

        foreach (var additionalType in additionalTypes)
        {
            mappings.XmlnsAttributes.Add(typeSystem.GetType(additionalType));
        }

        var configuration = new TransformerConfiguration(
            typeSystem,
            typeSystem.Assemblies[0],
            mappings);

        return(new MiniCompiler(configuration));
    }
        public async Task Should_Resolve_Base_Class_From_Xaml_File(string nameSpace, string className, string markup)
        {
            var xaml = await View.Load(markup);

            var compilation = View
                              .CreateAvaloniaCompilation()
                              .WithCustomTextBox();

            var types    = new RoslynTypeSystem(compilation);
            var resolver = new XamlXViewResolver(
                types,
                MiniCompiler.CreateDefault(types, MiniCompiler.AvaloniaXmlnsDefinitionAttribute));

            var resolvedClass = resolver.ResolveView(xaml);

            Assert.Equal(className, resolvedClass.ClassName);
            Assert.Equal(nameSpace, resolvedClass.Namespace);
        }
        private static INameGenerator CreateNameGenerator(GeneratorExecutionContext context)
        {
            var options = new GeneratorOptions(context);
            var types = new RoslynTypeSystem((CSharpCompilation)context.Compilation);
            var defaultFieldModifier = options.AvaloniaNameGeneratorDefaultFieldModifier.ToString().ToLowerInvariant();
            ICodeGenerator generator = options.AvaloniaNameGeneratorBehavior switch {
                Behavior.OnlyProperties => new OnlyPropertiesCodeGenerator(),
                Behavior.InitializeComponent => new InitializeComponentCodeGenerator(types),
                _ => throw new ArgumentOutOfRangeException()
            };

            var compiler = MiniCompiler.CreateDefault(types, MiniCompiler.AvaloniaXmlnsDefinitionAttribute);
            return new AvaloniaNameGenerator(
                new GlobPatternGroup(options.AvaloniaNameGeneratorFilterByPath),
                new GlobPatternGroup(options.AvaloniaNameGeneratorFilterByNamespace),
                new XamlXViewResolver(types, compiler, true, type => ReportInvalidType(context, type)),
                new XamlXNameResolver(defaultFieldModifier),
                generator);
        }