/// <summary> /// Restore a DependencyGraphSpec /// </summary> public static async Task <List <RestoreSummary> > RunRestore(DependencyGraphSpec input, SimpleTestPathContext pathContext, ILogger logger) { var providerCache = new RestoreCommandProvidersCache(); var sources = new List <string>() { pathContext.PackageSource }; using (var cacheContext = new SourceCacheContext()) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = false, GlobalPackagesFolder = pathContext.UserPackagesFolder, Sources = sources, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(sources.Select(s => new PackageSource(s)))), PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>() { new DependencyGraphSpecRequestProvider(providerCache, input) } }; var summaries = await RestoreRunner.RunAsync(restoreContext); return(summaries.ToList()); } }
public async Task RestoreRunner_WarnIfNoProject() { // If an input folder is provided to RestoreRunner that doesn't contain a project, // it should report an error. using (var workingDir = TestFileSystemUtility.CreateRandomTestFolder()) { // Arrange var logger = new TestLogger(); var providerCache = new RestoreCommandProvidersCache(); var restoreContext = new RestoreArgs() { CacheContext = new SourceCacheContext(), DisableParallel = true, Inputs = new List <string>() { workingDir }, Log = logger, RequestProviders = new List <IRestoreRequestProvider>() { new ProjectJsonRestoreRequestProvider(providerCache) } }; // Act var summaries = await RestoreRunner.Run(restoreContext); // Assert Assert.Equal(0, summaries.Count); var matchingError = logger.Messages.ToList().Find(error => error.Contains(workingDir)); Assert.NotNull(matchingError); } }
private async Task <RestoreTargetGraph?> RestoreProjectAsync(IEnumerable <NuGetReference> packages, IEnumerable <TargetFrameworkMoniker> tfms, CancellationToken token) { var tfmInfo = tfms.Select(tfm => new TargetFrameworkInformation { FrameworkName = NuGetFramework.Parse(tfm.ToFullString()) }).ToList(); // Create a project in a unique and temporary directory var path = Path.Combine(Path.GetTempPath(), "dotnet-ua", "restores", Guid.NewGuid().ToString(), "project.txt"); var spec = new PackageSpec(tfmInfo) { Dependencies = packages.Select(i => new LibraryDependency { LibraryRange = new LibraryRange(i.Name, new VersionRange(i.GetNuGetVersion()), LibraryDependencyTarget.Package), }).ToList(), RestoreMetadata = new() { ProjectPath = path, ProjectName = Path.GetFileNameWithoutExtension(path), ProjectStyle = ProjectStyle.PackageReference, ProjectUniqueName = path, OutputPath = Path.GetTempPath(), OriginalTargetFrameworks = tfms.Select(tfm => tfm.ToFullString()).ToArray(), ConfigFilePaths = _settings.GetConfigFilePaths(), PackagesPath = SettingsUtility.GetGlobalPackagesFolder(_settings), Sources = _packageSources.ToList(), FallbackFolders = SettingsUtility.GetFallbackPackageFolders(_settings).ToList(), }, FilePath = path, Name = Path.GetFileNameWithoutExtension(path), }; var dependencyGraphSpec = new DependencyGraphSpec(); dependencyGraphSpec.AddProject(spec); dependencyGraphSpec.AddRestore(spec.RestoreMetadata.ProjectUniqueName); var requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec); var restoreArgs = new RestoreArgs { AllowNoOp = true, CacheContext = _context, CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(_settings)), Log = new NuGetLogger(_logger), }; // Create requests from the arguments var requests = await requestProvider.CreateRequests(restoreArgs).ConfigureAwait(false); // Restore the package without generating extra files var result = await RestoreRunner.RunWithoutCommit(requests, restoreArgs).ConfigureAwait(false); if (result.Count == 0) { return(null); } return(result[0].Result.RestoreGraphs.FirstOrDefault()); }
private static async Task <IReadOnlyList <RestoreSummary> > RunRestore( SimpleTestPathContext pathContext, TestLogger logger, List <PackageSource> sources, DependencyGraphSpec dgFile, SourceCacheContext cacheContext) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = pathContext.UserPackagesFolder, Sources = new List <string>() { pathContext.PackageSource }, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(sources)), PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>() { new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dgFile) } }; return(await RestoreRunner.RunAsync(restoreContext)); }
/// <summary> /// Restore many projects without writing the lock file /// SourceRepositories(sources) is only used for the CachingSourceProvider, the project-specific sources will still be resolved in RestoreRunner. /// </summary> internal static async Task <IEnumerable <RestoreResultPair> > PreviewRestoreProjectsAsync( ISolutionManager solutionManager, IEnumerable <BuildIntegratedNuGetProject> projects, IEnumerable <PackageSpec> updatedNugetPackageSpecs, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, Guid parentId, ILogger log, CancellationToken token) { token.ThrowIfCancellationRequested(); // Add the new spec to the dg file and fill in the rest. var dgFile = await GetSolutionRestoreSpec(solutionManager, context); dgFile = dgFile.WithoutRestores() .WithPackageSpecs(updatedNugetPackageSpecs); foreach (var project in projects) { dgFile.AddRestore(project.MSBuildProjectPath); } using (var sourceCacheContext = new SourceCacheContext()) { // Update cache context cacheContextModifier(sourceCacheContext); // Settings passed here will be used to populate the restore requests. var restoreContext = GetRestoreArgs( context, providerCache, sourceCacheContext, sources, dgFile, parentId, forceRestore: true, isRestoreOriginalAction: false, restoreForceEvaluate: true, additionalMessasges: null, progressReporter: null); var requests = await RestoreRunner.GetRequests(restoreContext); var results = await RestoreRunner.RunWithoutCommit(requests, restoreContext); return(results); } }
/// <summary> /// Restore a solution and cache the dg spec to context. /// </summary> public static async Task <IReadOnlyList <RestoreSummary> > RestoreAsync( ISolutionManager solutionManager, DependencyGraphSpec dgSpec, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, Guid parentId, bool forceRestore, bool isRestoreOriginalAction, IReadOnlyList <IAssetsLogMessage> additionalMessages, ILogger log, CancellationToken token) { // TODO: This will flow from UI once we enable UI option to trigger reevaluation var restoreForceEvaluate = false; // Check if there are actual projects to restore before running. if (dgSpec.Restore.Count > 0) { using (var sourceCacheContext = new SourceCacheContext()) { // Update cache context cacheContextModifier(sourceCacheContext); var restoreContext = GetRestoreContext( context, providerCache, sourceCacheContext, sources, dgSpec, parentId, forceRestore, isRestoreOriginalAction, restoreForceEvaluate, additionalMessages); var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, token); RestoreSummary.Log(log, restoreSummaries); await PersistDGSpec(dgSpec); return(restoreSummaries); } } return(new List <RestoreSummary>()); }
internal static async Task <RestoreResult> RestoreAsync(RestoreParams restoreParameters, ILogger logger, CancellationToken cancellationToken = default) { var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = false; cacheContext.IgnoreFailedSources = true; var providers = new List <IPreLoadedRestoreRequestProvider>(); var dgSpec = new DependencyGraphSpec(); dgSpec.AddRestore(restoreParameters.ProjectName); var projectSpec = new PackageSpec { Name = restoreParameters.ProjectName, FilePath = restoreParameters.ProjectName, RestoreMetadata = CreateRestoreMetadata(restoreParameters), TargetFrameworks = { CreateTargetFramework(restoreParameters) } }; dgSpec.AddProject(projectSpec); providers.Add(new DependencyGraphSpecRequestProvider(providerCache, dgSpec)); var restoreContext = new RestoreArgs { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, DisableParallel = false, Log = logger, MachineWideSettings = new XPlatMachineWideSetting(), PreLoadedRequestProviders = providers, AllowNoOp = true, HideWarningsAndErrors = true }; var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, cancellationToken).ConfigureAwait(false); var result = new RestoreResult { NoOp = restoreSummaries.All(x => x.NoOpRestore), Success = restoreSummaries.All(x => x.Success), Errors = restoreSummaries.SelectMany(x => x.Errors).Select(x => x.Message).ToImmutableArray() }; return(result); } }
/// <summary> /// Restore without writing the lock file /// </summary> internal static async Task <RestoreResultPair> PreviewRestoreAsync( ISolutionManager solutionManager, BuildIntegratedNuGetProject project, PackageSpec packageSpec, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, Guid parentId, ILogger log, CancellationToken token) { // Restoring packages var logger = context.Logger; // Add the new spec to the dg file and fill in the rest. var dgFile = await GetSolutionRestoreSpec(solutionManager, context); dgFile = dgFile.WithoutRestores() .WithReplacedSpec(packageSpec); dgFile.AddRestore(project.MSBuildProjectPath); using (var sourceCacheContext = new SourceCacheContext()) { // Update cache context cacheContextModifier(sourceCacheContext); // Settings passed here will be used to populate the restore requests. var restoreContext = GetRestoreContext( context, providerCache, sourceCacheContext, sources, dgFile, parentId, forceRestore: true, isRestoreOriginalAction: false, restoreForceEvaluate: true, additionalMessasges: null); var requests = await RestoreRunner.GetRequests(restoreContext); var results = await RestoreRunner.RunWithoutCommit(requests, restoreContext); return(results.Single()); } }
static async Task Main(string[] args) { using var cacheContext = new SourceCacheContext(); using var pathContext = new SimpleTestPathContext(); var providerCache = new RestoreCommandProvidersCache(); var dgFile = new DependencyGraphSpec(); var sources = new List <string>() { pathContext.PackageSource }; var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = pathContext.UserPackagesFolder, Sources = sources, Log = new NullLogger(), CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider( new List <PackageSource>() { new PackageSource(pathContext.PackageSource) })), PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>() { new DependencyGraphSpecRequestProvider(providerCache, dgFile) } }; var request = (await RestoreRunner.GetRequests(restoreContext)).Single(); var providers = providerCache.GetOrCreate(pathContext.UserPackagesFolder, sources, new List <SourceRepository>(), cacheContext, new NullLogger()); var command = new RestoreCommand(request.Request); // Add to cache before install on all providers var globalPackages = providers.GlobalPackages; var packages = globalPackages.FindPackagesById("a"); foreach (var local in providers.LocalProviders) { await local.GetDependenciesAsync( new LibraryIdentity("a", NuGetVersion.Parse("1.0.0"), LibraryType.Package), NuGetFramework.Parse("net46"), cacheContext, new NullLogger(), CancellationToken.None); } // Run restore using an incorrect cache var result = await command.ExecuteAsync(); }
/// <summary> /// Restore a solution and cache the dg spec to context. /// </summary> public static async Task <IReadOnlyList <RestoreSummary> > RestoreAsync( ISolutionManager solutionManager, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, string userPackagesPath, ISettings settings, ILogger log, CancellationToken token) { // Get full dg spec var dgSpec = await GetSolutionRestoreSpec(solutionManager, context); // Cache spec context.SolutionSpec = dgSpec; context.SolutionSpecHash = dgSpec.GetHash(); // Check if there are actual projects to restore before running. if (dgSpec.Restore.Count > 0) { using (var sourceCacheContext = new SourceCacheContext()) { // Update cache context cacheContextModifier(sourceCacheContext); var restoreContext = GetRestoreContext( context, providerCache, settings, sourceCacheContext, sources, dgSpec, userPackagesPath); var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, token); RestoreSummary.Log(log, restoreSummaries); return(restoreSummaries); } } return(new List <RestoreSummary>()); }
private static async Task <RestoreResultPair> PreviewAddPackageReference(PackageReferenceArgs packageReferenceArgs, DependencyGraphSpec dgSpec, PackageSpec originalPackageSpec) { // Set user agent and connection settings. XPlatUtility.ConfigureProtocol(); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = false; cacheContext.IgnoreFailedSources = false; // Pre-loaded request provider containing the graph file var providers = new List <IPreLoadedRestoreRequestProvider>(); // Create a copy to avoid modifying the original spec which may be shared. var updatedPackageSpec = originalPackageSpec.Clone(); PackageSpecOperations.AddOrUpdateDependency(updatedPackageSpec, packageReferenceArgs.PackageDependency); providers.Add(new DependencyGraphSpecRequestProvider(providerCache, dgSpec)); var restoreContext = new RestoreArgs() { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, Log = packageReferenceArgs.Logger, MachineWideSettings = new XPlatMachineWideSetting(), GlobalPackagesFolder = packageReferenceArgs.PackageDirectory, PreLoadedRequestProviders = providers, Sources = packageReferenceArgs.Sources?.ToList() }; // Generate Restore Requests. There will always be 1 request here since we are restoring for 1 project. var restoreRequests = await RestoreRunner.GetRequests(restoreContext); // Run restore without commit. This will always return 1 Result pair since we are restoring for 1 request. var restoreResult = await RestoreRunner.RunWithoutCommit(restoreRequests, restoreContext); return(restoreResult.Single()); } }
public static async Task <RestoreResult> RestoreProjectAsync( ISolutionManager solutionManager, BuildIntegratedNuGetProject project, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, Guid parentId, ILogger log, CancellationToken token) { if (project == null) { throw new ArgumentNullException(nameof(project)); } // Restore var specs = await project.GetPackageSpecsAsync(context); var spec = specs.Single(e => e.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference || e.RestoreMetadata.ProjectStyle == ProjectStyle.ProjectJson); // Do not restore global tools Project Style in VS. var result = await PreviewRestoreAsync( solutionManager, project, spec, context, providerCache, cacheContextModifier, sources, parentId, token); // Throw before writing if this has been canceled token.ThrowIfCancellationRequested(); // Write out the lock file and msbuild files var summary = await RestoreRunner.CommitAsync(result, token); RestoreSummary.Log(log, new[] { summary }); return(result.Result); }
private static async Task <RestoreResultPair> PreviewAddPackageReferenceAsync(PackageReferenceArgs packageReferenceArgs, DependencyGraphSpec dgSpec) { // Set user agent and connection settings. XPlatUtility.ConfigureProtocol(); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = false; cacheContext.IgnoreFailedSources = false; // Pre-loaded request provider containing the graph file var providers = new List <IPreLoadedRestoreRequestProvider> { new DependencyGraphSpecRequestProvider(providerCache, dgSpec) }; var restoreContext = new RestoreArgs() { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, Log = packageReferenceArgs.Logger, MachineWideSettings = new XPlatMachineWideSetting(), GlobalPackagesFolder = packageReferenceArgs.PackageDirectory, PreLoadedRequestProviders = providers, Sources = packageReferenceArgs.Sources?.ToList() }; // Generate Restore Requests. There will always be 1 request here since we are restoring for 1 project. var restoreRequests = await RestoreRunner.GetRequests(restoreContext); //Setup the Credential Service DefaultCredentialServiceUtility.SetupDefaultCredentialService(restoreContext.Log, !packageReferenceArgs.Interactive); // Run restore without commit. This will always return 1 Result pair since we are restoring for 1 request. var restoreResult = await RestoreRunner.RunWithoutCommit(restoreRequests, restoreContext); return(restoreResult.Single()); } }
internal static async Task LaunchCommandAsync(this GeneralCommandInfos command, ILogger logger, CancellationToken ct) { switch (command) { case CleanCommand clean: await CleanRunner.CleanAsync(clean, logger, ct); break; case RestoreCommand restoreCommand: await RestoreRunner.RestoreAsync(restoreCommand, logger, ct); break; case DropDatabaseCommand drop: await DropRunner.DropDatabasesAsync(drop, logger, ct); break; } }
/// <summary> /// Restore a solution and cache the dg spec to context. /// </summary> public static async Task <IReadOnlyList <RestoreSummary> > RestoreAsync( ISolutionManager solutionManager, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, Guid parentId, bool forceRestore, DependencyGraphSpec dgSpec, ILogger log, CancellationToken token) { // Check if there are actual projects to restore before running. if (dgSpec.Restore.Count > 0) { using (var sourceCacheContext = new SourceCacheContext()) { // Update cache context cacheContextModifier(sourceCacheContext); var restoreContext = GetRestoreContext( context, providerCache, sourceCacheContext, sources, dgSpec, parentId, forceRestore); var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, token); RestoreSummary.Log(log, restoreSummaries); await PersistDGSpec(dgSpec); return(restoreSummaries); } } return(new List <RestoreSummary>()); }
public override bool Execute() { var args = new RestoreArgs { Inputs = Inputs.Select(item => item.ItemSpec).ToList(), ConfigFile = ConfigFile, GlobalPackagesFolder = PackagesDir, Sources = new List <string>(), CacheContext = new SourceCacheContext(), RequestProviders = new List <IRestoreRequestProvider> { new WorkaroundProjectJsonRestoreRequestProvider(new RestoreCommandProvidersCache()) }, Log = new NugetMsBuildLogger(new TaskLoggingHelper(this)) }; RestoreRunner.RunAsync(args).Wait(); return(!Log.HasLoggedErrors); }
public async Task <int> ExecuteCommand(PackageReferenceArgs packageReferenceArgs, MSBuildAPIUtility msBuild) { packageReferenceArgs.Logger.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Info_AddPkgAddingReference, packageReferenceArgs.PackageDependency.Id, packageReferenceArgs.ProjectPath)); if (packageReferenceArgs.NoRestore) { packageReferenceArgs.Logger.LogWarning(string.Format(CultureInfo.CurrentCulture, Strings.Warn_AddPkgWithoutRestore)); var libraryDependency = new LibraryDependency { LibraryRange = new LibraryRange( name: packageReferenceArgs.PackageDependency.Id, versionRange: packageReferenceArgs.PackageDependency.VersionRange, typeConstraint: LibraryDependencyTarget.Package) }; msBuild.AddPackageReference(packageReferenceArgs.ProjectPath, libraryDependency); return(0); } // 1. Get project dg file packageReferenceArgs.Logger.LogDebug("Reading project Dependency Graph"); var dgSpec = ReadProjectDependencyGraph(packageReferenceArgs); if (dgSpec == null) { // Logging non localized error on debug stream. packageReferenceArgs.Logger.LogDebug(Strings.Error_NoDgSpec); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.Error_NoDgSpec)); } packageReferenceArgs.Logger.LogDebug("Project Dependency Graph Read"); var projectFullPath = Path.GetFullPath(packageReferenceArgs.ProjectPath); var matchingPackageSpecs = dgSpec .Projects .Where(p => p.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference && PathUtility.GetStringComparerBasedOnOS().Equals(Path.GetFullPath(p.RestoreMetadata.ProjectPath), projectFullPath)) .ToArray(); // This ensures that the DG specs generated in previous steps contain exactly 1 project with the same path as the project requesting add package. // Throw otherwise since we cannot proceed further. if (matchingPackageSpecs.Length != 1) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.Error_UnsupportedProject, packageReferenceArgs.PackageDependency.Id, packageReferenceArgs.ProjectPath)); } // Parse the user specified frameworks once to avoid re-do's var userSpecifiedFrameworks = Enumerable.Empty <NuGetFramework>(); if (packageReferenceArgs.Frameworks?.Any() == true) { userSpecifiedFrameworks = packageReferenceArgs .Frameworks .Select(f => NuGetFramework.Parse(f)); } var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); // Create a copy to avoid modifying the original spec which may be shared. var updatedPackageSpec = originalPackageSpec.Clone(); if (packageReferenceArgs.Frameworks?.Any() == true) { // If user specified frameworks then just use them to add the dependency PackageSpecOperations.AddOrUpdateDependency(updatedPackageSpec, packageReferenceArgs.PackageDependency, userSpecifiedFrameworks); } else { // If the user has not specified a framework, then just add it to all frameworks PackageSpecOperations.AddOrUpdateDependency(updatedPackageSpec, packageReferenceArgs.PackageDependency, updatedPackageSpec.TargetFrameworks.Select(e => e.FrameworkName)); } var updatedDgSpec = dgSpec.WithReplacedSpec(updatedPackageSpec).WithoutRestores(); updatedDgSpec.AddRestore(updatedPackageSpec.RestoreMetadata.ProjectUniqueName); // 2. Run Restore Preview packageReferenceArgs.Logger.LogDebug("Running Restore preview"); var restorePreviewResult = await PreviewAddPackageReferenceAsync(packageReferenceArgs, updatedDgSpec); packageReferenceArgs.Logger.LogDebug("Restore Review completed"); // 3. Process Restore Result var compatibleFrameworks = new HashSet <NuGetFramework>( restorePreviewResult .Result .CompatibilityCheckResults .Where(t => t.Success) .Select(t => t.Graph.Framework), new NuGetFrameworkFullComparer()); if (packageReferenceArgs.Frameworks?.Any() == true) { // If the user has specified frameworks then we intersect that with the compatible frameworks. var userSpecifiedFrameworkSet = new HashSet <NuGetFramework>( userSpecifiedFrameworks, new NuGetFrameworkFullComparer()); compatibleFrameworks.IntersectWith(userSpecifiedFrameworkSet); } // 4. Write to Project if (compatibleFrameworks.Count == 0) { // Package is compatible with none of the project TFMs // Do not add a package reference, throw appropriate error packageReferenceArgs.Logger.LogError(string.Format(CultureInfo.CurrentCulture, Strings.Error_AddPkgIncompatibleWithAllFrameworks, packageReferenceArgs.PackageDependency.Id, packageReferenceArgs.Frameworks?.Any() == true ? Strings.AddPkg_UserSpecified : Strings.AddPkg_All, packageReferenceArgs.ProjectPath)); return(1); } // Ignore the graphs with RID else if (compatibleFrameworks.Count == restorePreviewResult.Result.CompatibilityCheckResults.Where(r => string.IsNullOrEmpty(r.Graph.RuntimeIdentifier)).Count()) { // Package is compatible with all the project TFMs // Add an unconditional package reference to the project packageReferenceArgs.Logger.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Info_AddPkgCompatibleWithAllFrameworks, packageReferenceArgs.PackageDependency.Id, packageReferenceArgs.ProjectPath)); // generate a library dependency with all the metadata like Include, Exlude and SuppressParent var libraryDependency = GenerateLibraryDependency(updatedPackageSpec, packageReferenceArgs, restorePreviewResult, userSpecifiedFrameworks); msBuild.AddPackageReference(packageReferenceArgs.ProjectPath, libraryDependency); } else { // Package is compatible with some of the project TFMs // Add conditional package references to the project for the compatible TFMs packageReferenceArgs.Logger.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Info_AddPkgCompatibleWithSubsetFrameworks, packageReferenceArgs.PackageDependency.Id, packageReferenceArgs.ProjectPath)); var compatibleOriginalFrameworks = originalPackageSpec.RestoreMetadata .OriginalTargetFrameworks .Where(s => compatibleFrameworks.Contains(NuGetFramework.Parse(s))); // generate a library dependency with all the metadata like Include, Exlude and SuppressParent var libraryDependency = GenerateLibraryDependency(updatedPackageSpec, packageReferenceArgs, restorePreviewResult, userSpecifiedFrameworks); msBuild.AddPackageReferencePerTFM(packageReferenceArgs.ProjectPath, libraryDependency, compatibleOriginalFrameworks); } // 5. Commit restore result await RestoreRunner.CommitAsync(restorePreviewResult, CancellationToken.None); return(0); }
public static async Task <bool> RestoreAsync( DependencyGraphSpec dependencyGraphSpec, bool interactive, bool recursive, bool noCache, bool ignoreFailedSources, bool disableParallel, bool force, bool forceEvaluate, bool hideWarningsAndErrors, bool restorePC, bool cleanupAssetsForUnsupportedProjects, Common.ILogger log, CancellationToken cancellationToken) { if (dependencyGraphSpec == null) { throw new ArgumentNullException(nameof(dependencyGraphSpec)); } if (log == null) { throw new ArgumentNullException(nameof(log)); } try { DefaultCredentialServiceUtility.SetupDefaultCredentialService(log, !interactive); // Set connection limit NetworkProtocolUtility.SetConnectionLimit(); // Set user agent string used for network calls #if IS_CORECLR UserAgent.SetUserAgentString(new UserAgentStringBuilder("NuGet .NET Core MSBuild Task") .WithOSDescription(RuntimeInformation.OSDescription)); #else // OS description is set by default on Desktop UserAgent.SetUserAgentString(new UserAgentStringBuilder("NuGet Desktop MSBuild Task")); #endif X509TrustStore.InitializeForDotNetSdk(log); var restoreSummaries = new List <RestoreSummary>(); var providerCache = new RestoreCommandProvidersCache(); #if IS_DESKTOP if (restorePC && dependencyGraphSpec.Projects.Any(i => i.RestoreMetadata.ProjectStyle == ProjectStyle.PackagesConfig)) { var v2RestoreResult = await PerformNuGetV2RestoreAsync(log, dependencyGraphSpec, noCache, disableParallel, interactive); restoreSummaries.Add(v2RestoreResult); if (restoreSummaries.Count < 1) { var message = string.Format( Strings.InstallCommandNothingToInstall, "packages.config" ); log.LogMinimal(message); } if (!v2RestoreResult.Success) { v2RestoreResult .Errors .Where(l => l.Level == LogLevel.Warning) .ForEach(message => { log.LogWarning(message.Message); }); } } #endif using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = noCache; cacheContext.IgnoreFailedSources = ignoreFailedSources; // Pre-loaded request provider containing the graph file var providers = new List <IPreLoadedRestoreRequestProvider>(); if (dependencyGraphSpec.Restore.Count > 0) { // Add all child projects if (recursive) { AddAllProjectsForRestore(dependencyGraphSpec); } providers.Add(new DependencyGraphSpecRequestProvider(providerCache, dependencyGraphSpec)); var restoreContext = new RestoreArgs() { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, // 'dotnet restore' fails on slow machines (https://github.com/NuGet/Home/issues/6742) // The workaround is to pass the '--disable-parallel' option. // We apply the workaround by default when the system has 1 cpu. // This will fix restore failures on VMs with 1 CPU and containers with less or equal to 1 CPU assigned. DisableParallel = Environment.ProcessorCount == 1 ? true : disableParallel, Log = log, MachineWideSettings = new XPlatMachineWideSetting(), PreLoadedRequestProviders = providers, AllowNoOp = !force, HideWarningsAndErrors = hideWarningsAndErrors, RestoreForceEvaluate = forceEvaluate }; if (restoreContext.DisableParallel) { HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore(); } cancellationToken.ThrowIfCancellationRequested(); restoreSummaries.AddRange(await RestoreRunner.RunAsync(restoreContext, cancellationToken)); } if (cleanupAssetsForUnsupportedProjects) { // Restore assets are normally left on disk between restores for all projects. This can cause a condition where a project that supports PackageReference was restored // but then a user changes a branch or some other condition and now the project does not use PackageReference. Since the restore assets are left on disk, the build // consumes them which can cause build errors. The code below cleans up all of the files that we write so that they are not used during build Parallel.ForEach(dependencyGraphSpec.Projects.Where(i => !DoesProjectSupportRestore(i)), project => { if (project.RestoreMetadata == null || string.IsNullOrWhiteSpace(project.RestoreMetadata.OutputPath) || string.IsNullOrWhiteSpace(project.RestoreMetadata.ProjectPath)) { return; } // project.assets.json FileUtility.Delete(Path.Combine(project.RestoreMetadata.OutputPath, LockFileFormat.AssetsFileName)); // project.csproj.nuget.cache FileUtility.Delete(project.RestoreMetadata.CacheFilePath); // project.csproj.nuget.g.props FileUtility.Delete(BuildAssetsUtils.GetMSBuildFilePathForPackageReferenceStyleProject(project, BuildAssetsUtils.PropsExtension)); // project..csproj.nuget.g.targets FileUtility.Delete(BuildAssetsUtils.GetMSBuildFilePathForPackageReferenceStyleProject(project, BuildAssetsUtils.TargetsExtension)); // project.csproj.nuget.dgspec.json FileUtility.Delete(Path.Combine(project.RestoreMetadata.OutputPath, DependencyGraphSpec.GetDGSpecFileName(Path.GetFileName(project.RestoreMetadata.ProjectPath)))); }); } } if (restoreSummaries.Count < 1) { log.LogMinimal(Strings.NoProjectsToRestore); } else { RestoreSummary.Log(log, restoreSummaries); } return(restoreSummaries.All(x => x.Success)); } finally { // The CredentialService lifetime is for the duration of the process. We should not leave a potentially unavailable logger. // We need to update the delegating logger with a null instance // because the tear downs of the plugins and similar rely on idleness and process exit. DefaultCredentialServiceUtility.UpdateCredentialServiceDelegatingLogger(NullLogger.Instance); } }
private async Task <bool> ExecuteAsync(Common.ILogger log) { if (RestoreGraphItems.Length < 1 && !HideWarningsAndErrors) { log.LogWarning(Strings.NoProjectsProvidedToTask); return(true); } // Set user agent and connection settings. ConfigureProtocol(); // Convert to the internal wrapper var wrappedItems = RestoreGraphItems.Select(MSBuildUtility.WrapMSBuildItem); //var graphLines = RestoreGraphItems; var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = RestoreNoCache; cacheContext.IgnoreFailedSources = RestoreIgnoreFailedSources; // Pre-loaded request provider containing the graph file var providers = new List <IPreLoadedRestoreRequestProvider>(); var dgFile = MSBuildRestoreUtility.GetDependencySpec(wrappedItems); if (dgFile.Restore.Count < 1) { // Restore will fail if given no inputs, but here we should skip it and provide a friendly message. log.LogMinimal(Strings.NoProjectsToRestore); return(true); } // Add all child projects if (RestoreRecursive) { BuildTasksUtility.AddAllProjectsForRestore(dgFile); } providers.Add(new DependencyGraphSpecRequestProvider(providerCache, dgFile)); var restoreContext = new RestoreArgs() { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, DisableParallel = RestoreDisableParallel, Log = log, MachineWideSettings = new XPlatMachineWideSetting(), PreLoadedRequestProviders = providers, AllowNoOp = !RestoreForce, HideWarningsAndErrors = HideWarningsAndErrors, RestoreForceEvaluate = RestoreForceEvaluate }; // 'dotnet restore' fails on slow machines (https://github.com/NuGet/Home/issues/6742) // The workaround is to pass the '--disable-parallel' option. // We apply the workaround by default when the system has 1 cpu. // This will fix restore failures on VMs with 1 CPU and containers with less or equal to 1 CPU assigned. if (Environment.ProcessorCount == 1) { restoreContext.DisableParallel = true; } if (restoreContext.DisableParallel) { HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore(); } _cts.Token.ThrowIfCancellationRequested(); var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, _cts.Token); // Summary RestoreSummary.Log(log, restoreSummaries); return(restoreSummaries.All(x => x.Success)); } }
/// <summary> /// Determine if a file is v2 or v3 /// </summary> private void GetInputsFromFile(string projectFilePath, PackageRestoreInputs packageRestoreInputs) { // An argument was passed in. It might be a solution file or directory, // project file, or packages.config file var projectFileName = Path.GetFileName(projectFilePath); if (IsPackagesConfig(projectFileName)) { // restoring from packages.config or packages.projectname.config file packageRestoreInputs.PackagesConfigFiles.Add(projectFilePath); } else if (projectFileName.EndsWith("proj", StringComparison.OrdinalIgnoreCase)) { packageRestoreInputs.ProjectFiles.Add(projectFilePath); } else if (projectFileName.EndsWith(".dg", StringComparison.OrdinalIgnoreCase)) { packageRestoreInputs.RestoreV3Context.Inputs.Add(projectFilePath); } else if (projectFileName.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) { ProcessSolutionFile(projectFilePath, packageRestoreInputs); } else if (ProjectJsonPathUtilities.IsProjectConfig(projectFileName)) { // project.json is no longer allowed throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("Error_ProjectJsonNotAllowed"), projectFileName)); } else { // Not a file we know about. Try to be helpful without response. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, RestoreRunner.GetInvalidInputErrorMessage(projectFileName), projectFileName)); } }
public async Task RestoreRunner_BasicRestore_VerifyFailureWritesFiles() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""net45"": { ""dependencies"": { ""x"": ""1.0.0"" } } } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var logger = new TestLogger(); var lockPath = Path.Combine(project1.FullName, "project.lock.json"); var sourceRepos = sources.Select(source => Repository.Factory.GetCoreV3(source.Source)).ToList(); var packageX = new SimpleTestPackageContext() { Id = "x", Version = "1.0.0" }; packageX.AddFile("build/net45/x.targets"); var packageY = new SimpleTestPackageContext("y"); packageX.Dependencies.Add(packageY); var yPath = SimpleTestPackageUtility.CreateFullPackage(packageSource.FullName, packageY); SimpleTestPackageUtility.CreateFullPackage(packageSource.FullName, packageX); // y does not exist yPath.Delete(); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = packagesDir.FullName, Sources = new List <string>() { packageSource.FullName }, Inputs = new List <string>() { specPath1 }, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(sources)), RequestProviders = new List <IRestoreRequestProvider>() { new ProjectJsonRestoreRequestProvider(providerCache) } }; var targetsPath = Path.Combine(project1.FullName, "project1.nuget.targets"); var propsPath = Path.Combine(project1.FullName, "project1.nuget.props"); // Act var summaries = await RestoreRunner.Run(restoreContext); var summary = summaries.Single(); var targets = TargetsUtility.GetMSBuildPackageImports(targetsPath); // Assert Assert.False(summary.Success); Assert.True(File.Exists(lockPath), lockPath); Assert.True(File.Exists(targetsPath)); Assert.False(File.Exists(propsPath)); Assert.Equal(1, targets.Count); } } }
public static async Task <bool> RestoreAsync( DependencyGraphSpec dependencyGraphSpec, bool interactive, bool recursive, bool noCache, bool ignoreFailedSources, bool disableParallel, bool force, bool forceEvaluate, bool hideWarningsAndErrors, bool restorePC, Common.ILogger log, CancellationToken cancellationToken) { if (dependencyGraphSpec == null) { throw new ArgumentNullException(nameof(dependencyGraphSpec)); } if (log == null) { throw new ArgumentNullException(nameof(log)); } try { DefaultCredentialServiceUtility.SetupDefaultCredentialService(log, !interactive); // Set connection limit NetworkProtocolUtility.SetConnectionLimit(); // Set user agent string used for network calls #if IS_CORECLR UserAgent.SetUserAgentString(new UserAgentStringBuilder("NuGet .NET Core MSBuild Task") .WithOSDescription(RuntimeInformation.OSDescription)); #else // OS description is set by default on Desktop UserAgent.SetUserAgentString(new UserAgentStringBuilder("NuGet Desktop MSBuild Task")); #endif // This method has no effect on .NET Core. NetworkProtocolUtility.ConfigureSupportedSslProtocols(); var restoreSummaries = new List <RestoreSummary>(); var providerCache = new RestoreCommandProvidersCache(); #if IS_DESKTOP if (restorePC && dependencyGraphSpec.Projects.Any(i => i.RestoreMetadata.ProjectStyle == ProjectStyle.PackagesConfig)) { var v2RestoreResult = await PerformNuGetV2RestoreAsync(log, dependencyGraphSpec, noCache, disableParallel, interactive); restoreSummaries.Add(v2RestoreResult); if (restoreSummaries.Count < 1) { var message = string.Format( Strings.InstallCommandNothingToInstall, "packages.config" ); log.LogMinimal(message); } if (!v2RestoreResult.Success) { v2RestoreResult .Errors .Where(l => l.Level == LogLevel.Warning) .ForEach(message => { log.LogWarning(message.Message); }); } } #endif using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = noCache; cacheContext.IgnoreFailedSources = ignoreFailedSources; // Pre-loaded request provider containing the graph file var providers = new List <IPreLoadedRestoreRequestProvider>(); if (dependencyGraphSpec.Restore.Count > 0) { // Add all child projects if (recursive) { AddAllProjectsForRestore(dependencyGraphSpec); } providers.Add(new DependencyGraphSpecRequestProvider(providerCache, dependencyGraphSpec)); var restoreContext = new RestoreArgs() { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, // 'dotnet restore' fails on slow machines (https://github.com/NuGet/Home/issues/6742) // The workaround is to pass the '--disable-parallel' option. // We apply the workaround by default when the system has 1 cpu. // This will fix restore failures on VMs with 1 CPU and containers with less or equal to 1 CPU assigned. DisableParallel = Environment.ProcessorCount == 1 ? true : disableParallel, Log = log, MachineWideSettings = new XPlatMachineWideSetting(), PreLoadedRequestProviders = providers, AllowNoOp = !force, HideWarningsAndErrors = hideWarningsAndErrors, RestoreForceEvaluate = forceEvaluate }; if (restoreContext.DisableParallel) { HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore(); } cancellationToken.ThrowIfCancellationRequested(); restoreSummaries.AddRange(await RestoreRunner.RunAsync(restoreContext, cancellationToken)); } } if (restoreSummaries.Count < 1) { log.LogMinimal(Strings.NoProjectsToRestore); } else { RestoreSummary.Log(log, restoreSummaries); } return(restoreSummaries.All(x => x.Success)); } finally { // The CredentialService lifetime is for the duration of the process. We should not leave a potentially unavailable logger. // We need to update the delegating logger with a null instance // because the tear downs of the plugins and similar rely on idleness and process exit. DefaultCredentialServiceUtility.UpdateCredentialServiceDelegatingLogger(NullLogger.Instance); } }
public static async Task <(RestoreRequest, RestoreResult)> Restore(ILogger logger, string packageName, VersionRange versionRange) { var settings = NuGet.Configuration.Settings.LoadDefaultSettings(null); var packageSourceProvider = new PackageSourceProvider(settings); var installPath = SettingsUtility.GetGlobalPackagesFolder(settings); var assemblies = new List <string>(); var projectPath = Path.Combine("XenkoNugetResolver.json"); var spec = new PackageSpec() { Name = Path.GetFileNameWithoutExtension(projectPath), // make sure this package never collides with a dependency FilePath = projectPath, Dependencies = new List <LibraryDependency>() { new LibraryDependency { LibraryRange = new LibraryRange(packageName, versionRange, LibraryDependencyTarget.Package), } }, TargetFrameworks = { new TargetFrameworkInformation { FrameworkName = NuGetFramework.Parse("net48"), } }, RestoreMetadata = new ProjectRestoreMetadata { ProjectPath = projectPath, ProjectName = Path.GetFileNameWithoutExtension(projectPath), ProjectStyle = ProjectStyle.PackageReference, ProjectUniqueName = projectPath, OutputPath = Path.Combine(Path.GetTempPath(), $"XenkoNugetResolver-{packageName}-{versionRange.MinVersion.ToString()}"), OriginalTargetFrameworks = new[] { "net48" }, ConfigFilePaths = settings.GetConfigFilePaths(), PackagesPath = SettingsUtility.GetGlobalPackagesFolder(settings), Sources = SettingsUtility.GetEnabledSources(settings).ToList(), FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList() }, }; // remove all remote sources, so we don't have to worry about connectivity issues // we are only restoring local dev packages anyway for (int i = 0; i < spec.RestoreMetadata.Sources.Count; i++) { var s = spec.RestoreMetadata.Sources[i]; if (s.IsLocal == false) { spec.RestoreMetadata.Sources.RemoveAt(i); i--; } } using (var context = new SourceCacheContext()) { context.IgnoreFailedSources = true; var dependencyGraphSpec = new DependencyGraphSpec(); dependencyGraphSpec.AddProject(spec); dependencyGraphSpec.AddRestore(spec.RestoreMetadata.ProjectUniqueName); IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec); var restoreArgs = new RestoreArgs { AllowNoOp = true, CacheContext = context, CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings)), Log = logger, }; // Create requests from the arguments var requests = requestProvider.CreateRequests(restoreArgs).Result; // Restore the packages for (int tryCount = 0; tryCount < 2; ++tryCount) { try { var results = await RestoreRunner.RunWithoutCommit(requests, restoreArgs); // Commit results so that noop cache works next time foreach (var result in results) { await result.Result.CommitAsync(logger, CancellationToken.None); } var mainResult = results.First(); return(mainResult.SummaryRequest.Request, mainResult.Result); } catch (Exception e) when(e is UnauthorizedAccessException || e is IOException) { // If we have an unauthorized access exception, it means assemblies are locked by running Xenko process // During first try, kill some known harmless processes, and try again if (tryCount == 1) { throw; } foreach (var process in new[] { "Xenko.ConnectionRouter" }.SelectMany(Process.GetProcessesByName)) { try { if (process.Id != Process.GetCurrentProcess().Id) { process.Kill(); process.WaitForExit(); } } catch (Exception) { } } } } throw new InvalidOperationException("Unreachable code"); } }
public async Task RestoreRunner_BasicRestore_VerifyFailureWritesFiles_NETCore() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""net45"": { ""dependencies"": { ""x"": ""1.0.0"" } } } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); spec1.RestoreMetadata = new ProjectRestoreMetadata(); spec1.RestoreMetadata.OutputPath = Path.Combine(project1.FullName, "obj"); spec1.RestoreMetadata.ProjectStyle = ProjectStyle.PackageReference; spec1.RestoreMetadata.ProjectName = "project1"; spec1.RestoreMetadata.ProjectPath = Path.Combine(project1.FullName, "project1.csproj"); spec1.RestoreMetadata.ProjectUniqueName = spec1.RestoreMetadata.ProjectPath; spec1.RestoreMetadata.TargetFrameworks.Add(new ProjectRestoreMetadataFrameworkInfo(NuGetFramework.Parse("net45"))); spec1.RestoreMetadata.OriginalTargetFrameworks.Add("net45"); spec1.FilePath = spec1.RestoreMetadata.ProjectPath; var dgSpec = new DependencyGraphSpec(); dgSpec.AddProject(spec1); dgSpec.AddRestore(spec1.RestoreMetadata.ProjectUniqueName); var logger = new TestLogger(); var assetsPath = Path.Combine(project1.FullName, "obj", "project.assets.json"); var sourceRepos = sources.Select(source => Repository.Factory.GetCoreV3(source.Source)).ToList(); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = packagesDir.FullName, Sources = new List <string>() { packageSource.FullName }, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(sources)), PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>() { new DependencyGraphSpecRequestProvider(providerCache, dgSpec) } }; // Act var summaries = await RestoreRunner.Run(restoreContext); var summary = summaries.Single(); // Assert Assert.False(summary.Success); Assert.True(File.Exists(assetsPath), assetsPath); Assert.True(File.Exists(Path.Combine(project1.FullName, "obj", "project1.csproj.nuget.g.props"))); Assert.True(File.Exists(Path.Combine(project1.FullName, "obj", "project1.csproj.nuget.g.targets"))); } } }
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); } }
public async Task RestoreRunner_BasicRestoreWithConfigFile() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""net45"": { } } }"; var configFile = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <add key=""nuget.org"" value=""{0}"" /> </packageSources> </configuration> "; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); File.WriteAllText(Path.Combine(workingDir, "NuGet.Config"), String.Format(configFile, packageSource.FullName)); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var configPath = Path.Combine(workingDir, "NuGet.Config"); var logger = new TestLogger(); var lockPath = Path.Combine(project1.FullName, "project.lock.json"); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = packagesDir.FullName, ConfigFile = configPath, Inputs = new List <string>() { specPath1 }, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(new List <PackageSource>())), RequestProviders = new List <IRestoreRequestProvider>() { new ProjectJsonRestoreRequestProvider(providerCache) } }; // Act var summaries = await RestoreRunner.Run(restoreContext); var summary = summaries.Single(); // Assert Assert.True(summary.Success, "Failed: " + string.Join(Environment.NewLine, logger.Messages)); Assert.Equal(1, summary.FeedsUsed.Count); Assert.True(File.Exists(lockPath), lockPath); } } }
private async Task <bool> ExecuteAsync(Common.ILogger log) { if (RestoreGraphItems.Length < 1) { log.LogWarning(Strings.NoProjectsProvidedToTask); return(true); } // Set user agent and connection settings. ConfigureProtocol(); // Convert to the internal wrapper var wrappedItems = RestoreGraphItems.Select(MSBuildUtility.WrapMSBuildItem); //var graphLines = RestoreGraphItems; var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { cacheContext.NoCache = RestoreNoCache; cacheContext.IgnoreFailedSources = RestoreIgnoreFailedSources; // Pre-loaded request provider containing the graph file var providers = new List <IPreLoadedRestoreRequestProvider>(); var dgFile = MSBuildRestoreUtility.GetDependencySpec(wrappedItems); if (dgFile.Restore.Count < 1) { // Restore will fail if given no inputs, but here we should skip it and provide a friendly message. log.LogMinimal(Strings.NoProjectsToRestore); return(true); } // Add all child projects if (RestoreRecursive) { BuildTasksUtility.AddAllProjectsForRestore(dgFile); } providers.Add(new DependencyGraphSpecRequestProvider(providerCache, dgFile)); var defaultSettings = Settings.LoadDefaultSettings(root: null, configFileName: null, machineWideSettings: null); var sourceProvider = new CachingSourceProvider(new PackageSourceProvider(defaultSettings)); var restoreContext = new RestoreArgs() { CacheContext = cacheContext, LockFileVersion = LockFileFormat.Version, ConfigFile = MSBuildStringUtility.TrimAndGetNullForEmpty(RestoreConfigFile), DisableParallel = RestoreDisableParallel, GlobalPackagesFolder = RestorePackagesPath, Log = log, MachineWideSettings = new XPlatMachineWideSetting(), PreLoadedRequestProviders = providers, CachingSourceProvider = sourceProvider, AllowNoOp = !RestoreForce, HideWarningsAndErrors = HideWarningsAndErrors }; if (!string.IsNullOrEmpty(RestoreSources)) { var sources = MSBuildStringUtility.Split(RestoreSources); restoreContext.Sources.AddRange(sources); } if (restoreContext.DisableParallel) { HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore(); } _cts.Token.ThrowIfCancellationRequested(); var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, _cts.Token); // Summary RestoreSummary.Log(log, restoreSummaries); return(restoreSummaries.All(x => x.Success)); } }
public async Task RestoreRunner_RestoreWithExternalFile_NetCoreOutput() { // Arrange var sources = new List <PackageSource>(); var targetFrameworkInfo1 = new TargetFrameworkInformation(); targetFrameworkInfo1.FrameworkName = NuGetFramework.Parse("net45"); var frameworks1 = new[] { targetFrameworkInfo1 }; var targetFrameworkInfo2 = new TargetFrameworkInformation(); targetFrameworkInfo2.FrameworkName = NuGetFramework.Parse("net45"); var frameworks2 = new[] { targetFrameworkInfo2 }; // Create two net45 projects var spec1 = new PackageSpec(frameworks1); spec1.RestoreMetadata = new ProjectRestoreMetadata(); spec1.RestoreMetadata.ProjectUniqueName = "project1"; spec1.RestoreMetadata.ProjectName = "project1"; spec1.RestoreMetadata.ProjectStyle = ProjectStyle.PackageReference; spec1.RestoreMetadata.OriginalTargetFrameworks.Add("net45"); var spec2 = new PackageSpec(frameworks2); spec2.RestoreMetadata = new ProjectRestoreMetadata(); spec2.RestoreMetadata.ProjectUniqueName = "project2"; spec2.RestoreMetadata.ProjectName = "project2"; spec2.RestoreMetadata.ProjectStyle = ProjectStyle.PackageReference; spec2.RestoreMetadata.OriginalTargetFrameworks.Add("net45"); var specs = new[] { spec1, spec2 }; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); var project2 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2")); packagesDir.Create(); packageSource.Create(); project1.Create(); project2.Create(); sources.Add(new PackageSource(packageSource.FullName)); var projPath1 = Path.Combine(project1.FullName, "project1.csproj"); var projPath2 = Path.Combine(project2.FullName, "project2.csproj"); File.WriteAllText(projPath1, string.Empty); File.WriteAllText(projPath2, string.Empty); spec1.RestoreMetadata.ProjectPath = projPath1; spec1.FilePath = projPath1; spec1.Name = "project1"; spec2.RestoreMetadata.ProjectPath = projPath2; spec2.FilePath = projPath1; spec2.Name = "project2"; var logger = new TestLogger(); var objPath1 = Path.Combine(project1.FullName, "obj"); var objPath2 = Path.Combine(project2.FullName, "obj"); spec1.RestoreMetadata.OutputPath = objPath1; spec2.RestoreMetadata.OutputPath = objPath2; spec1.RestoreMetadata.OriginalTargetFrameworks.Add("net45"); spec2.RestoreMetadata.OriginalTargetFrameworks.Add("net45"); var lockPath1 = Path.Combine(objPath1, "project.assets.json"); var lockPath2 = Path.Combine(objPath2, "project.assets.json"); // Link projects spec1.TargetFrameworks.Single().Dependencies.Add(new LibraryDependency() { LibraryRange = new LibraryRange() { Name = "project2", TypeConstraint = LibraryDependencyTarget.ExternalProject } }); spec1.RestoreMetadata.TargetFrameworks.Add(new ProjectRestoreMetadataFrameworkInfo(NuGetFramework.Parse("net45"))); spec1.RestoreMetadata.TargetFrameworks .Single() .ProjectReferences .Add(new ProjectRestoreReference() { ProjectPath = projPath2, ProjectUniqueName = "project2" }); // Create dg file var dgFile = new DependencyGraphSpec(); foreach (var spec in specs) { dgFile.AddRestore(spec.RestoreMetadata.ProjectName); dgFile.AddProject(spec); } var dgPath = Path.Combine(workingDir, "input.dg"); dgFile.Save(dgPath); var sourceRepos = sources.Select(source => Repository.Factory.GetCoreV3(source.Source)).ToList(); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = packagesDir.FullName, Sources = new List <string>() { packageSource.FullName }, Inputs = new List <string>() { dgPath }, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(sources)), RequestProviders = new List <IRestoreRequestProvider>() { new DependencyGraphFileRequestProvider(providerCache) } }; // Act var summaries = await RestoreRunner.Run(restoreContext); var success = summaries.All(s => s.Success); var lockFormat = new LockFileFormat(); var lockFile1 = lockFormat.Read(lockPath1); var project2Lib = lockFile1.Libraries.First(); // Assert Assert.True(success, "Failed: " + string.Join(Environment.NewLine, logger.Messages)); Assert.True(File.Exists(lockPath1), lockPath1); Assert.True(File.Exists(lockPath2), lockPath2); Assert.Equal("project2", project2Lib.Name); } } }
public async Task InstallPackageFromAnotherProcessVerifyCacheIsClearedAsync() { // Arrange var logger = new TestLogger(); using (var cacheContext = new SourceCacheContext()) using (var pathContext = new SimpleTestPathContext()) { var tfi = new List <TargetFrameworkInformation> { new TargetFrameworkInformation() { FrameworkName = NuGetFramework.Parse("net462") } }; var spec = NETCoreRestoreTestUtility.GetProject(projectName: "projectA", framework: "net46"); spec.Dependencies.Add(new LibraryDependency() { LibraryRange = new LibraryRange("a", VersionRange.Parse("1.0.0"), LibraryDependencyTarget.Package) }); var project = NETCoreRestoreTestUtility.CreateProjectsFromSpecs(pathContext, spec).Single(); var packageA = new SimpleTestPackageContext("a"); await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packageA); // Create dg file var dgFile = new DependencyGraphSpec(); dgFile.AddProject(spec); dgFile.AddRestore(spec.RestoreMetadata.ProjectUniqueName); dgFile.Save(Path.Combine(pathContext.WorkingDirectory, "out.dg")); var providerCache = new RestoreCommandProvidersCache(); var sources = new List <string>() { pathContext.PackageSource }; var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = pathContext.UserPackagesFolder, Sources = sources, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(new List <PackageSource>() { new PackageSource(pathContext.PackageSource) })), PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>() { new DependencyGraphSpecRequestProvider(providerCache, dgFile) } }; var request = (await RestoreRunner.GetRequests(restoreContext)).Single(); var providers = providerCache.GetOrCreate(pathContext.UserPackagesFolder, sources, new List <SourceRepository>(), cacheContext, logger, false); var command = new RestoreCommand(request.Request); // Add to cache before install on all providers var globalPackages = providers.GlobalPackages; var packages = globalPackages.FindPackagesById("a"); packages.Should().BeEmpty("has not been installed yet"); foreach (var local in providers.LocalProviders) { await local.GetDependenciesAsync(new LibraryIdentity("a", NuGetVersion.Parse("1.0.0"), LibraryType.Package), NuGetFramework.Parse("net46"), cacheContext, logger, CancellationToken.None); } // Install the package without updating the cache await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.UserPackagesFolder, PackageSaveMode.Defaultv3, packageA); // Run restore using an incorrect cache var result = await command.ExecuteAsync(); // Verify a is in the output assets file result.Success.Should().BeTrue(); result.LockFile.GetLibrary("a", new NuGetVersion(1, 0, 0)).Should().NotBeNull(); } }
public async Task RestoreRunner_RestoreWithRuntime() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""net45"": { } } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var logger = new TestLogger(); var lockPath1 = Path.Combine(project1.FullName, "project.lock.json"); var sourceRepos = sources.Select(source => Repository.Factory.GetCoreV3(source.Source)).ToList(); var providerCache = new RestoreCommandProvidersCache(); using (var cacheContext = new SourceCacheContext()) { var restoreContext = new RestoreArgs() { CacheContext = cacheContext, DisableParallel = true, GlobalPackagesFolder = packagesDir.FullName, Sources = new List <string>() { packageSource.FullName }, Inputs = new List <string>() { specPath1 }, Log = logger, CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(sources)), RequestProviders = new List <IRestoreRequestProvider>() { new ProjectJsonRestoreRequestProvider(providerCache) } }; restoreContext.Runtimes.Add("linux-x86"); // Act var summaries = await RestoreRunner.Run(restoreContext); var success = summaries.All(s => s.Success); var lockFormat = new LockFileFormat(); var lockFile = lockFormat.Read(lockPath1); // Assert Assert.True(success, "Failed: " + string.Join(Environment.NewLine, logger.Messages)); Assert.True(lockFile.Targets.Any(graph => graph.RuntimeIdentifier == "linux-x86")); } } }