Skip to content

Commit d485e2d

Browse files
committed
Rearrange some things to try to reduce MSBuild dependencies in fake-cli
1 parent 73fc961 commit d485e2d

File tree

5 files changed

+145
-114
lines changed

5 files changed

+145
-114
lines changed

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

Lines changed: 70 additions & 107 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 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

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module internal GlobalJson
2+
3+
open System.IO
4+
open System.Text.Json
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+
32+
let json =
33+
JsonDocument.Parse(content, JsonDocumentOptions(CommentHandling = JsonCommentHandling.Skip))
34+
35+
let sdk = json.RootElement.GetProperty("sdk")
36+
37+
match sdk.TryGetProperty("version") with
38+
| false, _ -> None
39+
| true, version -> Some(version.GetString())
40+
with exn ->
41+
failwithf "Could not parse `sdk.version` from global.json at '%s': %s" globalJson.FullName exn.Message
42+
43+
44+
45+
/// <summary>
46+
/// Gets the DotNet SDK from the global.json, starts searching in the given directory.
47+
/// </summary>
48+
let internal getSDKVersionFromGlobalJsonDir startDir : string =
49+
tryGetSDKVersionFromGlobalJsonDir startDir
50+
|> function
51+
| Some version -> version
52+
| None -> failwithf "global.json not found"
53+
54+
/// <summary>
55+
/// Tries the DotNet SDK from the global.json. This file can exist in the working
56+
/// directory or any of the parent directories Returns None if global.json is not found
57+
/// </summary>
58+
let tryGetSDKVersionFromGlobalJson () : string option = tryGetSDKVersionFromGlobalJsonDir "."
59+
60+
/// <summary>
61+
/// Gets the DotNet SDK from the global.json. This file can exist in the working
62+
/// directory or any of the parent directories
63+
/// </summary>
64+
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: 5 additions & 4 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
@@ -315,7 +315,7 @@ type SdkAssemblyResolver(logLevel: Trace.VerboseLevel) =
315315
this.GetProductReleasesForSdk version |> List.tryHead
316316

317317
member this.ResolveSdkRuntimeVersions() =
318-
let versionOptions dotnetRoot (options: DotNet.VersionOptions) =
318+
let versionOptions dotnetRoot (options: InternalDotNet.VersionOptions) =
319319
// If a custom CLI path is provided, configure the version command
320320
// to use that path. This really only accomodates a test scenarios
321321
// in which FAKE_SDK_RESOLVER_CUSTOM_DOTNET_PATH is set.
@@ -328,10 +328,11 @@ type SdkAssemblyResolver(logLevel: Trace.VerboseLevel) =
328328

329329
let sdkVersions =
330330
if Array.isEmpty dotnetRoots then
331-
[ DotNet.getVersion (versionOptions None) |> ReleaseVersion ]
331+
[ InternalDotNet.getVersion (versionOptions None) |> ReleaseVersion ]
332332
else
333333
dotnetRoots
334-
|> Seq.map (fun dotnetRoot -> DotNet.getVersion (versionOptions (Some dotnetRoot)) |> ReleaseVersion)
334+
|> Seq.map (fun dotnetRoot ->
335+
InternalDotNet.getVersion (versionOptions (Some dotnetRoot)) |> ReleaseVersion)
335336
|> Seq.toList
336337

337338
let productReleases =

0 commit comments

Comments
 (0)