Skip to content

Commit

Permalink
add Config.LoadNonUserConfigs()
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Mar 3, 2024
1 parent 34c830e commit 448e0f2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Source/Components/ImageGlass.Settings/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,10 @@ public static class Config
/// <summary>
/// Loads and parsse configs from file
/// </summary>
public static void Load()
public static void Load(IConfigurationRoot? items = null)
{
#nullable disable
var items = Source.LoadUserConfigs();
items ??= Source.LoadUserConfigs();

// save the config for all tools
ToolSettings = items.GetValueObj(nameof(ToolSettings)).GetValue(nameof(ToolSettings), new ExpandoObject());
Expand Down
52 changes: 42 additions & 10 deletions Source/Components/ImageGlass.Settings/Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public class Source
public bool IsCompatible { get; set; } = true;


/// <summary>
/// Gets user settings from command line arguments
/// </summary>
public static string[] SettingsFromCmdLine => Environment.GetCommandLineArgs()
// filter the command lines begin with '/'
// example: ImageGlass.exe /FrmMainWidth=900
.Where(cmd => cmd.StartsWith(Const.CONFIG_CMD_PREFIX))
.Select(cmd => cmd[1..]) // trim '/' from the command
.ToArray();

#endregion


Expand All @@ -73,13 +83,6 @@ public class Source
/// </summary>
public static IConfigurationRoot LoadUserConfigs()
{
// filter the command lines begin with '/'
// example: ImageGlass.exe /FrmMainWidth=900
var args = Environment.GetCommandLineArgs()
.Where(cmd => cmd.StartsWith(Const.CONFIG_CMD_PREFIX))
.Select(cmd => cmd[1..]) // trim '/' from the command
.ToArray();

var startUpDir = App.StartUpDir();
var configDir = App.ConfigDir(PathType.Dir);

Expand All @@ -106,7 +109,7 @@ public static IConfigurationRoot LoadUserConfigs()
.AddJsonFile(UserFilename, optional: true)

// command line
.AddCommandLine(args)
.AddCommandLine(SettingsFromCmdLine)

.AddConfiguration(adminConfig)
.Build();
Expand All @@ -120,16 +123,45 @@ public static IConfigurationRoot LoadUserConfigs()
var fallBackConfig = new ConfigurationBuilder()
.SetBasePath(startUpDir)
.AddJsonFile(DefaultFilename, optional: true) // igconfig.default.json
.AddCommandLine(args) // command line
.AddCommandLine(SettingsFromCmdLine) // command line
.AddJsonFile(AdminFilename, optional: true) // igconfig.admin.json
.Build();

return fallBackConfig;
}


#endregion
/// <summary>
/// Loads configs, takes pre-defined settings from
/// <see cref="DefaultFilename"/> and <see cref="AdminFilename"/>.
/// </summary>
public static IConfigurationRoot LoadNonUserConfigs()
{
var startUpDir = App.StartUpDir();

// igconfig.default.json
var defaultConfig = new ConfigurationBuilder()
.SetBasePath(startUpDir)
.AddJsonFile(DefaultFilename, optional: true)
.Build();

// admin.igconfig.json
var adminConfig = new ConfigurationBuilder()
.SetBasePath(startUpDir)
.AddJsonFile(AdminFilename, optional: true)
.Build();

// final config
var userConfig = new ConfigurationBuilder()
.AddConfiguration(defaultConfig)
.AddCommandLine(SettingsFromCmdLine)
.AddConfiguration(adminConfig)
.Build();

return userConfig;
}

#endregion

}

Expand Down

0 comments on commit 448e0f2

Please sign in to comment.