Skip to content

Commit

Permalink
Experiment: Separate out the System.Configuration.ConfigurationManage…
Browse files Browse the repository at this point in the history
…r support
  • Loading branch information
Numpsy committed Mar 17, 2024
1 parent 5fb339f commit 5b94ad7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 56 deletions.
20 changes: 13 additions & 7 deletions Argu.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2002
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Argu", "src\Argu\Argu.fsproj", "{D3E958F9-5FFF-462E-BDC7-A03272A9F5D9}"
EndProject
Expand Down Expand Up @@ -62,32 +62,38 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "resource", "resource", "{B1
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{A15390A5-BC39-433F-BA25-8775B4C2E329}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Argu.ConfigurationManager", "src\Argu.ConfigurationManager\Argu.ConfigurationManager.fsproj", "{0EA62775-1A5D-46CD-A21B-2120BA73FBF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Release|Any CPU.Build.0 = Release|Any CPU
{D3E958F9-5FFF-462E-BDC7-A03272A9F5D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3E958F9-5FFF-462E-BDC7-A03272A9F5D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3E958F9-5FFF-462E-BDC7-A03272A9F5D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3E958F9-5FFF-462E-BDC7-A03272A9F5D9}.Release|Any CPU.Build.0 = Release|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0563A30-4451-4DF2-9D21-26C76EB85C22}.Release|Any CPU.Build.0 = Release|Any CPU
{8E234746-5098-4400-98F1-62C77AB91711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E234746-5098-4400-98F1-62C77AB91711}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E234746-5098-4400-98F1-62C77AB91711}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E234746-5098-4400-98F1-62C77AB91711}.Release|Any CPU.Build.0 = Release|Any CPU
{0EA62775-1A5D-46CD-A21B-2120BA73FBF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0EA62775-1A5D-46CD-A21B-2120BA73FBF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EA62775-1A5D-46CD-A21B-2120BA73FBF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EA62775-1A5D-46CD-A21B-2120BA73FBF5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E0563A30-4451-4DF2-9D21-26C76EB85C22} = {A15390A5-BC39-433F-BA25-8775B4C2E329}
{EE42293D-9398-4EF4-B151-9DE992EBB85A} = {83FE66F8-6E59-4178-8962-11117B51F39C}
{10ED3503-22F2-42EF-8B35-C979A5B700D0} = {C9E28045-41C6-422E-9928-4220EF3FF19B}
{E0563A30-4451-4DF2-9D21-26C76EB85C22} = {A15390A5-BC39-433F-BA25-8775B4C2E329}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {99215464-9AB5-4BC3-A033-92EA250F8FC1}
Expand Down
25 changes: 25 additions & 0 deletions src/Argu.ConfigurationManager/Argu.ConfigurationManager.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="Library.fs" />
</ItemGroup>

<ItemGroup>
<!-- SourceLink etc -->
<PackageReference Include="DotNet.ReproducibleBuilds" PrivateAssets="All" />

<PackageReference Include="FSharp.Core" />

<PackageReference Include="System.Configuration.ConfigurationManager" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Argu\Argu.fsproj" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions src/Argu.ConfigurationManager/Library.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Argu

open System.Configuration
open System.IO

/// AppSettings XML configuration reader
type AppSettingsConfigurationReader () =
interface IConfigurationReader with
member _.Name = "AppSettings configuration reader"
member _.GetValue(key:string) = ConfigurationManager.AppSettings[key]

/// AppSettings XML configuration reader
type AppSettingsConfigurationFileReader private (xmlPath : string, kv : KeyValueConfigurationCollection) =
member _.Path = xmlPath
interface IConfigurationReader with
member _.Name = sprintf "App.config configuration reader: %s" xmlPath
member _.GetValue(key:string) =
match kv[key] with
| null -> null
| entry -> entry.Value

/// Create used supplied XML file path
static member Create(path : string) =
if not <| File.Exists path then raise <| FileNotFoundException(path)
let fileMap = ExeConfigurationFileMap()
fileMap.ExeConfigFilename <- path
let config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
AppSettingsConfigurationFileReader(path, config.AppSettings.Settings)

[<AutoOpen>]
module ConfigurationReaderExtensions =
open System.Reflection
open System

/// Configuration reader implementations
type ConfigurationReader with

/// Create a configuration reader instance using the application's resident AppSettings configuration
static member FromAppSettings() = AppSettingsConfigurationReader() :> IConfigurationReader
/// Create a configuration reader instance using a local xml App.Config file
static member FromAppSettingsFile(path : string) = AppSettingsConfigurationFileReader.Create(path) :> IConfigurationReader
/// Create a configuration reader instance using the location of an assembly file
static member FromAppSettings(assembly : Assembly) =
let path = assembly.Location
if String.IsNullOrEmpty path then
sprintf "Assembly location for '%O' is null or empty." assembly.Location
|> invalidArg assembly.FullName

AppSettingsConfigurationFileReader.Create(path + ".config") :> IConfigurationReader
2 changes: 0 additions & 2 deletions src/Argu/Argu.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@
<PackageReference Include="DotNet.ReproducibleBuilds" PrivateAssets="All"/>

<PackageReference Include="FSharp.Core"/>

<PackageReference Include="System.Configuration.ConfigurationManager"/>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Argu/ArgumentParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
let configurationReader =
match configurationReader with
| Some c -> c
| None -> ConfigurationReader.FromAppSettings()
| None -> ConfigurationReader.NullReader

try
let appSettingsResults = parseKeyValueConfig configurationReader argInfo
Expand Down
39 changes: 0 additions & 39 deletions src/Argu/ConfigReaders.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace Argu

open System
open System.IO
open System.Configuration
open System.Collections.Generic
open System.Reflection

/// Abstract key/value configuration reader
type IConfigurationReader =
Expand Down Expand Up @@ -56,30 +53,6 @@ type FunctionConfigurationReader (configFunc : string -> string option, ?name :
| None -> null
| Some v -> v

/// AppSettings XML configuration reader
type AppSettingsConfigurationReader () =
interface IConfigurationReader with
member _.Name = "AppSettings configuration reader"
member _.GetValue(key:string) = ConfigurationManager.AppSettings[key]

/// AppSettings XML configuration reader
type AppSettingsConfigurationFileReader private (xmlPath : string, kv : KeyValueConfigurationCollection) =
member _.Path = xmlPath
interface IConfigurationReader with
member _.Name = sprintf "App.config configuration reader: %s" xmlPath
member _.GetValue(key:string) =
match kv[key] with
| null -> null
| entry -> entry.Value

/// Create used supplied XML file path
static member Create(path : string) =
if not <| File.Exists path then raise <| FileNotFoundException(path)
let fileMap = ExeConfigurationFileMap()
fileMap.ExeConfigFilename <- path
let config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
AppSettingsConfigurationFileReader(path, config.AppSettings.Settings)

/// Configuration reader implementations
type ConfigurationReader =
/// Create a configuration reader that always returns null
Expand All @@ -97,15 +70,3 @@ type ConfigurationReader =
static member FromEnvironmentVariables() =
EnvironmentVariableConfigurationReader() :> IConfigurationReader

/// Create a configuration reader instance using the application's resident AppSettings configuration
static member FromAppSettings() = AppSettingsConfigurationReader() :> IConfigurationReader
/// Create a configuration reader instance using a local xml App.Config file
static member FromAppSettingsFile(path : string) = AppSettingsConfigurationFileReader.Create(path) :> IConfigurationReader
/// Create a configuration reader instance using the location of an assembly file
static member FromAppSettings(assembly : Assembly) =
let path = assembly.Location
if String.IsNullOrEmpty path then
sprintf "Assembly location for '%O' is null or empty." assembly.Location
|> invalidArg assembly.FullName

AppSettingsConfigurationFileReader.Create(path + ".config") :> IConfigurationReader
15 changes: 8 additions & 7 deletions tests/Argu.Tests/Argu.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Tests.fs"/>
<Compile Include="Tests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Argu\Argu.fsproj"/>
<ProjectReference Include="..\..\src\Argu.ConfigurationManager\Argu.ConfigurationManager.fsproj" />
<ProjectReference Include="..\..\src\Argu\Argu.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" VersionOverride="6.0.7"/>
<PackageReference Include="FSharp.Core" VersionOverride="6.0.7" />

<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="Unquote"/>
<PackageReference Include="xunit"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Unquote" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>
</Project>

0 comments on commit 5b94ad7

Please sign in to comment.