コード例 #1
0
        private static TargetFrameworkInformation ToTargetFrameworkInformation(
            IVsTargetFrameworkInfo targetFrameworkInfo)
        {
            var tfi = new TargetFrameworkInformation
            {
                FrameworkName = NuGetFramework.Parse(targetFrameworkInfo.TargetFrameworkMoniker)
            };

            var ptf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, PackageTargetFallback))
                      .Select(NuGetFramework.Parse)
                      .ToList();

            var atf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, AssetTargetFallback))
                      .Select(NuGetFramework.Parse)
                      .ToList();

            // Update TFI with fallback properties
            AssetTargetFallbackUtility.ApplyFramework(tfi, ptf, atf);

            if (targetFrameworkInfo.PackageReferences != null)
            {
                tfi.Dependencies.AddRange(
                    targetFrameworkInfo.PackageReferences
                    .Cast <IVsReferenceItem>()
                    .Select(ToPackageLibraryDependency));
            }

            return(tfi);
        }
コード例 #2
0
        static void AddPackageTargetFallbacks(PackageSpec packageSpec, IDotNetProject project)
        {
            var packageTargetFallback = GetPackageTargetFallbackList(project)
                                        .Select(NuGetFramework.Parse)
                                        .ToList();

            var assetTargetFallback = GetAssetTargetFallbackList(project)
                                      .Select(NuGetFramework.Parse)
                                      .ToList();

            if (!packageTargetFallback.Any() && !assetTargetFallback.Any())
            {
                return;
            }

            AssetTargetFallbackUtility.EnsureValidFallback(
                packageTargetFallback,
                assetTargetFallback,
                packageSpec.FilePath);

            var frameworks = GetProjectFrameworks(project);

            foreach (var framework in frameworks)
            {
                var frameworkInfo = packageSpec.GetTargetFramework(framework);
                AssetTargetFallbackUtility.ApplyFramework(frameworkInfo, packageTargetFallback, assetTargetFallback);
            }
        }
コード例 #3
0
        public void AssetTargetFallbackUtility_VerifyGetInvalidFallbackCombinationMessage()
        {
            var message = AssetTargetFallbackUtility.GetInvalidFallbackCombinationMessage("/tmp/project.csproj");

            message.Code.Should().Be(NuGetLogCode.NU1003);
            message.FilePath.Should().Be("/tmp/project.csproj");
            message.TargetGraphs.Should().BeEmpty("this applies to the entire project");
            message.Level.Should().Be(LogLevel.Error);
            message.Message.Should().Be("PackageTargetFallback is deprecated. Replace PackageTargetFallback references with AssetTargetFallback in the project environment.");
        }
コード例 #4
0
        internal static TargetFrameworkInformation ToTargetFrameworkInformation(
            IVsTargetFrameworkInfo targetFrameworkInfo)
        {
            var tfi = new TargetFrameworkInformation
            {
                FrameworkName = NuGetFramework.Parse(targetFrameworkInfo.TargetFrameworkMoniker)
            };

            var ptf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.PackageTargetFallback))
                      .Select(NuGetFramework.Parse)
                      .ToList();

            var atf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.AssetTargetFallback))
                      .Select(NuGetFramework.Parse)
                      .ToList();

            // Update TFI with fallback properties
            AssetTargetFallbackUtility.ApplyFramework(tfi, ptf, atf);


            tfi.RuntimeIdentifierGraphPath = GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.RuntimeIdentifierGraphPath);

            if (targetFrameworkInfo.PackageReferences != null)
            {
                tfi.Dependencies.AddRange(
                    targetFrameworkInfo.PackageReferences
                    .Cast <IVsReferenceItem>()
                    .Select(ToPackageLibraryDependency));
            }

            if (targetFrameworkInfo is IVsTargetFrameworkInfo2 targetFrameworkInfo2)
            {
                if (targetFrameworkInfo2.PackageDownloads != null)
                {
                    tfi.DownloadDependencies.AddRange(
                        targetFrameworkInfo2.PackageDownloads
                        .Cast <IVsReferenceItem>()
                        .Select(ToPackageDownloadDependency));
                }

                if (targetFrameworkInfo2.FrameworkReferences != null)
                {
                    PopulateFrameworkDependencies(tfi, targetFrameworkInfo2);
                }
            }

            return(tfi);
        }
コード例 #5
0
        public void AssetTargetFallbackUtility_HasInvalidFallbackCombinationVerifyFalseWithATFOnly()
        {
            var tfis = new List <TargetFrameworkInformation>
            {
                new TargetFrameworkInformation()
                {
                    FrameworkName = NuGetFramework.Parse("netcoreapp2.0")
                }
            };

            tfis[0].AssetTargetFallback.Add(NuGetFramework.Parse("net461"));

            var project = new PackageSpec(tfis);

            AssetTargetFallbackUtility.HasInvalidFallbackCombination(project).Should().BeFalse();
        }
コード例 #6
0
        public async Task AssetTargetFallbackUtility_ValidateFallbackFrameworkVerifyTrue()
        {
            var testLogger = new TestLogger();
            var tfis       = new List <TargetFrameworkInformation>
            {
                new TargetFrameworkInformation()
                {
                    FrameworkName = NuGetFramework.Parse("netcoreapp2.0")
                }
            };

            tfis[0].AssetTargetFallback.Add(NuGetFramework.Parse("net461"));

            var project = new PackageSpec(tfis);

            var success = await AssetTargetFallbackUtility.ValidateFallbackFrameworkAsync(project, testLogger);

            success.Should().BeTrue();
            testLogger.Messages.Should().BeEmpty();
        }
コード例 #7
0
        /// <summary>
        /// Emulates a JSON deserialization from project.json to PackageSpec in a post-project.json world
        /// </summary>
        private async Task <PackageSpec> GetPackageSpecAsync(ISettings settings)
        {
            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            var projectReferences = await ProjectServices
                                    .ReferencesReader
                                    .GetProjectReferencesAsync(Common.NullLogger.Instance, CancellationToken.None);

            var targetFramework = await _vsProjectAdapter.GetTargetFrameworkAsync();

            var packageReferences = (await ProjectServices
                                     .ReferencesReader
                                     .GetPackageReferencesAsync(targetFramework, CancellationToken.None))
                                    .ToList();

            var packageTargetFallback = MSBuildStringUtility.Split(_vsProjectAdapter.PackageTargetFallback)
                                        .Select(NuGetFramework.Parse)
                                        .ToList();

            var assetTargetFallback = MSBuildStringUtility.Split(_vsProjectAdapter.AssetTargetFallback)
                                      .Select(NuGetFramework.Parse)
                                      .ToList();

            var projectTfi = new TargetFrameworkInformation
            {
                FrameworkName = targetFramework,
                Dependencies  = packageReferences,
            };

            // Apply fallback settings
            AssetTargetFallbackUtility.ApplyFramework(projectTfi, packageTargetFallback, assetTargetFallback);

            // Build up runtime information.
            var runtimes = await _vsProjectAdapter.GetRuntimeIdentifiersAsync();

            var supports = await _vsProjectAdapter.GetRuntimeSupportsAsync();

            var runtimeGraph = new RuntimeGraph(runtimes, supports);

            // In legacy CSProj, we only have one target framework per project
            var tfis = new TargetFrameworkInformation[] { projectTfi };

            var projectName = _projectName ?? _projectUniqueName;

            return(new PackageSpec(tfis)
            {
                Name = projectName,
                Version = new NuGetVersion(_vsProjectAdapter.Version),
                Authors = new string[] { },
                Owners = new string[] { },
                Tags = new string[] { },
                ContentFiles = new string[] { },
                FilePath = _projectFullPath,
                RuntimeGraph = runtimeGraph,
                RestoreMetadata = new ProjectRestoreMetadata
                {
                    ProjectStyle = ProjectStyle.PackageReference,
                    OutputPath = GetMSBuildProjectExtensionsPath(),
                    ProjectPath = _projectFullPath,
                    ProjectName = projectName,
                    ProjectUniqueName = _projectFullPath,
                    OriginalTargetFrameworks = tfis
                                               .Select(tfi => tfi.FrameworkName.GetShortFolderName())
                                               .ToList(),
                    TargetFrameworks = new List <ProjectRestoreMetadataFrameworkInfo>
                    {
                        new ProjectRestoreMetadataFrameworkInfo(tfis[0].FrameworkName)
                        {
                            ProjectReferences = projectReferences?.ToList()
                        }
                    },
                    SkipContentFileWrite = true,
                    CacheFilePath = await GetCacheFilePathAsync(),
                    PackagesPath = GetPackagesPath(settings),
                    Sources = GetSources(settings),
                    FallbackFolders = GetFallbackFolders(settings),
                    ConfigFilePaths = GetConfigFilePaths(settings),
                    ProjectWideWarningProperties = WarningProperties.GetWarningProperties(
                        treatWarningsAsErrors: _vsProjectAdapter.TreatWarningsAsErrors,
                        noWarn: _vsProjectAdapter.NoWarn,
                        warningsAsErrors: _vsProjectAdapter.WarningsAsErrors)
                }
            });
        }
コード例 #8
0
        internal static TargetFrameworkInformation ToTargetFrameworkInformation(
            IVsTargetFrameworkInfo targetFrameworkInfo, bool cpvmEnabled, string projectFullPath)
        {
            var tfi = new TargetFrameworkInformation
            {
                FrameworkName = GetTargetFramework(targetFrameworkInfo.Properties, projectFullPath),
                TargetAlias   = GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.TargetFramework)
            };

            var ptf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.PackageTargetFallback))
                      .Select(NuGetFramework.Parse)
                      .ToList();

            var atf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.AssetTargetFallback))
                      .Select(NuGetFramework.Parse)
                      .ToList();

            // Update TFI with fallback properties
            AssetTargetFallbackUtility.ApplyFramework(tfi, ptf, atf);


            tfi.RuntimeIdentifierGraphPath = GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.RuntimeIdentifierGraphPath);

            if (targetFrameworkInfo.PackageReferences != null)
            {
                tfi.Dependencies.AddRange(
                    targetFrameworkInfo.PackageReferences
                    .Cast <IVsReferenceItem>()
                    .Select(pr => ToPackageLibraryDependency(pr, cpvmEnabled)));
            }

            if (targetFrameworkInfo is IVsTargetFrameworkInfo2 targetFrameworkInfo2)
            {
                if (targetFrameworkInfo2.PackageDownloads != null)
                {
                    tfi.DownloadDependencies.AddRange(
                        targetFrameworkInfo2.PackageDownloads
                        .Cast <IVsReferenceItem>()
                        .Select(ToPackageDownloadDependency));
                }

                if (cpvmEnabled && targetFrameworkInfo is IVsTargetFrameworkInfo3 targetFrameworkInfo3)
                {
                    if (targetFrameworkInfo3.CentralPackageVersions != null)
                    {
                        tfi.CentralPackageVersions.AddRange(
                            targetFrameworkInfo3.CentralPackageVersions
                            .Cast <IVsReferenceItem>()
                            .Select(ToCentralPackageVersion)
                            .Distinct(CentralPackageVersionNameComparer.Default)
                            .ToDictionary(cpv => cpv.Name));
                    }
                }

                if (targetFrameworkInfo2.FrameworkReferences != null)
                {
                    PopulateFrameworkDependencies(tfi, targetFrameworkInfo2);
                }
            }

            return(tfi);
        }
コード例 #9
0
        /// <summary>
        /// Emulates a JSON deserialization from project.json to PackageSpec in a post-project.json world
        /// </summary>
        private async Task <PackageSpec> GetPackageSpecAsync(ISettings settings)
        {
            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            var projectReferences = await ProjectServices
                                    .ReferencesReader
                                    .GetProjectReferencesAsync(Common.NullLogger.Instance, CancellationToken.None);

            var targetFramework = await _vsProjectAdapter.GetTargetFrameworkAsync();

            var packageReferences = (await ProjectServices
                                     .ReferencesReader
                                     .GetPackageReferencesAsync(targetFramework, CancellationToken.None))
                                    .ToList();

            var packageTargetFallback = MSBuildStringUtility.Split(_vsProjectAdapter.PackageTargetFallback)
                                        .Select(NuGetFramework.Parse)
                                        .ToList();

            var assetTargetFallback = MSBuildStringUtility.Split(_vsProjectAdapter.AssetTargetFallback)
                                      .Select(NuGetFramework.Parse)
                                      .ToList();

            var projectTfi = new TargetFrameworkInformation
            {
                FrameworkName = targetFramework,
                Dependencies  = packageReferences,
            };

            bool isCpvmEnabled = await IsCentralPackageManagementVersionsEnabledAsync();

            if (isCpvmEnabled)
            {
                // Add the central version information and merge the information to the package reference dependencies
                projectTfi.CentralPackageVersions.AddRange(await GetCentralPackageVersionsAsync());
                LibraryDependency.ApplyCentralVersionInformation(projectTfi.Dependencies, projectTfi.CentralPackageVersions);
            }

            // Apply fallback settings
            AssetTargetFallbackUtility.ApplyFramework(projectTfi, packageTargetFallback, assetTargetFallback);

            // Build up runtime information.
            var runtimes = await _vsProjectAdapter.GetRuntimeIdentifiersAsync();

            var supports = await _vsProjectAdapter.GetRuntimeSupportsAsync();

            var runtimeGraph = new RuntimeGraph(runtimes, supports);

            // In legacy CSProj, we only have one target framework per project
            var tfis = new TargetFrameworkInformation[] { projectTfi };

            var projectName = _projectName ?? _projectUniqueName;

            string specifiedPackageId = await GetSpecifiedPackageIdAsync();

            if (!string.IsNullOrWhiteSpace(specifiedPackageId))
            {
                projectName = specifiedPackageId;
            }
            else
            {
                string specifiedAssemblyName = await GetSpecifiedAssemblyNameAsync();

                if (!string.IsNullOrWhiteSpace(specifiedAssemblyName))
                {
                    projectName = specifiedAssemblyName;
                }
            }

            return(new PackageSpec(tfis)
            {
                Name = projectName,
                Version = new NuGetVersion(_vsProjectAdapter.Version),
                FilePath = _projectFullPath,
                RuntimeGraph = runtimeGraph,
                RestoreMetadata = new ProjectRestoreMetadata
                {
                    ProjectStyle = ProjectStyle.PackageReference,
                    OutputPath = await GetMSBuildProjectExtensionsPathAsync(),
                    ProjectPath = _projectFullPath,
                    ProjectName = projectName,
                    ProjectUniqueName = _projectFullPath,
                    OriginalTargetFrameworks = tfis
                                               .Select(tfi => tfi.FrameworkName.GetShortFolderName())
                                               .ToList(),
                    TargetFrameworks = new List <ProjectRestoreMetadataFrameworkInfo>
                    {
                        new ProjectRestoreMetadataFrameworkInfo(tfis[0].FrameworkName)
                        {
                            ProjectReferences = projectReferences?.ToList()
                        }
                    },
                    SkipContentFileWrite = true,
                    CacheFilePath = await GetCacheFilePathAsync(),
                    PackagesPath = GetPackagesPath(settings),
                    Sources = GetSources(settings),
                    FallbackFolders = GetFallbackFolders(settings),
                    ConfigFilePaths = GetConfigFilePaths(settings),
                    ProjectWideWarningProperties = WarningProperties.GetWarningProperties(
                        treatWarningsAsErrors: _vsProjectAdapter.TreatWarningsAsErrors,
                        noWarn: _vsProjectAdapter.NoWarn,
                        warningsAsErrors: _vsProjectAdapter.WarningsAsErrors),
                    RestoreLockProperties = new RestoreLockProperties(
                        await _vsProjectAdapter.GetRestorePackagesWithLockFileAsync(),
                        await _vsProjectAdapter.GetNuGetLockFilePathAsync(),
                        await _vsProjectAdapter.IsRestoreLockedAsync()),
                    CentralPackageVersionsEnabled = isCpvmEnabled
                }
            });
        }