Skip to content

Commit 01a8964

Browse files
authored
allow overwriting nuget cache location (#591)
* safely handle if 'localappdata' folder is not specified * allow overriding nuget cache location * add custom exception for failed cache directory creation
1 parent 2262234 commit 01a8964

3 files changed

Lines changed: 59 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Note: Depending on the size and number of packages you need to install, the `Res
152152
153153
If you are interested in the process NuGetForUnity follows or you are trying to debug an issue, you can force NuGetForUnity to use verbose logging to output an increased amount of data to the Unity console. Add the line `<add key="verbose" value="true" />` to the `<config>` element in the _NuGet.config_ file. You can disable verbose logging by either setting the value to false or completely deleting the line.
154154
155-
The _.nupkg_ files downloaded from the NuGet server are cached locally in the current user's Application Data folder. (`C:\Users\[username]\AppData\Local\NuGet\Cache`). Packages previously installed are installed via the cache folder instead of downloading it from the server again.
155+
The _.nupkg_ files downloaded from the NuGet server are cached locally in the current user's Application Data folder `%localappdata%\NuGet\Cache` (`C:\Users\[username]\AppData\Local\NuGet\Cache`). The cache location can be overwrtten by setting the `NuGetCachePath` environment variable. Packages previously installed are installed via the cache folder instead of downloading it from the server again.
156156
157157
# Advanced settings
158158

src/NuGetForUnity/Editor/Helper/CredentialProviderHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,14 @@ private static IEnumerable<string> GetEnvironmentCredentialProviderPaths()
194194

195195
private static string GetDefaultCredentialProvidersPath()
196196
{
197-
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Nuget", "CredentialProviders");
197+
var baseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
198+
if (string.IsNullOrEmpty(baseDirectory))
199+
{
200+
// we need a place to store the credential provider so we fallback to the temp location.
201+
baseDirectory = Path.GetTempPath();
202+
}
203+
204+
return Path.Combine(baseDirectory, "Nuget", "CredentialProviders");
198205
}
199206

200207
private static bool DownloadCredentialProviders([NotNull] Uri feedUri)

src/NuGetForUnity/Editor/PackageCacheManager.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
1-
using System;
1+
#pragma warning disable SA1512,SA1124 // Single-line comments should not be followed by blank line
2+
3+
using System;
24
using System.IO;
35
using JetBrains.Annotations;
46
using NugetForUnity.Configuration;
57
using NugetForUnity.Models;
68
using NugetForUnity.PackageSource;
79

10+
#region No ReShaper
11+
12+
// ReSharper disable All
13+
// needed because 'JetBrains.Annotations.NotNull' and 'System.Diagnostics.CodeAnalysis.NotNull' collide if this file is compiled with a never version of Unity / C#
14+
using SuppressMessageAttribute = System.Diagnostics.CodeAnalysis.SuppressMessageAttribute;
15+
16+
// ReSharper restore All
17+
18+
#endregion
19+
820
namespace NugetForUnity
921
{
1022
/// <summary>
1123
/// Manages the NuGet package cache, which is where .nupkg files are stored after they are downloaded from the server, but before they are installed.
1224
/// </summary>
1325
internal static class PackageCacheManager
1426
{
27+
private const string CacheEnvironmentVariableName = "NuGetCachePath";
28+
1529
/// <summary>
1630
/// The path where to put created (packed) and downloaded (not installed yet) .nupkg files.
1731
/// </summary>
18-
internal static readonly string CacheOutputDirectory = Path.Combine(
19-
Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)),
20-
"NuGet",
21-
"Cache");
32+
[SuppressMessage(
33+
"StyleCop.CSharp.OrderingRules",
34+
"SA1202:Elements should be ordered by access",
35+
Justification = "Conflicts with other warnings.")]
36+
internal static readonly string CacheOutputDirectory = DetermineCacheOutputDirectory();
2237

2338
/// <summary>
2439
/// Initializes static members of the <see cref="PackageCacheManager" /> class.
2540
/// Static constructor called only once.
2641
/// </summary>
42+
[SuppressMessage("SonarQube", "S3877:Exceptions should not be thrown from unexpected methods", Justification = "Need to fail here.")]
2743
static PackageCacheManager()
2844
{
29-
// create the nupkgs directory, if it doesn't exist
30-
Directory.CreateDirectory(CacheOutputDirectory);
45+
try
46+
{
47+
// create the cache directory, if it doesn't exist
48+
Directory.CreateDirectory(CacheOutputDirectory);
49+
}
50+
catch (Exception exception)
51+
{
52+
throw new InvalidOperationException(
53+
$"Failed to create the cache output directory '{CacheOutputDirectory}'. The cache directory can be changed by specifying the '{CacheEnvironmentVariableName}' environment variable.",
54+
exception);
55+
}
3156
}
3257

3358
/// <summary>
@@ -156,5 +181,23 @@ private static INugetPackage GetInstalledPackage([NotNull] INugetPackageIdentifi
156181
installedPackage.Version);
157182
return installedPackage;
158183
}
184+
185+
private static string DetermineCacheOutputDirectory()
186+
{
187+
var cacheFromEnvironment = Environment.GetEnvironmentVariable(CacheEnvironmentVariableName);
188+
if (!string.IsNullOrEmpty(cacheFromEnvironment))
189+
{
190+
return Path.GetFullPath(cacheFromEnvironment);
191+
}
192+
193+
var localApplicationDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
194+
if (string.IsNullOrEmpty(localApplicationDataFolder))
195+
{
196+
throw new InvalidOperationException(
197+
$"There is no system folder specified for '{Environment.SpecialFolder.LocalApplicationData}' you need to either configure it or set the NuGet cache directory by specifying the '{CacheEnvironmentVariableName}' environment variable.");
198+
}
199+
200+
return Path.Combine(Path.GetFullPath(localApplicationDataFolder), "NuGet", "Cache");
201+
}
159202
}
160203
}

0 commit comments

Comments
 (0)