diff --git a/src/app/FakeLib/EnvironmentHelper.fs b/src/app/FakeLib/EnvironmentHelper.fs index a92de04427a..5384f769cae 100644 --- a/src/app/FakeLib/EnvironmentHelper.fs +++ b/src/app/FakeLib/EnvironmentHelper.fs @@ -52,6 +52,12 @@ let environVarOrNone name = if String.IsNullOrEmpty var then None else Some var +/// Splits the entries of an environment variable and removes the empty ones. +let splitEnvironVar name = + let var = environVarOrNone name + if var = None then [ ] + else var.Value.Split([| Path.PathSeparator |]) |> Array.toList + /// Retrieves the application settings variable with the given name let appSetting (name : string) = ConfigurationManager.AppSettings.[name] diff --git a/src/app/FakeLib/FileSystemHelper.fs b/src/app/FakeLib/FileSystemHelper.fs index 4ea77b6b15d..6d57a63ce94 100644 --- a/src/app/FakeLib/FileSystemHelper.fs +++ b/src/app/FakeLib/FileSystemHelper.fs @@ -97,3 +97,9 @@ let isDirectory path = /// Detects whether the given path is a file. let isFile path = isDirectory path |> not + +/// Detects whether the given path does not contains invalid characters. +let isValidPath (path:string) = + Path.GetInvalidPathChars() + |> Array.filter (fun char -> path.Contains(char.ToString())) + |> Array.isEmpty diff --git a/src/app/FakeLib/ProcessHelper.fs b/src/app/FakeLib/ProcessHelper.fs index 63a1d291451..7027b916bc6 100644 --- a/src/app/FakeLib/ProcessHelper.fs +++ b/src/app/FakeLib/ProcessHelper.fs @@ -180,6 +180,13 @@ let ExecProcessElevated cmd args timeOut = si.FileName <- cmd si.UseShellExecute <- true) timeOut +/// Gets the list of valid directories included in the PATH environment variable. +let pathDirectories = + splitEnvironVar "PATH" + |> Seq.map (fun value -> value.Trim()) + |> Seq.filter (fun value -> not <| isNullOrEmpty value) + |> Seq.filter isValidPath + /// Sets the environment Settings for the given startInfo. /// Existing values will be overriden. /// [omit] @@ -339,8 +346,8 @@ let findFile dirs file = /// ## Parameters /// - `exe` - The executable file to locate let tryFindFileOnPath (file : string) : string option = - Environment.GetEnvironmentVariable("PATH").Split([| Path.PathSeparator |]) - |> Seq.append ["."] + pathDirectories + |> Seq.append [ "." ] |> fun path -> tryFindFile path file /// Returns the AppSettings for the key - Splitted on ; diff --git a/src/app/FakeLib/RestorePackageHelper.fs b/src/app/FakeLib/RestorePackageHelper.fs index cc3cd426486..903007333ed 100755 --- a/src/app/FakeLib/RestorePackageHelper.fs +++ b/src/app/FakeLib/RestorePackageHelper.fs @@ -13,6 +13,7 @@ let findNuget defaultPath = currentDirectory @@ ".nuget" currentDirectory @@ "packages" @@ "NuGet.Commandline" @@ "tools" currentDirectory @@ "packages" @@ "Nuget.Commandline" @@ "tools"] + |> Seq.append pathDirectories let exeNames = ["nuget.exe"; "NuGet.exe"; "Nuget.exe"]