private void ProcessSolutionFile(string solutionFileFullPath, PackageRestoreInputs restoreInputs) { restoreInputs.DirectoryOfSolutionFile = Path.GetDirectoryName(solutionFileFullPath); restoreInputs.NameOfSolutionFile = Path.GetFileNameWithoutExtension(solutionFileFullPath); // restore packages for the solution var solutionLevelPackagesConfig = Path.Combine( restoreInputs.DirectoryOfSolutionFile, NuGetConstants.NuGetSolutionSettingsFolder, Constants.PackageReferenceFile); if (File.Exists(solutionLevelPackagesConfig)) { restoreInputs.PackagesConfigFiles.Add(solutionLevelPackagesConfig); } var projectFiles = MsBuildUtility.GetAllProjectFileNames(solutionFileFullPath, MsBuildDirectory.Value.Path); foreach (var projectFile in projectFiles) { if (!File.Exists(projectFile)) { var message = string.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("RestoreCommandProjectNotFound"), projectFile); Console.LogWarning(message); continue; } var normalizedProjectFile = Path.GetFullPath(projectFile); // Add everything restoreInputs.ProjectFiles.Add(normalizedProjectFile); } }
/// <summary> /// Read project inputs using MSBuild /// </summary> private async Task DetermineInputsFromMSBuildAsync(PackageRestoreInputs packageRestoreInputs) { // Find P2P graph for v3 inputs. // Ignore xproj files as top level inputs var projectsWithPotentialP2PReferences = packageRestoreInputs .ProjectFiles .Where(path => !path.EndsWith(".xproj", StringComparison.OrdinalIgnoreCase)) .ToArray(); if (projectsWithPotentialP2PReferences.Length > 0) { DependencyGraphSpec dgFileOutput = null; try { dgFileOutput = await GetDependencyGraphSpecAsync(projectsWithPotentialP2PReferences, GetSolutionDirectory(packageRestoreInputs), packageRestoreInputs.NameOfSolutionFile, ConfigFile); } catch (Exception ex) { // At this point reading the project has failed, to keep backwards // compatibility this should warn instead of error if // packages.config files exist, but no project.json files. // This will skip NETCore projects which is a problem, but there is // not a good way to know if they exist, or if this is an old type of // project that the targets file cannot handle. // Log exception for debug Console.LogDebug(ex.ToString()); // Check for packages.config but no project.json files if (projectsWithPotentialP2PReferences.Where(HasPackagesConfigFile).Any() && !projectsWithPotentialP2PReferences.Where(HasProjectJsonFile).Any()) { // warn to let the user know that NETCore will be skipped Console.LogWarning(LocalizedResourceManager.GetString("Warning_ReadingProjectsFailed")); // Add packages.config packageRestoreInputs.PackagesConfigFiles .AddRange(projectsWithPotentialP2PReferences .Select(GetPackagesConfigFile) .Where(path => path != null)); } else { // If there are project.json files or no packages.config files // continue to fail throw; } } // Process the DG file and add both v2 and v3 inputs if (dgFileOutput != null) { AddInputsFromDependencyGraphSpec(packageRestoreInputs, dgFileOutput); } } }
private void ProcessSolutionFile(string solutionFileFullPath, PackageRestoreInputs restoreInputs) { restoreInputs.DirectoryOfSolutionFile = Path.GetDirectoryName(solutionFileFullPath); // restore packages for the solution var solutionLevelPackagesConfig = Path.Combine( restoreInputs.DirectoryOfSolutionFile, NuGetConstants.NuGetSolutionSettingsFolder, Constants.PackageReferenceFile); if (File.Exists(solutionLevelPackagesConfig)) { restoreInputs.PackagesConfigFiles.Add(solutionLevelPackagesConfig); } var projectFiles = MsBuildUtility.GetAllProjectFileNames(solutionFileFullPath, _msbuildDirectory.Value); foreach (var projectFile in projectFiles) { if (!File.Exists(projectFile)) { var message = string.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("RestoreCommandProjectNotFound"), projectFile); Console.LogWarning(message); continue; } var normalizedProjectFile = Path.GetFullPath(projectFile); // packages.config var packagesConfigFilePath = GetPackageReferenceFile(normalizedProjectFile); // project.json var dir = Path.GetDirectoryName(normalizedProjectFile); var projectName = Path.GetFileNameWithoutExtension(normalizedProjectFile); var projectJsonPath = ProjectJsonPathUtilities.GetProjectConfigPath(dir, projectName); // project.json overrides packages.config if (File.Exists(projectJsonPath)) { // project.json inputs are resolved again against the p2p file // and are matched with the solution there // For known msbuild project types use the project if (MsBuildUtility.IsMsBuildBasedProject(normalizedProjectFile)) { restoreInputs.RestoreV3Context.Inputs.Add(normalizedProjectFile); } else { // For unknown types restore the project.json file without p2ps restoreInputs.RestoreV3Context.Inputs.Add(projectJsonPath); } } else if (File.Exists(packagesConfigFilePath)) { restoreInputs.PackagesConfigFiles.Add(packagesConfigFilePath); } } }
private void WarnForHTTPSources(IList <PackageSource> packageSources) { List <PackageSource> httpPackageSources = null; foreach (PackageSource packageSource in packageSources) { if (packageSource.IsHttp && !packageSource.IsHttps) { if (httpPackageSources == null) { httpPackageSources = new(); } httpPackageSources.Add(packageSource); } } if (httpPackageSources != null && httpPackageSources.Count != 0) { if (httpPackageSources.Count == 1) { Console.LogWarning( string.Format(CultureInfo.CurrentCulture, NuGetResources.Warning_HttpServerUsage, "search", httpPackageSources[0])); } else { Console.LogWarning( string.Format(CultureInfo.CurrentCulture, NuGetResources.Warning_HttpSources_Multiple, "search", Environment.NewLine + string.Join(Environment.NewLine, httpPackageSources.Select(e => e.Name)))); } } }
public override async Task ExecuteCommandAsync() { if (DisableParallelProcessing) { HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore(); } CalculateEffectivePackageSaveMode(); var restoreSummaries = new List <RestoreSummary>(); if (!string.IsNullOrEmpty(SolutionDirectory)) { SolutionDirectory = Path.GetFullPath(SolutionDirectory); } var restoreInputs = await DetermineRestoreInputsAsync(); var hasPackagesConfigFiles = restoreInputs.PackagesConfigFiles.Count > 0; var hasProjectJsonOrPackageReferences = restoreInputs.RestoreV3Context.Inputs.Any(); if (!hasPackagesConfigFiles && !hasProjectJsonOrPackageReferences) { Console.LogMinimal(LocalizedResourceManager.GetString(restoreInputs.RestoringWithSolutionFile ? "SolutionRestoreCommandNoPackagesConfigOrProjectJson" : "ProjectRestoreCommandNoPackagesConfigOrProjectJson")); return; } // packages.config if (hasPackagesConfigFiles) { var v2RestoreResults = await PerformNuGetV2RestoreAsync(restoreInputs); restoreSummaries.AddRange(v2RestoreResults); foreach (var restoreResult in v2RestoreResults.Where(r => !r.Success)) { restoreResult .Errors .Where(l => l.Level == LogLevel.Warning) .ForEach(l => Console.LogWarning(l.FormatWithCode())); } } // project.json and PackageReference if (hasProjectJsonOrPackageReferences) { // Read the settings outside of parallel loops. ReadSettings(restoreInputs); // Check if we can restore based on the nuget.config settings CheckRequireConsent(); using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = NoCache; cacheContext.DirectDownload = DirectDownload; var restoreContext = restoreInputs.RestoreV3Context; var providerCache = new RestoreCommandProvidersCache(); // Add restore args to the restore context restoreContext.CacheContext = cacheContext; restoreContext.DisableParallel = DisableParallelProcessing; restoreContext.AllowNoOp = !Force; // if force, no-op is not allowed restoreContext.ConfigFile = ConfigFile; restoreContext.MachineWideSettings = MachineWideSettings; restoreContext.Log = Console; restoreContext.CachingSourceProvider = GetSourceRepositoryProvider(); restoreContext.RestoreForceEvaluate = ForceEvaluate; var packageSaveMode = EffectivePackageSaveMode; if (packageSaveMode != Packaging.PackageSaveMode.None) { restoreContext.PackageSaveMode = EffectivePackageSaveMode; } // Providers // Use the settings loaded above in ReadSettings(restoreInputs) if (restoreInputs.ProjectReferenceLookup.Restore.Count > 0) { // Remove input list, everything has been loaded already restoreContext.Inputs.Clear(); restoreContext.PreLoadedRequestProviders.Add(new DependencyGraphSpecRequestProvider( providerCache, restoreInputs.ProjectReferenceLookup)); } else { // Allow an external .dg file restoreContext.RequestProviders.Add(new DependencyGraphFileRequestProvider(providerCache)); } // Run restore var v3Summaries = await RestoreRunner.RunAsync(restoreContext); restoreSummaries.AddRange(v3Summaries); } } // Summaries RestoreSummary.Log(Console, restoreSummaries, logErrors: true); if (restoreSummaries.Any(x => !x.Success)) { throw new ExitCodeException(exitCode: 1); } }