@@ -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 System.Text .Json
2126
2227 /// <summary>
2328 /// .NET Core SDK default install directory (set to default SDK installer paths
@@ -39,64 +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-
68- let json =
69- JsonDocument.Parse( content, JsonDocumentOptions( CommentHandling = JsonCommentHandling.Skip))
70-
71- let sdk = json.RootElement.GetProperty( " sdk" )
72-
73- match sdk.TryGetProperty( " version" ) with
74- | false , _ -> None
75- | true , version -> Some( version.GetString())
76- with exn ->
77- failwithf " Could not parse `sdk.version` from global.json at '%s ': %s " globalJson.FullName exn.Message
78-
79-
80- /// <summary>
81- /// Gets the DotNet SDK from the global.json, starts searching in the given directory.
82- /// </summary>
83- let internal getSDKVersionFromGlobalJsonDir startDir : string =
84- tryGetSDKVersionFromGlobalJsonDir startDir
85- |> function
86- | Some version -> version
87- | None -> failwithf " global.json not found"
88-
8947 /// <summary>
9048 /// Tries the DotNet SDK from the global.json. This file can exist in the working
9149 /// directory or any of the parent directories Returns None if global.json is not found
9250 /// </summary>
93- let tryGetSDKVersionFromGlobalJson () : string option = tryGetSDKVersionFromGlobalJsonDir " ."
51+ let tryGetSDKVersionFromGlobalJson () : string option =
52+ GlobalJson.tryGetSDKVersionFromGlobalJson ()
9453
9554 /// <summary>
9655 /// Gets the DotNet SDK from the global.json. This file can exist in the working
9756 /// directory or any of the parent directories
9857 /// </summary>
99- let getSDKVersionFromGlobalJson () : string = getSDKVersionFromGlobalJsonDir " ."
58+ let getSDKVersionFromGlobalJson () : string =
59+ GlobalJson.getSDKVersionFromGlobalJson ()
10060
10161 /// <summary>
10262 /// Get dotnet cli executable path. Probes the provided path first, then as a fallback tries the system PATH
@@ -698,7 +658,7 @@ module DotNet =
698658 | UsePreviousFile
699659 | ReplaceWith of string list
700660
701- let internal runRaw ( firstArg : FirstArgReplacement ) options ( c : CreateProcess < 'a >) =
661+ let internal runRaw ( firstArg : FirstArgReplacement ) ( options : Options ) ( c : CreateProcess < 'a >) =
702662 //let timeout = TimeSpan.MaxValue
703663 let results = System.Collections.Generic.List< ConsoleMessage>()
704664
@@ -809,6 +769,64 @@ module DotNet =
809769 |> runRaw ( FirstArgReplacement.ReplaceWith firstArgs) options
810770 |> CreateProcess.map fst
811771
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+
812830 /// <summary>
813831 /// Setup the environment (<c>PATH</c> and <c>DOTNET_ROOT</c>) in such a way that started processes use the given
814832 /// dotnet SDK installation. This is useful for example when using fable,
@@ -919,62 +937,6 @@ module DotNet =
919937 __. MarkSuccess()
920938 { RID = rid.Value }
921939
922-
923- /// <summary>
924- /// dotnet --version command options
925- /// </summary>
926- type VersionOptions =
927- {
928- /// Common tool options
929- Common: Options
930- }
931-
932- /// Parameter default values.
933- static member Create () =
934- { Common = Options.Create() .WithRedirectOutput true }
935-
936- /// Gets the current environment
937- member x.Environment = x.Common.Environment
938-
939- /// Changes the "Common" properties according to the given function
940- member inline x.WithCommon f = { x with Common = f x.Common }
941-
942- /// Sets the current environment variables.
943- member x.WithEnvironment map =
944- x.WithCommon( fun c -> { c with Environment = map })
945-
946- /// Sets a value indicating whether the output for the given process is redirected.
947- member x.WithRedirectOutput shouldRedirect =
948- { x with Common = x.Common.WithRedirectOutput shouldRedirect }
949-
950-
951- /// <summary>
952- /// dotnet info result
953- /// </summary>
954- type VersionResult = string
955-
956- /// <summary>
957- /// Execute dotnet --version command
958- /// </summary>
959- ///
960- /// <param name="setParams">set version command parameters</param>
961- let getVersion setParams =
962- use __ = Trace.traceTask " DotNet:version" " running dotnet --version"
963- let param = VersionOptions.Create() |> setParams
964- let args = " --version"
965- let result = exec ( fun _ -> param.Common) " " args
966-
967- if not result.OK then
968- failwithf " dotnet --version failed with code %i " result.ExitCode
969-
970- let version = result.Messages |> String.separated " \n " |> String.trim
971-
972- if String.isNullOrWhiteSpace version then
973- failwithf " could not read version from output: \n %s " ( String.Join( " \n " , result.Messages))
974-
975- __. MarkSuccess()
976- version
977-
978940 /// <summary>
979941 /// Install .NET Core SDK if required
980942 /// </summary>
@@ -2076,3 +2038,4 @@ module DotNet =
20762038 | false -> failwithf $" dotnet new --uninstall failed with code %i {result.ExitCode}"
20772039
20782040 __. MarkSuccess()
2041+ #endif
0 commit comments