Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vswhere to search for vs2017+ install directory to locate msbuild #1752

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app/Fake.Core.Environment/Environment.fs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ module Environment =
| _ -> environVar "ProgramFiles"
|> fun detected -> if isNull detected then @"C:\Program Files (x86)\" else detected

/// The path to vswhere utility which helps determine VS2017+ installation directory
let VSWhere = ProgramFilesX86 + @"\Microsoft Visual Studio\Installer\vswhere.exe"

/// The system root environment variable. Typically "C:\Windows"
let SystemRoot = environVar "SystemRoot"

Expand Down
22 changes: 21 additions & 1 deletion src/app/Fake.DotNet.MSBuild/MSBuild.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,27 @@ module private MSBuildExe =
items |> Seq.map (fun f -> f.Version, f.Paths) |> Map.ofSeq

let private getAllKnownPaths =
(knownMSBuildEntries |> List.collect (fun m -> m.Paths)) @ oldMSBuildLocations
let getVSPathFromVSWhere ver =
try
Process.execWithResult (fun proc ->
{ proc with
FileName = Fake.Core.Environment.VSWhere
Arguments = sprintf "-version %s -property installationPath" ver })
TimeSpan.MaxValue
|> fun processResult ->
if processResult.OK then
processResult.Messages |> List.tryHead
|> Option.map(fun vsRoot -> sprintf @"%s\MSBuild\%s\Bin" vsRoot ver)
else Option.None
with _ -> Option.None

let paths =
(knownMsBuildEntries |> List.collect (fun m -> m.Paths)) @ oldMsBuildLocations

let version = Environment.environVarOrNone "VisualStudioVersion"
Option.bind getVSPathFromVSWhere version
|> Option.fold(fun paths path -> path :: paths) paths


/// Versions of Mono prior to this one have faulty implementations of MSBuild
/// NOTE: in System.Version 5.0 >= 5.0.0.0 is false while 5.0.0.0 >= 5.0 is true...
Expand Down
5 changes: 5 additions & 0 deletions src/legacy/FakeLib/EnvironmentHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ let ProgramFiles =
[<System.Obsolete("Use Fake.Core.Environment instead (FAKE0001 - package: Fake.Core.Environment)")>]
let ProgramFilesX86 = Environment.GetFolderPath Environment.SpecialFolder.ProgramFilesX86

/// The path to vswhere utility which helps determine VS2017+ installation directory
[<System.Obsolete("Use Fake.Core.Environment instead (FAKE0001 - package: Fake.Core.Environment)")>]
let VSWhere = ProgramFilesX86 + @"\Microsoft Visual Studio\Installer\vswhere.exe"


/// The system root environment variable. Typically "C:\Windows"
[<System.Obsolete("Use Fake.Core.Environment instead (FAKE0001 - package: Fake.Core.Environment)")>]
let SystemRoot = environVar "SystemRoot"
Expand Down
19 changes: 17 additions & 2 deletions src/legacy/FakeLib/MSBuildHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ let msBuildExe =
]
defaultArg (sources |> List.choose id |> List.tryHead) "xbuild"
| false, _ ->
let getVSPathFromVSWhere ver =
try
ExecProcessAndReturnMessages(fun info ->
info.FileName <- VSWhere
info.Arguments <- sprintf "-version %s -property installationPath" ver) TimeSpan.MaxValue
|> fun processResult ->
if processResult.OK then
processResult.Messages.ToArray() |> Array.tryHead
|> Option.map(fun vsRoot -> sprintf @"%s\MSBuild\%s\Bin" vsRoot ver)
else Option.None
with _ -> Option.None

let configIgnoreMSBuild =
if "true".Equals(ConfigurationManager.AppSettings.["IgnoreMSBuild"], StringComparison.OrdinalIgnoreCase)
Expand All @@ -113,8 +124,12 @@ let msBuildExe =
let findOnVSPathsThenSystemPath =
let dict = toDict knownMsBuildEntries
let vsVersionPaths =
defaultArg (EnvironmentHelper.environVarOrNone "VisualStudioVersion" |> Option.bind dict.TryFind) getAllKnownPaths
|> List.map ((@@) ProgramFilesX86)
let version = EnvironmentHelper.environVarOrNone "VisualStudioVersion"
let paths =
defaultArg (version |> Option.bind dict.TryFind) getAllKnownPaths
|> List.map ((@@) ProgramFilesX86)
Option.bind getVSPathFromVSWhere version
|> Option.fold(fun paths path -> path :: paths) paths

ProcessHelper.tryFindFileInDirsThenPath vsVersionPaths "MSBuild.exe"

Expand Down