Exemplo n.º 1
0
        public string GetSatisfyingSdkVersion(
            ISourceRepo sourceRepo,
            string runtimeVersion,
            IEnumerable <string> availableSdks)
        {
            string sdkVersion;

            if (sourceRepo.FileExists(DotNetCoreConstants.GlobalJsonFileName))
            {
                var globalJsonContent = sourceRepo.ReadFile(
                    Path.Combine(sourceRepo.RootPath, DotNetCoreConstants.GlobalJsonFileName));

                _logger.LogDebug(
                    "Detected presence of global.json file with content {globalJsonContent}",
                    globalJsonContent);

                var globalJsonModel = JsonConvert.DeserializeObject <GlobalJsonModel>(globalJsonContent);
                sdkVersion = GetSatisfyingSdkVersion(globalJsonModel, availableSdks);

                _logger.LogDebug(
                    "Resolved sdk version to {resolvedSdkVersion} based on global.json file and available sdk versions",
                    sdkVersion);
            }
            else
            {
                // As per global.json spec, if a global.json file is not present, then roll forward policy is
                // considered as 'latestMajor'. This can cause end users apps to fail since in this case even prelreease
                // versions are considered. So here we minimize the impact by relying on the runtime version instead.
                // We choose only the 'major' and 'minor' part of the runtime version.
                // For example, 2.1.14 of runtime will result in a latest minor sdk in '1', for example
                // 2.1.202 or 2.1.400
                var version         = new SemVer.Version(runtimeVersion);
                var globalJsonModel = new GlobalJsonModel
                {
                    Sdk = new SdkModel
                    {
                        Version = $"{version.Major}.{version.Minor}.100",

                        // Get latest feature and patch of the version
                        RollForward     = RollForwardPolicy.LatestFeature,
                        AllowPreRelease = true,
                    },
                };

                _logger.LogDebug(
                    "global.json file was not find in the repo, so choosing an sdk version which satisfies the " +
                    "version {defaultSdkVersion}, roll forward policy of {defaultRollForwardPolicy} and " +
                    "allowPrerelease value of {defaultAllowPrerelease}.",
                    globalJsonModel.Sdk.Version,
                    globalJsonModel.Sdk.RollForward,
                    globalJsonModel.Sdk.AllowPreRelease);

                sdkVersion = GetSatisfyingSdkVersion(globalJsonModel, availableSdks);
            }

            return(sdkVersion);
        }
Exemplo n.º 2
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);
        }