-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigExtensions.cs
More file actions
158 lines (140 loc) · 6.24 KB
/
ConfigExtensions.cs
File metadata and controls
158 lines (140 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#region copyright
/*
ConfigExtensions.cs is part of SimpleMSI.
Copyright (C) 2025 Julian Rossbach
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#endregion
using WixSharp;
namespace SimpleMSI;
/// <summary>
/// Non-public extension methods for <see cref="Config"/>.
/// </summary>
internal static class ConfigExtensions
{
/// <summary>
/// Gets <see cref="Config.GeneralConfig.Guid"/> from the config as a <see cref="Guid"/> instance.
/// </summary>
/// <returns>The parsed <see cref="Guid"/>, or <c>null</c> if invalid</returns>
public static Guid? GetGuid(this Config.GeneralConfig config)
{
bool result = Guid.TryParse(config.Guid, out var guid);
return result ? guid : null;
}
/// <summary>
/// Gets the WixSharp <see cref="Platform"/> from the <see cref="Config.GeneralConfig.Platform"/> attribute.
/// </summary>
/// <returns>The parsed <see cref="Platform"/> or <c>null</c> if invalid</returns>
public static Platform? GetWixPlatform(this Config.GeneralConfig config)
{
return config.Platform switch
{
"x86" => Platform.x86,
"x64" => Platform.x64,
"arm32" => Platform.arm,
"arm64" => Platform.arm64,
//default to x64
null => Platform.x64,
_ => null
};
}
/// <summary>
/// Gets <see cref="Config.GeneralConfig.Version"/> as a <see cref="Version"/> instance.
/// </summary>
/// <returns>The parsed <see cref="Version"/> or <c>null</c> if invalid</returns>
public static Version? GetVersion(this Config.GeneralConfig config)
{
bool result = Version.TryParse(config.Version, out var version);
return result ? version : new(1, 0);
}
/// <summary>
/// Determines the installation scope based on the configuration settings.
/// </summary>
/// <returns>An <see cref="InstallScope"/> value representing the installation scope. Returns <see
/// cref="InstallScope.perMachine"/> if the scope is set to "machine" or is <see langword="null"/>. Returns <see
/// cref="InstallScope.perUser"/> if the scope is set to "user". Returns <see langword="null"/> for any other
/// value.</returns>
public static InstallScope? GetInstallScope(this Config.GeneralConfig config)
{
return config.InstallScope switch
{
"machine" => InstallScope.perMachine,
"user" => InstallScope.perUser,
//default to perMachine
null => InstallScope.perMachine,
_ => null
};
}
/// <summary>
/// Determines the Wix UI mode based on the specified configuration.
/// </summary>
/// <remarks>This method maps the UI mode string from the configuration to a predefined Wix UI mode. If
/// the UI mode is not specified or is invalid, it defaults to <see cref="WUI.WixUI_ProgressOnly"/>.</remarks>
/// <returns>A <see cref="WUI"/> value representing the corresponding Wix UI mode: <see cref="WUI.WixUI_ProgressOnly"/> for
/// "none" or a null value, <see cref="WUI.WixUI_Minimal"/> for "basic", <see cref="WUI.WixUI_InstallDir"/> for
/// "full", or <c>null</c> if the UI mode is unrecognized.</returns>
public static WUI? GetWixUiMode(this Config.GeneralConfig config)
{
return config.UiMode switch
{
"none" => WUI.WixUI_ProgressOnly,
"basic" => WUI.WixUI_Minimal,
"full" => WUI.WixUI_InstallDir,
//default to none
null => WUI.WixUI_ProgressOnly,
_ => null
};
}
/// <summary>
/// Determines the environment variable part from the configuration.
/// </summary>
/// <returns>
/// An <see cref="EnvVarPart"/> value representing the environment variable part: <see cref="EnvVarPart.all"/> for "all" or a null value,
/// A <see cref="EnvVarPart.first"/> for "prefix", <see cref="EnvVarPart.last"/> for "suffix", or <c>null</c> if the part is unrecognized.
/// </returns>
public static EnvVarPart? GetEnvVarPart(this Config.InstallationConfig.EnvVarConfig config)
{
return config.Part switch
{
"all" => EnvVarPart.all,
"prefix" => EnvVarPart.first,
"suffix" => EnvVarPart.last,
//default to all
null => EnvVarPart.all,
_ => null
};
}
/// <summary>
/// Determines the hash algorithm type specified by the file hash configuration.
/// </summary>
/// <remarks>If the configuration specifies an unrecognized algorithm, the method defaults to
/// HashAlgorithmType.sha1.</remarks>
/// <param name="config">The file hash configuration from which to determine the hash algorithm. Cannot be null.</param>
/// <returns>A value of the HashAlgorithmType enumeration that corresponds to the algorithm specified in the configuration.
/// Returns HashAlgorithmType.sha1 if the algorithm is not recognized.</returns>
public static HashAlgorithmType? GetHashAlgorithm(this Config.InstallationConfig.SigningConfig config)
{
return config.HashAlgorithm switch
{
"sha1" => HashAlgorithmType.sha1,
"sha256" => HashAlgorithmType.sha256,
// default to sha1
null => HashAlgorithmType.sha256,
_ => null
};
}
public static StoreType? GetStoreType(this Config.InstallationConfig.SigningConfig config)
{
return config.StoreType switch
{
"sha1" => StoreType.sha1Hash,
"name" => StoreType.commonName,
"pfx" => StoreType.file,
// default to file
null => StoreType.file,
_ => null
};
}
}