Exemple #1
0
        private string GetLatestFeature(IEnumerable <SdkVersionInfo> availableSdks, SdkVersionInfo versionToResolve)
        {
            /*
             *  Uses the highest installed feature band and patch level that matches the requested major and minor
             *  with a feature band that is greater or equal than the specified value.
             *  If not found, fails.
             */
            availableSdks = availableSdks
                            .Where(availableSdk =>
            {
                return(availableSdk.Major == versionToResolve.Major &&
                       availableSdk.Minor == versionToResolve.Minor &&
                       availableSdk.Feature >= versionToResolve.Feature);
            })
                            .OrderByDescending(availableSdk => availableSdk);

            var satisfyingVersion = availableSdks.FirstOrDefault();

            if (satisfyingVersion != null)
            {
                return(satisfyingVersion.RawString);
            }

            return(null);
        }
Exemple #2
0
        private string GetMajor(IEnumerable <SdkVersionInfo> availableSdks, SdkVersionInfo versionToResolve)
        {
            /*
             *  Uses the latest patch level for the specified major, minor, and feature band.
             *  If not found, rolls forward to the next higher feature band within the same major/minor version
             *  and uses the latest patch level for that feature band.
             *  If not found, rolls forward to the next higher minor and feature band within the same major and
             *  uses the latest patch level for that feature band.
             *  If not found, rolls forward to the next higher major, minor, and feature band and uses the latest
             *  patch level for that feature band.
             *  If not found, fails.
             */
            var result = GetMinor(availableSdks, versionToResolve);

            if (!string.IsNullOrEmpty(result))
            {
                return(result);
            }

            var higherMajorSdks = availableSdks.Where(sdk => sdk.Major > versionToResolve.Major);

            if (higherMajorSdks.Any())
            {
                var nextHighestMajorSdk  = higherMajorSdks.OrderBy(sdk => sdk).First();
                var nextHighestMajorSdks = higherMajorSdks.Where(sdk => sdk.Major == nextHighestMajorSdk.Major);
                return(GetMinor(nextHighestMajorSdks, nextHighestMajorSdk));
            }

            return(null);
        }
Exemple #3
0
        private string GetFeature(IEnumerable <SdkVersionInfo> availableSdks, SdkVersionInfo versionToResolve)
        {
            /*
             *  Uses the latest patch level for the specified major, minor, and feature band.
             *  If not found, rolls forward to the next higher feature band within the same major/minor and
             *  uses the latest patch level for that feature band.
             *  If not found, fails.
             */
            availableSdks = availableSdks.Where(availableSdk =>
            {
                return(availableSdk.Major == versionToResolve.Major &&
                       availableSdk.Minor == versionToResolve.Minor);
            });

            var sameFeatureSdks = availableSdks.Where(availableSdk => availableSdk.Feature == versionToResolve.Feature);

            if (sameFeatureSdks.Any())
            {
                return(GetLatestPatch(sameFeatureSdks, versionToResolve));
            }
            else
            {
                var higherFeatureSdks = availableSdks
                                        .Where(availableSdk => availableSdk.Feature > versionToResolve.Feature);
                if (higherFeatureSdks.Any())
                {
                    var nextHigherFeatureSdk  = higherFeatureSdks.OrderBy(sdk => sdk).First();
                    var nextHigherFeatureSdks = higherFeatureSdks.Where(sdk => sdk.Feature == nextHigherFeatureSdk.Feature);
                    return(GetLatestPatch(nextHigherFeatureSdks, nextHigherFeatureSdk));
                }
            }

            return(null);
        }
Exemple #4
0
        private string GetDisable(IEnumerable <SdkVersionInfo> availableSdks, SdkVersionInfo versionToResolve)
        {
            // From spec: Doesn't roll forward. Exact match required.
            if (availableSdks.Any(availableSdk => availableSdk.Equals(versionToResolve)))
            {
                return(versionToResolve.RawString);
            }

            return(null);
        }
Exemple #5
0
 private string GetPatch(IEnumerable <SdkVersionInfo> availableSdks, SdkVersionInfo versionToResolve)
 {
     /*
      *  Uses the specified version.
      *  If not found, rolls forward to the latest patch level.
      *  If not found, fails.
      *  This value is the legacy behavior from the earlier versions of the SDK.
      */
     if (availableSdks.Any(availableSdk => availableSdk.Equals(versionToResolve)))
     {
         return(versionToResolve.RawString);
     }
     else
     {
         return(GetLatestPatch(availableSdks, versionToResolve));
     }
 }
Exemple #6
0
        private string GetLatestMajor(IEnumerable <SdkVersionInfo> availableSdks, SdkVersionInfo versionToResolve)
        {
            /*
             *  Uses the highest installed .NET Core SDK with a major that is greater or equal than the specified value.
             *  If not found, fail.
             */
            availableSdks = availableSdks
                            .Where(availableSdk =>
            {
                return(availableSdk.Major >= versionToResolve.Major);
            })
                            .OrderByDescending(availableSdk => availableSdk);

            var satisfyingVersion = availableSdks.FirstOrDefault();

            if (satisfyingVersion != null)
            {
                return(satisfyingVersion.RawString);
            }

            return(null);
        }
Exemple #7
0
        public string GetSatisfyingSdkVersion(GlobalJsonModel globalJson, IEnumerable <string> availableSdks)
        {
            /*
             *  From spec:
             *  If no global.json file is found, or global.json doesn't specify an SDK version nor an
             *  allowPrerelease value, the highest installed SDK version is used (equivalent to setting rollForward
             *  to latestMajor).
             */
            if (globalJson?.Sdk == null)
            {
                globalJson     = new GlobalJsonModel();
                globalJson.Sdk = new SdkModel
                {
                    Version     = "0.0.000",
                    RollForward = RollForwardPolicy.LatestMajor,
                };

                _logger.LogDebug(
                    $"No 'sdk' provided in global.json. Choosing a version using the " +
                    $"default 'rollForward' policy: {globalJson.Sdk.RollForward}");
            }

            var sdkNodeInGlobalJson = globalJson.Sdk;

            if (!SdkVersionInfo.TryParse(sdkNodeInGlobalJson.Version, out var sdkVersionInGlobalJson))
            {
                throw new InvalidUsageException($"Invalid version format '{sdkNodeInGlobalJson}' in global.json");
            }

            var parsedSdkVersions   = new List <SdkVersionInfo>();
            var unparsedSdkVersions = new List <string>();

            foreach (var sdkVersion in availableSdks)
            {
                if (SdkVersionInfo.TryParse(sdkVersion, out var parsedSdkVersion))
                {
                    parsedSdkVersions.Add(parsedSdkVersion);
                }
                else
                {
                    unparsedSdkVersions.Add(sdkVersion);
                }
            }

            if (unparsedSdkVersions.Count > 0)
            {
                _logger.LogDebug(
                    "Unable to parse sdk versions: {unparsedSdkVersions}",
                    string.Join(", ", unparsedSdkVersions));
            }

            var availableSdkVersions = parsedSdkVersions.AsEnumerable();

            if (!sdkNodeInGlobalJson.AllowPreRelease)
            {
                availableSdkVersions = availableSdkVersions.Where(sdk => !sdk.IsPrerelease);
            }

            string resolvedVersion = null;

            switch (sdkNodeInGlobalJson.RollForward)
            {
            case RollForwardPolicy.Disable:
                resolvedVersion = GetDisable(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.Patch:
                resolvedVersion = GetPatch(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.Feature:
                resolvedVersion = GetFeature(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.Minor:
                resolvedVersion = GetMinor(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.Major:
                resolvedVersion = GetMajor(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.LatestPatch:
                resolvedVersion = GetLatestPatch(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.LatestFeature:
                resolvedVersion = GetLatestFeature(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.LatestMinor:
                resolvedVersion = GetLatestMinor(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            case RollForwardPolicy.LatestMajor:
                resolvedVersion = GetLatestMajor(availableSdkVersions, sdkVersionInGlobalJson);
                break;

            default:
                _logger.LogDebug(
                    "Value {invalidRollForwardPolicy} is invalid for 'rollFoward' policy.",
                    sdkNodeInGlobalJson.RollForward.ToString());
                return(null);
            }

            if (resolvedVersion == null)
            {
                _logger.LogDebug(
                    "Could not resolve a version using roll forward policy {rollForwardPolicy} and available sdk " +
                    "versions {availableSdkVersions}",
                    sdkNodeInGlobalJson.RollForward.ToString(),
                    string.Join(", ", availableSdks));
            }

            return(resolvedVersion);
        }