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

feat: nuget servers #126

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions TwinpackShared/Protocol/Native/TwinpackServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@

namespace Twinpack.Protocol
{
class NativePackagingServerFactory : IPackagingServerFactory
{
public IPackageServer Create(string name, string uri)
{
return new TwinpackServer(name, uri);
}

public string ServerType { get; } = "Twinpack Repository";
}
public class TwinpackServer : IPackageServer
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
Expand Down
43 changes: 43 additions & 0 deletions TwinpackShared/Protocol/Nuget/BeckhoffServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Twinpack.Models;
using Twinpack.Exceptions;
using System.Reflection;
using AdysTech.CredentialManager;
using System.Threading;
using System.Security.Cryptography;
using System.Runtime.Remoting.Messaging;
using System.Runtime.CompilerServices;

namespace Twinpack.Protocol
{
class BeckhoffPackagingServerFactory : IPackagingServerFactory
{
public IPackageServer Create(string name, string uri)
{
return new BeckhoffServer(name, uri);
}

public string ServerType { get; } = "Beckhoff Repository";
}

public class BeckhoffServer : NugetServer, IPackageServer
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
public new string ServerType { get; } = "Beckhoff Repository";

public BeckhoffServer(string name = "", string url = null) : base(name,url)
{

}
}
}
210 changes: 210 additions & 0 deletions TwinpackShared/Protocol/Nuget/NugetServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Twinpack.Models;
using Twinpack.Exceptions;
using System.Reflection;
using AdysTech.CredentialManager;
using System.Threading;
using System.Security.Cryptography;
using System.Runtime.Remoting.Messaging;
using System.Runtime.CompilerServices;

namespace Twinpack.Protocol
{
class NugetPackagingServerFactory : IPackagingServerFactory
{
public IPackageServer Create(string name, string uri)
{
return new NugetServer(name, uri);
}

public string ServerType { get; } = "NuGet Repository";
}

public class NugetServer : IPackageServer
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
public static string DefaultLibraryCachePath { get { return $@"{Directory.GetCurrentDirectory()}\.Zeugwerk\libraries"; } }
public string ServerType { get; } = "NuGet Repository";
public string Name { get; set; }
public string UrlBase { get; set; }
public string Url
{
get => UrlBase;
}
public string UrlRegister
{
get => UrlBase;
}

public string Username { get; set; }
public string Password { get; set; }
public LoginPostResponse UserInfo { get; set; }
public bool LoggedIn { get { return UserInfo?.User != null; } }
public bool Connected { get { return UserInfo?.UpdateVersion != null; } }

public NugetServer(string name = "", string url = null)
{
Name = name;
UrlBase = url;

try
{
var credentials = CredentialManager.GetCredentials(UrlBase);
Username = credentials?.UserName;
Password = credentials?.Password;
}
catch
{
Username = null;
Password = null;
}
}

public Version ClientVersion
{
get
{
try
{
return Assembly.GetExecutingAssembly()?.GetName()?.Version ?? Assembly.GetEntryAssembly()?.GetName()?.Version;
}
catch (Exception)
{
return new Version("0.0.0.0");
}
}
}

public bool IsClientUpdateAvailable
{
get
{
return UserInfo?.UpdateVersion != null && new Version(UserInfo?.UpdateVersion) > ClientVersion;
}
}

public async Task<PackageVersionGetResponse> PostPackageVersionAsync(PackageVersionPostRequest packageVersion, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task<Tuple<IEnumerable<CatalogItemGetResponse>, bool>> GetCatalogAsync(string search, int page = 1, int perPage = 5, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task<Tuple<IEnumerable<PackageVersionGetResponse>, bool>> GetPackageVersionsAsync(PlcLibrary library, int page = 1, int perPage = 5, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task<Tuple<IEnumerable<PackageVersionGetResponse>, bool>> GetPackageVersionsAsync(PlcLibrary library, string branch, string configuration, string target, int page = 1, int perPage = 5, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}

public async Task<PackageVersionGetResponse> ResolvePackageVersionAsync(PlcLibrary library, string preferredTarget = null, string preferredConfiguration = null, string preferredBranch = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task DownloadPackageVersionAsync(PackageVersionGetResponse packageVersion, ChecksumMode checksumMode, string cachePath = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}

public async Task<PackageVersionGetResponse> GetPackageVersionAsync(PlcLibrary library, string branch, string configuration, string target, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}

public async Task<PackageGetResponse> GetPackageAsync(string distributorName, string packageName, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}

public async Task<PackageVersionGetResponse> PutPackageVersionAsync(PackageVersionPatchRequest package, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}

public async Task<PackageGetResponse> PutPackageAsync(PackagePatchRequest package, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}

public async Task<LoginPostResponse> LoginAsync(string username = null, string password = null, CancellationToken cancellationToken = default)
{
// _client.Invalidate(); // clear the cache
var credentials = CredentialManager.GetCredentials(UrlBase);

Username = username ?? credentials?.UserName;
Password = password ?? credentials?.Password;

// reset token to get a new one
if (UserInfo?.Token != null)
UserInfo.Token = null;


try
{

}
catch
{
DeleteCredential();
throw;
}

return new LoginPostResponse();
}

private void DeleteCredential()
{
UserInfo = new LoginPostResponse();
Username = "";
Password = "";
try
{
CredentialManager.RemoveCredentials(UrlBase);
}
catch { }
}

public void Logout()
{
_logger.Info("Log out from NuGet Server");

UserInfo = new LoginPostResponse();
Username = "";
Password = "";

try
{
CredentialManager.RemoveCredentials(UrlBase);
}
catch (Exception) { }
}

public void InvalidateCache()
{
_logger.Info("Resetting NuGet Cache");
}
}
}
10 changes: 0 additions & 10 deletions TwinpackShared/Protocol/PackagingServerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,4 @@ interface IPackagingServerFactory
IPackageServer Create(string name, string uri);
string ServerType { get; }
}

class NativePackagingServer : IPackagingServerFactory
{
public IPackageServer Create(string name, string uri)
{
return new TwinpackServer(name, uri);
}

public string ServerType { get; } = "Twinpack Repository";
}
}
5 changes: 4 additions & 1 deletion TwinpackShared/Protocol/PackagingServerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class PackagingServerRegistry
public static async Task InitializeAsync()
{
_filePath = Environment.ExpandEnvironmentVariables(_filePath);
_factories = new List<IPackagingServerFactory>() { new NativePackagingServer() };
_factories = new List<IPackagingServerFactory>() { new NativePackagingServerFactory() };
_factories = new List<IPackagingServerFactory>() { new NugetPackagingServerFactory() };
_factories = new List<IPackagingServerFactory>() { new BeckhoffPackagingServerFactory() };
_servers = new PackageServerCollection();

try
Expand All @@ -32,6 +34,7 @@ public static async Task InitializeAsync()
catch
{
await AddServerAsync("Twinpack Repository", "twinpack.dev", TwinpackServer.DefaultUrlBase);
await AddServerAsync("Beckhoff Repository", "public.tcpkg.beckhoff-cloud.com (stable)", "https://public.tcpkg.beckhoff-cloud.com/api/v1/feeds/stable");
}
}
public static IEnumerable<string> ServerTypes { get { return _factories.Select(x => x.ServerType); } }
Expand Down
5 changes: 2 additions & 3 deletions TwinpackShared/TwinpackShared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Models\SourceRepositories.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\PackagingServer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Protocol\Authentication.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Protocol\Nuget\BeckhoffServer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Protocol\Nuget\NugetServer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Protocol\PackagingServerFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Protocol\Native\CachedHttpClient.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectoryExtension.cs" />
Expand All @@ -34,7 +36,4 @@
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)app.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Protocol\" />
</ItemGroup>
</Project>