-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathConfigMerger.cs
More file actions
103 lines (87 loc) · 4.48 KB
/
ConfigMerger.cs
File metadata and controls
103 lines (87 loc) · 4.48 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
// -----------------------------------------------------------------------
// <copyright file="ConfigMerger.cs" company="Petabridge, LLC">
// Copyright (C) 2025 - 2025 Petabridge, LLC <https://petabridge.com>
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Linq;
namespace Incrementalist.Cmd.Config
{
/// <summary>
/// Helper class for merging and processing configuration settings.
/// </summary>
public static class ConfigMerger
{
/// <summary>
/// Merges command-line options with configuration file settings.
/// Command-line options take precedence over configuration file values.
/// </summary>
/// <param name="options">The command-line options.</param>
/// <param name="config">The configuration file settings.</param>
/// <returns>A new SlnOptions instance with the merged values.</returns>
public static SlnOptions Merge(SlnOptions options, IncrementalistConfig? config)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
if (config == null)
return options;
// PrepareForMerge the current options
var merged = options.PrepareForMerge();
// Merge string properties (CLI takes precedence)
merged.SolutionFilePath = options.SolutionFilePath ?? config.SolutionFilePath;
merged.OutputFile = options.OutputFile ?? config.OutputFile;
merged.GitBranch = (options.GitBranch ?? config.GitBranch) ?? "dev";
merged.WorkingDirectory = options.WorkingDirectory ?? config.WorkingDirectory;
merged.NameApplicationToStart = options.NameApplicationToStart ?? config.NameApplicationToStart;;
// Merge bool properties (config takes precedence)
merged.Verbose = config.Verbose.GetValueOrDefault(false);
merged.ContinueOnError = config.ContinueOnError.GetValueOrDefault(true);
merged.RunInParallel = config.RunInParallel.GetValueOrDefault(false);
merged.FailOnNoProjects = config.FailOnNoProjects.GetValueOrDefault(false);
merged.SkipGlobs = MergeGlobs(options.SkipGlobs?.ToArray(), config.SkipGlob);
merged.TargetGlobs = MergeGlobs(options.TargetGlobs?.ToArray(), config.TargetGlob);
// Merge int properties (config takes precedence)
merged.TimeoutMinutes = config.TimeoutMinutes.GetValueOrDefault(2);
merged.ParallelLimit = config.ParallelLimit;
// Override with any non-default CLI values
if (options.Verbose) merged.Verbose = true;
if (!options.ContinueOnError) merged.ContinueOnError = false;
if (options.RunInParallel) merged.RunInParallel = true;
if (options.FailOnNoProjects) merged.FailOnNoProjects = true;
if (options.TimeoutMinutes != 2) merged.TimeoutMinutes = options.TimeoutMinutes;
// Bugfix for https://github.com/petabridge/Incrementalist/issues/381 and
// https://github.com/petabridge/Incrementalist/issues/380
if (options.ConfigFile != null) merged.ConfigFile = options.ConfigFile;
return merged;
}
private static string[] MergeGlobs(string[]? primary, string[]? secondary)
{
if(primary is { Length: > 0 })
return primary;
if (secondary is { Length: > 0 })
return secondary;
return [];
}
/// <summary>
/// Apply default values to any null properties.
/// </summary>
/// <param name="options">The options to apply defaults to.</param>
/// <returns>The same instance with defaults applied.</returns>
public static SlnOptions ApplyDefaults(SlnOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
// Default values for string properties
options.GitBranch ??= "dev";
// Default values for int properties
if (options.TimeoutMinutes == 0)
options.TimeoutMinutes = 2;
// Default values for bool properties
// These are already initialized to their default values by C#
// but we'll set them explicitly for clarity
if (!options.ContinueOnError) // Default is true
options.ContinueOnError = true;
return options;
}
}
}