public void TestDetectingNewestFramework() { var autoDetectedFW = NuGetUtility.TryAutoDetectThisProcessFramework(); var specifiedFW = NuGetFramework.ParseFolder(Environment.GetEnvironmentVariable("THIS_TFM")); //XDocument csProj; //using ( var fs = File.Open( Path.GetFullPath( Path.Combine( Environment.GetEnvironmentVariable( "GIT_DIR" ), "Source", "Tests", "Tests.NuGetUtils.Lib.Common", "Tests.NuGetUtils.Lib.Common.csproj" ) ), FileMode.Open, FileAccess.Read, FileShare.Read ) ) //{ // csProj = await XDocument.LoadAsync( fs, LoadOptions.None, default ); //} //var csProjFW = NuGetFramework.ParseFolder( csProj.XPathSelectElement( "/Project/PropertyGroup/TargetFramework" ).Value ); Assert.AreEqual(specifiedFW, autoDetectedFW, "CSProj and process frameworks must match."); }
/// <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); }