Skip to content

Commit 0b68c7d

Browse files
committed
Bang some things into a different shape
1 parent 050e199 commit 0b68c7d

File tree

5 files changed

+139
-110
lines changed

5 files changed

+139
-110
lines changed

src/app/Fake.DotNet.Cli/DotNet.fs

Lines changed: 70 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -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
710
module 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

src/app/Fake.DotNet.Cli/Fake.DotNet.Cli.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</PropertyGroup>
1818
<ItemGroup>
1919
<Compile Include="VisibleTo.fs" />
20+
<Compile Include="GlobalJson.fs" />
2021
<Compile Include="DotNet.fs" />
2122
<Compile Include="CreateProcessExt.fs" />
2223
</ItemGroup>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module internal GlobalJson
2+
3+
open System.IO
4+
open Newtonsoft.Json.Linq
5+
6+
/// <summary>
7+
/// Tries to get the DotNet SDK from the global.json, starts searching in the given directory.
8+
/// Returns None if global.json is not found
9+
/// </summary>
10+
///
11+
/// <param name="startDir">The directory to start search from</param>
12+
let internal tryGetSDKVersionFromGlobalJsonDir startDir : string option =
13+
let globalJsonPaths rootDir =
14+
let rec loop (dir: DirectoryInfo) =
15+
seq {
16+
match dir.GetFiles "global.json" with
17+
| [| json |] -> yield json
18+
| _ -> ()
19+
20+
if not (isNull dir.Parent) then
21+
yield! loop dir.Parent
22+
}
23+
24+
loop (DirectoryInfo rootDir)
25+
26+
match Seq.tryHead (globalJsonPaths startDir) with
27+
| None -> None
28+
| Some globalJson ->
29+
try
30+
let content = File.ReadAllText globalJson.FullName
31+
let json = JObject.Parse content
32+
let sdk = json.Item("sdk") :?> JObject
33+
34+
match sdk.Property("version") with
35+
| null -> None
36+
| version -> Some(version.Value.ToString())
37+
with exn ->
38+
failwithf "Could not parse `sdk.version` from global.json at '%s': %s" globalJson.FullName exn.Message
39+
40+
41+
/// <summary>
42+
/// Gets the DotNet SDK from the global.json, starts searching in the given directory.
43+
/// </summary>
44+
let internal getSDKVersionFromGlobalJsonDir startDir : string =
45+
tryGetSDKVersionFromGlobalJsonDir startDir
46+
|> function
47+
| Some version -> version
48+
| None -> failwithf "global.json not found"
49+
50+
/// <summary>
51+
/// Tries the DotNet SDK from the global.json. This file can exist in the working
52+
/// directory or any of the parent directories Returns None if global.json is not found
53+
/// </summary>
54+
let tryGetSDKVersionFromGlobalJson () : string option = tryGetSDKVersionFromGlobalJsonDir "."
55+
56+
/// <summary>
57+
/// Gets the DotNet SDK from the global.json. This file can exist in the working
58+
/// directory or any of the parent directories
59+
/// </summary>
60+
let getSDKVersionFromGlobalJson () : string = getSDKVersionFromGlobalJsonDir "."

src/app/Fake.Runtime/Fake.Runtime.fsproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
4-
<DefineConstants>$(DefineConstants);CORE_CLR;DOTNETCORE;EXPLICIT_DEPENDENCIES;NETSTANDARD;FAKE_RUNTIME</DefineConstants>
4+
<DefineConstants>$(DefineConstants);CORE_CLR;DOTNETCORE;EXPLICIT_DEPENDENCIES;NETSTANDARD;FAKE_RUNTIME;FAKE_INTERNAL_DOTNET_CORE_CLI</DefineConstants>
55
<AssemblyName>Fake.Runtime</AssemblyName>
66
<NoWarn>FS3186</NoWarn>
77
</PropertyGroup>
@@ -13,6 +13,8 @@
1313
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
1414
</PropertyGroup>
1515
<ItemGroup>
16+
<Compile Include="..\Fake.DotNet.Cli\GlobalJson.fs" Link="GlobalJson.fs" />
17+
<Compile Include="..\Fake.DotNet.Cli\DotNet.fs" Link="DotNet.fs" />
1618
<Compile Include="YaafFSharpScripting.fs" />
1719
<Compile Include="ThisAssemblyInfo.fs" />
1820
<Compile Include="..\Fake.Core.Process\CmdLineParsing.fs" />
@@ -36,7 +38,7 @@
3638
</ItemGroup>
3739
<ItemGroup>
3840
<ProjectReference Include="..\Fake.Core.Context\Fake.Core.Context.fsproj" />
39-
<ProjectReference Include="..\Fake.DotNet.Cli\Fake.DotNet.Cli.fsproj" />
41+
<ProjectReference Include="..\Fake.Core.Process\Fake.Core.Process.fsproj" />
4042
<ProjectReference Include="..\Fake.IO.FileSystem\Fake.IO.FileSystem.fsproj" />
4143
<ProjectReference Include="..\Fake.Core.DependencyManager.Paket\Fake.Core.DependencyManager.Paket.fsproj" />
4244
</ItemGroup>

src/app/Fake.Runtime/SdkAssemblyResolver.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type SdkAssemblyResolver(logLevel: Trace.VerboseLevel) =
6666
member this.SdkVersion = this.SdkVersions |> Seq.head
6767
member this.PaketFrameworkIdentifier = this.PaketFrameworkIdentifiers |> Seq.head
6868

69-
member this.SdkVersionFromGlobalJson = DotNet.tryGetSDKVersionFromGlobalJson ()
69+
member this.SdkVersionFromGlobalJson = GlobalJson.tryGetSDKVersionFromGlobalJson ()
7070

7171
member this.IsSdkVersionFromGlobalJsonSameAsSdkVersion() =
7272
match this.SdkVersionFromGlobalJson with
@@ -217,7 +217,7 @@ type SdkAssemblyResolver(logLevel: Trace.VerboseLevel) =
217217
this.GetProductReleasesForSdk version |> List.tryHead
218218

219219
member this.ResolveSdkRuntimeVersions() =
220-
let versionOptions (options: DotNet.VersionOptions) =
220+
let versionOptions (options: InternalDotNet.VersionOptions) =
221221
// If a custom CLI path is provided, configure the version command
222222
// to use that path. This really only accomodates a test scenarios
223223
// in which FAKE_SDK_RESOLVER_CUSTOM_DOTNET_PATH is set.
@@ -226,7 +226,7 @@ type SdkAssemblyResolver(logLevel: Trace.VerboseLevel) =
226226
options.WithCommon(fun common -> { common with DotNetCliPath = root </> this.DotNetBinaryName })
227227
| None -> options
228228

229-
let sdkVersion = DotNet.getVersion versionOptions |> ReleaseVersion
229+
let sdkVersion = InternalDotNet.getVersion versionOptions |> ReleaseVersion
230230

231231
match this.GetProductReleasesForSdk sdkVersion with
232232
| [] ->

0 commit comments

Comments
 (0)