/// <summary>
        /// Creates a NuGetFramework from individual components, using the given mappings.
        /// This method may have individual component preference, as described in the remarks.
        /// </summary>
        /// <remarks>
        /// Profiles and TargetPlatforms can't mix. As such the precedence order is profile over target platforms (TPI, TPV).
        /// .NETCoreApp,Version=v5.0 and later do not support profiles.
        /// Target Platforms are ignored for any frameworks not supporting them.
        /// This allows to handle the old project scenarios where the TargetPlatformIdentifier and TargetPlatformVersion may be set to Windows and v7.0 respectively.
        /// </remarks>
        internal static NuGetFramework ParseComponents(string targetFrameworkMoniker, string targetPlatformMoniker, IFrameworkNameProvider mappings)
        {
            if (string.IsNullOrEmpty(targetFrameworkMoniker))
            {
                throw new ArgumentException(Strings.ArgumentCannotBeNullOrEmpty, nameof(targetFrameworkMoniker));
            }

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

            NuGetFramework result;
            string         targetFrameworkIdentifier;
            Version        targetFrameworkVersion;
            var            parts = GetParts(targetFrameworkMoniker);

            // if the first part is a special framework, ignore the rest
            if (TryParseSpecialFramework(parts[0], out result))
            {
                return(result);
            }

            string profile;
            string targetFrameworkProfile;

            ParseFrameworkNameParts(mappings, parts, out targetFrameworkIdentifier, out targetFrameworkVersion, out targetFrameworkProfile);
            if (!mappings.TryGetProfile(targetFrameworkIdentifier, targetFrameworkProfile ?? string.Empty, out profile))
            {
                profile = targetFrameworkProfile;
            }

            if (StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.Portable, targetFrameworkIdentifier))
            {
                IEnumerable <NuGetFramework> clientFrameworks;
                if (mappings.TryGetPortableFrameworks(profile, out clientFrameworks))
                {
                    var profileNumber = -1;
                    if (mappings.TryGetPortableProfile(clientFrameworks, out profileNumber))
                    {
                        profile = FrameworkNameHelpers.GetPortableProfileNumberString(profileNumber);
                    }
                }
                else
                {
                    return(UnsupportedFramework);
                }
            }

            var isNet5EraTfm = targetFrameworkVersion.Major >= 5 &&
                               StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.NetCoreApp, targetFrameworkIdentifier);

            if (!string.IsNullOrEmpty(profile) && isNet5EraTfm)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentCulture,
                                                Strings.FrameworkDoesNotSupportProfiles,
                                                profile
                                                ));
            }

            if (!string.IsNullOrEmpty(targetPlatformMoniker) && isNet5EraTfm)
            {
                string  targetPlatformIdentifier;
                Version platformVersion;
                ParsePlatformParts(GetParts(targetPlatformMoniker), out targetPlatformIdentifier, out platformVersion);
                result = new NuGetFramework(targetFrameworkIdentifier, targetFrameworkVersion, targetPlatformIdentifier ?? string.Empty, platformVersion);
            }
            else
            {
                result = new NuGetFramework(targetFrameworkIdentifier, targetFrameworkVersion, profile);
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a NuGetFramework from a folder name using the given mappings.
        /// </summary>
        public static NuGetFramework ParseFolder(string folderName, IFrameworkNameProvider mappings)
        {
            if (folderName == null)
            {
                throw new ArgumentNullException("folderName");
            }

            if (mappings == null)
            {
                throw new ArgumentNullException("mappings");
            }

            if (folderName.IndexOf('%') > -1)
            {
                folderName = Uri.UnescapeDataString(folderName);
            }

            NuGetFramework result = null;

            // first check if we have a special or common framework
            if (!TryParseSpecialFramework(folderName, out result) &&
                !TryParseCommonFramework(folderName, out result))
            {
                // assume this is unsupported unless we find a match
                result = UnsupportedFramework;

                var parts = RawParse(folderName);

                if (parts != null)
                {
                    string framework = null;

                    if (mappings.TryGetIdentifier(parts.Item1, out framework))
                    {
                        var version = FrameworkConstants.EmptyVersion;

                        if (parts.Item2 == null ||
                            mappings.TryGetVersion(parts.Item2, out version))
                        {
                            var profileShort = parts.Item3;

                            if (version.Major >= 5 &&
                                StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.Net, framework))
                            {
                                // net should be treated as netcoreapp in 5.0 and later
                                framework = FrameworkConstants.FrameworkIdentifiers.NetCoreApp;
                                if (!string.IsNullOrEmpty(profileShort))
                                {
                                    bool validProfile = FrameworkConstants.FrameworkProfiles.Contains(profileShort);
                                    if (validProfile)
                                    {
                                        result = new NuGetFramework(framework, version, profileShort.ToLower());
                                    }
                                    else
                                    {
                                        return(result); // with result == UnsupportedFramework
                                    }
                                }
                                else
                                {
                                    result = new NuGetFramework(framework, version, string.Empty);
                                }
                            }
                            else
                            {
                                string profile = null;
                                if (!mappings.TryGetProfile(framework, profileShort, out profile))
                                {
                                    profile = profileShort ?? string.Empty;
                                }

                                if (StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.Portable, framework))
                                {
                                    IEnumerable <NuGetFramework> clientFrameworks = null;
                                    if (!mappings.TryGetPortableFrameworks(profileShort, out clientFrameworks))
                                    {
                                        result = UnsupportedFramework;
                                    }
                                    else
                                    {
                                        var profileNumber = -1;
                                        if (mappings.TryGetPortableProfile(clientFrameworks, out profileNumber))
                                        {
                                            var portableProfileNumber = FrameworkNameHelpers.GetPortableProfileNumberString(profileNumber);
                                            result = new NuGetFramework(framework, version, portableProfileNumber);
                                        }
                                        else
                                        {
                                            result = new NuGetFramework(framework, version, profileShort);
                                        }
                                    }
                                }
                                else
                                {
                                    result = new NuGetFramework(framework, version, profile);
                                }
                            }
                        }
                    }
                }
                else
                {
                    // If the framework was not recognized check if it is a deprecated framework
                    NuGetFramework deprecated = null;

                    if (TryParseDeprecatedFramework(folderName, out deprecated))
                    {
                        result = deprecated;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Creates a NuGetFramework from a folder name using the given mappings.
        /// </summary>
        public static NuGetFramework ParseFolder(string folderName, IFrameworkNameProvider mappings)
        {
            if (folderName == null)
            {
                throw new ArgumentNullException(nameof(folderName));
            }

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

            if (folderName.IndexOf('%') > -1)
            {
                folderName = Uri.UnescapeDataString(folderName);
            }

            NuGetFramework result = null;

            // first check if we have a special or common framework
            if (!TryParseSpecialFramework(folderName, out result) &&
                !TryParseCommonFramework(folderName, out result))
            {
                // assume this is unsupported unless we find a match
                result = UnsupportedFramework;

                var parts = RawParse(folderName);

                if (parts != null)
                {
                    string framework = null;

                    if (mappings.TryGetIdentifier(parts.Item1, out framework))
                    {
                        var version = FrameworkConstants.EmptyVersion;

                        if (parts.Item2 == null ||
                            mappings.TryGetVersion(parts.Item2, out version))
                        {
                            var profileShort = parts.Item3;

                            if (version.Major >= 5 &&
                                (StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.Net, framework) ||
                                 StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.NetCoreApp, framework)
                                )
                                )
                            {
                                // net should be treated as netcoreapp in 5.0 and later
                                framework = FrameworkConstants.FrameworkIdentifiers.NetCoreApp;
                                if (!string.IsNullOrEmpty(profileShort))
                                {
                                    // Find a platform version if it exists and yank it out
                                    var platformChars = profileShort;
                                    var versionStart  = 0;
                                    while (versionStart < platformChars.Length &&
                                           IsLetterOrDot(platformChars[versionStart]))
                                    {
                                        versionStart++;
                                    }
                                    string platform = versionStart > 0 ? profileShort.Substring(0, versionStart) : profileShort;
                                    string platformVersionString = versionStart > 0 ? profileShort.Substring(versionStart, profileShort.Length - versionStart) : null;

                                    // Parse the version if it's there.
                                    Version platformVersion = FrameworkConstants.EmptyVersion;
                                    if ((string.IsNullOrEmpty(platformVersionString) || mappings.TryGetPlatformVersion(platformVersionString, out platformVersion)))
                                    {
                                        result = new NuGetFramework(framework, version, platform ?? string.Empty, platformVersion ?? FrameworkConstants.EmptyVersion);
                                    }
                                    else
                                    {
                                        return(result); // with result == UnsupportedFramework
                                    }
                                }
                                else
                                {
                                    result = new NuGetFramework(framework, version, string.Empty, FrameworkConstants.EmptyVersion);
                                }
                            }
                            else
                            {
                                string profile = null;
                                if (!mappings.TryGetProfile(framework, profileShort, out profile))
                                {
                                    profile = profileShort ?? string.Empty;
                                }

                                if (StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.Portable, framework))
                                {
                                    IEnumerable <NuGetFramework> clientFrameworks = null;
                                    if (!mappings.TryGetPortableFrameworks(profileShort, out clientFrameworks))
                                    {
                                        result = UnsupportedFramework;
                                    }
                                    else
                                    {
                                        var profileNumber = -1;
                                        if (mappings.TryGetPortableProfile(clientFrameworks, out profileNumber))
                                        {
                                            var portableProfileNumber = FrameworkNameHelpers.GetPortableProfileNumberString(profileNumber);
                                            result = new NuGetFramework(framework, version, portableProfileNumber);
                                        }
                                        else
                                        {
                                            result = new NuGetFramework(framework, version, profileShort);
                                        }
                                    }
                                }
                                else
                                {
                                    result = new NuGetFramework(framework, version, profile);
                                }
                            }
                        }
                    }
                }
                else
                {
                    // If the framework was not recognized check if it is a deprecated framework
                    NuGetFramework deprecated = null;

                    if (TryParseDeprecatedFramework(folderName, out deprecated))
                    {
                        result = deprecated;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Creates a NuGetFramework from a folder name using the given mappings.
        /// </summary>
        public static NuGetFramework ParseFolder(string folderName, IFrameworkNameProvider mappings)
        {
            if (folderName == null)
            {
                throw new ArgumentNullException("folderName");
            }

            if (folderName.IndexOf('%') > -1)
            {
                folderName = Uri.UnescapeDataString(folderName);
            }

            NuGetFramework result = null;

            // first check if we have a special or common framework
            if (!TryParseSpecialFramework(folderName, out result) && !TryParseCommonFramework(folderName, out result))
            {
                // assume this is unsupported unless we find a match
                result = UnsupportedFramework;

                Tuple <string, string, string> parts = RawParse(folderName);

                if (parts != null)
                {
                    string framework = null;

                    if (mappings.TryGetIdentifier(parts.Item1, out framework))
                    {
                        Version version = FrameworkConstants.EmptyVersion;

                        if (parts.Item2 == null || mappings.TryGetVersion(parts.Item2, out version))
                        {
                            string profileShort = parts.Item3;
                            string profile      = null;
                            if (!mappings.TryGetProfile(framework, profileShort, out profile))
                            {
                                profile = profileShort ?? string.Empty;
                            }

                            if (StringComparer.OrdinalIgnoreCase.Equals(FrameworkConstants.FrameworkIdentifiers.Portable, framework))
                            {
                                IEnumerable <NuGetFramework> clientFrameworks = null;
                                mappings.TryGetPortableFrameworks(profileShort, out clientFrameworks);

                                int profileNumber = -1;
                                if (mappings.TryGetPortableProfile(clientFrameworks, out profileNumber))
                                {
                                    string portableProfileNumber = FrameworkNameHelpers.GetPortableProfileNumberString(profileNumber);
                                    result = new NuGetFramework(framework, version, portableProfileNumber);
                                }
                                else
                                {
                                    // TODO: should this be unsupported?
                                    result = new NuGetFramework(framework, version, profileShort);
                                }
                            }
                            else
                            {
                                result = new NuGetFramework(framework, version, profile);
                            }
                        }
                    }
                }
                else
                {
                    // If the framework was not recognized check if it is a deprecated framework
                    NuGetFramework deprecated = null;

                    if (TryParseDeprecatedFramework(folderName, out deprecated))
                    {
                        result = deprecated;
                    }
                }
            }

            return(result);
        }