Skip to content

Commit

Permalink
refactor: split twinpack-registry into commands
Browse files Browse the repository at this point in the history
  • Loading branch information
iadonkey committed Jul 27, 2024
1 parent ea73ad1 commit 264bb29
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 112 deletions.
1 change: 0 additions & 1 deletion TwinpackCli/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public abstract class Command
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

protected Core.TwinpackService _twinpack;

public abstract Task<int> ExecuteAsync();
}
}
27 changes: 27 additions & 0 deletions TwinpackRegistry/Commands/Command.cs
Original file line number Diff line number Diff line change
@@ -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<int> ExecuteAsync();
}
}
37 changes: 37 additions & 0 deletions TwinpackRegistry/Commands/DumpCommand.cs
Original file line number Diff line number Diff line change
@@ -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<int> 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;
}
}
}
53 changes: 53 additions & 0 deletions TwinpackRegistry/Commands/PullCommand.cs
Original file line number Diff line number Diff line change
@@ -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<int> 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;
}
}
}
42 changes: 42 additions & 0 deletions TwinpackRegistry/Commands/UpdateDownloads.cs
Original file line number Diff line number Diff line change
@@ -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<int> 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;
}
}
}
128 changes: 19 additions & 109 deletions TwinpackRegistry/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -93,50 +28,11 @@ static int Main(string[] args)

try
{
return Parser.Default.ParseArguments<PullOptions, UpdateDownloadsOptions, DumpOptions>(args)
return Parser.Default.ParseArguments<PullCommand, UpdateDownloadsCommand, DumpCommand>(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)
Expand All @@ -160,5 +56,19 @@ static int Main(string[] args)
_logger.Info("-------------------------------------------------------------------------");
}
}

private static async Task<int> ExecuteAsync<T>(T command) where T : Commands.Command
{
var exitCode = await command.ExecuteAsync();

if (exitCode == 0)
{
_logger.Info("-------------------------------------------------------------------------");
_logger.Info("SUCCESS");
_logger.Info("-------------------------------------------------------------------------");
}

return exitCode;
}
}
}
7 changes: 5 additions & 2 deletions TwinpackRegistry/TwinpackRegistry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<LangVersion>8</LangVersion>

<LangVersion>8</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -63,6 +62,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\Command.cs" />
<Compile Include="Commands\DumpCommand.cs" />
<Compile Include="Commands\UpdateDownloads.cs" />
<Compile Include="Commands\PullCommand.cs" />
<Compile Include="IconUtils.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 264bb29

Please sign in to comment.