-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotNetConsoleDefaults.cs
More file actions
116 lines (101 loc) · 5.01 KB
/
DotNetConsoleDefaults.cs
File metadata and controls
116 lines (101 loc) · 5.01 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
namespace Neolution.DotNet.Console
{
using System;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Neolution.DotNet.Console.Internal;
/// <summary>
/// Provides default configurations and environment setup for the console application.
/// </summary>
internal static class DotNetConsoleDefaults
{
/// <summary>
/// The command argument used to trigger dependency validation.
/// </summary>
internal const string CheckDependenciesCommand = "check-deps";
/// <summary>
/// Determines if the given arguments represent a check-deps run.
/// </summary>
/// <param name="args">The command line arguments.</param>
/// <returns>True if this is a check-deps run, false otherwise.</returns>
public static bool IsCheckDependenciesRun(string[] args)
{
return args.Length == 1 && string.Equals(args[0], CheckDependenciesCommand, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Creates the console environment.
/// </summary>
/// <param name="args">The command line arguments.</param>
/// <returns>The <see cref="IHostEnvironment"/>.</returns>
internal static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "DOTNET_")
.AddCommandLine(args)
.Build();
// The apps root directory is where the appsettings.json are located
var appRootDirectory = AppContext.BaseDirectory;
// Check if this is a check-deps run - if so, always use Development environment
var isCheckDepsRun = IsCheckDependenciesRun(args);
// Default to Production for normal runs, matching ASP.NET Core behavior
// For check-deps, always use Development to ensure appsettings.Development.json is loaded
// Environment can be overridden via DOTNET_ENVIRONMENT or command line arguments
var defaultEnvironment = isCheckDepsRun ? Environments.Development : Environments.Production;
return new DotNetConsoleEnvironment
{
EnvironmentName = isCheckDepsRun ? Environments.Development : (configuration[HostDefaults.EnvironmentKey] ?? defaultEnvironment),
ApplicationName = AppDomain.CurrentDomain.FriendlyName,
ContentRootPath = appRootDirectory,
ContentRootFileProvider = new PhysicalFileProvider(appRootDirectory),
};
}
/// <summary>
/// Applies the default configuration to the console application.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <param name="args">The arguments.</param>
/// <param name="environment">The environment.</param>
/// <returns>The <see cref="IConfiguration" />.</returns>
internal static IConfiguration CreateConsoleConfiguration(Assembly assembly, string[] args, IHostEnvironment environment)
{
return CreateConsoleConfigurationBuilder(assembly, args, environment).Build();
}
/// <summary>
/// Creates the console configuration builder (without building it).
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <param name="args">The arguments.</param>
/// <param name="environment">The environment.</param>
/// <returns>The <see cref="IConfigurationBuilder" />.</returns>
internal static IConfigurationBuilder CreateConsoleConfigurationBuilder(Assembly assembly, string[] args, IHostEnvironment environment)
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddEnvironmentVariables(prefix: "DOTNET_");
AddCommandLineConfig(configurationBuilder, args);
configurationBuilder
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (environment.IsDevelopment())
{
configurationBuilder.AddUserSecrets(assembly, optional: true, reloadOnChange: true);
}
configurationBuilder.AddEnvironmentVariables();
return configurationBuilder;
}
/// <summary>
/// Adds the command line configuration.
/// </summary>
/// <param name="configBuilder">The configuration builder.</param>
/// <param name="args">The arguments.</param>
private static void AddCommandLineConfig(IConfigurationBuilder configBuilder, string[]? args)
{
if (args is { Length: > 0 })
{
configBuilder.AddCommandLine(args);
}
}
}
}