-
Notifications
You must be signed in to change notification settings - Fork 69
/
find_msbuild.ps1
84 lines (77 loc) · 2.45 KB
/
find_msbuild.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
param([switch]$strict = $false)
$version = "17.10.5"
$preview = ""
if ($preview.length -gt 0) {
$description = "version $version preview $preview"
$path = "VisualStudioPreview/$version-pre.$preview.0+"
} else {
$description = "version $version"
$path = "VisualStudio/$version+"
}
$vswhere = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$names = &$vswhere `
-prerelease `
-all `
-requires Microsoft.Component.MSBuild `
-property installationName
$msbuildpaths = &$vswhere `
-prerelease `
-all `
-requires Microsoft.Component.MSBuild `
-find MSBuild\**\Bin\amd64\MSBuild.exe
$i = 0;
foreach ($name in $names) {
if ($name.startswith("$path")) {
return ($msbuildpaths | select-object -index $i)
}
++$i
}
function version-tuple($name) {
$tuple = [double[]]$name.split([char[]]"/+")[1].split(
[string[]]@(".", "-pre."), [StringSplitOptions]::none)
if ($tuple.length -lt 5) {
# Count non-previews as preview ∞.0.
$tuple = $tuple + @((1.0 / 0.0), 0.0)
}
return [tuple]::Create($tuple[0], $tuple[1], $tuple[2], $tuple[3], $tuple[4])
}
if ($strict) {
write-error ("Could not find Visual Studio $description;" +
" found the following versions:`n$(
[string]::join("`n", $names))")
} else {
$earlier = $null
$earlier_index = $null
$later = $null
$later_index = $null
$i = 0
foreach ($name in $names) {
if (((version-tuple $name) -lt (version-tuple $path)) -and
(($earlier -eq $null) -or
((version-tuple $name) -gt (version-tuple $earlier)))) {
$earlier = $name
$earlier_index = $i
}
if (((version-tuple $name) -gt (version-tuple $path)) -and
(($later -eq $null) -or
((version-tuple $name) -lt (version-tuple $later)))) {
$later = $name
$later_index = $i
}
++$i
}
if ($later -ne $null) {
$best_match = $later
$i = $later_index
} elseif ($earlier -ne $null) {
$best_match = $earlier
$i = $earlier_index
} else {
write-error "Could not find Visual Studio"
exit 1
}
write-warning ("Could not find Visual Studio $description;" +
" falling back to $best_match from:`n$(
[string]::join("`n", $names))")
return ($msbuildpaths | select-object -index $i)
}