protected virtual bool?PCLCompare(NuGetFramework framework, NuGetFramework other) { // TODO: PCLs can only depend on other PCLs? if (framework.IsPCL && !other.IsPCL) { return(false); } IEnumerable <NuGetFramework> frameworks = null; IEnumerable <NuGetFramework> otherFrameworks = null; if (framework.IsPCL) { // do not include optional frameworks here since we might be unable to tell what is optional on the other framework _mappings.TryGetPortableFrameworks(framework.Profile, false, out frameworks); } else { frameworks = new NuGetFramework[] { framework }; } if (other.IsPCL) { // include optional frameworks here, the larger the list the more compatible it is _mappings.TryGetPortableFrameworks(other.Profile, true, out otherFrameworks); } else { otherFrameworks = new NuGetFramework[] { other }; } // check if we this is a compatible superset return(PCLInnerCompare(frameworks, otherFrameworks)); }
private bool IsPCLCompatible(NuGetFramework target, NuGetFramework candidate) { if (target.IsPCL && !candidate.IsPCL) { return(IsCompatibleWithTarget(target, candidate)); } IEnumerable <NuGetFramework> targetFrameworks; IEnumerable <NuGetFramework> candidateFrameworks; if (target.IsPCL) { // do not include optional frameworks here since we might be unable to tell what is optional on the other framework _mappings.TryGetPortableFrameworks(target.Profile, false, out targetFrameworks); } else { targetFrameworks = new NuGetFramework[] { target }; } if (candidate.IsPCL) { // include optional frameworks here, the larger the list the more compatible it is _mappings.TryGetPortableFrameworks(candidate.Profile, true, out candidateFrameworks); } else { candidateFrameworks = new NuGetFramework[] { candidate }; } // check if we this is a compatible superset return(PCLInnerCompare(targetFrameworks, candidateFrameworks)); }
/// <summary> /// portable-net45+win8 -> net45, win8 /// </summary> private IEnumerable <NuGetFramework> ExplodePortableFramework(NuGetFramework pcl, bool includeOptional = true) { IEnumerable <NuGetFramework> frameworks = null; if (!_mappings.TryGetPortableFrameworks(pcl.Profile, includeOptional, out frameworks)) { Debug.Fail("Unable to get portable frameworks from: " + pcl.ToString()); frameworks = Enumerable.Empty <NuGetFramework>(); } return(frameworks); }
/// <summary> /// Creates the shortened version of the framework using the given mappings. /// </summary> public virtual string GetShortFolderName(IFrameworkNameProvider mappings) { // Check for rewrites var framework = mappings.GetShortNameReplacement(this); var sb = new StringBuilder(); if (IsSpecificFramework) { var shortFramework = string.Empty; // get the framework if (!mappings.TryGetShortIdentifier( GetFrameworkIdentifier(), out shortFramework)) { shortFramework = GetLettersAndDigitsOnly(framework.Framework); } if (string.IsNullOrEmpty(shortFramework)) { throw new FrameworkException(string.Format( CultureInfo.CurrentCulture, Strings.InvalidFrameworkIdentifier, shortFramework)); } // add framework sb.Append(shortFramework); // add the version if it is non-empty if (!AllFrameworkVersions) { sb.Append(mappings.GetVersionString(framework.Framework, framework.Version)); } if (IsPCL) { sb.Append("-"); IEnumerable <NuGetFramework> frameworks = null; if (framework.HasProfile && mappings.TryGetPortableFrameworks(framework.Profile, false, out frameworks) && frameworks.Any()) { var required = new HashSet <NuGetFramework>(frameworks, Comparer); // Normalize by removing all optional frameworks mappings.TryGetPortableFrameworks(framework.Profile, false, out frameworks); // sort the PCL frameworks by alphabetical order var sortedFrameworks = required.Select(e => e.GetShortFolderName(mappings)).OrderBy(e => e, StringComparer.OrdinalIgnoreCase).ToList(); sb.Append(string.Join("+", sortedFrameworks)); } else { throw new FrameworkException(string.Format( CultureInfo.CurrentCulture, Strings.MissingPortableFrameworks, framework.DotNetFrameworkName)); } } else if (IsNet5Era) { if (!string.IsNullOrEmpty(framework.Platform)) { sb.Append("-"); sb.Append(framework.Platform.ToLowerInvariant()); if (framework.PlatformVersion != FrameworkConstants.EmptyVersion) { sb.Append(mappings.GetVersionString(framework.Framework, framework.PlatformVersion)); } } } else { // add the profile var shortProfile = string.Empty; if (framework.HasProfile && !mappings.TryGetShortProfile(framework.Framework, framework.Profile, out shortProfile)) { // if we have a profile, but can't get a mapping, just use the profile as is shortProfile = framework.Profile; } if (!string.IsNullOrEmpty(shortProfile)) { sb.Append("-"); sb.Append(shortProfile); } } } else { // unsupported, any, agnostic sb.Append(Framework); } return(sb.ToString().ToLowerInvariant()); }
/// <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; 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); var profileNumber = -1; if (mappings.TryGetPortableProfile(clientFrameworks, out profileNumber)) { var 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; }
/// <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); }
/// <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 (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 the shortened version of the framework using the given mappings. /// </summary> public string GetShortFolderName(IFrameworkNameProvider mappings) { var sb = new StringBuilder(); if (IsSpecificFramework) { var shortFramework = string.Empty; // get the framework if (!mappings.TryGetShortIdentifier(Framework, out shortFramework)) { shortFramework = GetLettersAndDigitsOnly(Framework); } if (String.IsNullOrEmpty(shortFramework)) { throw new FrameworkException(Strings.InvalidFrameworkIdentifier); } // add framework sb.Append(shortFramework); // add the version if it is non-empty if (!AllFrameworkVersions) { sb.Append(mappings.GetVersionString(Version)); } if (IsPCL) { sb.Append("-"); IEnumerable <NuGetFramework> frameworks = null; if (HasProfile && mappings.TryGetPortableFrameworks(Profile, false, out frameworks) && frameworks.Any()) { var required = new HashSet <NuGetFramework>(frameworks, Comparer); // Normalize by removing all optional frameworks mappings.TryGetPortableFrameworks(Profile, false, out frameworks); // TODO: is there a scenario where optional frameworks are still needed in the string? // mappings.TryGetPortableFrameworks(Profile, true, out frameworks); // HashSet<NuGetFramework> optional = new HashSet<NuGetFramework>(frameworks.Where(e => !required.Contains(e)), NuGetFramework.Comparer); // sort the PCL frameworks by alphabetical order var sortedFrameworks = required.Select(e => e.GetShortFolderName(mappings)).OrderBy(e => e, StringComparer.OrdinalIgnoreCase).ToList(); // add optional frameworks at the end // sortedFrameworks.AddRange(optional.Select(e => e.GetShortFolderName(mappings)).OrderBy(e => e, StringComparer.OrdinalIgnoreCase)); sb.Append(String.Join("+", sortedFrameworks)); } else { throw new FrameworkException(Strings.InvalidPortableFrameworks); } } else { // add the profile var shortProfile = string.Empty; if (HasProfile && !mappings.TryGetShortProfile(Framework, Profile, out shortProfile)) { // if we have a profile, but can't get a mapping, just use the profile as is shortProfile = Profile; } if (!String.IsNullOrEmpty(shortProfile)) { sb.Append("-"); sb.Append(shortProfile); } } } else { // unsupported, any, agnostic sb.Append(Framework); } return(sb.ToString().ToLowerInvariant()); }
/// <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); }