Example #1
0
        internal static bool IsTypePresent(string typeName, RoslynMetadataHelper metadataHelper)
        {
            var typeSymbol = metadataHelper.FindTypeByFullName(typeName);

            if (typeSymbol == null)
            {
                return(false);
            }

            if (typeSymbol.GetAttributes().Any(a => a.AttributeClass?.Name == "NotImplementedAttribute"))
            {
                return(false);
            }

            return(true);
        }
Example #2
0
 public XamlFileParser(string[] excludeXamlNamespaces, string[] includeXamlNamespaces, RoslynMetadataHelper roslynMetadataHelper)
 {
     _excludeXamlNamespaces = excludeXamlNamespaces;
     _includeXamlNamespaces = includeXamlNamespaces;
     this._metadataHelper   = roslynMetadataHelper;
 }
Example #3
0
#pragma warning restore 649 // Unused member

        public XamlCodeGeneration(Compilation sourceCompilation, ProjectInstance msbProject, Project roslynProject)
        {
            // To easily debug XAML code generation:
            // 1. Uncomment the line below
            // 2. Build Uno.UI.SourceGenerators and override local files (following instructions here: doc/articles/uno-development/debugging-uno-ui.md#debugging-unoui)
            // 3. Build project containing your XAML. When prompted to attach a Visual Studio instance:
            //		- if it's in an external solution, attach the VS instance running Uno.UI
            //		- if you're debugging XAML generation inside the Uno solution, opt to create a new VS instance
            //
            //Debugger.Launch();

            InitTelemetry(msbProject);

            _legacyTypes = msbProject
                           .GetItems("LegacyTypes")
                           .Select(i => i.EvaluatedInclude)
                           .ToList()
                           .ToDictionary(fullyQualifiedName => fullyQualifiedName.Split('.').Last());

            _metadataHelper      = new RoslynMetadataHelper("Debug", sourceCompilation, msbProject, roslynProject, null, _legacyTypes);
            _assemblySearchPaths = new string[0];
            _projectInstance     = msbProject;

            _configuration = msbProject.GetProperty("Configuration")?.EvaluatedValue
                             ?? throw new InvalidOperationException("The configuration property must be provided");

            _isDebug = string.Equals(_configuration, "Debug", StringComparison.OrdinalIgnoreCase);

            var xamlItems = msbProject.GetItems("Page")
                            .Concat(msbProject.GetItems("ApplicationDefinition"));

            _xamlSourceFiles = xamlItems.Select(d => d.EvaluatedInclude)
                               .ToArray();

            _xamlSourceLinks = xamlItems.Select(GetSourceLink)
                               .ToArray();

            _excludeXamlNamespaces = msbProject
                                     .GetItems("ExcludeXamlNamespaces")
                                     .Select(i => i.EvaluatedInclude)
                                     .ToArray();

            _includeXamlNamespaces = msbProject
                                     .GetItems("IncludeXamlNamespaces")
                                     .Select(i => i.EvaluatedInclude)
                                     .ToArray();

            _analyzerSuppressions = msbProject
                                    .GetItems("XamlGeneratorAnalyzerSuppressions")
                                    .Select(i => i.EvaluatedInclude)
                                    .ToArray();

            _resourceFiles = msbProject
                             .GetItems("PRIResource")
                             .Select(i => i.EvaluatedInclude)
                             .ToArray();

            if (bool.TryParse(msbProject.GetProperty("UseUnoXamlParser")?.EvaluatedValue, out var useUnoXamlParser) && useUnoXamlParser)
            {
                XamlRedirection.XamlConfig.IsUnoXaml = useUnoXamlParser || XamlRedirection.XamlConfig.IsMono;
            }

            if (bool.TryParse(msbProject.GetProperty("UnoSkipUserControlsInVisualTree")?.EvaluatedValue, out var skipUserControlsInVisualTree))
            {
                _skipUserControlsInVisualTree = skipUserControlsInVisualTree;
            }

            if (bool.TryParse(msbProject.GetProperty("ShouldWriteErrorOnInvalidXaml")?.EvaluatedValue, out var shouldWriteErrorOnInvalidXaml))
            {
                XamlFileGenerator.ShouldWriteErrorOnInvalidXaml = shouldWriteErrorOnInvalidXaml;
            }

            if (!bool.TryParse(msbProject.GetProperty("IsUiAutomationMappingEnabled")?.EvaluatedValue ?? "", out _isUiAutomationMappingEnabled))
            {
                _isUiAutomationMappingEnabled = false;
            }

            if (bool.TryParse(msbProject.GetProperty("ShouldAnnotateGeneratedXaml")?.EvaluatedValue, out var shouldAnnotateGeneratedXaml))
            {
                _shouldAnnotateGeneratedXaml = shouldAnnotateGeneratedXaml;
            }

            _targetPath = Path.Combine(
                Path.GetDirectoryName(msbProject.FullPath),
                msbProject.GetProperty("IntermediateOutputPath").EvaluatedValue
                );

            _defaultLanguage = msbProject.GetProperty("DefaultLanguage")?.EvaluatedValue;

            _analyzerSuppressions = msbProject
                                    .GetItems("XamlGeneratorAnalyzerSuppressions")
                                    .Select(i => i.EvaluatedInclude)
                                    .ToArray();

            _uiAutomationMappings = msbProject
                                    .GetItems("CustomUiAutomationMemberMapping")
                                    .Select(i => new
            {
                Key   = i.EvaluatedInclude,
                Value = i.MetadataNames
                        .Select(i.GetMetadataValue)
                        .FirstOrDefault()
                        ?.Split()
                        .Select(m => m.Trim())
                        .Where(m => m.HasValueTrimmed())
            })
                                    .GroupBy(p => p.Key)
                                    .ToDictionary(p => p.Key, p => p.SelectMany(x => x.Value.Safe()).ToArray());

            _defaultNamespace = msbProject.GetPropertyValue("RootNamespace");

            _isWasm = msbProject.GetProperty("DefineConstants").EvaluatedValue?.Contains("__WASM__") ?? false;
        }
Example #4
0
 internal static bool IsTypeNotPresent(string typeName, RoslynMetadataHelper metadataHelper) => !IsTypePresent(typeName, metadataHelper);
Example #5
0
#pragma warning restore 649 // Unused member

        public XamlCodeGeneration(Compilation sourceCompilation, ProjectInstance msbProject, Project roslynProject)
        {
            InitTelemetry(msbProject);

            _legacyTypes = msbProject
                           .GetItems("LegacyTypes")
                           .Select(i => i.EvaluatedInclude)
                           .ToList()
                           .ToDictionary(fullyQualifiedName => fullyQualifiedName.Split('.').Last());

            _medataHelper        = new RoslynMetadataHelper("Debug", sourceCompilation, msbProject, roslynProject, null, _legacyTypes);
            _assemblySearchPaths = new string[0];
            _projectInstance     = msbProject;

            _configuration = msbProject.GetProperty("Configuration")?.EvaluatedValue
                             ?? throw new InvalidOperationException("The configuration property must be provided");

            _isDebug = string.Equals(_configuration, "Debug", StringComparison.OrdinalIgnoreCase);

            var xamlPages = msbProject.GetItems("Page")
                            .Select(d => d.EvaluatedInclude);

            _xamlSourceFiles = msbProject.GetItems("ApplicationDefinition")
                               .Select(d => d.EvaluatedInclude)
                               .Concat(xamlPages)
                               .ToArray();

            _excludeXamlNamespaces = msbProject
                                     .GetItems("ExcludeXamlNamespaces")
                                     .Select(i => i.EvaluatedInclude)
                                     .ToArray();

            _includeXamlNamespaces = msbProject
                                     .GetItems("IncludeXamlNamespaces")
                                     .Select(i => i.EvaluatedInclude)
                                     .ToArray();

            _analyzerSuppressions = msbProject
                                    .GetItems("XamlGeneratorAnalyzerSuppressions")
                                    .Select(i => i.EvaluatedInclude)
                                    .ToArray();

            _resourceFiles = msbProject
                             .GetItems("PRIResource")
                             .Select(i => i.EvaluatedInclude)
                             .ToArray();

            if (bool.TryParse(msbProject.GetProperty("UseUnoXamlParser")?.EvaluatedValue, out var useUnoXamlParser) && useUnoXamlParser)
            {
                XamlRedirection.XamlConfig.IsUnoXaml = useUnoXamlParser || XamlRedirection.XamlConfig.IsMono;
            }

            if (bool.TryParse(msbProject.GetProperty("ShouldWriteErrorOnInvalidXaml")?.EvaluatedValue, out var shouldWriteErrorOnInvalidXaml))
            {
                XamlFileGenerator.ShouldWriteErrorOnInvalidXaml = shouldWriteErrorOnInvalidXaml;
            }

            if (!bool.TryParse(msbProject.GetProperty("IsUiAutomationMappingEnabled")?.EvaluatedValue ?? "", out _isUiAutomationMappingEnabled))
            {
                _isUiAutomationMappingEnabled = false;
            }

            _targetPath = Path.Combine(
                Path.GetDirectoryName(msbProject.FullPath),
                msbProject.GetProperty("IntermediateOutputPath").EvaluatedValue
                );

            _defaultLanguage = msbProject.GetProperty("DefaultLanguage")?.EvaluatedValue;

            _analyzerSuppressions = msbProject
                                    .GetItems("XamlGeneratorAnalyzerSuppressions")
                                    .Select(i => i.EvaluatedInclude)
                                    .ToArray();

            _uiAutomationMappings = msbProject
                                    .GetItems("CustomUiAutomationMemberMapping")
                                    .Select(i => new
            {
                Key   = i.EvaluatedInclude,
                Value = i.MetadataNames
                        .Select(i.GetMetadataValue)
                        .FirstOrDefault()
                        ?.Split()
                        .Select(m => m.Trim())
                        .Where(m => m.HasValueTrimmed())
            })
                                    .GroupBy(p => p.Key)
                                    .ToDictionary(p => p.Key, p => p.SelectMany(x => x.Value.Safe()).ToArray());

            _defaultNamespace = msbProject.GetPropertyValue("RootNamespace");

            _isWasm = msbProject.GetProperty("DefineConstants").EvaluatedValue?.Contains("__WASM__") ?? false;
        }
Example #6
0
		public XamlCodeGeneration(GeneratorExecutionContext context)
		{
			// To easily debug XAML code generation:
			// Add <UnoUISourceGeneratorDebuggerBreak>True</UnoUISourceGeneratorDebuggerBreak> to your project

			if (!Helpers.DesignTimeHelper.IsDesignTime(context)
				&& (context.GetMSBuildPropertyValue("UnoUISourceGeneratorDebuggerBreak")?.Equals("True", StringComparison.OrdinalIgnoreCase) ?? false))
			{
				Debugger.Launch();
			}

			_generatorContext = context;
			InitTelemetry(context);

			_legacyTypes = context
				.GetMSBuildPropertyValue("LegacyTypesProperty")
				.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				.ToList()
				.ToDictionary(k => k, fullyQualifiedName => fullyQualifiedName.Split('.').Last());

			_metadataHelper = new RoslynMetadataHelper("Debug", context, _legacyTypes);
			_assemblySearchPaths = new string[0];

			_configuration = context.GetMSBuildPropertyValue("Configuration")
				?? throw new InvalidOperationException("The configuration property must be provided");

			_isDebug = string.Equals(_configuration, "Debug", StringComparison.OrdinalIgnoreCase);

			_projectFullPath = context.GetMSBuildPropertyValue("MSBuildProjectFullPath");
			_projectDirectory = Path.GetDirectoryName(_projectFullPath)
				?? throw new InvalidOperationException($"MSBuild property MSBuildProjectFullPath value {_projectFullPath} is not valid");

			var xamlItems = context.GetMSBuildItems("Page")
				.Concat(context.GetMSBuildItems("ApplicationDefinition"));

			_xamlSourceFiles = xamlItems.Select(i => i.Identity).ToArray();

			_xamlSourceLinks = xamlItems.Select(GetSourceLink)
				.ToArray();

			_excludeXamlNamespaces = context.GetMSBuildPropertyValue("ExcludeXamlNamespacesProperty").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

			_includeXamlNamespaces = context.GetMSBuildPropertyValue("IncludeXamlNamespacesProperty").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

			_analyzerSuppressions = context.GetMSBuildPropertyValue("XamlGeneratorAnalyzerSuppressionsProperty").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

			_resourceFiles = context.GetMSBuildItems("PRIResource").Select(i => i.Identity).ToArray();

			if (bool.TryParse(context.GetMSBuildPropertyValue("UseUnoXamlParser"), out var useUnoXamlParser) && useUnoXamlParser)
			{
				XamlRedirection.XamlConfig.IsUnoXaml = useUnoXamlParser || XamlRedirection.XamlConfig.IsMono;
			}

			if (bool.TryParse(context.GetMSBuildPropertyValue("UnoSkipUserControlsInVisualTree"), out var skipUserControlsInVisualTree))
			{
				_skipUserControlsInVisualTree = skipUserControlsInVisualTree;
			}

			if (bool.TryParse(context.GetMSBuildPropertyValue("ShouldWriteErrorOnInvalidXaml"), out var shouldWriteErrorOnInvalidXaml))
			{
				XamlFileGenerator.ShouldWriteErrorOnInvalidXaml = shouldWriteErrorOnInvalidXaml;
			}

			if (!bool.TryParse(context.GetMSBuildPropertyValue("IsUiAutomationMappingEnabled") ?? "", out _isUiAutomationMappingEnabled))
			{
				_isUiAutomationMappingEnabled = false;
			}

			if (bool.TryParse(context.GetMSBuildPropertyValue("ShouldAnnotateGeneratedXaml"), out var shouldAnnotateGeneratedXaml))
			{
				_shouldAnnotateGeneratedXaml = shouldAnnotateGeneratedXaml;
			}

			if (bool.TryParse(context.GetMSBuildPropertyValue("UnoXamlLazyVisualStateManagerEnabled"), out var isLazyVisualStateManagerEnabled))
			{
				_isLazyVisualStateManagerEnabled = isLazyVisualStateManagerEnabled;
			}

			_targetPath = Path.Combine(
				_projectDirectory,
				context.GetMSBuildPropertyValue("IntermediateOutputPath")
			);

			_defaultLanguage = context.GetMSBuildPropertyValue("DefaultLanguage");

			_analyzerSuppressions = context.GetMSBuildItems("XamlGeneratorAnalyzerSuppressions").Select(i => i.Identity).ToArray();

			_uiAutomationMappings = context.GetMSBuildItems("CustomUiAutomationMemberMappingAdjusted")
				.Select(i => new
				{
					Key = i.Identity,
					Value = i.GetMetadataValue("Mappings")
						?.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
						.Select(m => m.Trim())
						.Where(m => m.HasValueTrimmed())
				})
				.GroupBy(p => p.Key)
				.ToDictionary(p => p.Key, p => p.SelectMany(x => x.Value.Safe()).ToArray());

			_defaultNamespace = context.GetMSBuildPropertyValue("RootNamespace");

			_isWasm = context.GetMSBuildPropertyValue("DefineConstantsProperty")?.Contains("__WASM__") ?? false;
			_isDesignTimeBuild = Helpers.DesignTimeHelper.IsDesignTime(context);
		}