Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/NuGet.BuildTasks.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJason Malinowski <jason.malinowski@microsoft.com>2016-06-14 22:28:01 +0300
committerJason Malinowski <jason.malinowski@microsoft.com>2016-06-15 02:19:52 +0300
commitd37c9176cf0b93ab86367675719fa61bbf757d2d (patch)
tree187ac2c795e088494576ab523a555834908d4be6 /src
parent4a5f2805e0625fe58f3a7386e18ec89770cf8253 (diff)
Support reading package folder locations out of the lock file
The task previously was constructing the path to the user's packages location itself, which had various issues. It meant that if the restore was done with a different path, we don't know that. It also meant we didn't handle the paths specified in nuget.config. Now, if the paths are present in the lock file, we'll use that. Otherwise, we'll construct our old path as before. Any explicitly given path to the task is still used no matter what.
Diffstat (limited to 'src')
-rw-r--r--src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.Designer.cs28
-rw-r--r--src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.resx3
-rw-r--r--src/Microsoft.NuGet.Build.Tasks.Tests/Json/LockFileWithWithSpecifiedPackageFolders.json52
-rw-r--r--src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj2
-rw-r--r--src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs26
-rw-r--r--src/Microsoft.NuGet.Build.Tasks.Tests/PackageFolderTests.cs24
-rw-r--r--src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs81
7 files changed, 183 insertions, 33 deletions
diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.Designer.cs b/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.Designer.cs
index 542db47..da6220a 100644
--- a/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.Designer.cs
+++ b/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.Designer.cs
@@ -149,6 +149,34 @@ namespace Microsoft.NuGet.Build.Tasks.Tests.Json {
/// <summary>
/// Looks up a localized string similar to {
/// &quot;locked&quot;: false,
+ /// &quot;version&quot;: 2,
+ /// &quot;targets&quot;: {
+ /// &quot;.NETFramework,Version=v4.5&quot;: {
+ /// &quot;Newtonsoft.Json/8.0.3&quot;: {
+ /// &quot;type&quot;: &quot;package&quot;,
+ /// &quot;compile&quot;: {
+ /// &quot;lib/net45/Newtonsoft.Json.dll&quot;: {}
+ /// },
+ /// &quot;runtime&quot;: {
+ /// &quot;lib/net45/Newtonsoft.Json.dll&quot;: {}
+ /// }
+ /// }
+ /// }
+ /// },
+ /// &quot;libraries&quot;: {
+ /// &quot;Newtonsoft.Json/8.0.3&quot;: {
+ /// &quot;sha512&quot;: &quot;KGsYQdS2zLH+H8x2cZaSI7e+YZ4SFIbyy1YJQYl6GYBWjf5o4H1A68nxyq+WTyVSOJQ4GqS/DiPE+UseUizgMg==&quot;,
+ /// &quot;type&quot;: &quot; [rest of string was truncated]&quot;;.
+ /// </summary>
+ internal static string LockFileWithWithSpecifiedPackageFolders {
+ get {
+ return ResourceManager.GetString("LockFileWithWithSpecifiedPackageFolders", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to {
+ /// &quot;locked&quot;: false,
/// &quot;version&quot;: 1,
/// &quot;targets&quot;: {
/// &quot;.NETCore,Version=v5.0&quot;: {
diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.resx b/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.resx
index 7656ea1..f569b2c 100644
--- a/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.resx
+++ b/src/Microsoft.NuGet.Build.Tasks.Tests/Json/Json.resx
@@ -139,4 +139,7 @@
<data name="nativeWinMD" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>nativeWinMD.json;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
+ <data name="LockFileWithWithSpecifiedPackageFolders" type="System.Resources.ResXFileRef, System.Windows.Forms">
+ <value>lockfilewithwithspecifiedpackagefolders.json;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
+ </data>
</root> \ No newline at end of file
diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/Json/LockFileWithWithSpecifiedPackageFolders.json b/src/Microsoft.NuGet.Build.Tasks.Tests/Json/LockFileWithWithSpecifiedPackageFolders.json
new file mode 100644
index 0000000..6df6a2a
--- /dev/null
+++ b/src/Microsoft.NuGet.Build.Tasks.Tests/Json/LockFileWithWithSpecifiedPackageFolders.json
@@ -0,0 +1,52 @@
+{
+ "locked": false,
+ "version": 2,
+ "targets": {
+ ".NETFramework,Version=v4.5": {
+ "Newtonsoft.Json/8.0.3": {
+ "type": "package",
+ "compile": {
+ "lib/net45/Newtonsoft.Json.dll": {}
+ },
+ "runtime": {
+ "lib/net45/Newtonsoft.Json.dll": {}
+ }
+ }
+ }
+ },
+ "libraries": {
+ "Newtonsoft.Json/8.0.3": {
+ "sha512": "KGsYQdS2zLH+H8x2cZaSI7e+YZ4SFIbyy1YJQYl6GYBWjf5o4H1A68nxyq+WTyVSOJQ4GqS/DiPE+UseUizgMg==",
+ "type": "package",
+ "path": "newtonsoft.json/8.0.3",
+ "files": [
+ "lib/net20/Newtonsoft.Json.dll",
+ "lib/net20/Newtonsoft.Json.xml",
+ "lib/net35/Newtonsoft.Json.dll",
+ "lib/net35/Newtonsoft.Json.xml",
+ "lib/net40/Newtonsoft.Json.dll",
+ "lib/net40/Newtonsoft.Json.xml",
+ "lib/net45/Newtonsoft.Json.dll",
+ "lib/net45/Newtonsoft.Json.xml",
+ "lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.dll",
+ "lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.xml",
+ "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll",
+ "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.xml",
+ "newtonsoft.json.8.0.3.nupkg.sha512",
+ "newtonsoft.json.nuspec",
+ "tools/install.ps1"
+ ]
+ }
+ },
+ "projectFileDependencyGroups": {
+ "": [
+ "Newtonsoft.Json"
+ ],
+ ".NETFramework,Version=v4.5": []
+ },
+ "tools": {},
+ "projectFileToolGroups": {},
+ "packageFolders": {
+ "C:\\PackageFolder\\": {}
+ }
+} \ No newline at end of file
diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj b/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj
index 7f669ae..12878ba 100644
--- a/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj
+++ b/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj
@@ -55,6 +55,7 @@
<DependentUpon>Json.resx</DependentUpon>
</Compile>
<Compile Include="NuGetTestHelpers.cs" />
+ <Compile Include="PackageFolderTests.cs" />
<Compile Include="PreprocessorTests.cs" />
<Compile Include="ProjectReferences\ProjectReferenceTests.cs" />
<Compile Include="ProjectReferences\Resources.Designer.cs">
@@ -78,6 +79,7 @@
<None Include="Json\FluentAssertions.lock.json" />
<None Include="Json\FluentAssertionsAndWin10.lock.json" />
<None Include="Json\nativeWinMD.json" />
+ <None Include="Json\LockFileWithWithSpecifiedPackageFolders.json" />
<None Include="Json\Win10.Edm.json" />
<None Include="Json\Win10.json" />
<None Include="Json\Win10.xunit.json" />
diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs b/src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs
index 4a0391a..03df913 100644
--- a/src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs
+++ b/src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs
@@ -26,13 +26,13 @@ namespace Microsoft.NuGet.Build.Tasks.Tests
TryGetRuntimeVersion tryGetRuntimeVersion = null,
bool includeFrameworkReferences = true,
string projectJsonFileContents = null,
- IEnumerable<ITaskItem> projectReferencesCreatingPackages = null)
+ IEnumerable<ITaskItem> projectReferencesCreatingPackages = null,
+ bool createTemporaryFolderForPackages = true)
{
var rootDirectory = new TempRoot();
using (rootDirectory)
{
var projectDirectory = rootDirectory.CreateDirectory();
- var packagesDirectory = rootDirectory.CreateDirectory();
var projectLockJsonFile = projectDirectory.CreateFile("project.lock.json");
projectLockJsonFile.WriteAllText(projectLockJsonFileContents);
@@ -43,21 +43,31 @@ namespace Microsoft.NuGet.Build.Tasks.Tests
projectJsonFile.WriteAllText(projectJsonFileContents);
}
- var filesInPackages = new HashSet<string>(
- GetFakeFileNamesFromPackages(projectLockJsonFileContents, packagesDirectory.Path),
- StringComparer.OrdinalIgnoreCase);
+ var filesInPackages = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
+ DisposableDirectory packagesDirectory = null;
+
+ if (createTemporaryFolderForPackages)
+ {
+ packagesDirectory = rootDirectory.CreateDirectory();
+
+ foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, packagesDirectory.Path))
+ {
+ filesInPackages.Add(fileInPackage);
+ }
+ }
// Don't require the packages be restored on the machine
- DirectoryExists directoryExists = path => path.StartsWith(packagesDirectory.Path) || Directory.Exists(path);
+ ResolveNuGetPackageAssets task = null;
+ DirectoryExists directoryExists = path => task.GetPackageFolders().Any(l => path.StartsWith(l)) || Directory.Exists(path);
FileExists fileExists = path => filesInPackages.Contains(path) || File.Exists(path);
- ResolveNuGetPackageAssets task = new ResolveNuGetPackageAssets(directoryExists, fileExists, tryGetRuntimeVersion);
+ task = new ResolveNuGetPackageAssets(directoryExists, fileExists, tryGetRuntimeVersion);
var sw = new StringWriter();
task.BuildEngine = new MockBuildEngine(sw);
task.AllowFallbackOnTargetSelection = allowFallbackOnTargetSelection;
task.IncludeFrameworkReferences = includeFrameworkReferences;
- task.NuGetPackagesDirectory = packagesDirectory.Path;
+ task.NuGetPackagesDirectory = packagesDirectory?.Path;
task.RuntimeIdentifier = runtimeIdentifier;
task.ProjectReferencesCreatingPackages = (projectReferencesCreatingPackages ?? Enumerable.Empty<ITaskItem>()).ToArray();
task.ProjectLockFile = projectLockJsonFile.Path;
diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/PackageFolderTests.cs b/src/Microsoft.NuGet.Build.Tasks.Tests/PackageFolderTests.cs
new file mode 100644
index 0000000..d8ef819
--- /dev/null
+++ b/src/Microsoft.NuGet.Build.Tasks.Tests/PackageFolderTests.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace Microsoft.NuGet.Build.Tasks.Tests
+{
+ public class PackageFolderTests
+ {
+ [Fact]
+ public void ResolveWithLockFileWithPackageFolders()
+ {
+ var result = NuGetTestHelpers.ResolvePackagesWithJsonFileContents(
+ Json.Json.LockFileWithWithSpecifiedPackageFolders,
+ ".NETFramework,Version=v4.5",
+ runtimeIdentifier: null,
+ createTemporaryFolderForPackages: false);
+
+ Assert.Equal(@"C:\PackageFolder\Newtonsoft.Json\8.0.3\lib\net45\Newtonsoft.Json.dll", result.References.Single().ItemSpec);
+ }
+ }
+}
diff --git a/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs b/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs
index 2191241..eb425a3 100644
--- a/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs
+++ b/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs
@@ -44,6 +44,8 @@ namespace Microsoft.NuGet.Build.Tasks
private readonly List<ITaskItem> _contentItems = new List<ITaskItem>();
private readonly List<ITaskItem> _fileWrites = new List<ITaskItem>();
+ private readonly List<string> _packageFolders = new List<string>();
+
private readonly Dictionary<string, string> _projectReferencesToOutputBasePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
#region UnitTestSupport
@@ -72,6 +74,13 @@ namespace Microsoft.NuGet.Build.Tasks
_reportExceptionsToMSBuildLogger = false;
}
+
+ // For unit testing.
+ internal IEnumerable<string> GetPackageFolders()
+ {
+ return _packageFolders;
+ }
+
#endregion
/// <summary>
@@ -233,6 +242,8 @@ namespace Microsoft.NuGet.Build.Tasks
lockFile = JObject.Load(new JsonTextReader(streamReader));
}
+ PopulatePackageFolders(lockFile);
+
PopulateProjectReferenceMaps();
GetReferences(lockFile);
GetCopyLocalItems(lockFile);
@@ -241,6 +252,41 @@ namespace Microsoft.NuGet.Build.Tasks
ProduceContentAssets(lockFile);
}
+ private void PopulatePackageFolders(JObject lockFile)
+ {
+ // If we explicitly were given a path, let's use that
+ if (!string.IsNullOrEmpty(NuGetPackagesDirectory))
+ {
+ _packageFolders.Add(NuGetPackagesDirectory);
+ }
+
+ // Newer versions of NuGet can now specify the final list of locations in the lock file
+ var packageFolders = lockFile["packageFolders"] as JObject;
+
+ if (packageFolders != null)
+ {
+ foreach (var packageFolder in packageFolders.Properties())
+ {
+ _packageFolders.Add(packageFolder.Name);
+ }
+ }
+
+ // If we didn't have any folders, let's fall back to the environment variable or user profile
+ if (_packageFolders.Count == 0)
+ {
+ string packagesFolder = Environment.GetEnvironmentVariable("NUGET_PACKAGES");
+
+ if (!string.IsNullOrEmpty(packagesFolder))
+ {
+ _packageFolders.Add(packagesFolder);
+ }
+ else
+ {
+ _packageFolders.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"));
+ }
+ }
+ }
+
private void PopulateProjectReferenceMaps()
{
foreach (var projectReference in ProjectReferencesCreatingPackages ?? new ITaskItem[] { })
@@ -579,7 +625,7 @@ namespace Microsoft.NuGet.Build.Tasks
private void ProduceContentAsset(NuGetPackageObject package, JProperty sharedAsset, IReadOnlyDictionary<string, string> preprocessorValues, string preprocessedOutputDirectory)
{
string pathToFinalAsset = package.GetFullPathToFile(sharedAsset.Name);
-
+
if (sharedAsset.Value["ppOutputPath"] != null)
{
if (preprocessedOutputDirectory == null)
@@ -775,7 +821,7 @@ namespace Microsoft.NuGet.Build.Tasks
{
targetPath = Path.Combine(culture, Path.GetFileName(file.Name));
}
-
+
var item = CreateItem(package, package.GetFullPathToFile(file.Name), targetPath);
item.SetMetadata("Private", "false");
@@ -844,32 +890,17 @@ namespace Microsoft.NuGet.Build.Tasks
private string GetNuGetPackagePath(string packageId, string packageVersion)
{
- string packagesFolder = GetNuGetPackagesPath();
- string packagePath = Path.Combine(packagesFolder, packageId, packageVersion);
-
- if (!_directoryExists(packagePath))
+ foreach (var packagesFolder in _packageFolders)
{
- throw new ExceptionFromResource(nameof(Strings.PackageFolderNotFound), packageId, packageVersion, packagesFolder);
- }
-
- return packagePath;
- }
+ string packagePath = Path.Combine(packagesFolder, packageId, packageVersion);
- private string GetNuGetPackagesPath()
- {
- if (!string.IsNullOrEmpty(NuGetPackagesDirectory))
- {
- return NuGetPackagesDirectory;
- }
-
- string packagesFolder = Environment.GetEnvironmentVariable("NUGET_PACKAGES");
-
- if (!string.IsNullOrEmpty(packagesFolder))
- {
- return packagesFolder;
+ if (_directoryExists(packagePath))
+ {
+ return packagePath;
+ }
}
- return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages");
+ throw new ExceptionFromResource(nameof(Strings.PackageFolderNotFound), packageId, packageVersion, string.Join(", ", _packageFolders));
}
private IEnumerable<NuGetPackageObject> GetPackagesFromTarget(JObject lockFile, JObject target)
@@ -915,7 +946,7 @@ namespace Microsoft.NuGet.Build.Tasks
};
}
else
- {
+ {
fullPackagePathGenerator = () => GetNuGetPackagePath(id, version);
}