Пример #1
0
        public RoslynMetadataHelper(string configuration, GeneratorExecutionContext context, Dictionary <string, string> legacyTypes = null)
        {
            Compilation         = context.Compilation;
            _additionalTypesMap = GenerateAdditionalTypesMap();

            _findTypesByName           = Funcs.Create <string, ITypeSymbol[]>(SourceFindTypesByName).AsLockedMemoized();
            _findTypeByFullName        = Funcs.Create <string, ITypeSymbol>(SourceFindTypeByFullName).AsLockedMemoized();
            _legacyTypes               = BuildLegacyTypes(legacyTypes);
            _getAllDerivingTypes       = Funcs.Create <INamedTypeSymbol, INamedTypeSymbol[]>(GetAllDerivingTypes).AsLockedMemoized();
            _getAllTypesAttributedWith = Funcs.Create <INamedTypeSymbol, INamedTypeSymbol[]>(SourceGetAllTypesAttributedWith).AsLockedMemoized();
            _nullableSymbol            = Compilation.GetTypeByMetadataName("System.Nullable`1");

            _namedSymbolsLookup = Compilation.GetSymbolNameLookup();
        }
        Environment.GetEnvironmentVariable("APPVEYOR").HasValue();                    // https://www.appveyor.com/docs/environment-variables/

        private void InitTelemetry(GeneratorExecutionContext context)
        {
            var telemetryOptOut = context.GetMSBuildPropertyValue("UnoPlatformTelemetryOptOut");

            bool?isTelemetryOptout()
            => telemetryOptOut.Equals("true", StringComparison.OrdinalIgnoreCase) ||
            telemetryOptOut.Equals("1", StringComparison.OrdinalIgnoreCase);

            _telemetry = new Telemetry.Telemetry(isTelemetryOptout);

            if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information))
            {
                this.Log().InfoFormat($"Telemetry enabled: {_telemetry.Enabled}");
            }
        }
        Environment.GetEnvironmentVariable("APPVEYOR").HasValue();                    // https://www.appveyor.com/docs/environment-variables/

        private void InitTelemetry(GeneratorExecutionContext context)
        {
            var telemetryOptOut = context.GetMSBuildPropertyValue("UnoPlatformTelemetryOptOut");

            bool?isTelemetryOptout()
            => telemetryOptOut.Equals("true", StringComparison.OrdinalIgnoreCase) ||
            telemetryOptOut.Equals("1", StringComparison.OrdinalIgnoreCase) ||
            _isDesignTimeBuild;

            _telemetry = new Telemetry.Telemetry(isTelemetryOptout);

#if DEBUG
            Console.WriteLine($"Telemetry enabled: {_telemetry.Enabled}");
#endif
        }
Пример #4
0
        public RoslynMetadataHelper(string configuration, GeneratorExecutionContext context, Dictionary <string, string> legacyTypes = null)
        {
            Compilation         = context.Compilation;
            _additionalTypesMap = GenerateAdditionalTypesMap();

            _findTypesByName           = Funcs.Create <string, ITypeSymbol[]>(SourceFindTypesByName).AsLockedMemoized();
            _findTypeByFullName        = Funcs.Create <string, ITypeSymbol>(SourceFindTypeByFullName).AsLockedMemoized();
            _additionalTypes           = new string[0];
            _legacyTypes               = BuildLegacyTypes(legacyTypes);
            _getAllDerivingTypes       = Funcs.Create <INamedTypeSymbol, INamedTypeSymbol[]>(GetAllDerivingTypes).AsLockedMemoized();
            _getAllTypesAttributedWith = Funcs.Create <INamedTypeSymbol, INamedTypeSymbol[]>(SourceGetAllTypesAttributedWith).AsLockedMemoized();
            _nullableSymbol            = Compilation.GetTypeByMetadataName("System.Nullable`1");

            var refs = from metadataReference in Compilation.References
                       let assembly = Compilation.GetAssemblyOrModuleSymbol(metadataReference) as IAssemblySymbol
                                      where assembly != null
                                      from type in assembly.GlobalNamespace.GetNamespaceTypes()
                                      group type by type.Name into names
                                      select new { names.Key, names };

            _namedSymbolsLookup = refs.ToDictionary(k => k.Key, p => p.names.AsEnumerable());
        }
Пример #5
0
#pragma warning restore 649 // Unused member

        public XamlCodeGeneration(GeneratorExecutionContext context)
        {
            // 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();
            _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);

            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;
            }

            _targetPath = Path.Combine(
                Path.GetDirectoryName(context.GetMSBuildPropertyValue("MSBuildProjectFullPath")),
                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;
        }
Пример #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);
		}