diff options
author | Matt Ward <matt.ward@xamarin.com> | 2016-06-23 15:42:12 +0300 |
---|---|---|
committer | Matt Ward <matt.ward@xamarin.com> | 2016-06-23 16:02:36 +0300 |
commit | 29215728e63f70ff5bb2aefcd26deda6eb5fa058 (patch) | |
tree | d9fc0aca6fa49f7d1b83a19ec47bf5c40150f2c0 /main/src/addins | |
parent | 9e008139f0205376f1e68d61f53c126690e9363c (diff) |
[NuGet] Always add .targets files to end of project.
When installing a NuGet package that has a .targets file the
Import element created was grouped with the existing Import elements.
This is OK most of the time however if there are other items in
the project added after the import then any build targets may fail
since these items are included after the import. One example is the
netfx-System.StringResources NuGet package which will not find any
resource files that are added to the project after the import.
Now .targets files are added as the last element in the project file.
This also makes the behaviour consistent with how NuGet works in
Visual Studio.
Diffstat (limited to 'main/src/addins')
2 files changed, 55 insertions, 2 deletions
diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/MSBuildProjectExtensionsTests.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/MSBuildProjectExtensionsTests.cs index 070de63866..f9b5eb38da 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/MSBuildProjectExtensionsTests.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/MSBuildProjectExtensionsTests.cs @@ -179,8 +179,10 @@ namespace MonoDevelop.PackageManagement.Tests string import = @"..\packages\Foo.0.1\build\Foo.targets";
AddImportIfMissingAtTop (import);
-
+
+ var firstItem = project.GetAllObjects ().First () as MSBuildImport;
AssertFirstMSBuildImportElementHasProjectAttributeValue (@"..\packages\Foo.0.1\build\Foo.targets");
+ Assert.AreEqual (import, firstItem.Project);
}
[Test]
@@ -193,7 +195,9 @@ namespace MonoDevelop.PackageManagement.Tests AddImportIfMissingAtTop (import, condition);
+ var firstItem = project.GetAllObjects ().First () as MSBuildImport;
AssertFirstMSBuildImportElementHasCondition (condition);
+ Assert.AreEqual (condition, firstItem.Condition);
}
[Test]
@@ -207,5 +211,43 @@ namespace MonoDevelop.PackageManagement.Tests Assert.AreEqual (1, project.Imports.Count ());
}
+
+ [Test]
+ public void AddImportIfMissing_AddImportToBottomWhenOtherImportIsNotLastElementInProject_ImportAddedAsLastElementInProject ()
+ {
+ CreateProject ();
+ var itemGroup = project.AddNewItemGroup ();
+ itemGroup.AddNewItem ("File.cs", "File.cs");
+ project.AddNewImport ("test", null, itemGroup);
+ string importFile = @"..\packages\Foo.0.1\build\Foo.targets";
+ string condition = "Exists('..\\packages\\Foo.0.1\\build\\Foo.targets')";
+ var lastItemBeforeAddingNewImport = project.GetAllObjects ().Last () as MSBuildItemGroup;
+
+ AddImportIfMissingAtBottom (importFile, condition);
+
+ var lastItem = project.GetAllObjects ().Last () as MSBuildImport;
+ Assert.IsNotNull (lastItem);
+ Assert.AreEqual (importFile, lastItem.Project);
+ Assert.AreEqual (condition, lastItem.Condition);
+ Assert.IsNotNull (lastItemBeforeAddingNewImport);
+ Assert.AreEqual ("File.cs", lastItemBeforeAddingNewImport.Items.First ().Include);
+ }
+
+ [Test]
+ public void AddImportIfMissing_AddImportToTopOfProjectWhenItemGroupIsFirstChildElementOfProject_ImportAddedAsFirstChildElement ()
+ {
+ CreateProject ();
+ var itemGroup = project.AddNewItemGroup ();
+ itemGroup.AddNewItem ("File.cs", "File.cs");
+ string import = @"..\packages\Foo.0.1\build\Foo.targets";
+ string condition = "Exists('..\\packages\\Foo.0.1\\build\\Foo.targets')";
+
+ AddImportIfMissingAtTop (import, condition);
+
+ var firstItem = project.GetAllObjects ().First () as MSBuildImport;
+ AssertFirstMSBuildImportElementHasCondition (condition);
+ Assert.AreEqual (import, firstItem.Project);
+ Assert.AreEqual (condition, firstItem.Condition);
+ }
}
}
\ No newline at end of file diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MSBuildProjectExtensions.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MSBuildProjectExtensions.cs index 0012f52cfe..37545c9cd8 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MSBuildProjectExtensions.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MSBuildProjectExtensions.cs @@ -64,9 +64,20 @@ namespace MonoDevelop.PackageManagement ImportLocation importLocation,
string condition)
{
- var before = importLocation == ImportLocation.Top ? project.GetAllObjects ().FirstOrDefault () : null;
+ MSBuildObject before = GetInsertBeforeObject (project, importLocation);
project.AddNewImport (importedProjectFile, condition, before);
}
+
+ static MSBuildObject GetInsertBeforeObject (MSBuildProject project, ImportLocation importLocation)
+ {
+ if (importLocation == ImportLocation.Top) {
+ return project.GetAllObjects ().FirstOrDefault ();
+ }
+
+ // Return an unknown MSBuildItem instead of null so the MSBuildProject adds the import as the last
+ // child in the project.
+ return new MSBuildItem ();
+ }
public static void RemoveImportIfExists (this MSBuildProject project, string importedProjectFile)
{
|