/// <summary> /// Given this <see cref="NuGetUsageConfiguration{TLogLevel}"/>, creates a new instance of <see cref="BoundRestoreCommandUser"/> utilizing the properties of <see cref="NuGetUsageConfiguration{TLogLevel}"/>, and then calls given <paramref name="callback"/>. /// </summary> /// <typeparam name="TResult">The return type of given <paramref name="callback"/>.</typeparam> /// <param name="configuration">This <see cref="NuGetUsageConfiguration{TLogLevel}"/>.</param> /// <param name="nugetSettingsPath">The object specifying what to pass as first parameter <see cref="NuGetUtility.GetNuGetSettingsWithDefaultRootDirectory"/>: if <see cref="String"/>, then it is passed directly as is, otherwise when it is <see cref="Type"/>, the <see cref="Assembly.CodeBase"/> of the <see cref="Assembly"/> holding the given <see cref="Type"/> is used to extract directory, and that directory is then passed on to <see cref="NuGetUtility.GetNuGetSettingsWithDefaultRootDirectory"/> method.</param> /// <param name="lockFileCacheDirEnvName">The environment name of the variable holding default lock file cache directory.</param> /// <param name="lockFileCacheDirWithinHomeDir">The directory name within home directory of current user which can be used as lock file cache directory.</param> /// <param name="callback">The callback to use created <see cref="BoundRestoreCommandUser"/>. The parameter contains <see cref="BoundRestoreCommandUser"/> as first tuple component, the SDK package ID deduced using <see cref="BoundRestoreCommandUser.ThisFramework"/> and <see cref="NuGetUsageConfiguration{TLogLevel}.SDKFrameworkPackageID"/> as second tuple component, and the SDK package version deduced using <see cref="BoundRestoreCommandUser.ThisFramework"/>, SDK package ID, and <see cref="NuGetUsageConfiguration{TLogLevel}.SDKFrameworkPackageVersion"/> as third tuple component.</param> /// <param name="loggerFactory">The callback to create <see cref="ILogger"/> for the <see cref="BoundRestoreCommandUser"/>. May be <c>null</c>.</param> /// <returns>The return value of <paramref name="callback"/>.</returns> /// <exception cref="NullReferenceException">If this <see cref="NuGetUsageConfiguration{TLogLevel}"/> is <c>null</c>.</exception> public static TResult CreateAndUseRestorerAsync <TResult>( this NuGetUsageConfiguration <LogLevel> configuration, EitherOr <String, Type> nugetSettingsPath, String lockFileCacheDirEnvName, String lockFileCacheDirWithinHomeDir, Func <(BoundRestoreCommandUser Restorer, String SDKPackageID, String SDKPackageVersion), TResult> callback,
/// <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); }