Skip to content

Commit

Permalink
AppSettingsProvider: Added possibility to change the application that…
Browse files Browse the repository at this point in the history
… is being configured.
  • Loading branch information
Thorium committed Dec 18, 2015
1 parent 6d80ccd commit d88444a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 14 additions & 1 deletion docs/content/AppSettingsProvider.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ Settings.ConfigFileName

// read a connection string from the config
Settings.ConnectionStrings.Test
// [fsi:val it : string = "Server=.;Database=SomeDatabase;Integrated Security=true"]
// [fsi:val it : string = "Server=.;Database=SomeDatabase;Integrated Security=true"]

(**
Using AppSettingsProvider in *.fsx-script
-----------------------------------------
The default executable is the current project .config. (Which is Fsi.exe.config in F# interactive.)
How ever, if you want to modify the configuration of some other application, you can do with SelectExecutableFile-method:
*)

let path = System.IO.Path.Combine [|__SOURCE_DIRECTORY__ ; "bin"; "myProject.exe" |]
Settings.SelectExecutableFile path
Settings.Test2
19 changes: 18 additions & 1 deletion src/FSharp.Configuration/AppSettingsProvider.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ open System.Globalization
open System.Web.Hosting
open System.Runtime.Caching

let mutable private exePath = ""
let ``Set .exe file path`` filePath =
exePath <- filePath

let private getConfig() =
let path = exePath
if not(String.IsNullOrEmpty path) && System.IO.File.Exists(path) then ConfigurationManager.OpenExeConfiguration path
else

if HostingEnvironment.IsHosted then
Web.Configuration.WebConfigurationManager.OpenWebConfiguration "~"
else ConfigurationManager.OpenExeConfiguration ConfigurationUserLevel.None

let getConfigValue key =
match getConfig().AppSettings.Settings.[key] with
| null -> raise <| KeyNotFoundException(message = sprintf "Cannot find name %s in <appSettings> section of config file." key)
| null -> raise <| KeyNotFoundException(message = sprintf "Cannot find name %s in <appSettings> section of config file. (%s)" key (getConfig().FilePath))
| settings -> settings.Value

let setConfigValue (key, value) =
Expand Down Expand Up @@ -114,6 +122,15 @@ let internal typedAppSettings (context: Context) =

typeDef.AddMember prop


let executeSelector =
ProvidedMethod(niceName names "SelectExecutableFile", [ProvidedParameter("pathOfExe",typeof<string>);],
typeof<Unit>, IsStaticMethod=true,
InvokeCode = (fun args -> <@@ ``Set .exe file path``(%%args.[0]) @@>))
executeSelector.AddXmlDoc "Property to change the executable file that is read for configurations. This idea is that you can manage other executables also (e.g. from script)."
typeDef.AddMember executeSelector


let prop =
ProvidedProperty(niceName names "ConfigFileName", typeof<string>, GetterCode = fun _ -> <@@ filePath @@>)

Expand Down

0 comments on commit d88444a

Please sign in to comment.