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

Commit 82e98ec

Browse files
committed
Support custom NuGet package directory.
NuGet allows a solution to override the standard packages directory with a repositoryPath setting in the NuGet.config file at the solution level in the .nuget directory. <configuration> <config> <add key="repositoryPath" value="../../MyPackages" /> </config> </configuration> This is now supported by SharpDevelop.
1 parent 8fa0692 commit 82e98ec

6 files changed

Lines changed: 58 additions & 9 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Linq;
22+
2123
using NuGet;
2224

2325
namespace ICSharpCode.PackageManagement.Design
@@ -36,6 +38,8 @@ public List<KeyValuePair<string, string>> DisabledPackageSources
3638
public Dictionary<string, IList<KeyValuePair<string, string>>> Sections
3739
= new Dictionary<string, IList<KeyValuePair<string, string>>>();
3840

41+
public const string ConfigSectionName = "config";
42+
3943
public FakeSettings()
4044
{
4145
Sections.Add(RegisteredPackageSourceSettings.PackageSourcesSectionName, PackageSources);
@@ -200,7 +204,11 @@ public bool IsPackageRestoreSectionDeleted {
200204

201205
public string GetValue(string section, string key, bool isPath)
202206
{
203-
throw new NotImplementedException();
207+
if (Sections.ContainsKey(section)) {
208+
var matchedSection = Sections[section];
209+
return matchedSection.FirstOrDefault(item => item.Key == key).Value;
210+
}
211+
return null;
204212
}
205213

206214
public IList<KeyValuePair<string, string>> GetValues(string section, bool isPath)
@@ -212,5 +220,12 @@ public IList<SettingValue> GetSettingValues(string section, bool isPath)
212220
{
213221
throw new NotImplementedException();
214222
}
223+
224+
public void SetRepositoryPathSetting(string fullPath)
225+
{
226+
var items = new List<KeyValuePair<string, string>> ();
227+
items.Add (new KeyValuePair<string, string>("repositoryPath", fullPath));
228+
Sections.Add(ConfigSectionName, items);
229+
}
215230
}
216231
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,10 @@ void RecentPackagesCollectionChanged(object sender, NotifyCollectionChangedEvent
9595
{
9696
properties.SetList(RecentPackagesPropertyName, recentPackages);
9797
}
98+
99+
public string GetCustomPackagesDirectory()
100+
{
101+
return registeredPackageSourceSettings.Settings.GetRepositoryPath();
102+
}
98103
}
99104
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,9 @@ void ResetPackageSources()
204204
packageSources = null;
205205
}
206206
}
207+
208+
public ISettings Settings {
209+
get { return settings; }
210+
}
207211
}
208212
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public SharpDevelopPackageManagerFactory()
3232
: this(
3333
new SharpDevelopPackageRepositoryFactory(),
3434
new SharpDevelopProjectSystemFactory(),
35-
new PackageManagementOptions())
35+
PackageManagementServices.Options)
3636
{
3737
}
3838

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ namespace ICSharpCode.PackageManagement
2525
{
2626
public class SolutionPackageRepositoryPath
2727
{
28-
string packagesRelativeDirectory;
2928
ISolution solution;
3029
DefaultPackagePathResolver pathResolver;
3130

3231
public SolutionPackageRepositoryPath(IProject project)
33-
: this(project, new PackageManagementOptions())
32+
: this(project, PackageManagementServices.Options)
3433
{
3534
}
3635

@@ -41,14 +40,18 @@ public SolutionPackageRepositoryPath(IProject project, PackageManagementOptions
4140

4241
public SolutionPackageRepositoryPath(ISolution solution, PackageManagementOptions options)
4342
{
44-
packagesRelativeDirectory = options.PackagesDirectory;
4543
this.solution = solution;
46-
GetSolutionPackageRepositoryPath();
44+
PackageRepositoryPath = GetSolutionPackageRepositoryPath(options);
4745
}
4846

49-
void GetSolutionPackageRepositoryPath()
47+
string GetSolutionPackageRepositoryPath(PackageManagementOptions options)
5048
{
51-
PackageRepositoryPath = Path.Combine(solution.Directory, packagesRelativeDirectory);
49+
string customPath = options.GetCustomPackagesDirectory ();
50+
if (!String.IsNullOrEmpty (customPath)) {
51+
return customPath;
52+
}
53+
54+
return Path.Combine (solution.Directory, options.PackagesDirectory);
5255
}
5356

5457
public string PackageRepositoryPath { get; private set; }

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public class SolutionPackageRepositoryPathTests
3232
{
3333
SolutionPackageRepositoryPath repositoryPath;
3434
IProject testProject;
35-
PackageManagementOptions options;
35+
TestablePackageManagementOptions options;
3636
ISolution solution;
37+
FakeSettings settings;
3738

3839
void CreateSolutionPackageRepositoryPath()
3940
{
@@ -61,6 +62,12 @@ void CreateSolution(string fileName)
6162
void CreateOptions()
6263
{
6364
options = new TestablePackageManagementOptions();
65+
settings = options.FakeSettings;
66+
}
67+
68+
void SolutionNuGetConfigFileHasCustomPackagesPath(string fullPath)
69+
{
70+
settings.SetRepositoryPathSetting(fullPath);
6471
}
6572

6673
[Test]
@@ -109,5 +116,20 @@ public void GetInstallPath_GetInstallPathForPackage_ReturnsPackagePathInsideSolu
109116

110117
Assert.AreEqual(expectedInstallPath, installPath);
111118
}
119+
120+
[Test]
121+
public void PackageRepositoryPath_SolutionHasNuGetFileThatOverridesDefaultPackagesRepositoryPath_OverriddenPathReturned()
122+
{
123+
CreateOptions();
124+
CreateSolution(@"d:\projects\MySolution\MySolution.sln");
125+
options.PackagesDirectory = "Packages";
126+
SolutionNuGetConfigFileHasCustomPackagesPath(@"d:\Team\MyPackages");
127+
CreateSolutionPackageRepositoryPath(solution);
128+
string expectedPath = @"d:\Team\MyPackages";
129+
130+
string path = repositoryPath.PackageRepositoryPath;
131+
132+
Assert.AreEqual(expectedPath, path);
133+
}
112134
}
113135
}

0 commit comments

Comments
 (0)