Skip to content

Commit

Permalink
Merge pull request #2220 from severisv/dotnet-use-globaljson-when-fin…
Browse files Browse the repository at this point in the history
…ding-path

DotNet take version into account when determining dotnet cli path
  • Loading branch information
matthid authored Jan 10, 2019
2 parents b6ee1ba + cf3e157 commit 139ea07
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/app/Fake.DotNet.Cli/DotNet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ module DotNet =
then "/usr/local/share/dotnet"
else @"C:\Program Files\dotnet"

/// Gets the DotNet SDK from the global.json, starts searching in the given directory.
let internal getSDKVersionFromGlobalJsonDir startDir : string =
/// Tries to get the DotNet SDK from the global.json, starts searching in the given directory. Returns None if global.json is not found
let internal tryGetSDKVersionFromGlobalJsonDir startDir : string option =
let globalJsonPaths rootDir =
let rec loop (dir: DirectoryInfo) = seq {
match dir.GetFiles "global.json" with
Expand All @@ -43,21 +43,35 @@ module DotNet =

match Seq.tryHead (globalJsonPaths startDir) with
| None ->
failwithf "global.json not found"
None
| Some globalJson ->
try
let content = File.ReadAllText globalJson.FullName
let json = JObject.Parse content
let sdk = json.Item("sdk") :?> JObject
let version = sdk.Property("version").Value.ToString()
version
Some version
with
| exn -> failwithf "Could not parse `sdk.version` from global.json at '%s': %s" globalJson.FullName exn.Message


/// Gets the DotNet SDK from the global.json, starts searching in the given directory.
let internal getSDKVersionFromGlobalJsonDir startDir : string =
tryGetSDKVersionFromGlobalJsonDir startDir
|> function
| Some version -> version
| None -> failwithf "global.json not found"

/// Tries the DotNet SDK from the global.json
/// This file can exist in the working directory or any of the parent directories
/// Returns None if global.json is not found
let tryGetSDKVersionFromGlobalJson() : string option =
tryGetSDKVersionFromGlobalJsonDir "."

/// Gets the DotNet SDK from the global.json
/// This file can exist in the working directory or any of the parent directories
let getSDKVersionFromGlobalJson() : string = getSDKVersionFromGlobalJsonDir "."

let getSDKVersionFromGlobalJson() : string =
getSDKVersionFromGlobalJsonDir "."

/// Get dotnet cli executable path. Probes the provided path first, then as a fallback tries the system PATH
/// ## Parameters
Expand Down Expand Up @@ -487,9 +501,17 @@ module DotNet =
}
static member Create() = {
DotNetCliPath =
let version = tryGetSDKVersionFromGlobalJson()
findPossibleDotnetCliPaths None
|> Seq.tryHead
// shouldn't hit this one because the previous two probe PATH...
|> Seq.tryFind (fun cliPath ->
match version with
| Some version ->
version
|> Path.combine "sdk"
|> Path.combine (Path.getDirectory cliPath)
|> Directory.Exists
| None -> true
)
|> Option.defaultWith (fun () -> if Environment.isUnix then "dotnet" else "dotnet.exe")
WorkingDirectory = Directory.GetCurrentDirectory()
CustomParams = None
Expand Down

0 comments on commit 139ea07

Please sign in to comment.