Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch dotnet5&6 And add auto import Folder #794

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nuget.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="nuget.cdn" value="https://nuget.cdn.azure.cn/v3/index.json" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>

<IsPackable>false</IsPackable>
<NoWarn>IDE0007</NoWarn>
Expand Down
2 changes: 1 addition & 1 deletion samples/BaGetWebApplication/BaGetWebApplication.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/BaGet.Core/Configuration/BaGetOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class BaGetOptions
/// </summary>
public bool IsReadOnlyMode { get; set; } = false;

public string LocalPackages { get; set; }

/// <summary>
/// The URLs the BaGet server will use.
/// As per documentation <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-3.1#server-urls">here (Server URLs)</a>.
Expand Down
2 changes: 2 additions & 0 deletions src/BaGet.Core/Extensions/BaGetApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using BaGet.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand Down
2 changes: 1 addition & 1 deletion src/BaGet.Web/BaGet.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>

<PackageTags>NuGet</PackageTags>
<Description>BaGet's NuGet server implementation</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/BaGet/BaGet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
92 changes: 92 additions & 0 deletions src/BaGet/BackgroundServices/ImportLocalPackagesService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using BaGet.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Timer = System.Timers.Timer;

namespace BaGet.BackgroundServices
{
public class ImportLocalPackagesService : BackgroundService
{
private readonly string _localPackages;
private readonly IServiceProvider _serviceProvider;
private readonly Timer _timer;
private IPackageIndexingService _packageIndexingService;

public ImportLocalPackagesService(IServiceProvider serviceProvider, IConfiguration configuration)
{
_serviceProvider = serviceProvider;
var options = configuration.Get<BaGetOptions>();
_localPackages = options.LocalPackages;
if (!string.IsNullOrWhiteSpace(_localPackages))
{
_localPackages = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _localPackages);

if (!Directory.Exists(_localPackages)) Directory.CreateDirectory(_localPackages);
}

_timer = new Timer(300);
_timer.Elapsed += Timer_Elapsed;
}

private IPackageIndexingService PackageIndexingService
{
get =>
_packageIndexingService ??= _serviceProvider.CreateScope()
.ServiceProvider
.GetRequiredService<IPackageIndexingService>();
set => _packageIndexingService = value;
}

private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
_timer.Stop();
await ImportPackages(default);
}

private const string ExtName = ".nupkg";

private async Task ImportPackages(CancellationToken stoppingToken)
{
var files = Directory.GetFiles(_localPackages);
foreach (var file in files)
{
if (!ExtName.Equals(Path.GetExtension(file)))
{
continue;
}
using (var stream = File.OpenRead(file))
{
await PackageIndexingService.IndexAsync(stream, stoppingToken);
}

File.Delete(file);
}

PackageIndexingService = null;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!string.IsNullOrWhiteSpace(_localPackages))
{
await ImportPackages(stoppingToken);
var watcher = new FileSystemWatcher(_localPackages);
watcher.Created += WatcherCreated;
watcher.EnableRaisingEvents = true;
while (stoppingToken.IsCancellationRequested) await Task.Delay(100, stoppingToken);
}
}

private void WatcherCreated(object sender, FileSystemEventArgs e)
{
_timer.Stop();
_timer.Start();
}
}
}
18 changes: 9 additions & 9 deletions src/BaGet/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50557/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
Expand All @@ -25,5 +17,13 @@
},
"applicationUrl": "http://localhost:50561/"
}
},
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50557/",
"sslPort": 0
}
}
}
}
2 changes: 2 additions & 0 deletions src/BaGet/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BaGet.BackgroundServices;
using BaGet.Core;
using BaGet.Web;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -51,6 +52,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton<IConfigureOptions<MvcRazorRuntimeCompilationOptions>, ConfigureRazorRuntimeCompilation>();

services.AddHostedService<ImportLocalPackagesService>();
services.AddCors();
}

Expand Down
3 changes: 3 additions & 0 deletions src/BaGet/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"PackageDeletionBehavior": "Unlist",
"AllowPackageOverwrites": false,

// Auto Import nupkg files
"LocalPackages": "Sources",

"Database": {
"Type": "Sqlite",
"ConnectionString": "Data Source=baget.db"
Expand Down
5 changes: 3 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<!-- Compiler properties -->
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>7.2</LangVersion>
<!--<LangVersion>7.2</LangVersion>-->

<!-- Don't warn if there are missing XMl comment for publicly visible type or member-->
<NoWarn>$(NoWarn);1591</NoWarn>
Expand All @@ -40,7 +40,8 @@
<MicrosoftAspNetCorePackageVersion>3.1.18</MicrosoftAspNetCorePackageVersion>
<MicrosoftEntityFrameworkCorePackageVersion>3.1.18</MicrosoftEntityFrameworkCorePackageVersion>
<MicrosoftExtensionsPackageVersion>3.1.18</MicrosoftExtensionsPackageVersion>
<NuGetPackageVersion>5.10.0</NuGetPackageVersion>
<NuGetPackageVersion>6.9.1</NuGetPackageVersion>
<NewtonsoftJsonPackageVersion>13.0.3</NewtonsoftJsonPackageVersion>
</PropertyGroup>

<ItemGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' == ''">
Expand Down
2 changes: 1 addition & 1 deletion tests/BaGet.Core.Tests/BaGet.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/BaGet.Protocol.Tests/BaGet.Protocol.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions tests/BaGet.Tests/BaGet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

Expand All @@ -10,8 +10,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
<PackageReference Include="NuGet.Protocol" Version="$(NuGetPackageVersion)" />
<!--<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />-->
<!--<PackageReference Include="NuGet.Protocol" Version="$(NuGetPackageVersion)" />-->
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(MicrosoftAspNetCorePackageVersion)" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion tests/BaGet.Web.Tests/BaGet.Web.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down