-
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.
refactor(CLI.Installer): Rename to TwincatProfileFactory and refactor
Closes #3
- Loading branch information
1 parent
e2270c6
commit 1560c30
Showing
4 changed files
with
82 additions
and
42 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 was deleted.
Oops, something went wrong.
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,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); |
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,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; | ||
} | ||
} |