Beispiel #1
0
            public static ProjectData Create(string projectFilePath, MSB.Execution.ProjectInstance projectInstance, MSB.Evaluation.Project project)
            {
                var projectFolderPath = Path.GetDirectoryName(projectFilePath);

                var guid                   = PropertyConverter.ToGuid(projectInstance.GetPropertyValue(PropertyNames.ProjectGuid));
                var name                   = projectInstance.GetPropertyValue(PropertyNames.ProjectName);
                var assemblyName           = projectInstance.GetPropertyValue(PropertyNames.AssemblyName);
                var targetPath             = projectInstance.GetPropertyValue(PropertyNames.TargetPath);
                var outputPath             = projectInstance.GetPropertyValue(PropertyNames.OutputPath);
                var intermediateOutputPath = projectInstance.GetPropertyValue(PropertyNames.IntermediateOutputPath);
                var projectAssetsFile      = projectInstance.GetPropertyValue(PropertyNames.ProjectAssetsFile);
                var configuration          = projectInstance.GetPropertyValue(PropertyNames.Configuration);
                var platform               = projectInstance.GetPropertyValue(PropertyNames.Platform);
                var defaultNamespace       = projectInstance.GetPropertyValue(PropertyNames.RootNamespace);

                var targetFramework = new FrameworkName(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));

                var targetFrameworkValue = projectInstance.GetPropertyValue(PropertyNames.TargetFramework);
                var targetFrameworks     = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworks), ';');

                if (!string.IsNullOrWhiteSpace(targetFrameworkValue) && targetFrameworks.Length == 0)
                {
                    targetFrameworks = ImmutableArray.Create(targetFrameworkValue);
                }

                var languageVersion           = PropertyConverter.ToLanguageVersion(projectInstance.GetPropertyValue(PropertyNames.LangVersion));
                var allowUnsafeCode           = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.AllowUnsafeBlocks), defaultValue: false);
                var checkForOverflowUnderflow = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.CheckForOverflowUnderflow), defaultValue: false);
                var outputKind                     = PropertyConverter.ToOutputKind(projectInstance.GetPropertyValue(PropertyNames.OutputType));
                var nullableContextOptions         = PropertyConverter.ToNullableContextOptions(projectInstance.GetPropertyValue(PropertyNames.Nullable));
                var documentationFile              = projectInstance.GetPropertyValue(PropertyNames.DocumentationFile);
                var preprocessorSymbolNames        = PropertyConverter.ToPreprocessorSymbolNames(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
                var suppressedDiagnosticIds        = PropertyConverter.ToSuppressedDiagnosticIds(projectInstance.GetPropertyValue(PropertyNames.NoWarn));
                var warningsAsErrors               = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsAsErrors), ',');
                var warningsNotAsErrors            = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsNotAsErrors), ',');
                var signAssembly                   = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
                var treatWarningsAsErrors          = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
                var runAnalyzers                   = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzers), defaultValue: true);
                var runAnalyzersDuringLiveAnalysis = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzersDuringLiveAnalysis), defaultValue: true);
                var assemblyOriginatorKeyFile      = projectInstance.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);

                var ruleset = ResolveRulesetIfAny(projectInstance);

                var sourceFiles = GetFullPaths(
                    projectInstance.GetItems(ItemNames.Compile), filter: FileNameIsNotGenerated);

                var projectReferences       = ImmutableArray.CreateBuilder <string>();
                var projectReferenceAliases = ImmutableDictionary.CreateBuilder <string, string>();

                var references       = ImmutableArray.CreateBuilder <string>();
                var referenceAliases = ImmutableDictionary.CreateBuilder <string, string>();

                foreach (var referencePathItem in projectInstance.GetItems(ItemNames.ReferencePath))
                {
                    var referenceSourceTarget = referencePathItem.GetMetadataValue(MetadataNames.ReferenceSourceTarget);
                    var aliases = referencePathItem.GetMetadataValue(MetadataNames.Aliases);

                    // If this reference came from a project reference, count it as such. We never want to directly look
                    // at the ProjectReference items in the project, as those don't always create project references
                    // if things like OutputItemType or ReferenceOutputAssembly are set. It's also possible that other
                    // MSBuild logic is adding or removing properties too.
                    if (StringComparer.OrdinalIgnoreCase.Equals(referenceSourceTarget, ItemNames.ProjectReference))
                    {
                        var projectReferenceOriginalItemSpec = referencePathItem.GetMetadataValue(MetadataNames.ProjectReferenceOriginalItemSpec);
                        if (projectReferenceOriginalItemSpec.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
                        {
                            var projectReferenceFilePath = Path.GetFullPath(Path.Combine(projectFolderPath, projectReferenceOriginalItemSpec));

                            projectReferences.Add(projectReferenceFilePath);

                            if (!string.IsNullOrEmpty(aliases))
                            {
                                projectReferenceAliases[projectReferenceFilePath] = aliases;
                            }

                            continue;
                        }
                    }

                    var fullPath = referencePathItem.GetMetadataValue(MetadataNames.FullPath);
                    if (!string.IsNullOrEmpty(fullPath))
                    {
                        references.Add(fullPath);

                        if (!string.IsNullOrEmpty(aliases))
                        {
                            referenceAliases[fullPath] = aliases;
                        }
                    }
                }

                var packageReferences = GetPackageReferences(projectInstance.GetItems(ItemNames.PackageReference));
                var analyzers         = GetFullPaths(projectInstance.GetItems(ItemNames.Analyzer));
                var additionalFiles   = GetFullPaths(projectInstance.GetItems(ItemNames.AdditionalFiles));
                var editorConfigFiles = GetFullPaths(projectInstance.GetItems(ItemNames.EditorConfigFiles));
                var includeGlobs      = project.GetAllGlobs().Select(x => x.MsBuildGlob).ToImmutableArray();

                return(new ProjectData(
                           guid,
                           name,
                           assemblyName,
                           targetPath,
                           outputPath,
                           intermediateOutputPath,
                           projectAssetsFile,
                           configuration,
                           platform,
                           targetFramework,
                           targetFrameworks,
                           outputKind,
                           languageVersion,
                           nullableContextOptions,
                           allowUnsafeCode,
                           checkForOverflowUnderflow,
                           documentationFile,
                           preprocessorSymbolNames,
                           suppressedDiagnosticIds,
                           warningsAsErrors,
                           warningsNotAsErrors,
                           signAssembly,
                           assemblyOriginatorKeyFile,
                           sourceFiles,
                           projectReferences.ToImmutable(),
                           references.ToImmutable(),
                           packageReferences,
                           analyzers,
                           additionalFiles,
                           editorConfigFiles,
                           treatWarningsAsErrors,
                           defaultNamespace,
                           runAnalyzers,
                           runAnalyzersDuringLiveAnalysis,
                           ruleset,
                           referenceAliases.ToImmutableDictionary(),
                           projectReferenceAliases.ToImmutable(),
                           includeGlobs));
            }
            public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance, MSB.Evaluation.Project project)
            {
                var guid                   = PropertyConverter.ToGuid(projectInstance.GetPropertyValue(PropertyNames.ProjectGuid));
                var name                   = projectInstance.GetPropertyValue(PropertyNames.ProjectName);
                var assemblyName           = projectInstance.GetPropertyValue(PropertyNames.AssemblyName);
                var targetPath             = projectInstance.GetPropertyValue(PropertyNames.TargetPath);
                var outputPath             = projectInstance.GetPropertyValue(PropertyNames.OutputPath);
                var intermediateOutputPath = projectInstance.GetPropertyValue(PropertyNames.IntermediateOutputPath);
                var projectAssetsFile      = projectInstance.GetPropertyValue(PropertyNames.ProjectAssetsFile);
                var configuration          = projectInstance.GetPropertyValue(PropertyNames.Configuration);
                var platform               = projectInstance.GetPropertyValue(PropertyNames.Platform);
                var defaultNamespace       = projectInstance.GetPropertyValue(PropertyNames.RootNamespace);

                var targetFramework = new FrameworkName(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));

                var targetFrameworkValue = projectInstance.GetPropertyValue(PropertyNames.TargetFramework);
                var targetFrameworks     = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworks), ';');

                if (!string.IsNullOrWhiteSpace(targetFrameworkValue) && targetFrameworks.Length == 0)
                {
                    targetFrameworks = ImmutableArray.Create(targetFrameworkValue);
                }

                var languageVersion           = PropertyConverter.ToLanguageVersion(projectInstance.GetPropertyValue(PropertyNames.LangVersion));
                var allowUnsafeCode           = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.AllowUnsafeBlocks), defaultValue: false);
                var checkForOverflowUnderflow = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.CheckForOverflowUnderflow), defaultValue: false);
                var outputKind                     = PropertyConverter.ToOutputKind(projectInstance.GetPropertyValue(PropertyNames.OutputType));
                var nullableContextOptions         = PropertyConverter.ToNullableContextOptions(projectInstance.GetPropertyValue(PropertyNames.Nullable));
                var documentationFile              = projectInstance.GetPropertyValue(PropertyNames.DocumentationFile);
                var preprocessorSymbolNames        = PropertyConverter.ToPreprocessorSymbolNames(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
                var suppressedDiagnosticIds        = PropertyConverter.ToSuppressedDiagnosticIds(projectInstance.GetPropertyValue(PropertyNames.NoWarn));
                var warningsAsErrors               = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsAsErrors), ',');
                var warningsNotAsErrors            = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsNotAsErrors), ',');
                var signAssembly                   = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
                var treatWarningsAsErrors          = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
                var runAnalyzers                   = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzers), defaultValue: true);
                var runAnalyzersDuringLiveAnalysis = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzersDuringLiveAnalysis), defaultValue: true);
                var assemblyOriginatorKeyFile      = projectInstance.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);

                var ruleset = ResolveRulesetIfAny(projectInstance);

                var sourceFiles = GetFullPaths(
                    projectInstance.GetItems(ItemNames.Compile), filter: FileNameIsNotGenerated);

                var projectReferences       = ImmutableArray.CreateBuilder <string>();
                var projectReferenceAliases = ImmutableDictionary.CreateBuilder <string, string>();
                var projectReferencesAdded  = new HashSet <string>();

                foreach (var projectReferenceItem in projectInstance.GetItems(ItemNames.ProjectReference))
                {
                    var fullPath = projectReferenceItem.GetMetadataValue(MetadataNames.FullPath);

                    if (IsCSharpProject(fullPath) && projectReferencesAdded.Add(fullPath))
                    {
                        projectReferences.Add(fullPath);

                        var aliases = projectReferenceItem.GetMetadataValue(MetadataNames.Aliases);
                        if (!string.IsNullOrEmpty(aliases))
                        {
                            projectReferenceAliases[fullPath] = aliases;
                        }
                    }
                }

                var references       = ImmutableArray.CreateBuilder <string>();
                var referenceAliases = ImmutableDictionary.CreateBuilder <string, string>();

                foreach (var referencePathItem in projectInstance.GetItems(ItemNames.ReferencePath))
                {
                    var referenceSourceTarget = referencePathItem.GetMetadataValue(MetadataNames.ReferenceSourceTarget);

                    if (StringComparer.OrdinalIgnoreCase.Equals(referenceSourceTarget, ItemNames.ProjectReference))
                    {
                        // If the reference was sourced from a project reference, we have two choices:
                        //
                        //   1. If the reference is a C# project reference, we shouldn't add it because it'll just duplicate
                        //      the project reference.
                        //   2. If the reference is *not* a C# project reference, we should keep this reference because the
                        //      project reference was already removed.

                        var originalItemSpec = referencePathItem.GetMetadataValue(MetadataNames.OriginalItemSpec);
                        if (originalItemSpec.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    var fullPath = referencePathItem.GetMetadataValue(MetadataNames.FullPath);
                    if (!string.IsNullOrEmpty(fullPath))
                    {
                        references.Add(fullPath);

                        var aliases = referencePathItem.GetMetadataValue(MetadataNames.Aliases);
                        if (!string.IsNullOrEmpty(aliases))
                        {
                            referenceAliases[fullPath] = aliases;
                        }
                    }
                }

                var packageReferences = GetPackageReferences(projectInstance.GetItems(ItemNames.PackageReference));
                var analyzers         = GetFullPaths(projectInstance.GetItems(ItemNames.Analyzer));
                var additionalFiles   = GetFullPaths(projectInstance.GetItems(ItemNames.AdditionalFiles));
                var editorConfigFiles = GetFullPaths(projectInstance.GetItems(ItemNames.EditorConfigFiles));
                var includeGlobs      = project.GetAllGlobs().Select(x => x.MsBuildGlob).ToImmutableArray();

                return(new ProjectData(
                           guid,
                           name,
                           assemblyName,
                           targetPath,
                           outputPath,
                           intermediateOutputPath,
                           projectAssetsFile,
                           configuration,
                           platform,
                           targetFramework,
                           targetFrameworks,
                           outputKind,
                           languageVersion,
                           nullableContextOptions,
                           allowUnsafeCode,
                           checkForOverflowUnderflow,
                           documentationFile,
                           preprocessorSymbolNames,
                           suppressedDiagnosticIds,
                           warningsAsErrors,
                           warningsNotAsErrors,
                           signAssembly,
                           assemblyOriginatorKeyFile,
                           sourceFiles,
                           projectReferences.ToImmutable(),
                           references.ToImmutable(),
                           packageReferences,
                           analyzers,
                           additionalFiles,
                           editorConfigFiles,
                           treatWarningsAsErrors,
                           defaultNamespace,
                           runAnalyzers,
                           runAnalyzersDuringLiveAnalysis,
                           ruleset,
                           referenceAliases.ToImmutableDictionary(),
                           projectReferenceAliases.ToImmutable(),
                           includeGlobs));
            }