-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split twinpack-registry into commands
- Loading branch information
Showing
7 changed files
with
183 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters