public bool TryParse(string input, out IVsNuGetFramework nuGetFramework)
        {
            const string eventName = nameof(IVsFrameworkParser2) + "." + nameof(TryParse);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName,
                                                                           new
            {
                Framework = input
            });

            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            try
            {
                NuGetFramework framework = NuGetFramework.Parse(input);

                string targetFrameworkMoniker  = framework.DotNetFrameworkName;
                string targetPlatformMoniker   = framework.DotNetPlatformName;
                string targetPlatforMinVersion = null;

                nuGetFramework = new VsNuGetFramework(targetFrameworkMoniker, targetPlatformMoniker, targetPlatforMinVersion);
                return(framework.IsSpecificFramework);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsFrameworkParser).FullName);
                throw;
            }
        }
        public IVsNuGetFramework GetNearest(IVsNuGetFramework targetFramework, IEnumerable <IVsNuGetFramework> fallbackTargetFrameworks, IEnumerable <IVsNuGetFramework> frameworks)
        {
            const string eventName = nameof(IVsFrameworkCompatibility3) + "." + nameof(GetNearest) + ".3";

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName);

            return(GetNearestImpl(targetFramework, fallbackTargetFrameworks, frameworks));
        }
        public void GetNearest_InvalidFramework_ThrowsArgumentException()
        {
            // Arrange
            var target = new VsFrameworkCompatibility(_telemetryProvider.Object);

            var invalidFramework = new VsNuGetFramework("Invalid,Version=vWrong", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var netstandard2_0   = new VsNuGetFramework(".NETStandard,Version=v2.0", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var frameworks       = new IVsNuGetFramework[] { netstandard2_0 };

            // Act & Assert
            Assert.Throws <ArgumentException>(() => target.GetNearest(invalidFramework, frameworks));
        }
        public void GetNearest_NullItemInEnumerable_ThrowsArgumentException()
        {
            // Arrange
            var target = new VsFrameworkCompatibility(_telemetryProvider.Object);

            var net5           = new VsNuGetFramework(".NETCoreApp,Version=v5.0", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var netstandard2_0 = new VsNuGetFramework(".NETStandard,Version=v2.0", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var goodFrameworks = new IVsNuGetFramework[] { netstandard2_0 };
            var nullFrameworks = new IVsNuGetFramework[] { null };

            // Act & Asset
            Assert.Throws <ArgumentException>(() => target.GetNearest(targetFramework: net5, frameworks: nullFrameworks));
            Assert.Throws <ArgumentException>(() => target.GetNearest(targetFramework: net5, fallbackTargetFrameworks: goodFrameworks, frameworks: nullFrameworks));
            Assert.Throws <ArgumentException>(() => target.GetNearest(targetFramework: net5, fallbackTargetFrameworks: nullFrameworks, frameworks: goodFrameworks));
        }
        public void GetNearest_NoCompatibleFramework_ReturnsNull()
        {
            // Arrange
            var target = new VsFrameworkCompatibility(_telemetryProvider.Object);

            var net5       = new VsNuGetFramework(".NETCoreApp,Version=v5.0", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var net472     = new VsNuGetFramework(".NETFramework,Version=v4.7.2", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var frameworks = new IVsNuGetFramework[] { net472 };

            // Act
            var actual = target.GetNearest(net5, frameworks);

            // Assert
            Assert.Null(actual);
        }
        public void GetNearest_WithCompatibleFramework_Succeeds()
        {
            // Arrange
            var target       = new VsFrameworkCompatibility(_telemetryProvider.Object);
            var net5         = new VsNuGetFramework(".NETCoreApp,Version=v5.0", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var net472       = new VsNuGetFramework(".NETFramework,Version=v4.7.2", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var netcoreapp31 = new VsNuGetFramework(".NETCoreApp,Version=v3.1", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var frameworks   = new IVsNuGetFramework[] { net472, netcoreapp31 };

            // Act
            var actual = target.GetNearest(net5, frameworks);

            // Assert
            Assert.Equal(netcoreapp31, actual);
        }
Esempio n. 7
0
        public void IVsFrameworkCompatibility3_GetNearest_WithFallbackTfm_Succeeds()
        {
            // Arrange
            var target = new VsFrameworkCompatibility();

            var net5               = new VsNuGetFramework(".NETCoreApp,Version=v5.0", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var net472             = new VsNuGetFramework(".NETFramework,Version=v4.7.2", targetPlatformMoniker: null, targetPlatformMinVersion: null);
            var fallbackFrameworks = new IVsNuGetFramework[] { net472 };
            var frameworks         = new IVsNuGetFramework[] { net472 };

            // Act
            var actual = target.GetNearest(net5, fallbackFrameworks, frameworks);

            // Assert
            Assert.Equal(net472, actual);
        }
Esempio n. 8
0
        public bool TryParse(string input, out IVsNuGetFramework nuGetFramework)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            NuGetFramework framework = NuGetFramework.Parse(input);

            string targetFrameworkMoniker  = framework.DotNetFrameworkName;
            string targetPlatformMoniker   = framework.DotNetPlatformName;
            string targetPlatforMinVersion = null;

            nuGetFramework = new VsNuGetFramework(targetFrameworkMoniker, targetPlatformMoniker, targetPlatforMinVersion);
            return(framework.IsSpecificFramework);
        }
Esempio n. 9
0
        public bool TryParse(string input, out IVsNuGetFramework nuGetFramework)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            try
            {
                NuGetFramework framework = NuGetFramework.Parse(input);

                string targetFrameworkMoniker  = framework.DotNetFrameworkName;
                string targetPlatformMoniker   = framework.DotNetPlatformName;
                string targetPlatforMinVersion = null;

                nuGetFramework = new VsNuGetFramework(targetFrameworkMoniker, targetPlatformMoniker, targetPlatforMinVersion);
                return(framework.IsSpecificFramework);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsFrameworkParser).FullName);
                throw;
            }
        }
        public IVsNuGetFramework GetNearestImpl(IVsNuGetFramework targetFramework, IEnumerable <IVsNuGetFramework> fallbackTargetFrameworks, IEnumerable <IVsNuGetFramework> frameworks)
        {
            if (targetFramework == null)
            {
                throw new ArgumentNullException(nameof(targetFramework));
            }

            if (fallbackTargetFrameworks == null)
            {
                throw new ArgumentNullException(nameof(fallbackTargetFrameworks));
            }

            if (frameworks == null)
            {
                throw new ArgumentNullException(nameof(frameworks));
            }

            var inputFrameworks = new Dictionary <NuGetFramework, IVsNuGetFramework>();

            NuGetFramework ToNuGetFramework(IVsNuGetFramework framework, string paramName)
            {
                NuGetFramework nugetFramework = MSBuildProjectFrameworkUtility.GetProjectFramework(
                    projectFilePath: null,
                    targetFrameworkMoniker: framework.TargetFrameworkMoniker,
                    targetPlatformMoniker: framework.TargetPlatformMoniker,
                    targetPlatformMinVersion: framework.TargetPlatformMinVersion);

                if (!nugetFramework.IsSpecificFramework)
                {
                    throw new ArgumentException($"Framework '{framework}' could not be parsed", paramName);
                }
                inputFrameworks[nugetFramework] = framework;
                return(nugetFramework);
            }

            List <NuGetFramework> ToNuGetFrameworks(IEnumerable <IVsNuGetFramework> enumerable, string paramName)
            {
                var list = new List <NuGetFramework>();

                foreach (var framework in enumerable)
                {
                    if (framework == null)
                    {
                        throw new ArgumentException("Enumeration contains a null value", paramName);
                    }
                    NuGetFramework nugetFramework = ToNuGetFramework(framework, paramName);
                    list.Add(nugetFramework);
                }
                return(list);
            }

            NuGetFramework        targetNuGetFramework          = ToNuGetFramework(targetFramework, nameof(targetFramework));
            List <NuGetFramework> nugetFallbackTargetFrameworks = ToNuGetFrameworks(fallbackTargetFrameworks, nameof(fallbackTargetFrameworks));
            List <NuGetFramework> nugetFrameworks = ToNuGetFrameworks(frameworks, nameof(frameworks));

            try
            {
                if (nugetFallbackTargetFrameworks.Count > 0)
                {
                    targetNuGetFramework = new FallbackFramework(targetNuGetFramework, nugetFallbackTargetFrameworks);
                }

                var reducer = new FrameworkReducer();
                var nearest = reducer.GetNearest(targetNuGetFramework, nugetFrameworks);

                if (nearest == null)
                {
                    return(null);
                }

                var originalFrameworkString = inputFrameworks[nearest];
                return(originalFrameworkString);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsFrameworkCompatibility).FullName);
                throw;
            }
        }
 public IVsNuGetFramework GetNearest(IVsNuGetFramework targetFramework, IEnumerable <IVsNuGetFramework> frameworks)
 {
     return(GetNearest(targetFramework, Enumerable.Empty <IVsNuGetFramework>(), frameworks));
 }