Skip to content

Commit 86ff94e

Browse files
committed
Added NET 6.0 packages. Included option for force caching Nuget download results.
1 parent b6d1f9c commit 86ff94e

File tree

13 files changed

+120
-30
lines changed

13 files changed

+120
-30
lines changed

global.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"sdk": {
3-
"version": "3.1.101",
4-
"rollForward": "latestFeature"
3+
"version": "6.0.203"
54
}
65
}

samples/WebAppWithNuget/Startup.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Builder;
@@ -30,18 +31,23 @@ public void ConfigureServices(IServiceCollection services)
3031
{
3132
NugetPluginCatalogOptions.Defaults.LoggerFactory = () => new NugetLogger(services);
3233

34+
var options = new NugetPluginCatalogOptions
35+
{
36+
ForcePackageCaching = true,
37+
CustomPackagesFolder = Path.Combine(Path.GetTempPath(), "NugetPackagePluginCatalog", "Sample")
38+
};
39+
3340
var nugetCatalog = new NugetPackagePluginCatalog("Weikio.PluginFramework.Samples.SharedPlugins", includePrerelease: true, configureFinder: finder =>
3441
{
3542
finder.Implements<IOperator>();
36-
});
43+
}, options: options);
3744

3845
services.AddPluginFramework()
3946
.AddPluginCatalog(nugetCatalog)
4047
.AddPluginType<IOperator>();
4148

4249
services.AddControllers();
4350
}
44-
4551

4652
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
4753
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

src/Weikio.PluginFramework.AspNetCore/Weikio.PluginFramework.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
55
<IsPackable>true</IsPackable>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Description>Plugin Framework for ASP.NET Core.</Description>

src/Weikio.PluginFramework.Catalogs.NuGet/NugetFeedPluginCatalog.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class NugetFeedPluginCatalog : IPluginCatalog
2727

2828
public NugetFeedPluginCatalog(NuGetFeed packageFeed, string searchTerm = null,
2929
bool includePrereleases = false, int maxPackages = 128,
30-
string packagesFolder = null, Action<TypeFinderCriteriaBuilder> configureFinder = null, Dictionary<string, TypeFinderCriteria> criterias = null,
30+
string packagesFolder = null, Action<TypeFinderCriteriaBuilder> configureFinder = null, Dictionary<string, TypeFinderCriteria> criterias = null,
3131
NugetFeedPluginCatalogOptions options = null)
3232
{
3333
_packageFeed = packageFeed;
@@ -58,13 +58,13 @@ public NugetFeedPluginCatalog(NuGetFeed packageFeed, string searchTerm = null,
5858

5959
_options.TypeFinderOptions.TypeFinderCriterias.Add(criteria);
6060
}
61-
61+
6262
foreach (var finderCriteria in criterias)
6363
{
6464
finderCriteria.Value.Tags = new List<string>() { finderCriteria.Key };
65-
65+
6666
_options.TypeFinderOptions.TypeFinderCriterias.Add(finderCriteria.Value);
67-
}
67+
}
6868
}
6969

7070
Plugin IPluginCatalog.Get(string name, Version version)
@@ -94,8 +94,13 @@ public async Task Initialize()
9494

9595
foreach (var packageAndRepo in packages)
9696
{
97-
var options = new NugetPluginCatalogOptions() { TypeFinderOptions = _options.TypeFinderOptions, PluginNameOptions = _options.PluginNameOptions};
98-
97+
var options = new NugetPluginCatalogOptions()
98+
{
99+
TypeFinderOptions = _options.TypeFinderOptions,
100+
PluginNameOptions = _options.PluginNameOptions,
101+
ForcePackageCaching = _options.ForcePackageCaching
102+
};
103+
99104
var packageCatalog = new NugetPackagePluginCatalog(packageAndRepo.Package.Identity.Id, packageAndRepo.Package.Identity.Version.ToString(),
100105
_includePrereleases, _packageFeed, PackagesFolder, options: options);
101106

src/Weikio.PluginFramework.Catalogs.NuGet/NugetFeedPluginCatalogOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public class NugetFeedPluginCatalogOptions
2424
/// </summary>
2525
public PluginNameOptions PluginNameOptions { get; set; } = Defaults.PluginNameOptions;
2626

27+
/// <summary>
28+
/// Gets or sets if Plugin Framework should take care of package caching. In some cases Nuget will download already downloaded package. This flag
29+
/// tries to make sure that the package is downloaded only once. Note: Requires that PackagesFolder is set
30+
/// </summary>
31+
public bool ForcePackageCaching { get; set; } = false;
32+
2733
public static class Defaults
2834
{
2935
/// <summary>

src/Weikio.PluginFramework.Catalogs.NuGet/NugetPackagePluginCatalog.cs

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,46 @@ public class NugetPackagePluginCatalog : IPluginCatalog
2626

2727
public string PackagesFolder { get; }
2828

29-
private bool _isCustomPackagesFolder;
29+
private bool HasCustomPackagesFolder
30+
{
31+
get
32+
{
33+
return string.IsNullOrWhiteSpace(PackagesFolder) == false;
34+
}
35+
}
36+
37+
private bool ForcePackageCache
38+
{
39+
get
40+
{
41+
if (HasCustomPackagesFolder == false)
42+
{
43+
return false;
44+
}
45+
46+
if (_options?.ForcePackageCaching != true)
47+
{
48+
return false;
49+
}
50+
51+
return true;
52+
}
53+
}
54+
55+
private string NugetDownloadResultFilePath
56+
{
57+
get
58+
{
59+
if (string.IsNullOrWhiteSpace(PackagesFolder))
60+
{
61+
return null;
62+
}
63+
64+
var result = Path.Combine(PackagesFolder, ".nugetDownloadResult.json");
65+
66+
return result;
67+
}
68+
}
3069

3170
public NugetPackagePluginCatalog(string packageName, string packageVersion = null, bool includePrerelease = false, NuGetFeed packageFeed = null,
3271
string packagesFolder = null, Action<TypeFinderCriteriaBuilder> configureFinder = null, Dictionary<string, TypeFinderCriteria> criterias = null,
@@ -37,9 +76,12 @@ public NugetPackagePluginCatalog(string packageName, string packageVersion = nul
3776
_includePrerelease = includePrerelease;
3877
_packageFeed = packageFeed;
3978

40-
PackagesFolder = packagesFolder ?? Path.Combine(Path.GetTempPath(), "NugetPackagePluginCatalog", Path.GetRandomFileName());
79+
PackagesFolder = packagesFolder ?? options?.CustomPackagesFolder;
4180

42-
_isCustomPackagesFolder = packagesFolder != null;
81+
if (string.IsNullOrWhiteSpace(PackagesFolder))
82+
{
83+
PackagesFolder = Path.Combine(Path.GetTempPath(), "NugetPackagePluginCatalog", Path.GetRandomFileName());
84+
}
4385

4486
if (!Directory.Exists(PackagesFolder))
4587
{
@@ -99,18 +141,30 @@ public async Task Initialize()
99141
{
100142
NugetDownloadResult nugetDownloadResult = null;
101143

102-
if (_isCustomPackagesFolder && File.Exists(PackagesFolder + "/nugetDownloadResult.json"))
144+
var logger = _options.LoggerFactory();
145+
146+
if (ForcePackageCache && File.Exists(NugetDownloadResultFilePath))
103147
{
104-
var jsonFromDisk = await File.ReadAllTextAsync(PackagesFolder + "/nugetDownloadResult.json");
148+
try
149+
{
150+
var jsonFromDisk = await File.ReadAllTextAsync(NugetDownloadResultFilePath);
105151

106-
nugetDownloadResult = JsonConvert.DeserializeObject<NugetDownloadResult>(jsonFromDisk);
152+
nugetDownloadResult = JsonConvert.DeserializeObject<NugetDownloadResult>(jsonFromDisk);
153+
154+
logger?.LogDebug($"Using previously downloaded package from {PackagesFolder}");
155+
}
156+
catch (Exception e)
157+
{
158+
logger?.LogError($"Failed to deserialize nuget download result from path {NugetDownloadResultFilePath}: {e}");
159+
}
107160
}
108-
else
161+
162+
if (nugetDownloadResult == null)
109163
{
110164
var nuGetDownloader = new NuGetDownloader(_options.LoggerFactory());
111165

112166
nugetDownloadResult = await nuGetDownloader.DownloadAsync(PackagesFolder, _packageName, _packageVersion, _includePrerelease, _packageFeed,
113-
includeSecondaryRepositories: _options.IncludeSystemFeedsAsSecondary, targetFramework: _options.TargetFramework).ConfigureAwait(false);
167+
includeSecondaryRepositories: _options.IncludeSystemFeedsAsSecondary, targetFramework: _options.TargetFramework).ConfigureAwait(false);
114168
}
115169

116170
foreach (var f in nugetDownloadResult.PackageAssemblyFiles)
@@ -146,11 +200,20 @@ public async Task Initialize()
146200

147201
IsInitialized = true;
148202

149-
if (_isCustomPackagesFolder && File.Exists(PackagesFolder + "/nugetDownloadResult.json") == false)
203+
if (ForcePackageCache)
150204
{
151-
var jsonToWrite = JsonConvert.SerializeObject(nugetDownloadResult);
205+
try
206+
{
207+
var jsonToWrite = JsonConvert.SerializeObject(nugetDownloadResult, Formatting.Indented);
152208

153-
await File.WriteAllTextAsync(PackagesFolder + "/nugetDownloadResult.json", jsonToWrite);
209+
await File.WriteAllTextAsync(NugetDownloadResultFilePath, jsonToWrite);
210+
211+
logger?.LogDebug($"Stored downloaded package details to {NugetDownloadResultFilePath}");
212+
}
213+
catch (Exception e)
214+
{
215+
logger?.LogError($"Failed to store downloaded package details to {NugetDownloadResultFilePath}: {e}");
216+
}
154217
}
155218
}
156219
}

src/Weikio.PluginFramework.Catalogs.NuGet/NugetPluginCatalogOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ public class NugetPluginCatalogOptions
3333
/// Gets or sets the target platform. If not set, EntryAssembly's target framework is used.
3434
/// </summary>
3535
public string TargetFramework { get; set; } = Defaults.TargetFramework;
36+
37+
/// <summary>
38+
/// Gets or sets if Plugin Framework should take care of package caching. In some cases Nuget will download already downloaded package. This flag
39+
/// tries to make sure that the package is downloaded only once. Note: Requires that PackagesFolder is set
40+
/// </summary>
41+
public bool ForcePackageCaching { get; set; } = false;
42+
43+
/// <summary>
44+
/// Gets or sets the folder where package is installed. Defaults to unique random temp path.
45+
/// </summary>
46+
public string CustomPackagesFolder { get; set; } = string.Empty;
3647

3748
public static class Defaults
3849
{

src/Weikio.PluginFramework.Catalogs.NuGet/Weikio.PluginFramework.Catalogs.NuGet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
55
<IsPackable>true</IsPackable>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Description>NuGet catalog for Plugin Framework allows you to use NuGet packages as plugins with Plugin Framework.</Description>

src/Weikio.PluginFramework.Catalogs.Roslyn/Weikio.PluginFramework.Catalogs.Roslyn.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
55
<IsPackable>true</IsPackable>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Description>Roslyn catalog for Plugin Framework allows you to use Roslyn scripts as plugins with Plugin Framework.</Description>

src/Weikio.PluginFramework.Configuration/Weikio.PluginFramework.Configuration.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
55
<nullable>enable</nullable>
66
<IsPackable>true</IsPackable>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

0 commit comments

Comments
 (0)