Skip to content

Commit

Permalink
SdkAssemblyResolver: Use dotnet --version to resolve host SDK version
Browse files Browse the repository at this point in the history
  • Loading branch information
mclark1129 committed Apr 10, 2022
1 parent 04235f0 commit 48031e6
Show file tree
Hide file tree
Showing 2 changed files with 1,184 additions and 48 deletions.
82 changes: 38 additions & 44 deletions src/app/Fake.Runtime/SdkAssemblyResolver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ type SdkAssemblyResolver(logLevel:Trace.VerboseLevel) =

// following environment variables are used in testing for different scenarios that the SDK resolver
// could encounter, they are not intended to be used other than that!
let CustomDotNetHostPath = Environment.environVarOrDefault "FAKE_SDK_RESOLVER_CUSTOM_DOTNET_PATH" ""
let CustomDotNetHostPath = Environment.environVarOrNone "FAKE_SDK_RESOLVER_CUSTOM_DOTNET_PATH"
let RuntimeResolverResolveMethod = Environment.environVarOrDefault "FAKE_SDK_RESOLVER_RUNTIME_VERSION_RESOLVE_METHOD" ""


let dotnetBinaryName =
if Environment.isUnix then
"dotnet"
else
"dotnet.exe"

member this.LogLevel = logLevel

Expand Down Expand Up @@ -86,57 +91,46 @@ type SdkAssemblyResolver(logLevel:Trace.VerboseLevel) =
Trace.traceError $"Could not get SDK runtime version from cache due to: {ex.Message}"
None

member this.ResolveSdkRuntimeVersion() =

let resolvedSdkVersion =
this.SdkVersionFromGlobalJson
|> Option.get
|> ReleaseVersion

let resolutionMethod =
match not(String.isNullOrEmpty RuntimeResolverResolveMethod) with
| true ->
member this.GetProductReleaseForSdk (version: ReleaseVersion) =
let net60releases =
if RuntimeResolverResolveMethod = "cache" then
// for testing only!
match RuntimeResolverResolveMethod = "cache" with
| true -> this.TryResolveSdkRuntimeVersionFromCache()
| false -> this.TryResolveSdkRuntimeVersionFromNetwork()
| false ->
this.TryResolveSdkRuntimeVersionFromCache ()
else
// this is the default case, we will try the network, if we could not, then we will reach for cached file.
this.TryResolveSdkRuntimeVersionFromNetwork()
|> Option.orElseWith(fun _ ->
this.TryResolveSdkRuntimeVersionFromCache()
)
this.TryResolveSdkRuntimeVersionFromNetwork ()
|> Option.orElseWith(this.TryResolveSdkRuntimeVersionFromCache)

let sdkRelease (release: ProductRelease) =
release.Sdks
|> List.ofSeq
|> List.exists (fun sdk -> sdk.Version.Equals(version))

net60releases |> Option.bind (List.tryFind sdkRelease)

let resolved =
resolutionMethod
|> Option.orElseWith(fun _ ->
failwithf $"Could not find a suitable runtime version matching SDK version: {resolvedSdkVersion.ToString()}"
)
|> Option.get
|> List.tryFind
(fun release ->
release.Sdks
|> List.ofSeq
|> List.exists (fun sdk -> sdk.Version.Equals(resolvedSdkVersion)))
|> Option.get
member this.ResolveSdkRuntimeVersion() =
// Allows dotnet host version to be resolved from a different installation
let versionOptions (o: DotNet.VersionOptions) =
match CustomDotNetHostPath with
| Some path -> o.WithCommon(fun common -> { common with DotNetCliPath = path </> dotnetBinaryName })
| None -> o

let sdkVersion = DotNet.getVersion versionOptions |> ReleaseVersion
match this.GetProductReleaseForSdk sdkVersion with
| None ->
failwithf $"Could not find a suitable .NET 6 runtime version matching SDK version: {sdkVersion.ToString()}"
| Some release ->
if this.LogLevel.PrintVerbose then
Trace.trace $"Resolved runtime version: {release.Runtime.Version.ToString()}"

if this.LogLevel.PrintVerbose then
Trace.tracefn $"resolved runtime version: {resolved.Runtime.Version.ToString()}"
resolved.Runtime.Version.ToString()
release.Runtime.Version.ToString()

member this.SdkReferenceAssemblies() =
let isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
let isMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
let isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
let isUnix = isLinux || isMac

let dotnetBinaryName =
if Environment.isUnix then
"dotnet"
else
"dotnet.exe"

let potentialDotnetHostEnvVars =
[ "DOTNET_HOST_PATH", id // is a full path to dotnet binary
"DOTNET_ROOT", (fun s -> Path.Combine(s, dotnetBinaryName)) // needs dotnet binary appended
Expand Down Expand Up @@ -209,9 +203,9 @@ type SdkAssemblyResolver(logLevel:Trace.VerboseLevel) =
/// Also probes the PATH and checks the default installation locations
/// </summary>
let dotnetRoot =
if not(String.isNullOrEmpty CustomDotNetHostPath) then
Some CustomDotNetHostPath
else
match CustomDotNetHostPath with
| Some _ -> CustomDotNetHostPath
| None ->
tryFindFromEnvVar ()
|> Option.orElseWith tryFindFromPATH
|> Option.orElseWith tryFindFromDefaultDirs
Expand Down
Loading

0 comments on commit 48031e6

Please sign in to comment.