|
1 | | -using System; |
| 1 | +#pragma warning disable SA1512,SA1124 // Single-line comments should not be followed by blank line |
| 2 | + |
| 3 | +using System; |
2 | 4 | using System.IO; |
3 | 5 | using JetBrains.Annotations; |
4 | 6 | using NugetForUnity.Configuration; |
5 | 7 | using NugetForUnity.Models; |
6 | 8 | using NugetForUnity.PackageSource; |
7 | 9 |
|
| 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 | + |
8 | 20 | namespace NugetForUnity |
9 | 21 | { |
10 | 22 | /// <summary> |
11 | 23 | /// Manages the NuGet package cache, which is where .nupkg files are stored after they are downloaded from the server, but before they are installed. |
12 | 24 | /// </summary> |
13 | 25 | internal static class PackageCacheManager |
14 | 26 | { |
| 27 | + private const string CacheEnvironmentVariableName = "NuGetCachePath"; |
| 28 | + |
15 | 29 | /// <summary> |
16 | 30 | /// The path where to put created (packed) and downloaded (not installed yet) .nupkg files. |
17 | 31 | /// </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(); |
22 | 37 |
|
23 | 38 | /// <summary> |
24 | 39 | /// Initializes static members of the <see cref="PackageCacheManager" /> class. |
25 | 40 | /// Static constructor called only once. |
26 | 41 | /// </summary> |
| 42 | + [SuppressMessage("SonarQube", "S3877:Exceptions should not be thrown from unexpected methods", Justification = "Need to fail here.")] |
27 | 43 | static PackageCacheManager() |
28 | 44 | { |
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 | + } |
31 | 56 | } |
32 | 57 |
|
33 | 58 | /// <summary> |
@@ -156,5 +181,23 @@ private static INugetPackage GetInstalledPackage([NotNull] INugetPackageIdentifi |
156 | 181 | installedPackage.Version); |
157 | 182 | return installedPackage; |
158 | 183 | } |
| 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 | + } |
159 | 202 | } |
160 | 203 | } |
0 commit comments