Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 5a8f57f

Browse files
committed
Support version constraints in packages.config file.
NuGet allows you to define a range of package versions that are allowed in your project. http://docs.nuget.org/docs/reference/versioning This is done by editing the packages.config file and adding an allowedVersions attribute for the installed package. For example, the following restricts jQuery to be any version from 1.4 up to but not including version 1.8. <package id="jQuery" version="1.4.1" targetFramework="net40" allowedVersions="[1.4.1,1.8)" /> The NuGet addin now supports this information when updating the package from the PowerShell console when using the Update-Package cmdlet when no version is specified. With the above example when updating jQuery the NuGet addin will now update to jQuery 1.7.2 and not the latest 2.1.1 version. Previously it would update to the latest version of the NuGet package. Note that if you search for another NuGet package and install a later version that is outside the allowed range it will still install. This is the same behaviour as Visual Studio. The allowedVersions attribute is to prevent you from accidentally updating the version but you can explicitly pick a later NuGet package and update to it with the Manage Packages dialog.
1 parent 9424de8 commit 5a8f57f

8 files changed

Lines changed: 112 additions & 3 deletions

src/AddIns/Misc/PackageManagement/Project/Src/Design/FakePackageManagementProject.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public FakePackageManagementProject(string name)
3939
FakeUninstallPackageAction = new FakeUninstallPackageAction(this);
4040

4141
this.Name = name;
42+
43+
ConstraintProvider = NullConstraintProvider.Instance;
4244
}
4345

4446
private FakeInstallPackageAction FakeInstallPackageAction;
@@ -232,6 +234,11 @@ public void AddFakePackageToSourceRepository(string packageId)
232234
FakeSourceRepository.AddFakePackage(packageId);
233235
}
234236

237+
public FakePackage AddFakePackageToSourceRepository(string packageId, string version)
238+
{
239+
return FakeSourceRepository.AddFakePackageWithVersion(packageId, version);
240+
}
241+
235242
public void UpdatePackages(UpdatePackagesAction action)
236243
{
237244
}
@@ -274,5 +281,7 @@ public void UpdatePackageReference(IPackage package, IUpdatePackageSettings sett
274281
{
275282
throw new NotImplementedException();
276283
}
284+
285+
public IPackageConstraintProvider ConstraintProvider { get; set; }
277286
}
278287
}

src/AddIns/Misc/PackageManagement/Project/Src/IPackageManagementProject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public interface IPackageManagementProject
3838

3939
Project ConvertToDTEProject();
4040

41+
IPackageConstraintProvider ConstraintProvider { get; }
42+
4143
bool IsPackageInstalled(IPackage package);
4244
bool IsPackageInstalled(string packageId);
4345
bool HasOlderPackageInstalled(IPackage package);

src/AddIns/Misc/PackageManagement/Project/Src/PackageManagementProject.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,15 @@ public void UpdatePackageReference(IPackage package, IUpdatePackageSettings sett
178178
{
179179
packageManager.UpdatePackageReference(package, settings);
180180
}
181+
182+
public IPackageConstraintProvider ConstraintProvider {
183+
get {
184+
var constraintProvider = projectManager.LocalRepository as IPackageConstraintProvider;
185+
if (constraintProvider != null) {
186+
return constraintProvider;
187+
}
188+
return NullConstraintProvider.Instance;
189+
}
190+
}
181191
}
182192
}

src/AddIns/Misc/PackageManagement/Project/Src/ProcessPackageAction.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ void GetPackageIfMissing()
134134

135135
void FindPackage()
136136
{
137-
Package = Project
138-
.SourceRepository
139-
.FindPackage(PackageId, PackageVersion, AllowPrereleaseVersions, allowUnlisted: false);
137+
Package = Project.SourceRepository.FindPackage(
138+
PackageId,
139+
PackageVersion,
140+
Project.ConstraintProvider,
141+
AllowPrereleaseVersions,
142+
allowUnlisted: false);
140143
}
141144

142145
void ThrowPackageNotFoundError(string packageId)

src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<Compile Include="Src\Helpers\FakeCmdletLogger.cs" />
112112
<Compile Include="Src\Helpers\FakeCodeGenerator.cs" />
113113
<Compile Include="Src\Helpers\FakeOperationAwarePackageRepository.cs" />
114+
<Compile Include="Src\Helpers\FakePackageRepositoryWithConstraintProvider.cs" />
114115
<Compile Include="Src\Helpers\FakeSelectProjectsService.cs" />
115116
<Compile Include="Src\Helpers\FakeServiceBasedRepository.cs" />
116117
<Compile Include="Src\Helpers\FakeSolutionPackageRepositoryFactory.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using ICSharpCode.PackageManagement.Design;
21+
using NuGet;
22+
23+
namespace PackageManagement.Tests.Helpers
24+
{
25+
public class FakePackageRepositoryWithConstraintProvider : FakePackageRepository, IPackageConstraintProvider
26+
{
27+
DefaultConstraintProvider constraintProvider = new DefaultConstraintProvider();
28+
29+
public IVersionSpec GetConstraint(string packageId)
30+
{
31+
return constraintProvider.GetConstraint(packageId);
32+
}
33+
34+
public void AddConstraint(string packageId, IVersionSpec versionSpec)
35+
{
36+
constraintProvider.AddConstraint (packageId, versionSpec);
37+
}
38+
}
39+
}

src/AddIns/Misc/PackageManagement/Test/Src/PackageManagementProjectTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,5 +759,27 @@ public void UpdatePackageReference_PackageAndUpdateActionPassed_BothPassedToPack
759759
Assert.AreEqual(package, fakePackageManager.PackagePassedToUpdatePackageReference);
760760
Assert.AreEqual(updatePackagesAction, fakePackageManager.SettingsPassedToUpdatePackageReference);
761761
}
762+
763+
[Test]
764+
public void ConstraintProvider_LocalRepositoryDoesNotImplementIConstraintProvider_ReturnsNullConstraintProviderInstance ()
765+
{
766+
CreateProject ();
767+
768+
IPackageConstraintProvider provider = project.ConstraintProvider;
769+
770+
Assert.AreEqual (NullConstraintProvider.Instance, provider);
771+
}
772+
773+
[Test]
774+
public void ConstraintProvider_LocalRepositoryImplementsIConstraintProvider_ReturnsLocalRepository ()
775+
{
776+
CreateProject ();
777+
var localRepository = new FakePackageRepositoryWithConstraintProvider ();
778+
fakeProjectManager.FakeLocalRepository = localRepository;
779+
780+
IPackageConstraintProvider provider = project.ConstraintProvider;
781+
782+
Assert.AreEqual (localRepository, provider);
783+
}
762784
}
763785
}

src/AddIns/Misc/PackageManagement/Test/Src/UpdatePackageActionTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,5 +355,28 @@ public void Execute_PackagePassedAndUpdateIfPackageDoesNotExistInProjectSetToFal
355355

356356
Assert.AreEqual(expectedPackage, actualPackage);
357357
}
358+
359+
[Test]
360+
public void Execute_PackageHasConstraint_LatestPackageIsNotUpdatedButPackageWithHighestVersionThatMatchesConstraint ()
361+
{
362+
CreateSolution ();
363+
var constraintProvider = new DefaultConstraintProvider ();
364+
var versionSpec = new VersionSpec ();
365+
versionSpec.MinVersion = new SemanticVersion ("1.0");
366+
versionSpec.IsMinInclusive = true;
367+
versionSpec.IsMaxInclusive = true;
368+
versionSpec.MaxVersion = new SemanticVersion ("2.0");
369+
constraintProvider.AddConstraint ("MyPackage", versionSpec);
370+
fakeProject.ConstraintProvider = constraintProvider;
371+
fakeProject.AddFakePackageToSourceRepository ("MyPackage", "1.0");
372+
FakePackage packageVersion2 = fakeProject.AddFakePackageToSourceRepository ("MyPackage", "2.0");
373+
fakeProject.AddFakePackageToSourceRepository ("MyPackage", "3.0");
374+
fakeProject.FakePackages.Add (new FakePackage ("MyPackage", "1.0"));
375+
action.PackageId = "MyPackage";
376+
377+
action.Execute ();
378+
379+
Assert.AreEqual (packageVersion2, fakeProject.PackagePassedToUpdatePackage);
380+
}
358381
}
359382
}

0 commit comments

Comments
 (0)