@@ -4,20 +4,25 @@ namespace Fake.DotNet
44/// .NET Core + CLI tools helpers
55/// </summary>
66[<RequireQualifiedAccess>]
7+ #if FAKE_ INTERNAL_ DOTNET_ CORE_ CLI
8+ module InternalDotNet =
9+ #else
710module DotNet =
11+ #endif
812
913 // NOTE: The #if can be removed once we have a working release with the "new" API
1014 // Currently we #load this file in build.fsx
1115
1216 open Fake.Core
1317 open Fake.IO
1418 open Fake.IO .FileSystemOperators
19+ #if ! FAKE_ INTERNAL_ DOTNET_ CORE_ CLI
1520 open Fake.DotNet .NuGet
21+ #endif
1622 open System
1723 open System.IO
1824 open System.Security .Cryptography
1925 open System.Text
20- open Newtonsoft.Json .Linq
2126
2227 /// <summary>
2328 /// .NET Core SDK default install directory (set to default SDK installer paths
@@ -39,61 +44,19 @@ module DotNet =
3944 else
4045 @" C:\Program Files\dotnet"
4146
42- /// <summary>
43- /// Tries to get the DotNet SDK from the global.json, starts searching in the given directory.
44- /// Returns None if global.json is not found
45- /// </summary>
46- ///
47- /// <param name="startDir">The directory to start search from</param>
48- let internal tryGetSDKVersionFromGlobalJsonDir startDir : string option =
49- let globalJsonPaths rootDir =
50- let rec loop ( dir : DirectoryInfo ) =
51- seq {
52- match dir.GetFiles " global.json" with
53- | [| json |] -> yield json
54- | _ -> ()
55-
56- if not ( isNull dir.Parent) then
57- yield ! loop dir.Parent
58- }
59-
60- loop ( DirectoryInfo rootDir)
61-
62- match Seq.tryHead ( globalJsonPaths startDir) with
63- | None -> None
64- | Some globalJson ->
65- try
66- let content = File.ReadAllText globalJson.FullName
67- let json = JObject.Parse content
68- let sdk = json.Item( " sdk" ) :?> JObject
69-
70- match sdk.Property( " version" ) with
71- | null -> None
72- | version -> Some( version.Value.ToString())
73- with exn ->
74- failwithf " Could not parse `sdk.version` from global.json at '%s ': %s " globalJson.FullName exn.Message
75-
76-
77- /// <summary>
78- /// Gets the DotNet SDK from the global.json, starts searching in the given directory.
79- /// </summary>
80- let internal getSDKVersionFromGlobalJsonDir startDir : string =
81- tryGetSDKVersionFromGlobalJsonDir startDir
82- |> function
83- | Some version -> version
84- | None -> failwithf " global.json not found"
85-
8647 /// <summary>
8748 /// Tries the DotNet SDK from the global.json. This file can exist in the working
8849 /// directory or any of the parent directories Returns None if global.json is not found
8950 /// </summary>
90- let tryGetSDKVersionFromGlobalJson () : string option = tryGetSDKVersionFromGlobalJsonDir " ."
51+ let tryGetSDKVersionFromGlobalJson () : string option =
52+ GlobalJson.tryGetSDKVersionFromGlobalJson ()
9153
9254 /// <summary>
9355 /// Gets the DotNet SDK from the global.json. This file can exist in the working
9456 /// directory or any of the parent directories
9557 /// </summary>
96- let getSDKVersionFromGlobalJson () : string = getSDKVersionFromGlobalJsonDir " ."
58+ let getSDKVersionFromGlobalJson () : string =
59+ GlobalJson.getSDKVersionFromGlobalJson ()
9760
9861 /// <summary>
9962 /// Get dotnet cli executable path. Probes the provided path first, then as a fallback tries the system PATH
@@ -695,7 +658,7 @@ module DotNet =
695658 | UsePreviousFile
696659 | ReplaceWith of string list
697660
698- let internal runRaw ( firstArg : FirstArgReplacement ) options ( c : CreateProcess < 'a >) =
661+ let internal runRaw ( firstArg : FirstArgReplacement ) ( options : Options ) ( c : CreateProcess < 'a >) =
699662 //let timeout = TimeSpan.MaxValue
700663 let results = System.Collections.Generic.List< ConsoleMessage>()
701664
@@ -806,6 +769,64 @@ module DotNet =
806769 |> runRaw ( FirstArgReplacement.ReplaceWith firstArgs) options
807770 |> CreateProcess.map fst
808771
772+
773+ /// <summary>
774+ /// dotnet --version command options
775+ /// </summary>
776+ type VersionOptions =
777+ {
778+ /// Common tool options
779+ Common: Options
780+ }
781+
782+ /// Parameter default values.
783+ static member Create () =
784+ { Common = Options.Create() .WithRedirectOutput true }
785+
786+ /// Gets the current environment
787+ member x.Environment = x.Common.Environment
788+
789+ /// Changes the "Common" properties according to the given function
790+ member inline x.WithCommon f = { x with Common = f x.Common }
791+
792+ /// Sets the current environment variables.
793+ member x.WithEnvironment map =
794+ x.WithCommon( fun c -> { c with Environment = map })
795+
796+ /// Sets a value indicating whether the output for the given process is redirected.
797+ member x.WithRedirectOutput shouldRedirect =
798+ { x with Common = x.Common.WithRedirectOutput shouldRedirect }
799+
800+
801+ /// <summary>
802+ /// dotnet info result
803+ /// </summary>
804+ type VersionResult = string
805+
806+ /// <summary>
807+ /// Execute dotnet --version command
808+ /// </summary>
809+ ///
810+ /// <param name="setParams">set version command parameters</param>
811+ let getVersion setParams =
812+ use __ = Trace.traceTask " DotNet:version" " running dotnet --version"
813+ let param = VersionOptions.Create() |> setParams
814+ let args = " --version"
815+ let result = exec ( fun _ -> param.Common) " " args
816+
817+ if not result.OK then
818+ failwithf " dotnet --version failed with code %i " result.ExitCode
819+
820+ let version = result.Messages |> String.separated " \n " |> String.trim
821+
822+ if String.isNullOrWhiteSpace version then
823+ failwithf " could not read version from output: \n %s " ( String.Join( " \n " , result.Messages))
824+
825+ __. MarkSuccess()
826+ version
827+
828+ #if ! FAKE_ INTERNAL_ DOTNET_ CORE_ CLI
829+
809830 /// <summary>
810831 /// Setup the environment (<c>PATH</c> and <c>DOTNET_ROOT</c>) in such a way that started processes use the given
811832 /// dotnet SDK installation. This is useful for example when using fable,
@@ -916,62 +937,6 @@ module DotNet =
916937 __. MarkSuccess()
917938 { RID = rid.Value }
918939
919-
920- /// <summary>
921- /// dotnet --version command options
922- /// </summary>
923- type VersionOptions =
924- {
925- /// Common tool options
926- Common: Options
927- }
928-
929- /// Parameter default values.
930- static member Create () =
931- { Common = Options.Create() .WithRedirectOutput true }
932-
933- /// Gets the current environment
934- member x.Environment = x.Common.Environment
935-
936- /// Changes the "Common" properties according to the given function
937- member inline x.WithCommon f = { x with Common = f x.Common }
938-
939- /// Sets the current environment variables.
940- member x.WithEnvironment map =
941- x.WithCommon( fun c -> { c with Environment = map })
942-
943- /// Sets a value indicating whether the output for the given process is redirected.
944- member x.WithRedirectOutput shouldRedirect =
945- { x with Common = x.Common.WithRedirectOutput shouldRedirect }
946-
947-
948- /// <summary>
949- /// dotnet info result
950- /// </summary>
951- type VersionResult = string
952-
953- /// <summary>
954- /// Execute dotnet --version command
955- /// </summary>
956- ///
957- /// <param name="setParams">set version command parameters</param>
958- let getVersion setParams =
959- use __ = Trace.traceTask " DotNet:version" " running dotnet --version"
960- let param = VersionOptions.Create() |> setParams
961- let args = " --version"
962- let result = exec ( fun _ -> param.Common) " " args
963-
964- if not result.OK then
965- failwithf " dotnet --version failed with code %i " result.ExitCode
966-
967- let version = result.Messages |> String.separated " \n " |> String.trim
968-
969- if String.isNullOrWhiteSpace version then
970- failwithf " could not read version from output: \n %s " ( String.Join( " \n " , result.Messages))
971-
972- __. MarkSuccess()
973- version
974-
975940 /// <summary>
976941 /// Install .NET Core SDK if required
977942 /// </summary>
@@ -2073,3 +2038,4 @@ module DotNet =
20732038 | false -> failwithf $" dotnet new --uninstall failed with code %i {result.ExitCode}"
20742039
20752040 __. MarkSuccess()
2041+ #endif
0 commit comments