Skip to content

Commit 0c13673

Browse files
committed
custom value object for bool to make binding maybe work?
1 parent ea8a905 commit 0c13673

10 files changed

+82
-21
lines changed

src/Microsoft.Extensions.Configuration.DotnetCli/Models/BuildConfiguration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ public sealed class BuildConfiguration
1212
/// Gets or sets whether to run MSBuild out of process.
1313
/// Mapped from DOTNET_CLI_RUN_MSBUILD_OUTOFPROC environment variable.
1414
/// </summary>
15-
public bool RunMSBuildOutOfProc { get; set; } = false;
15+
public FlexibleBool RunMSBuildOutOfProc { get; set; } = false;
1616

1717
/// <summary>
1818
/// Gets or sets whether to use the MSBuild server for builds.
1919
/// Mapped from DOTNET_CLI_USE_MSBUILD_SERVER environment variable.
2020
/// </summary>
21-
public bool UseMSBuildServer { get; set; } = false;
21+
public FlexibleBool UseMSBuildServer { get; set; } = false;
2222

2323
/// <summary>
2424
/// Gets or sets the configuration for the MSBuild terminal logger.
@@ -30,11 +30,11 @@ public sealed class BuildConfiguration
3030
/// Gets or sets whether to disable publish and pack release configuration.
3131
/// Mapped from DOTNET_CLI_DISABLE_PUBLISH_AND_PACK_RELEASE environment variable.
3232
/// </summary>
33-
public bool DisablePublishAndPackRelease { get; set; } = false;
33+
public FlexibleBool DisablePublishAndPackRelease { get; set; } = false;
3434

3535
/// <summary>
3636
/// Gets or sets whether to enable lazy publish and pack release for solutions.
3737
/// Mapped from DOTNET_CLI_LAZY_PUBLISH_AND_PACK_RELEASE_FOR_SOLUTIONS environment variable.
3838
/// </summary>
39-
public bool LazyPublishAndPackReleaseForSolutions { get; set; } = false;
39+
public FlexibleBool LazyPublishAndPackReleaseForSolutions { get; set; } = false;
4040
}

src/Microsoft.Extensions.Configuration.DotnetCli/Models/CliUserExperienceConfiguration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public sealed class CliUserExperienceConfiguration
1414
/// Gets or sets whether telemetry collection is disabled.
1515
/// Mapped from DOTNET_CLI_TELEMETRY_OPTOUT environment variable.
1616
/// </summary>
17-
public bool TelemetryOptOut { get; set; } = CompileOptions.TelemetryOptOutDefault;
17+
public FlexibleBool TelemetryOptOut { get; set; } = CompileOptions.TelemetryOptOutDefault;
1818

1919
/// <summary>
2020
/// Gets or sets whether to suppress the .NET logo on startup.
2121
/// Mapped from DOTNET_NOLOGO environment variable.
2222
/// </summary>
23-
public bool NoLogo { get; set; } = false;
23+
public FlexibleBool NoLogo { get; set; } = false;
2424

2525
/// <summary>
2626
/// Gets or sets whether to force UTF-8 encoding for console output.
2727
/// Mapped from DOTNET_CLI_FORCE_UTF8_ENCODING environment variable.
2828
/// </summary>
29-
public bool ForceUtf8Encoding { get; set; } = false;
29+
public FlexibleBool ForceUtf8Encoding { get; set; } = false;
3030

3131
/// <summary>
3232
/// Gets or sets the UI language for the CLI.

src/Microsoft.Extensions.Configuration.DotnetCli/Models/DevelopmentConfiguration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed class DevelopmentConfiguration
1212
/// Gets or sets whether performance logging is enabled.
1313
/// Mapped from DOTNET_CLI_PERF_LOG environment variable.
1414
/// </summary>
15-
public bool PerfLogEnabled { get; set; } = false;
15+
public FlexibleBool PerfLogEnabled { get; set; } = false;
1616

1717
/// <summary>
1818
/// Gets or sets the number of performance log entries to collect.
@@ -30,11 +30,11 @@ public sealed class DevelopmentConfiguration
3030
/// Gets or sets whether to enable verbose context logging.
3131
/// Mapped from DOTNET_CLI_CONTEXT_VERBOSE environment variable.
3232
/// </summary>
33-
public bool ContextVerbose { get; set; } = false;
33+
public FlexibleBool ContextVerbose { get; set; } = false;
3434

3535
/// <summary>
3636
/// Gets or sets whether to allow targeting pack caching.
3737
/// Mapped from DOTNETSDK_ALLOW_TARGETING_PACK_CACHING environment variable.
3838
/// </summary>
39-
public bool AllowTargetingPackCaching { get; set; } = false;
39+
public FlexibleBool AllowTargetingPackCaching { get; set; } = false;
4040
}

src/Microsoft.Extensions.Configuration.DotnetCli/Models/FirstTimeUseConfiguration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ public sealed class FirstTimeUseConfiguration
1212
/// Gets or sets whether to generate ASP.NET Core HTTPS development certificates.
1313
/// Mapped from DOTNET_GENERATE_ASPNET_CERTIFICATE environment variable.
1414
/// </summary>
15-
public bool GenerateAspNetCertificate { get; set; } = true;
15+
public FlexibleBool GenerateAspNetCertificate { get; set; } = true;
1616

1717
/// <summary>
1818
/// Gets or sets whether to add global tools to the PATH.
1919
/// Mapped from DOTNET_ADD_GLOBAL_TOOLS_TO_PATH environment variable.
2020
/// </summary>
21-
public bool AddGlobalToolsToPath { get; set; } = true;
21+
public FlexibleBool AddGlobalToolsToPath { get; set; } = true;
2222

2323
/// <summary>
2424
/// Gets or sets whether to skip the first-time experience setup.
2525
/// Mapped from DOTNET_SKIP_FIRST_TIME_EXPERIENCE environment variable.
2626
/// </summary>
27-
public bool SkipFirstTimeExperience { get; set; } = false;
27+
public FlexibleBool SkipFirstTimeExperience { get; set; } = false;
2828
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Microsoft.Extensions.Configuration.DotnetCli;
4+
5+
public struct FlexibleBool
6+
{
7+
private string _originalValue;
8+
private readonly bool _value;
9+
10+
public FlexibleBool(bool value)
11+
{
12+
_originalValue = value.ToString();
13+
_value = value;
14+
}
15+
public FlexibleBool(int value)
16+
{
17+
_originalValue = value.ToString();
18+
_value = value != 0;
19+
}
20+
21+
public FlexibleBool(string value)
22+
{
23+
_originalValue = value;
24+
if (bool.TryParse(value, out var result))
25+
{
26+
_value = result;
27+
}
28+
else if (string.Equals(value, "yes", StringComparison.OrdinalIgnoreCase))
29+
{
30+
_value = true;
31+
}
32+
else if (string.Equals(value, "no", StringComparison.OrdinalIgnoreCase))
33+
{
34+
_value = false;
35+
}
36+
else if (int.TryParse(value, out var intValue))
37+
{
38+
_value = intValue != 0; // Treat non-zero as true, zero as false
39+
}
40+
else
41+
{
42+
throw new ArgumentException($"Invalid boolean value: {value}");
43+
}
44+
}
45+
46+
public override string ToString()
47+
{
48+
return _originalValue.ToString();
49+
}
50+
51+
public static implicit operator bool(FlexibleBool flexibleBool) => flexibleBool._value;
52+
public static implicit operator FlexibleBool(bool value) => new(value);
53+
public static implicit operator FlexibleBool(int value) => new(value);
54+
public static implicit operator FlexibleBool(string value) => new(value);
55+
public static bool operator true(FlexibleBool myBoolString) => (bool)myBoolString;
56+
public static bool operator false(FlexibleBool myBoolString) => !(bool)myBoolString;
57+
58+
public override bool Equals([NotNullWhen(true)] object? obj) => base.Equals(obj) && obj is FlexibleBool other && _value == other._value;
59+
public override int GetHashCode() => _originalValue.GetHashCode();
60+
61+
}

src/Microsoft.Extensions.Configuration.DotnetCli/Models/NuGetConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public sealed class NuGetConfiguration
1313
/// Mapped from DOTNET_NUGET_SIGNATURE_VERIFICATION environment variable.
1414
/// Defaults to true on Windows and Linux, false elsewhere.
1515
/// </summary>
16-
public bool SignatureVerificationEnabled { get; set; } = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
16+
public FlexibleBool SignatureVerificationEnabled { get; set; } = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
1717
}

src/Microsoft.Extensions.Configuration.DotnetCli/Models/RuntimeHostConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class RuntimeHostConfiguration
1818
/// Gets or sets whether to enable multilevel lookup for shared frameworks.
1919
/// Mapped from DOTNET_MULTILEVEL_LOOKUP environment variable.
2020
/// </summary>
21-
public bool MultilevelLookup { get; set; } = true;
21+
public FlexibleBool MultilevelLookup { get; set; } = false;
2222

2323
/// <summary>
2424
/// Gets or sets the roll-forward policy for framework version selection.

src/Microsoft.Extensions.Configuration.DotnetCli/Models/SdkResolverConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed class SdkResolverConfiguration
1212
/// Gets or sets whether to enable SDK resolver logging.
1313
/// Mapped from DOTNET_MSBUILD_SDK_RESOLVER_ENABLE_LOG environment variable.
1414
/// </summary>
15-
public bool EnableLog { get; set; } = false;
15+
public FlexibleBool EnableLog { get; set; } = false;
1616

1717
/// <summary>
1818
/// Gets or sets the directory containing SDKs.

src/Microsoft.Extensions.Configuration.DotnetCli/Models/ToolConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public sealed class ToolConfiguration
1212
/// Gets or sets whether to allow tool manifests in the repository root.
1313
/// Mapped from DOTNET_TOOLS_ALLOW_MANIFEST_IN_ROOT environment variable.
1414
/// </summary>
15-
public bool AllowManifestInRoot { get; set; } = false;
15+
public FlexibleBool AllowManifestInRoot { get; set; } = false;
1616
}

src/Microsoft.Extensions.Configuration.DotnetCli/Models/WorkloadConfiguration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed class WorkloadConfiguration
1212
/// Gets or sets whether to disable workload update notifications.
1313
/// Mapped from DOTNET_CLI_WORKLOAD_UPDATE_NOTIFY_DISABLE environment variable.
1414
/// </summary>
15-
public bool UpdateNotifyDisable { get; set; } = false;
15+
public FlexibleBool UpdateNotifyDisable { get; set; } = false;
1616

1717
/// <summary>
1818
/// Gets or sets the interval in hours between workload update notifications.
@@ -24,13 +24,13 @@ public sealed class WorkloadConfiguration
2424
/// Gets or sets whether to disable workload pack groups.
2525
/// Mapped from DOTNET_CLI_WORKLOAD_DISABLE_PACK_GROUPS environment variable.
2626
/// </summary>
27-
public bool DisablePackGroups { get; set; } = false;
27+
public FlexibleBool DisablePackGroups { get; set; } = false;
2828

2929
/// <summary>
3030
/// Gets or sets whether to skip workload integrity checks.
3131
/// Mapped from DOTNET_SKIP_WORKLOAD_INTEGRITY_CHECK environment variable.
3232
/// </summary>
33-
public bool SkipIntegrityCheck { get; set; } = false;
33+
public FlexibleBool SkipIntegrityCheck { get; set; } = false;
3434

3535
/// <summary>
3636
/// Gets or sets the manifest root directories for workloads.
@@ -48,5 +48,5 @@ public sealed class WorkloadConfiguration
4848
/// Gets or sets whether to ignore default manifest roots.
4949
/// Mapped from DOTNETSDK_WORKLOAD_MANIFEST_IGNORE_DEFAULT_ROOTS environment variable.
5050
/// </summary>
51-
public bool ManifestIgnoreDefaultRoots { get; set; } = false;
51+
public FlexibleBool ManifestIgnoreDefaultRoots { get; set; } = false;
5252
}

0 commit comments

Comments
 (0)