From 264bb296cfad91ee64cffee77453d78483b5adbf Mon Sep 17 00:00:00 2001 From: Donkey Date: Sat, 27 Jul 2024 22:45:30 +0200 Subject: [PATCH] refactor: split twinpack-registry into commands --- TwinpackCli/Commands/Command.cs | 1 - TwinpackRegistry/Commands/Command.cs | 27 ++++ TwinpackRegistry/Commands/DumpCommand.cs | 37 ++++++ TwinpackRegistry/Commands/PullCommand.cs | 53 ++++++++ TwinpackRegistry/Commands/UpdateDownloads.cs | 42 ++++++ TwinpackRegistry/Program.cs | 128 +++---------------- TwinpackRegistry/TwinpackRegistry.csproj | 7 +- 7 files changed, 183 insertions(+), 112 deletions(-) create mode 100644 TwinpackRegistry/Commands/Command.cs create mode 100644 TwinpackRegistry/Commands/DumpCommand.cs create mode 100644 TwinpackRegistry/Commands/PullCommand.cs create mode 100644 TwinpackRegistry/Commands/UpdateDownloads.cs diff --git a/TwinpackCli/Commands/Command.cs b/TwinpackCli/Commands/Command.cs index ebaa7da..ef57a0a 100644 --- a/TwinpackCli/Commands/Command.cs +++ b/TwinpackCli/Commands/Command.cs @@ -9,7 +9,6 @@ public abstract class Command private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); protected Core.TwinpackService _twinpack; - public abstract Task ExecuteAsync(); } } diff --git a/TwinpackRegistry/Commands/Command.cs b/TwinpackRegistry/Commands/Command.cs new file mode 100644 index 0000000..747453b --- /dev/null +++ b/TwinpackRegistry/Commands/Command.cs @@ -0,0 +1,27 @@ +using NLog; +using System; +using System.Threading.Tasks; + + +namespace Twinpack.Commands +{ + public abstract class Command + { + protected static readonly Logger _logger = LogManager.GetCurrentClassLogger(); + + protected Protocol.TwinpackServer _twinpackServer = new Protocol.TwinpackServer(); + + protected async Task LoginAsync(string username, string password) + { + // no need to login without credentials + if (username == null || password == null) + return; + + await _twinpackServer.LoginAsync(username, password); + if (!_twinpackServer.LoggedIn) + throw new Exception("Login to Twinpack Server failed!"); + } + + public abstract Task ExecuteAsync(); + } +} diff --git a/TwinpackRegistry/Commands/DumpCommand.cs b/TwinpackRegistry/Commands/DumpCommand.cs new file mode 100644 index 0000000..364a403 --- /dev/null +++ b/TwinpackRegistry/Commands/DumpCommand.cs @@ -0,0 +1,37 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.IO.Compression; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Twinpack.Commands +{ + [Verb("dump", HelpText = "")] + public class DumpCommand : Command + { + [Option('p', "path", Required = false, Default = "Twinpack-Registry", HelpText = "")] + public string Path { get; set; } + + public override async Task ExecuteAsync() + { + _logger.Info(">>> twinpack-registry:dump"); + + foreach (var file in Directory.GetFiles(Path)) + { + try + { + using (var memoryStream = new MemoryStream(File.ReadAllBytes(file))) + using (var zipArchive = new ZipArchive(memoryStream)) + { + var libraryInfo = LibraryReader.Read(File.ReadAllBytes(file), dumpFilenamePrefix: file); + } + } + catch (Exception) { } + } + return 0; + } + } +} diff --git a/TwinpackRegistry/Commands/PullCommand.cs b/TwinpackRegistry/Commands/PullCommand.cs new file mode 100644 index 0000000..2eaf025 --- /dev/null +++ b/TwinpackRegistry/Commands/PullCommand.cs @@ -0,0 +1,53 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Twinpack.Commands +{ + [Verb("pull", HelpText = "Downloads packages that are references in .Zeugwerk/config.json to .Zeugwerk/libraries, you can use RepTool.exe to install them into the TwinCAT library repository.")] + public class PullCommand : Command + { + [Option('u', "username", Required = false, Default = null, HelpText = "Username for Twinpack Server")] + public string Username { get; set; } + + [Option('p', "password", Required = false, Default = null, HelpText = "Password for Twinpack Server")] + public string Password { get; set; } + + [Option('r', "owner", Required = false, Default = "Zeugwerk", HelpText = "")] + public string RegistryOwner { get; set; } + + [Option('r', "name", Required = false, Default = "Twinpack-Registry", HelpText = "")] + public string RegistryName { get; set; } + + [Option('d', "dry-run", Required = false, Default = false, HelpText = "")] + public bool DryRun { get; set; } + + [Option('D', "dump", Required = false, Default = false, HelpText = "")] + public bool Dump { get; set; } + + [Option('t', "token", Required = false, Default = false, HelpText = "")] + public string Token { get; set; } + + public override async Task ExecuteAsync() + { + _logger.Info(">>> twinpack-registry:pull"); + + var registry = new TwinpackRegistry(_twinpackServer); + await LoginAsync(Username, Password); + + _logger.Info(new string('-', 3) + $" download"); + await registry.DownloadAsync(RegistryOwner, RegistryName, token: Token); + + if (!DryRun) + { + _logger.Info(new string('-', 3) + $" push"); + await _twinpackServer.PushAsync(TwinpackUtils.PlcProjectsFromConfig(compiled: false, target: "TC3.1"), "Release", "main", "TC3.1", null, false); + } + + return 0; + } + } +} diff --git a/TwinpackRegistry/Commands/UpdateDownloads.cs b/TwinpackRegistry/Commands/UpdateDownloads.cs new file mode 100644 index 0000000..5257286 --- /dev/null +++ b/TwinpackRegistry/Commands/UpdateDownloads.cs @@ -0,0 +1,42 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Twinpack.Commands +{ + [Verb("update-downloads", HelpText = "Downloads packages that are references in .Zeugwerk/config.json to .Zeugwerk/libraries, you can use RepTool.exe to install them into the TwinCAT library repository.")] + public class UpdateDownloadsCommand : Command + { + [Option('u', "username", Required = false, Default = null, HelpText = "Username for Twinpack Server")] + public string Username { get; set; } + + [Option('p', "password", Required = false, Default = null, HelpText = "Password for Twinpack Server")] + public string Password { get; set; } + + [Option('r', "owner", Required = false, Default = "Zeugwerk", HelpText = "")] + public string RegistryOwner { get; set; } + + [Option('r', "name", Required = false, Default = "Twinpack-Registry", HelpText = "")] + public string RegistryName { get; set; } + + [Option('d', "dry-run", Required = false, Default = false, HelpText = "")] + public bool DryRun { get; set; } + + [Option('t', "token", Required = false, Default = null, HelpText = "")] + public string Token { get; set; } + + public override async Task ExecuteAsync() + { + _logger.Info(">>> twinpack-registry:update-downloads"); + var registry = new TwinpackRegistry(_twinpackServer); + + await LoginAsync(Username, Password); + await registry.UpdateDownloadsAsync(RegistryOwner, RegistryName, token: Token, dryRun: DryRun); + + return 0; + } + } +} diff --git a/TwinpackRegistry/Program.cs b/TwinpackRegistry/Program.cs index 4aaf131..442caa0 100644 --- a/TwinpackRegistry/Program.cs +++ b/TwinpackRegistry/Program.cs @@ -6,80 +6,15 @@ using System.IO.Compression; using System.Linq; using System.Threading.Tasks; +using Twinpack.Commands; using Twinpack.Models; namespace Twinpack { class Program { - [Verb("pull", HelpText = "Downloads packages that are references in .Zeugwerk/config.json to .Zeugwerk/libraries, you can use RepTool.exe to install them into the TwinCAT library repository.")] - public class PullOptions - { - [Option('u', "username", Required = false, Default = null, HelpText = "Username for Twinpack Server")] - public string Username { get; set; } - - [Option('p', "password", Required = false, Default = null, HelpText = "Password for Twinpack Server")] - public string Password { get; set; } - - [Option('r', "owner", Required = false, Default = "Zeugwerk", HelpText = "")] - public string RegistryOwner { get; set; } - - [Option('r', "name", Required = false, Default = "Twinpack-Registry", HelpText = "")] - public string RegistryName { get; set; } - - [Option('d', "dry-run", Required = false, Default = false, HelpText = "")] - public bool DryRun { get; set; } - - [Option('D', "dump", Required = false, Default = false, HelpText = "")] - public bool Dump { get; set; } - - [Option('t', "token", Required = false, Default = false, HelpText = "")] - public string Token { get; set; } - } - - [Verb("update-downloads", HelpText = "Downloads packages that are references in .Zeugwerk/config.json to .Zeugwerk/libraries, you can use RepTool.exe to install them into the TwinCAT library repository.")] - public class UpdateDownloadsOptions - { - [Option('u', "username", Required = false, Default = null, HelpText = "Username for Twinpack Server")] - public string Username { get; set; } - - [Option('p', "password", Required = false, Default = null, HelpText = "Password for Twinpack Server")] - public string Password { get; set; } - - [Option('r', "owner", Required = false, Default = "Zeugwerk", HelpText = "")] - public string RegistryOwner { get; set; } - - [Option('r', "name", Required = false, Default = "Twinpack-Registry", HelpText = "")] - public string RegistryName { get; set; } - - [Option('d', "dry-run", Required = false, Default = false, HelpText = "")] - public bool DryRun { get; set; } - - [Option('t', "token", Required = false, Default = false, HelpText = "")] - public string Token { get; set; } - } - - [Verb("dump", HelpText = "")] - public class DumpOptions - { - [Option('p', "path", Required = false, Default = "Twinpack-Registry", HelpText = "")] - public string Path { get; set; } - } - private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); - private static Protocol.TwinpackServer _twinpackServer = new Protocol.TwinpackServer(); - - static void Login(string username, string password) - { - // no need to login without credentials - if (username == null || password == null) - return; - - _twinpackServer.LoginAsync(username, password).Wait(); - if (!_twinpackServer.LoggedIn) - throw new Exception("Login to Twinpack Server failed!"); - } [STAThread] static int Main(string[] args) @@ -93,50 +28,11 @@ static int Main(string[] args) try { - return Parser.Default.ParseArguments(args) + return Parser.Default.ParseArguments(args) .MapResult( - (PullOptions opts) => - { - _logger.Info(">>> twinpack-registry:pull"); - - Login(opts.Username, opts.Password); - _logger.Info(new string('-', 3) + $" download"); - new TwinpackRegistry(_twinpackServer).DownloadAsync(opts.RegistryOwner, opts.RegistryName, token: opts.Token).GetAwaiter().GetResult(); - - if(!opts.DryRun) - { - _logger.Info(new string('-', 3) + $" push"); - _twinpackServer.PushAsync(TwinpackUtils.PlcProjectsFromConfig(compiled: false, target: "TC3.1"), "Release", "main", "TC3.1", null, false).GetAwaiter().GetResult(); - } - - return 0; - }, - (UpdateDownloadsOptions opts) => - { - _logger.Info(">>> twinpack-registry:update-downloads"); - - Login(opts.Username, opts.Password); - new TwinpackRegistry(_twinpackServer).UpdateDownloadsAsync(opts.RegistryOwner, opts.RegistryName, token: opts.Token, dryRun: opts.DryRun).GetAwaiter().GetResult(); - - return 0; - }, - (DumpOptions opts) => - { - _logger.Info(">>> twinpack-registry:dump"); - - foreach (var file in Directory.GetFiles(opts.Path)) - { - try - { - using (var memoryStream = new MemoryStream(File.ReadAllBytes(file))) - using (var zipArchive = new ZipArchive(memoryStream)) - { - var libraryInfo = LibraryReader.Read(File.ReadAllBytes(file), dumpFilenamePrefix: file); - } - } catch(Exception) { } - } - return 0; - }, + (PullCommand command) => ExecuteAsync(command).GetAwaiter().GetResult(), + (UpdateDownloadsCommand command) => ExecuteAsync(command).GetAwaiter().GetResult(), + (DumpCommand command) => ExecuteAsync(command).GetAwaiter().GetResult(), errs => 1); } catch (Exception ex) @@ -160,5 +56,19 @@ static int Main(string[] args) _logger.Info("-------------------------------------------------------------------------"); } } + + private static async Task ExecuteAsync(T command) where T : Commands.Command + { + var exitCode = await command.ExecuteAsync(); + + if (exitCode == 0) + { + _logger.Info("-------------------------------------------------------------------------"); + _logger.Info("SUCCESS"); + _logger.Info("-------------------------------------------------------------------------"); + } + + return exitCode; + } } } diff --git a/TwinpackRegistry/TwinpackRegistry.csproj b/TwinpackRegistry/TwinpackRegistry.csproj index f4539e4..d916df1 100644 --- a/TwinpackRegistry/TwinpackRegistry.csproj +++ b/TwinpackRegistry/TwinpackRegistry.csproj @@ -13,8 +13,7 @@ true true - 8 - + 8 AnyCPU @@ -63,6 +62,10 @@ + + + +