-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bang some things into a different shape
- Loading branch information
Showing
5 changed files
with
139 additions
and
110 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module internal GlobalJson | ||
|
||
open System.IO | ||
open Newtonsoft.Json.Linq | ||
|
||
/// <summary> | ||
/// Tries to get the DotNet SDK from the global.json, starts searching in the given directory. | ||
/// Returns None if global.json is not found | ||
/// </summary> | ||
/// | ||
/// <param name="startDir">The directory to start search from</param> | ||
let internal tryGetSDKVersionFromGlobalJsonDir startDir : string option = | ||
let globalJsonPaths rootDir = | ||
let rec loop (dir: DirectoryInfo) = | ||
seq { | ||
match dir.GetFiles "global.json" with | ||
| [| json |] -> yield json | ||
| _ -> () | ||
|
||
if not (isNull dir.Parent) then | ||
yield! loop dir.Parent | ||
} | ||
|
||
loop (DirectoryInfo rootDir) | ||
|
||
match Seq.tryHead (globalJsonPaths startDir) with | ||
| None -> None | ||
| Some globalJson -> | ||
try | ||
let content = File.ReadAllText globalJson.FullName | ||
let json = JObject.Parse content | ||
let sdk = json.Item("sdk") :?> JObject | ||
|
||
match sdk.Property("version") with | ||
| null -> None | ||
| version -> Some(version.Value.ToString()) | ||
with exn -> | ||
failwithf "Could not parse `sdk.version` from global.json at '%s': %s" globalJson.FullName exn.Message | ||
|
||
|
||
/// <summary> | ||
/// Gets the DotNet SDK from the global.json, starts searching in the given directory. | ||
/// </summary> | ||
let internal getSDKVersionFromGlobalJsonDir startDir : string = | ||
tryGetSDKVersionFromGlobalJsonDir startDir | ||
|> function | ||
| Some version -> version | ||
| None -> failwithf "global.json not found" | ||
|
||
/// <summary> | ||
/// 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 | ||
/// </summary> | ||
let tryGetSDKVersionFromGlobalJson () : string option = tryGetSDKVersionFromGlobalJsonDir "." | ||
|
||
/// <summary> | ||
/// Gets the DotNet SDK from the global.json. This file can exist in the working | ||
/// directory or any of the parent directories | ||
/// </summary> | ||
let getSDKVersionFromGlobalJson () : string = getSDKVersionFromGlobalJsonDir "." |
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