forked from fsprojects/Argu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment: Separate out the System.Configuration.ConfigurationManage…
…r support
- Loading branch information
Showing
7 changed files
with
96 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Argu.ConfigurationManager/Argu.ConfigurationManager.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters