Exemplo n.º 1
0
        /// <summary>
        /// Restore a build integrated project and update the lock file
        /// </summary>
        public static async Task <RestoreResult> RestoreAsync(
            BuildIntegratedNuGetProject project,
            ExternalProjectReferenceContext context,
            IEnumerable <SourceRepository> sources,
            string effectiveGlobalPackagesFolder,
            Action <SourceCacheContext> cacheContextModifier,
            CancellationToken token)
        {
            using (var cacheContext = new SourceCacheContext())
            {
                cacheContextModifier(cacheContext);

                var providers = RestoreCommandProviders.Create(effectiveGlobalPackagesFolder,
                                                               sources,
                                                               cacheContext,
                                                               context.Logger);

                // Restore
                var result = await RestoreAsync(
                    project,
                    project.PackageSpec,
                    context,
                    providers,
                    token);

                // Throw before writing if this has been canceled
                token.ThrowIfCancellationRequested();

                // Write out the lock file and msbuild files
                await result.CommitAsync(context.Logger, token);

                return(result);
            }
        }
Exemplo n.º 2
0
 public TestRestoreRequest(
     PackageSpec project,
     IEnumerable <SourceRepository> sources,
     string packagesDirectory,
     IEnumerable <string> fallbackPackageFolders,
     SourceCacheContext cacheContext,
     ClientPolicyContext clientPolicyContext,
     ILogger log) : base(
         project,
         RestoreCommandProviders.Create(
             packagesDirectory,
             fallbackPackageFolderPaths: fallbackPackageFolders,
             sources: sources,
             cacheContext: cacheContext,
             packageFileCache: new LocalPackageFileCache(),
             log: log),
         cacheContext,
         clientPolicyContext,
         log)
 {
     // We need the dependency graph spec to go through the proper no-op code paths
     DependencyGraphSpec = new DependencyGraphSpec();
     DependencyGraphSpec.AddProject(project);
     DependencyGraphSpec.AddRestore(project.RestoreMetadata.ProjectUniqueName);
     AllowNoOp = true;
 }
Exemplo n.º 3
0
        public async Task <NuGetRestoreInfo> ExecuteAsync(CancellationToken cancellationToken = default)
        {
            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(tempPath);
            try
            {
                using var cacheContext = new SourceCacheContext();

                _packageSpec.RestoreMetadata = new ProjectRestoreMetadata
                {
                    OutputPath     = tempPath,
                    ProjectName    = _packageSpec.Name,
                    ProjectStyle   = ProjectStyle.PackageReference,
                    CrossTargeting = true
                };

                var settings = Settings.LoadDefaultSettings(tempPath);

                var logger = new NuGetLogger(_logger);

                var dependencyProviders = RestoreCommandProviders.Create(
                    SettingsUtility.GetGlobalPackagesFolder(settings),
                    Enumerable.Empty <string>(),
                    SettingsUtility.GetEnabledSources(settings).Select(source =>
                                                                       Repository.Factory.GetCoreV3(source.Source)),
                    cacheContext,
                    new LocalPackageFileCache(),
                    logger);

                var clientPolicyContext  = ClientPolicyContext.GetClientPolicy(settings, logger);
                var packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(settings);

                var restoreRequest = new RestoreRequest(_packageSpec, dependencyProviders, cacheContext,
                                                        clientPolicyContext, packageSourceMapping, logger, new LockFileBuilderCache())
                {
                    ProjectStyle = ProjectStyle.PackageReference, RestoreOutputPath = tempPath
                };

                var restoreCommand = new RestoreCommand(restoreRequest);

                var result = await restoreCommand.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                if (!result.Success)
                {
                    throw new NuGetRestoreException(result);
                }

                return(new NuGetRestoreInfo(dependencyProviders, result));
            }
            finally
            {
                Directory.Delete(tempPath, true);
            }
        }
        public async IAsyncEnumerable <MetadataReference> Generate([EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(tempPath);
            try
            {
                using var cacheContext = new SourceCacheContext();

                _packageSpec.RestoreMetadata = new ProjectRestoreMetadata
                {
                    OutputPath  = tempPath,
                    ProjectName = _packageSpec.Name
                };

                var settings = Settings.LoadDefaultSettings(tempPath);

                var logger = new NuGetLogger(_logger);

                var dependencyProviders = RestoreCommandProviders.Create(
                    SettingsUtility.GetGlobalPackagesFolder(settings),
                    Enumerable.Empty <string>(),
                    SettingsUtility.GetEnabledSources(settings).Select(source =>
                                                                       Repository.Factory.GetCoreV3(source.Source)),
                    cacheContext,
                    new LocalPackageFileCache(),
                    logger);

                var restoreRequest = new RestoreRequest(_packageSpec, dependencyProviders, cacheContext, null, logger)
                {
                    ProjectStyle      = ProjectStyle.PackageReference,
                    RestoreOutputPath = tempPath
                };

                var restoreCommand = new RestoreCommand(restoreRequest);

                var result = await restoreCommand.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                if (!result.Success)
                {
                    throw new NuGetRestoreException(result);
                }

                var dependencies = ExtractDependencies(result.LockFile);
                foreach (string dependency in dependencies)
                {
                    yield return(MetadataReference.CreateFromFile(dependency));
                }
            }
            finally
            {
                Directory.Delete(tempPath, true);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Restore without writing the lock file
        /// </summary>
        internal static async Task <RestoreResult> RestoreAsync(
            BuildIntegratedNuGetProject project,
            PackageSpec packageSpec,
            ExternalProjectReferenceContext context,
            RestoreCommandProviders providers,
            CancellationToken token)
        {
            // Restoring packages
            var logger = context.Logger;

            logger.LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                            Strings.BuildIntegratedPackageRestoreStarted,
                                            project.ProjectName));

            using (var request = new RestoreRequest(packageSpec, providers, logger, disposeProviders: false))
            {
                request.MaxDegreeOfConcurrency = PackageManagementConstants.DefaultMaxDegreeOfParallelism;
                request.LockFileVersion        = await GetLockFileVersion(project, context);

                // Add the existing lock file if it exists
                var lockFilePath = ProjectJsonPathUtilities.GetLockFilePath(project.JsonConfigPath);
                request.LockFilePath     = lockFilePath;
                request.ExistingLockFile = LockFileUtilities.GetLockFile(lockFilePath, logger);

                // Find the full closure of project.json files and referenced projects
                var projectReferences = await project.GetProjectReferenceClosureAsync(context);

                request.ExternalProjects = projectReferences.ToList();

                token.ThrowIfCancellationRequested();

                var command = new RestoreCommand(request);

                // Execute the restore
                var result = await command.ExecuteAsync(token);

                // Report a final message with the Success result
                if (result.Success)
                {
                    logger.LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                    Strings.BuildIntegratedPackageRestoreSucceeded,
                                                    project.ProjectName));
                }
                else
                {
                    logger.LogMinimal(string.Format(CultureInfo.CurrentCulture,
                                                    Strings.BuildIntegratedPackageRestoreFailed,
                                                    project.ProjectName));
                }

                return(result);
            }
        }
Exemplo n.º 6
0
 public TestRestoreRequest(
     PackageSpec project,
     IEnumerable <SourceRepository> sources,
     string packagesDirectory,
     IEnumerable <string> fallbackPackageFolders,
     SourceCacheContext cacheContext,
     ILogger log) : base(
         project,
         RestoreCommandProviders.Create(
             packagesDirectory,
             fallbackPackageFolderPaths: fallbackPackageFolders,
             sources: sources,
             cacheContext: cacheContext,
             log: log),
         cacheContext,
         log)
 {
 }
Exemplo n.º 7
0
 public TestRestoreRequest(
     PackageSpec project,
     IEnumerable <PackageSource> sources,
     string packagesDirectory,
     SourceCacheContext cacheContext,
     ClientPolicyContext clientPolicyContext,
     ILogger log) : base(
         project,
         RestoreCommandProviders.Create(
             packagesDirectory,
             fallbackPackageFolderPaths: new List <string>(),
             sources: sources.Select(source => Repository.Factory.GetCoreV3(source.Source)),
             cacheContext: cacheContext,
             packageFileCache: new LocalPackageFileCache(),
             log: log),
         cacheContext,
         clientPolicyContext,
         log)
 {
 }
Exemplo n.º 8
0
 public TestRestoreRequest(
     PackageSpec project,
     IEnumerable <PackageSource> sources,
     string packagesDirectory,
     SourceCacheContext cacheContext,
     PackageSourceMapping packageSourceMappingConfiguration,
     ILogger log) : base(
         project,
         RestoreCommandProviders.Create(
             packagesDirectory,
             Enumerable.Empty <string>(),
             sources: sources.Select(source => Repository.Factory.GetCoreV3(source.Source)),
             cacheContext: cacheContext,
             packageFileCache: new LocalPackageFileCache(),
             log: log),
         cacheContext,
         ClientPolicyContext.GetClientPolicy(NullSettings.Instance, log),
         packageSourceMappingConfiguration,
         log,
         new LockFileBuilderCache())
 {
 }
Exemplo n.º 9
0
        // Collect C# source generators from the direct NuGet dependencies (ignores transitive dependencies)
        public List <ISourceGenerator> GetSourceGenerators(RestoreCommandProviders dependencyProviders, RestoreResult result,
                                                           NuGetFramework targetFramework, AssemblyLoadContext assemblyLoadContext)
        {
            var generators = new List <ISourceGenerator>();

            LockFileTarget lockFileTarget = result.LockFile.Targets
                                            .First(p => p.TargetFramework == targetFramework);

            foreach (var directDependency in _packageSpec.Dependencies
                     .Concat(_packageSpec.TargetFrameworks
                             .Where(p => p.FrameworkName == targetFramework)
                             .SelectMany(framework => framework.Dependencies)))
            {
                // Get the exact version we restored
                var version = lockFileTarget.Libraries
                              .FirstOrDefault(p => string.Equals(p.Name, directDependency.Name, StringComparison.OrdinalIgnoreCase))?
                              .Version;
                if (version is not null)
                {
                    NuGet.Repositories.LocalPackageInfo localPackageInfo =
                        dependencyProviders.GlobalPackages.FindPackage(directDependency.Name, version);

                    // For now, we explicitly only handle Roslyn 4.0 analyzers or unversioned analyzers
                    // The regex also excludes resource assemblies in nested directories
                    foreach (Match file in localPackageInfo.Files
                             .Select(p => Regex.Match(p, @"^(analyzers/dotnet/(?:roslyn4\.0/)?cs/[^/]+\.dll$)"))
                             .Where(p => p.Success))
                    {
                        generators.AddRange(GetSourceGenerators(
                                                Path.Join(localPackageInfo.ExpandedPath, file.Groups[1].Value),
                                                assemblyLoadContext));
                    }
                }
            }

            return(generators);
        }
Exemplo n.º 10
0
 public NuGetRestoreInfo(RestoreCommandProviders providers, RestoreResult result)
 {
     Providers = providers;
     Result    = result;
 }
Exemplo n.º 11
0
        /// <summary>
        /// Fetch, if not already downloaded, and install the package represented by
        /// (<paramref name="packageId"/>, <paramref name="version"/>).
        /// </summary>
        /// <remarks>It is safe to call it concurrently be cause we operations are done using the FileLock.</remarks>
        /// <param name="packageId">Name of package to install.</param>
        /// <param name="version">Version of package to install.</param>
        public async Task <NugetLocalPackage> InstallPackage(string packageId, PackageVersion version, ProgressReport progress)
        {
            using (GetLocalRepositoryLock())
            {
                currentProgressReport = progress;
                try
                {
                    var identity = new PackageIdentity(packageId, version.ToNuGetVersion());

                    var resolutionContext = new ResolutionContext(
                        DependencyBehavior.Lowest,
                        true,
                        true,
                        VersionConstraints.None);

                    var repositories = PackageSources.Select(sourceRepositoryProvider.CreateRepository).ToArray();

                    var projectContext = new EmptyNuGetProjectContext()
                    {
                        ActionType = NuGetActionType.Install,
                        PackageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv3, XmlDocFileSaveMode.Skip, null, NativeLogger),
                    };

                    ActivityCorrelationId.StartNew();

                    {
                        var installPath = SettingsUtility.GetGlobalPackagesFolder(settings);

                        // Old version expects to be installed in GamePackages
                        if (packageId == "Xenko" && version < new PackageVersion(3, 0, 0, 0) && oldRootDirectory != null)
                        {
                            installPath = oldRootDirectory;
                        }

                        var specPath = Path.Combine("TestProject", "project.json");
                        var spec     = new PackageSpec()
                        {
                            Name         = "TestProject", // make sure this package never collides with a dependency
                            FilePath     = specPath,
                            Dependencies = new List <LibraryDependency>()
                            {
                                new LibraryDependency
                                {
                                    LibraryRange = new LibraryRange(packageId, new VersionRange(version.ToNuGetVersion()), LibraryDependencyTarget.Package),
                                }
                            },
                            TargetFrameworks =
                            {
                                new TargetFrameworkInformation
                                {
                                    FrameworkName = NuGetFramework.Parse("net472"),
                                }
                            },
                        };

                        using (var context = new SourceCacheContext())
                        {
                            context.IgnoreFailedSources = true;

                            var provider = RestoreCommandProviders.Create(installPath, new List <string>(), sourceRepositoryProvider.GetRepositories(), context, new LocalPackageFileCache(), NativeLogger);
                            var request  = new RestoreRequest(spec, provider, context, null, NativeLogger)
                            {
                                //RequestedRuntimes = { "win7-d3d11" },
                                ProjectStyle = ProjectStyle.DotnetCliTool,
                            };

                            var command = new RestoreCommand(request);

                            // Act
                            var result = await command.ExecuteAsync();

                            if (!result.Success)
                            {
                                throw new InvalidOperationException($"Could not restore package {packageId}");
                            }
                            foreach (var install in result.RestoreGraphs.Last().Install)
                            {
                                var package = result.LockFile.Libraries.FirstOrDefault(x => x.Name == install.Library.Name && x.Version == install.Library.Version);
                                if (package != null)
                                {
                                    var packagePath = Path.Combine(installPath, package.Path);
                                    OnPackageInstalled(this, new PackageOperationEventArgs(new PackageName(install.Library.Name, install.Library.Version.ToPackageVersion()), packagePath));
                                }
                            }
                        }

                        if (packageId == "Xenko" && version < new PackageVersion(3, 0, 0, 0))
                        {
                            UpdateTargetsHelper();
                        }
                    }

                    // Load the recently installed package
                    var installedPackages = GetPackagesInstalled(new[] { packageId });
                    return(installedPackages.FirstOrDefault(p => p.Version == version));
                }
                finally
                {
                    currentProgressReport = null;
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates new instance of <see cref="BoundRestoreCommandUser"/> with given parameters.
        /// </summary>
        /// <param name="nugetSettings">The settings to use.</param>
        /// <param name="thisFramework">The framework to bind to.</param>
        /// <param name="runtimeIdentifier">The runtime identifier. Will be used by <see cref="E_NuGetUtils.ExtractAssemblyPaths{TResult}(BoundRestoreCommandUser, LockFile, Func{String, IEnumerable{String}, TResult}, GetFileItemsDelegate, IEnumerable{String})"/> method.</param>
        /// <param name="runtimeGraph">Optional value indicating runtime graph information: either <see cref="global::NuGet.RuntimeModel.RuntimeGraph"/> directly, or <see cref="String"/> containing package ID of package holding <c>runtime.json</c> file, containing serialized runtime graph definition. If neither is specified, then <c>"Microsoft.NETCore.Platforms"</c> package ID used to locate <c>runtime.json</c> file, as per <see href="https://docs.microsoft.com/en-us/dotnet/core/rid-catalog">official documentation</see>.</param>
        /// <param name="nugetLogger">The logger to use in restore command.</param>
        /// <param name="sourceCacheContext">The optional <see cref="SourceCacheContext"/> to use.</param>
        /// <param name="nuspecCache">The optional <see cref="LocalPackageFileCache"/> to use.</param>
        /// <param name="clientPolicyContext">The optional <see cref="ClientPolicyContext"/> to use.</param>
        /// <param name="leaveSourceCacheOpen">Whether to leave the <paramref name="sourceCacheContext"/> open when disposing this <see cref="BoundRestoreCommandUser"/>.</param>
        /// <param name="lockFileCacheDir">The directory where to store serialized lock files returned by <see cref="RestoreIfNeeded"/>. If <c>null</c> or empty, then <paramref name="lockFileCacheEnvironmentVariableName"/> will be used. Set <paramref name="disableLockFileCacheDir"/> to true to disable caching lock files to file system.</param>
        /// <param name="lockFileCacheEnvironmentVariableName">The name of the environment variable containing the value for lock file cache directory. If <c>null</c> or empty, then environment variable reading will be skipped. If the environment variable itself is <c>null</c> or empty, then the user's home directory in conjunction with <paramref name="getDefaultLockFileCacheDir"/> will be used to deduce lock file cache directory. Set <paramref name="disableLockFileCacheDir"/> to true to disable caching lock files to file system.</param>
        /// <param name="getDefaultLockFileCacheDir">This callback will be used when <paramref name="lockFileCacheEnvironmentVariableName"/> is <c>null</c> or empty or when the named environment variable itself was <c>null</c> or empty. This callback will receive current user's home directory as parameter and should return the lock file cache directory. If <c>null</c>, then <see cref="GetDefaultLockFileDir"/> will be used. Set <paramref name="disableLockFileCacheDir"/> to true to disable caching lock files to file system.</param>
        /// <param name="disableLockFileCacheDir">This variable controls whether the results of <see cref="RestoreIfNeeded"/> will be stored to file system lock file cache directory. By default, the lock file caching is enabled. Set this parameter to <c>true</c> to completely disable caching lock files to file system.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="nugetSettings"/> is <c>null</c>.</exception>
        public BoundRestoreCommandUser(
            ISettings nugetSettings,
            NuGetFramework thisFramework = null,
            String runtimeIdentifier     = null,
            EitherOr <RuntimeGraph, String> runtimeGraph = default,
            ILogger nugetLogger = null,
            SourceCacheContext sourceCacheContext   = null,
            LocalPackageFileCache nuspecCache       = null,
            ClientPolicyContext clientPolicyContext = null,
            Boolean leaveSourceCacheOpen            = false,
            String lockFileCacheDir = null,
            String lockFileCacheEnvironmentVariableName      = DEFAULT_LOCK_FILE_CACHE_DIR_ENV_NAME,
            Func <String, String> getDefaultLockFileCacheDir = null,
            Boolean disableLockFileCacheDir = false
            )
        {
            ArgumentValidator.ValidateNotNull(nameof(nugetSettings), nugetSettings);
            this.ThisFramework = thisFramework ?? NuGetUtility.TryAutoDetectThisProcessFramework();
            if (nugetLogger == null)
            {
                nugetLogger = NullLogger.Instance;
            }

            var global    = SettingsUtility.GetGlobalPackagesFolder(nugetSettings);
            var fallbacks = SettingsUtility.GetFallbackPackageFolders(nugetSettings);

            if (sourceCacheContext == null)
            {
                leaveSourceCacheOpen = false;
            }
            var ctx = sourceCacheContext ?? new SourceCacheContext();
            var psp = new PackageSourceProvider(nugetSettings);
            var csp = new CachingSourceProvider(psp);

            this.RuntimeIdentifier          = NuGetUtility.TryAutoDetectThisProcessRuntimeIdentifier(runtimeIdentifier);
            this._cacheContext              = ctx;
            this._disposeSourceCacheContext = !leaveSourceCacheOpen;

            this.NuGetLogger             = nugetLogger;
            this._restoreCommandProvider = RestoreCommandProviders.Create(
                global,
                fallbacks,
                new PackageSourceProvider(nugetSettings).LoadPackageSources().Where(s => s.IsEnabled).Select(s => csp.CreateRepository(s)),
                ctx,
                nuspecCache ?? new LocalPackageFileCache(),
                nugetLogger
                );
            this._nugetRestoreRootDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            this._restoreTargetFW     = new TargetFrameworkInformation()
            {
                FrameworkName = this.ThisFramework
            };
            this.LocalRepositories = this._restoreCommandProvider.GlobalPackages.Singleton()
                                     .Concat(this._restoreCommandProvider.FallbackPackageFolders)
                                     .ToImmutableDictionary(r => r.RepositoryRoot, r => r);
            this.RuntimeGraph = new Lazy <RuntimeGraph>(() =>
            {
                var rGraph = runtimeGraph.GetFirstOrDefault();
                if (rGraph == null)
                {
                    var packageName = runtimeGraph.GetSecondOrDefault();
                    if (String.IsNullOrEmpty(packageName))
                    {
                        packageName = DEFAULT_RUNTIME_GRAPH_PACKAGE_ID;
                    }
                    var platformsPackagePath = this.LocalRepositories.Values
                                               .SelectMany(r => r.FindPackagesById(packageName))
                                               .OrderByDescending(p => p.Version)
                                               .FirstOrDefault()
                                               ?.ExpandedPath;
                    rGraph = String.IsNullOrEmpty(platformsPackagePath) ?
                             null :
                             JsonRuntimeFormat.ReadRuntimeGraph(Path.Combine(platformsPackagePath, global::NuGet.RuntimeModel.RuntimeGraph.RuntimeGraphFileName));
                }
                return(rGraph);
            }, LazyThreadSafetyMode.ExecutionAndPublication);

            if (!disableLockFileCacheDir)
            {
                this.DiskCacheDirectory = lockFileCacheDir
                                          .OrIfNullOrEmpty(String.IsNullOrEmpty(lockFileCacheEnvironmentVariableName) ? null : Environment.GetEnvironmentVariable(lockFileCacheEnvironmentVariableName))
                                          .OrIfNullOrEmpty((getDefaultLockFileCacheDir ?? GetDefaultLockFileDir)(Environment.GetEnvironmentVariable(
#if NET46
                                                                                                                     Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32S || Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.WinCE
#else
                                                                                                                     System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)
#endif
                  ? "USERPROFILE" : "HOME"))
                                                           )
                                          .OrIfNullOrEmpty(null);
            }
            this._allLockFiles        = new ConcurrentDictionary <ImmutableSortedSet <String>, ImmutableDictionary <ImmutableArray <NuGetVersion>, String> >();
            this._lockFileFormat      = new LockFileFormat();
            this._clientPolicyContext = clientPolicyContext ?? ClientPolicyContext.GetClientPolicy(nugetSettings, nugetLogger);
        }