Esempio n. 1
0
        protected override void ExecuteCore()
        {
            if (MissingWorkloadPacks.Any())
            {
                string?userProfileDir           = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
                var    workloadManifestProvider = new SdkDirectoryWorkloadManifestProvider(NetCoreRoot, NETCoreSdkVersion, userProfileDir);
                var    workloadResolver         = WorkloadResolver.Create(workloadManifestProvider, NetCoreRoot, NETCoreSdkVersion, userProfileDir);

                var suggestedWorkloads = workloadResolver.GetWorkloadSuggestionForMissingPacks(
                    MissingWorkloadPacks.Select(item => new WorkloadPackId(item.ItemSpec)).ToList(),
                    out ISet <WorkloadPackId> unsatisfiablePacks
                    );

                if (GenerateErrorsForMissingWorkloads)
                {
                    if (suggestedWorkloads is not null)
                    {
                        var suggestedInstallCommand = "dotnet workload install " + string.Join(" ", suggestedWorkloads.Select(w => w.Id));
                        var errorMessage            = string.Format(CultureInfo.CurrentCulture,
                                                                    Strings.WorkloadNotInstalled, string.Join(" ", suggestedWorkloads.Select(w => w.Id)), suggestedInstallCommand);
                        Log.LogError(errorMessage);
                    }
                    else
                    {
                        Log.LogError(Strings.WorkloadNotAvailable, string.Join(" ", unsatisfiablePacks));
                    }
                }

                if (suggestedWorkloads is not null)
                {
                    SuggestedWorkloads = suggestedWorkloads.Select(suggestedWorkload =>
                    {
                        var suggestedWorkloadsList = GetSuggestedWorkloadsList(suggestedWorkload);
                        var taskItem = new TaskItem(suggestedWorkload.Id);
                        taskItem.SetMetadata("VisualStudioComponentId", ToSafeId(suggestedWorkload.Id));
                        taskItem.SetMetadata("VisualStudioComponentIds", string.Join(";", suggestedWorkloadsList));
                        return(taskItem);
                    }).ToArray();
                }
            }
        }
Esempio n. 2
0
        private string GetPackPath(string packName, string packVersion)
        {
            IEnumerable <string> GetPackFolders()
            {
                var packRootEnvironmentVariable = Environment.GetEnvironmentVariable(EnvironmentVariableNames.WORKLOAD_PACK_ROOTS);

                if (!string.IsNullOrEmpty(packRootEnvironmentVariable))
                {
                    foreach (var packRoot in packRootEnvironmentVariable.Split(Path.PathSeparator))
                    {
                        yield return(Path.Combine(packRoot, "packs"));
                    }
                }

                if (!string.IsNullOrEmpty(NetCoreRoot) && !string.IsNullOrEmpty(NETCoreSdkVersion))
                {
                    if (WorkloadFileBasedInstall.IsUserLocal(NetCoreRoot, NETCoreSdkVersion) &&
                        CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath() is { } userProfileDir)
                    {
                        yield return(Path.Combine(userProfileDir, "packs"));
                    }
                }

                if (!string.IsNullOrEmpty(TargetingPackRoot))
                {
                    yield return(TargetingPackRoot);
                }
            }

            foreach (var packFolder in GetPackFolders())
            {
                string packPath = Path.Combine(packFolder, packName, packVersion);
                if (Directory.Exists(packPath))
                {
                    return(packPath);
                }
            }

            return(null);
        }
Esempio n. 3
0
        private string GetResolvedPackVersion(string packID, string packVersion)
        {
            if (!packVersion.Equals("**FromWorkload**", StringComparison.OrdinalIgnoreCase))
            {
                return(packVersion);
            }

            if (_workloadManifestProvider == null)
            {
                string userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
                _workloadManifestProvider = new SdkDirectoryWorkloadManifestProvider(NetCoreRoot, NETCoreSdkVersion, userProfileDir);
                _workloadResolver         = WorkloadResolver.Create(_workloadManifestProvider, NetCoreRoot, NETCoreSdkVersion, userProfileDir);
            }

            var packInfo = _workloadResolver.TryGetPackInfo(new WorkloadPackId(packID));

            if (packInfo == null)
            {
                Log.LogError("NETSDKZZZZ: Error getting pack version: Pack '{0}' was not present in workload manifests.", packID);
                return(packVersion);
            }
            return(packInfo.Version);
        }
Esempio n. 4
0
        public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext context, SdkResultFactory factory)
        {
            string dotnetRoot        = null;
            string msbuildSdksDir    = null;
            string netcoreSdkVersion = null;
            IDictionary <string, string>        propertiesToAdd = null;
            IDictionary <string, SdkResultItem> itemsToAdd      = null;
            List <string>           warnings         = null;
            CachingWorkloadResolver workloadResolver = null;

            if (context.State is CachedState priorResult)
            {
                dotnetRoot        = priorResult.DotnetRoot;
                msbuildSdksDir    = priorResult.MSBuildSdksDir;
                netcoreSdkVersion = priorResult.NETCoreSdkVersion;
                propertiesToAdd   = priorResult.PropertiesToAdd;
                workloadResolver  = priorResult.WorkloadResolver;
            }

            if (context.IsRunningInVisualStudio)
            {
                workloadResolver = _staticWorkloadResolver;
            }

            if (workloadResolver == null)
            {
                workloadResolver = new CachingWorkloadResolver();
            }

            if (msbuildSdksDir == null)
            {
                dotnetRoot = EnvironmentProvider.GetDotnetExeDirectory(_getEnvironmentVariable);
                string globalJsonStartDir = Path.GetDirectoryName(context.SolutionFilePath ?? context.ProjectFilePath);
                var    resolverResult     = _netCoreSdkResolver.ResolveNETCoreSdkDirectory(globalJsonStartDir, context.MSBuildVersion, context.IsRunningInVisualStudio, dotnetRoot);

                if (resolverResult.ResolvedSdkDirectory == null)
                {
                    return(Failure(
                               factory,
                               Strings.UnableToLocateNETCoreSdk));
                }

                msbuildSdksDir    = Path.Combine(resolverResult.ResolvedSdkDirectory, "Sdks");
                netcoreSdkVersion = new DirectoryInfo(resolverResult.ResolvedSdkDirectory).Name;

                // These are overrides that are used to force the resolved SDK tasks and targets to come from a given
                // base directory and report a given version to msbuild (which may be null if unknown. One key use case
                // for this is to test SDK tasks and targets without deploying them inside the .NET Core SDK.
                var msbuildSdksDirFromEnv    = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR");
                var netcoreSdkVersionFromEnv = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_VER");
                if (!string.IsNullOrEmpty(msbuildSdksDirFromEnv))
                {
                    msbuildSdksDir = msbuildSdksDirFromEnv;
                }
                if (!string.IsNullOrEmpty(netcoreSdkVersionFromEnv))
                {
                    netcoreSdkVersion = netcoreSdkVersionFromEnv;
                }

                if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, sdkReference.MinimumVersion))
                {
                    return(Failure(
                               factory,
                               Strings.NETCoreSDKSmallerThanMinimumRequestedVersion,
                               netcoreSdkVersion,
                               sdkReference.MinimumVersion));
                }

                Version minimumMSBuildVersion = _netCoreSdkResolver.GetMinimumMSBuildVersion(resolverResult.ResolvedSdkDirectory);
                if (context.MSBuildVersion < minimumMSBuildVersion)
                {
                    return(Failure(
                               factory,
                               Strings.MSBuildSmallerThanMinimumVersion,
                               netcoreSdkVersion,
                               minimumMSBuildVersion,
                               context.MSBuildVersion));
                }

                string minimumVSDefinedSDKVersion = GetMinimumVSDefinedSDKVersion();
                if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, minimumVSDefinedSDKVersion))
                {
                    return(Failure(
                               factory,
                               Strings.NETCoreSDKSmallerThanMinimumVersionRequiredByVisualStudio,
                               netcoreSdkVersion,
                               minimumVSDefinedSDKVersion));
                }

                if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson)
                {
                    if (warnings == null)
                    {
                        warnings = new List <string>();
                    }
                    warnings.Add(Strings.GlobalJsonResolutionFailed);
                    if (propertiesToAdd == null)
                    {
                        propertiesToAdd = new Dictionary <string, string>();
                    }
                    propertiesToAdd.Add("SdkResolverHonoredGlobalJson", "false");
                    propertiesToAdd.Add("SdkResolverGlobalJsonPath", resolverResult.GlobalJsonPath);
                }
            }

            context.State = new CachedState
            {
                DotnetRoot        = dotnetRoot,
                MSBuildSdksDir    = msbuildSdksDir,
                NETCoreSdkVersion = netcoreSdkVersion,
                PropertiesToAdd   = propertiesToAdd,
                WorkloadResolver  = workloadResolver
            };

            //  First check if requested SDK resolves to a workload SDK pack
            string userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
            var    workloadResult = workloadResolver.Resolve(sdkReference.Name, dotnetRoot, netcoreSdkVersion, userProfileDir);

            if (workloadResult is not CachingWorkloadResolver.NullResolutionResult)
            {
                return(workloadResult.ToSdkResult(sdkReference, factory));
            }

            string msbuildSdkDir = Path.Combine(msbuildSdksDir, sdkReference.Name, "Sdk");

            if (!Directory.Exists(msbuildSdkDir))
            {
                return(Failure(
                           factory,
                           Strings.MSBuildSDKDirectoryNotFound,
                           msbuildSdkDir));
            }

            return(factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion, propertiesToAdd, itemsToAdd, warnings));
        }