From d88444a44d11aae8f12b65fc2758d34aa0d46a47 Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Fri, 18 Dec 2015 19:49:43 +0200 Subject: [PATCH] AppSettingsProvider: Added possibility to change the application that is being configured. --- docs/content/AppSettingsProvider.fsx | 15 ++++++++++++++- .../AppSettingsProvider.fs | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/content/AppSettingsProvider.fsx b/docs/content/AppSettingsProvider.fsx index 6d546b97..13466a0d 100644 --- a/docs/content/AppSettingsProvider.fsx +++ b/docs/content/AppSettingsProvider.fsx @@ -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"] \ No newline at end of file +// [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 diff --git a/src/FSharp.Configuration/AppSettingsProvider.fs b/src/FSharp.Configuration/AppSettingsProvider.fs index 3be98726..e5010b1b 100644 --- a/src/FSharp.Configuration/AppSettingsProvider.fs +++ b/src/FSharp.Configuration/AppSettingsProvider.fs @@ -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 section of config file." key) + | null -> raise <| KeyNotFoundException(message = sprintf "Cannot find name %s in section of config file. (%s)" key (getConfig().FilePath)) | settings -> settings.Value let setConfigValue (key, value) = @@ -114,6 +122,15 @@ let internal typedAppSettings (context: Context) = typeDef.AddMember prop + + let executeSelector = + ProvidedMethod(niceName names "SelectExecutableFile", [ProvidedParameter("pathOfExe",typeof);], + typeof, 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, GetterCode = fun _ -> <@@ filePath @@>)