Skip to content

Commit

Permalink
refactor(CLI.Installer): Rename to TwincatProfileFactory and refactor
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
densogiaichned committed Feb 19, 2024
1 parent e2270c6 commit 1560c30
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/TcHaxx.Snappy.CLI.Installer/InstallerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class InstallerService : IInstallerService
public async Task<ExitCodes> Install(IInstallerOptions options, ILogger? logger)
{

var tcProfile = TcProfile.GetTwinCatProfile(options, logger);
var tcProfile = TwincatProfileFactory.GetTwinCatProfile(options, logger);
if (tcProfile is null)
{
return ExitCodes.E_ERROR;
Expand Down
41 changes: 0 additions & 41 deletions src/TcHaxx.Snappy.CLI.Installer/TcProfile.cs

This file was deleted.

5 changes: 5 additions & 0 deletions src/TcHaxx.Snappy.CLI.Installer/TwincatProfile.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
namespace TcHaxx.Snappy.CLI.Installer;

/// <summary>
/// Represents a TwinCAT PLC Profile.
/// </summary>
/// <param name="Profile">The profile string, e.g. "TwinCAT PLC Control_Build_4024.55"</param>
internal record TwincatProfile(string Profile);
76 changes: 76 additions & 0 deletions src/TcHaxx.Snappy.CLI.Installer/TwincatProfileFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Diagnostics.CodeAnalysis;
using Serilog;
using TcHaxx.Snappy.CLI.Installer.Options;

namespace TcHaxx.Snappy.CLI.Installer;
internal static class TwincatProfileFactory
{
internal static TwincatProfile? GetTwinCatProfile(IInstallerOptions options, ILogger? logger)
{
if (!IsDefaultProfile(options))
{
logger?.Information("Using provided TwinCAT profile \"{TwinCatProfile}\"", options.TcProfile);
return new TwincatProfile(options.TcProfile);
}

if (!TryGetTwincatInstallDirectory(out var tcInstallDir))
{
logger?.Error("Couldn't read TwinCAT installation directory from Registry.");
return default;
}

if (!TryGetProfilesDirectory(tcInstallDir, out var profilesDir))
{
logger?.Error("Directory \"{TwinCatProfileDirectory}\" doesn't exist.", profilesDir);
return default;
}

if (!TryGetProfileToUse(profilesDir, out var profileToUse))
{
logger?.Error("Couldn't find any profiles \"{TwinCatProfileGlob}\" in \"{TwinCatProfileDirectory}\".",
Constants.TC31_GLOB_PROFILE, profilesDir);
return default;
}

logger?.Information("Using TwinCAT profile \"{TwinCatProfile}\"", profileToUse);
return new TwincatProfile(Path.GetFileNameWithoutExtension(profileToUse));
}

internal static bool IsDefaultProfile(IInstallerOptions options)
{
return options.TcProfile.Equals(Constants.DEFAULT_OPTION_TCPROFILE, StringComparison.OrdinalIgnoreCase);
}

internal static bool TryGetTwincatInstallDirectory([NotNullWhen(true)] out string? tcInstallDir)
{
tcInstallDir = RegistryHelper.GetTwincatInstallDirectory();
if (string.IsNullOrWhiteSpace(tcInstallDir))
{
return false;
}
return true;
}

internal static bool TryGetProfilesDirectory(string tcDir, out string profilesDir)
{
profilesDir = Path.Join(tcDir, Constants.TC31_PROFILES_DIRECTORY);
if (!Directory.Exists(profilesDir))
{
return false;
}
return true;
}

internal static bool TryGetProfileToUse(string profilesDir, [NotNullWhen(true)] out string? profileToUse)
{
profileToUse = null;
var profiles = Directory.GetFiles(profilesDir, Constants.TC31_GLOB_PROFILE);
if (!profiles.Any())
{
return false;
}

profileToUse = profiles.OrderByDescending(x => x).First();
return true;
}
}

0 comments on commit 1560c30

Please sign in to comment.