@@ -4,20 +4,25 @@ namespace Fake.DotNet
4
4
/// .NET Core + CLI tools helpers
5
5
/// </summary>
6
6
[<RequireQualifiedAccess>]
7
+ #if FAKE_ INTERNAL_ DOTNET_ CORE_ CLI
8
+ module InternalDotNet =
9
+ #else
7
10
module DotNet =
11
+ #endif
8
12
9
13
// NOTE: The #if can be removed once we have a working release with the "new" API
10
14
// Currently we #load this file in build.fsx
11
15
12
16
open Fake.Core
13
17
open Fake.IO
14
18
open Fake.IO .FileSystemOperators
19
+ #if ! FAKE_ INTERNAL_ DOTNET_ CORE_ CLI
15
20
open Fake.DotNet .NuGet
21
+ #endif
16
22
open System
17
23
open System.IO
18
24
open System.Security .Cryptography
19
25
open System.Text
20
- open System.Text .Json
21
26
22
27
/// <summary>
23
28
/// .NET Core SDK default install directory (set to default SDK installer paths
@@ -39,64 +44,19 @@ module DotNet =
39
44
else
40
45
@" C:\Program Files\dotnet"
41
46
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
-
89
47
/// <summary>
90
48
/// Tries the DotNet SDK from the global.json. This file can exist in the working
91
49
/// directory or any of the parent directories Returns None if global.json is not found
92
50
/// </summary>
93
- let tryGetSDKVersionFromGlobalJson () : string option = tryGetSDKVersionFromGlobalJsonDir " ."
51
+ let tryGetSDKVersionFromGlobalJson () : string option =
52
+ GlobalJson.tryGetSDKVersionFromGlobalJson ()
94
53
95
54
/// <summary>
96
55
/// Gets the DotNet SDK from the global.json. This file can exist in the working
97
56
/// directory or any of the parent directories
98
57
/// </summary>
99
- let getSDKVersionFromGlobalJson () : string = getSDKVersionFromGlobalJsonDir " ."
58
+ let getSDKVersionFromGlobalJson () : string =
59
+ GlobalJson.getSDKVersionFromGlobalJson ()
100
60
101
61
/// <summary>
102
62
/// Get dotnet cli executable path. Probes the provided path first, then as a fallback tries the system PATH
@@ -698,7 +658,7 @@ module DotNet =
698
658
| UsePreviousFile
699
659
| ReplaceWith of string list
700
660
701
- let internal runRaw ( firstArg : FirstArgReplacement ) options ( c : CreateProcess < 'a >) =
661
+ let internal runRaw ( firstArg : FirstArgReplacement ) ( options : Options ) ( c : CreateProcess < 'a >) =
702
662
//let timeout = TimeSpan.MaxValue
703
663
let results = System.Collections.Generic.List< ConsoleMessage>()
704
664
@@ -809,6 +769,64 @@ module DotNet =
809
769
|> runRaw ( FirstArgReplacement.ReplaceWith firstArgs) options
810
770
|> CreateProcess.map fst
811
771
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
+
812
830
/// <summary>
813
831
/// Setup the environment (<c>PATH</c> and <c>DOTNET_ROOT</c>) in such a way that started processes use the given
814
832
/// dotnet SDK installation. This is useful for example when using fable,
@@ -919,62 +937,6 @@ module DotNet =
919
937
__. MarkSuccess()
920
938
{ RID = rid.Value }
921
939
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
-
978
940
/// <summary>
979
941
/// Install .NET Core SDK if required
980
942
/// </summary>
@@ -2076,3 +2038,4 @@ module DotNet =
2076
2038
| false -> failwithf $" dotnet new --uninstall failed with code %i {result.ExitCode}"
2077
2039
2078
2040
__. MarkSuccess()
2041
+ #endif
0 commit comments