-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore([no ci]/CLI.Installer): Sketch out install process
- Loading branch information
1 parent
3d9d0d1
commit e2270c6
Showing
18 changed files
with
250 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace TcHaxx.Snappy.CLI.Installer; | ||
public static class Constants | ||
{ | ||
public const string TC31_PROFILES_DIRECTORY = @"Components\Plc\Profiles"; | ||
public const string TC31_REPTOOL_EXE = @"Components\Plc\Common\RepTool.exe"; | ||
public const string TC31_GLOB_PROFILE = "TwinCAT PLC Control_Build_*.profile"; | ||
|
||
public const string DEFAULT_OPTION_TCPROFILE = "latest"; | ||
public const string DEFAULT_OPTION_TOOLSPATH = @"%USERPROFILE%\.dotnet\tools\.store\tchaxx.snappy.cli"; | ||
|
||
public static readonly int REPTOOL_EXE_TIMEOUT_MS = 180_000; | ||
} |
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,9 @@ | ||
using Serilog; | ||
using TcHaxx.Snappy.CLI.Installer.Options; | ||
using TcHaxx.Snappy.Common; | ||
|
||
namespace TcHaxx.Snappy.CLI.Installer; | ||
public interface IInstallerService | ||
{ | ||
public Task<ExitCodes> Install(IInstallerOptions options, ILogger? logger); | ||
} |
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,24 @@ | ||
using Serilog; | ||
using TcHaxx.Snappy.CLI.Installer.Options; | ||
using TcHaxx.Snappy.Common; | ||
|
||
namespace TcHaxx.Snappy.CLI.Installer; | ||
public class InstallerService : IInstallerService | ||
{ | ||
public async Task<ExitCodes> Install(IInstallerOptions options, ILogger? logger) | ||
{ | ||
|
||
var tcProfile = TcProfile.GetTwinCatProfile(options, logger); | ||
if (tcProfile is null) | ||
{ | ||
return ExitCodes.E_ERROR; | ||
} | ||
|
||
|
||
var expandedPath = Environment.ExpandEnvironmentVariables(options.ToolsPath); | ||
var sourceDirectoryInfo = new DirectoryInfo(expandedPath ?? options.ToolsPath); | ||
|
||
var exitCode = await RepToolProcess.RunRepToolAsync(tcProfile, sourceDirectoryInfo, logger); | ||
return exitCode; | ||
} | ||
} |
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,9 @@ | ||
namespace TcHaxx.Snappy.CLI.Installer.Options; | ||
internal class DefaultOptions : IInstallerOptions | ||
{ | ||
///<inheritdoc/> | ||
public string TcProfile { get; init; } = Constants.DEFAULT_OPTION_TCPROFILE; | ||
|
||
///<inheritdoc/> | ||
public string ToolsPath { get; init; } = Constants.DEFAULT_OPTION_TOOLSPATH; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/TcHaxx.Snappy.CLI.Installer/Options/IInstallerOptions.cs
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,15 @@ | ||
namespace TcHaxx.Snappy.CLI.Installer.Options; | ||
|
||
public interface IInstallerOptions | ||
{ | ||
/// <summary> | ||
/// TwinCAT profile to use, i.e "latest" or specific version "TwinCAT PLC Control_Build_4024.54" | ||
/// </summary> | ||
public string TcProfile { get; init; } | ||
|
||
/// <summary> | ||
/// Directory, where Dotnet global tools are installed, e.g. %USERPROFILE%\.dotnet\tools. | ||
/// </summary> | ||
public string ToolsPath { get; init; } | ||
|
||
} |
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,11 @@ | ||
using Microsoft.Win32; | ||
|
||
namespace TcHaxx.Snappy.CLI.Installer; | ||
internal static class RegistryHelper | ||
{ | ||
internal static string GetTwincatInstallDirectory() | ||
{ | ||
var regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Beckhoff\\TwinCAT3\\3.1"); | ||
return (regKey?.GetValue("InstallDir") as string) ?? string.Empty; | ||
} | ||
} |
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,69 @@ | ||
using System.Diagnostics; | ||
using Serilog; | ||
using TcHaxx.Snappy.Common; | ||
|
||
namespace TcHaxx.Snappy.CLI.Installer; | ||
internal static class RepToolProcess | ||
{ | ||
internal static async Task<ExitCodes> RunRepToolAsync(TwincatProfile tcProfile, DirectoryInfo sourceDirectory, ILogger? logger) | ||
{ | ||
var tcDir = RegistryHelper.GetTwincatInstallDirectory(); | ||
if (string.IsNullOrWhiteSpace(tcDir)) | ||
{ | ||
logger?.Error("Couldn't read TwinCAT installation directory from Registry."); | ||
return ExitCodes.E_ERROR; | ||
} | ||
|
||
var repToolExe = Path.Join(tcDir, Constants.TC31_REPTOOL_EXE); | ||
if (!File.Exists(repToolExe)) | ||
{ | ||
throw new FileNotFoundException("RepTool.exe doesn't exist.", repToolExe); | ||
} | ||
|
||
try | ||
{ | ||
using var process = new Process(); | ||
process.StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = repToolExe, | ||
Arguments = $"--profile='{tcProfile.Profile}' --installLibsRecursNoOverwrite \"{sourceDirectory.FullName}\"", | ||
UseShellExecute = false, | ||
RedirectStandardInput = false, | ||
RedirectStandardError = true, | ||
RedirectStandardOutput = true | ||
}; | ||
|
||
process.OutputDataReceived += (sender, args) => | ||
{ | ||
if (string.IsNullOrEmpty(args.Data)) | ||
{ | ||
return; | ||
} | ||
logger?.Information("RepTool.exe: {StandardOutput}", args.Data ?? string.Empty); | ||
}; | ||
process.ErrorDataReceived += (sender, args) => | ||
{ | ||
if (string.IsNullOrEmpty(args.Data)) | ||
{ | ||
return; | ||
} | ||
logger?.Error("RepTool.exe: {StandardError}", args.Data ?? string.Empty); | ||
}; | ||
|
||
logger?.Information("Installing TwinCAT libraries ..."); | ||
|
||
process.Start(); | ||
var stdout = await process.StandardOutput.ReadToEndAsync(); | ||
logger?.Information("RepTool.exe: {StandardOutput}", stdout ?? string.Empty); | ||
|
||
process.WaitForExit(Constants.REPTOOL_EXE_TIMEOUT_MS); | ||
return (ExitCodes)process.ExitCode; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger?.Fatal(ex, "Couldn't install TwinCAT libraries."); | ||
return ExitCodes.E_EXCEPTION; | ||
} | ||
} | ||
} |
24 changes: 23 additions & 1 deletion
24
src/TcHaxx.Snappy.CLI.Installer/TcHaxx.Snappy.CLI.Installer.csproj
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 |
---|---|---|
@@ -1,5 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup/> | ||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'"> | ||
<NoWarn>$(NoWarn);CS1591;CS1573;CA1416</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0|AnyCPU'"> | ||
<NoWarn>$(NoWarn);CS1591;CS1573;CA1416</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0|AnyCPU'"> | ||
<NoWarn>$(NoWarn);CS1591;CS1573;CA1416</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0|AnyCPU'"> | ||
<NoWarn>$(NoWarn);CS1591;CS1573;CA1416</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog" Version="3.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TcHaxx.Snappy.Common\TcHaxx.Snappy.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,41 @@ | ||
using Serilog; | ||
using TcHaxx.Snappy.CLI.Installer.Options; | ||
|
||
namespace TcHaxx.Snappy.CLI.Installer; | ||
internal static class TcProfile | ||
{ | ||
internal static TwincatProfile? GetTwinCatProfile(IInstallerOptions options, ILogger? logger) | ||
{ | ||
if (!options.TcProfile.Equals(Constants.DEFAULT_OPTION_TCPROFILE, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
logger?.Information("Using provided TwinCAT profile \"{TwinCatProfile}\"", options.TcProfile); | ||
return new TwincatProfile(options.TcProfile); | ||
} | ||
|
||
var tcDir = RegistryHelper.GetTwincatInstallDirectory(); | ||
if (string.IsNullOrWhiteSpace(tcDir)) | ||
{ | ||
logger?.Error("Couldn't read TwinCAT installation directory from Registry."); | ||
return default; | ||
} | ||
|
||
var profilesDir = Path.Join(tcDir, Constants.TC31_PROFILES_DIRECTORY); | ||
if (!Directory.Exists(profilesDir)) | ||
{ | ||
logger?.Error("Directory \"{TwinCatProfileDirectory}\" doesn't exist.", profilesDir); | ||
return default; | ||
} | ||
|
||
var profiles = Directory.GetFiles(profilesDir, Constants.TC31_GLOB_PROFILE); | ||
if (!profiles.Any()) | ||
{ | ||
logger?.Error("Couldn't find any profiles \"{TwinCatProfileGlob}\" in \"{TwinCatProfileDirectory}\".", | ||
Constants.TC31_GLOB_PROFILE, profilesDir); | ||
} | ||
|
||
var profileToUse = profiles.OrderByDescending(x => x).First(); | ||
logger?.Information("Using TwinCAT profile \"{TwinCatProfile}\"", profileToUse); | ||
return new TwincatProfile(Path.GetFileNameWithoutExtension(profileToUse)); | ||
} | ||
} | ||
|
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,2 @@ | ||
namespace TcHaxx.Snappy.CLI.Installer; | ||
internal record TwincatProfile(string Profile); |
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 |
---|---|---|
@@ -1,8 +1,16 @@ | ||
using CommandLine; | ||
using TcHaxx.Snappy.CLI.Installer.Options; | ||
|
||
namespace TcHaxx.Snappy.CLI.CLI; | ||
|
||
[Verb("install", false, HelpText = "Install TcHaxx.Snappy.library.")] | ||
internal class InstallOptions : BaseOptions | ||
[Verb("install", false, HelpText = "Install snappy.library and dependencies.")] | ||
public class InstallOptions : BaseOptions, IInstallerOptions | ||
{ | ||
///<inheritdoc /> | ||
[Option("tc-profile", Default = Installer.Constants.DEFAULT_OPTION_TCPROFILE, Required = false, HelpText = "TwinCAT profile to use, i.e \"latest\" or specific version \"TwinCAT PLC Control_Build_4024.54\".")] | ||
public string TcProfile { get; init; } = string.Empty; | ||
|
||
///<inheritdoc /> | ||
[Option("tool-path", Default = Installer.Constants.DEFAULT_OPTION_TOOLSPATH, Required = false, HelpText = "Directory, where dotnet global tools are installed.")] | ||
public string ToolsPath { get; init; } = string.Empty; | ||
} |
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
using Serilog; | ||
using TcHaxx.Snappy.CLI.CLI; | ||
using TcHaxx.Snappy.CLI.Installer; | ||
using TcHaxx.Snappy.Common; | ||
|
||
namespace TcHaxx.Snappy.CLI.Commands; | ||
|
||
internal class CommandInstall(ILogger? logger) : ICommandInstall | ||
{ | ||
private readonly ILogger? _logger = logger?.ForContext<CommandInstall>(); | ||
private readonly IInstallerService _installerService = new InstallerService(); | ||
|
||
public async Task<int> RunAndReturnExitCode(InstallOptions options) | ||
public async Task<ExitCodes> RunAndReturnExitCode(InstallOptions options) | ||
{ | ||
_logger?.Information("Installing ..."); | ||
await Task.Delay(1000); | ||
return 0; | ||
return await _installerService.Install(options, _logger); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using TcHaxx.Snappy.CLI.CLI; | ||
using TcHaxx.Snappy.Common; | ||
|
||
namespace TcHaxx.Snappy.CLI.Commands; | ||
|
||
internal interface ICommandInstall | ||
{ | ||
Task<int> RunAndReturnExitCode(InstallOptions options); | ||
Task<ExitCodes> RunAndReturnExitCode(InstallOptions options); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using TcHaxx.Snappy.CLI.CLI; | ||
using TcHaxx.Snappy.Common; | ||
|
||
namespace TcHaxx.Snappy.CLI.Commands; | ||
|
||
internal interface ICommandVerify | ||
{ | ||
Task<int> RunAndReturnExitCode(VerifyOptions options); | ||
Task<ExitCodes> RunAndReturnExitCode(VerifyOptions options); | ||
} |
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
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