Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
400 lines (362 loc) · 12.7 KB
/
Copy pathmain.rs
File metadata and controls
400 lines (362 loc) · 12.7 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use std::ffi::OsString;
use std::io::Read;
use std::os::windows::ffi::OsStringExt;
use std::os::windows::fs::OpenOptionsExt;
use std::path::PathBuf;
use std::process::ExitCode;
use std::{env, process::Command};
use find_msvc_tools::find_windows_sdk;
use serde::Deserialize;
use vssetup::{HRESULT, SetupConfiguration, Variant, com, wide_str};
// channelId=VisualStudio.17.Release
// channelUri=https://aka.ms/vs/17/release/channel
#[derive(Deserialize)]
struct VsConfig {
components: Vec<String>,
}
// TODO: properly handle errors.
fn instances() -> Vec<VisualStudio> {
let result = Vec::new();
let Ok(setup) = SetupConfiguration::new() else {
return result;
};
let Ok(instances) = setup.EnumInstances() else {
return result;
};
instances
.map(|instance| {
let product = instance.GetProduct().ok()??;
let product_id = product.GetId().ok()?.to_string();
let product_id = Product::from_str(&product_id)?;
let install_path = OsString::from_wide(&instance.GetInstallationPath().ok()?).into();
let display_name = instance.GetDisplayName(0x400).ok()?.to_string();
//let id = instance.GetInstanceId().ok()?.to_string();
let version = instance.GetInstallationVersion().ok()?.to_string();
let major = version
.split_once('.')
.and_then(|v| v.0.parse::<u16>().ok())
.unwrap_or_default();
// Don't attempt to install for Visual Studio 2017 as that's untested.
if major < 16 {
return None;
}
let setup_path = instance
.GetProperties()
.ok()??
.GetValue(wide_str!("setupEngineFilePath"))
.ok()?;
let setup_path: PathBuf = if let Variant::Bstr(setup_path) = setup_path {
OsString::from_wide(&setup_path).into()
} else {
// Fallback to %ProgramFile(x86)%\Microsoft Visual Studio\installer\setup.exe.
// If the "ProgramFiles(x86)" environment variable isn't set, then use the hardcoded value
// (which will almost certainly be correct for user systems).
let mut program_files = PathBuf::from(
env::var_os("ProgramFiles(x86)").unwrap_or(r"C:\Program Files (x86)".into()),
);
program_files.extend(["Microsoft Visual Studio", "installer", "setup.exe"]);
program_files
};
/*let channel_id = instance
.to_property_store()
.ok()?
.GetValue(wide_str!("channelId"))
.ok()?
.to_string();*/
let mut msvc = None;
let mut sdk = None;
if let Ok(packages) = instance.GetPackages() {
for package in packages.iter() {
if let Ok(id) = package.GetId() {
let component = Sdk::from_id(&id.to_string());
if component.is_some() && component > sdk {
sdk = component;
} else if let Some(component) = Msvc::from_id(&id.to_string())
&& component == Msvc::from_rust_arch(std::env::consts::ARCH)
{
msvc = Some(component)
}
}
}
}
return Some(VisualStudio {
display_name,
//id,
version: major,
product_id,
install_path,
//channel_id,
setup_path,
msvc: msvc,
sdk: sdk,
});
})
.filter_map(|v| v)
.collect()
}
fn main() -> ExitCode {
if let Err(e) = run_main() {
println!("Error {:#x}", e.0);
// FIXME: use ExitCode once ExitCode::from_raw is stable.
std::process::exit(e.0)
} else {
ExitCode::SUCCESS
}
}
fn run_main() -> Result<(), HRESULT> {
println!("Scanning for rust prerequisites...");
com::initialize()?;
let mut instances = instances();
if instances.is_empty() {
println!("\nVisual Studio is not installed");
println!("Download it from https://visualstudio.microsoft.com/");
// S_FALSE
return Err(HRESULT(1));
}
// Select the instance that's a closest match to our requirements.
instances.sort_unstable_by(|a, b| {
if a.msvc.is_none() != b.msvc.is_none() {
// Sort versions containing the msvc component first (if any).
a.msvc.is_none().cmp(&b.msvc.is_none())
} else if a.version != b.version {
// Sort versions in decending order.
b.version.cmp(&a.version)
} else {
// Sort products in order: Build tools, Enterprise, Professional, Community.
a.product_id.cmp(&b.product_id)
}
});
instances.truncate(1);
let mut instance = instances.remove(0);
let msvc_installed = instance.msvc.is_some();
if let Some(msvc) = instance.msvc {
println!("\tFound {}", msvc.id());
}
let sdk_installed = if let Some(sdk) = find_windows_sdk(std::env::consts::ARCH) {
println!("\tFound Windows SDK version {}", sdk.sdk_version());
true
} else {
false
};
if sdk_installed && msvc_installed {
println!("\nall rust prerequistes have been installed successfully");
} else {
instance.clear_components();
if !sdk_installed {
println!("\tMissing component: Windows SDK");
}
if !msvc_installed {
println!("\tMissing component: MSVC build tools");
}
println!("\nFinding components to install...");
if !sdk_installed {
avaliable_components(&mut instance).unwrap();
}
if !msvc_installed {
instance.msvc = Some(Msvc::from_rust_arch(std::env::consts::ARCH));
}
println!("\nFound components for {}:", &instance.display_name);
for component in instance.components() {
println!("\t{}", component);
}
println!("\nWould you like to install the missing components? [y/n]");
let mut line = String::new();
// Any errors in reading from stdio will be interpreted as "no".
let _ = std::io::stdin().read_line(&mut line);
let line = line.trim();
if line.eq_ignore_ascii_case("yes") || line.eq_ignore_ascii_case("y") {
let mut cmd = std::process::Command::new(&instance.setup_path);
cmd.arg("modify");
cmd.arg("--installPath");
cmd.arg(&instance.install_path);
// Display an interactive GUI focused on installing just the selected components.
cmd.arg("--focusedUi");
// Add the English language pack
cmd.args(["--addProductLang", "En-us"]);
// Add the components
for component in instance.components() {
cmd.args(["--add", component]);
}
// TODO: handle errors
let mut child = cmd.spawn().unwrap();
child.wait().unwrap();
}
}
return Ok(());
}
fn components_json(instance: &VisualStudio) -> std::io::Result<String> {
const FILE_SHARE_READ: u32 = 1;
const FILE_SHARE_WRITE: u32 = 2;
let config = tempfile::Builder::new()
.prefix("vsconfig-")
.suffix(".json")
.rand_bytes(16)
.tempfile()?;
// We need to pass the tempfile path to an external process
// which doesn't like the way tempfile opens a file handle.
// So we open our own one and turn the tempfile into a path.
// The path will still be cleaned up on drop.
let mut config_file = std::fs::File::options()
.read(true)
.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
.open(&config.path())?;
let config_path = config.into_temp_path();
// Build tools uses a different workload name
let workload = if instance.product_id == Product::BuildTools {
"Microsoft.VisualStudio.Workload.VCTools"
} else {
"Microsoft.VisualStudio.Workload.NativeDesktop"
};
// run the setup program.
let mut cmd = Command::new(&instance.setup_path)
.args(&["export", "--quiet", "--noUpdateInstaller", "--noWeb"])
.arg("--config")
.arg(&config_path)
.arg("--installPath")
.arg(&instance.install_path)
.arg("--productId")
.arg(instance.product_id.as_str())
.arg("--add")
.arg(workload)
.arg("--includeRecommended")
// This might be excessive
.arg("--includeOptional")
.stdout(std::process::Stdio::null())
.spawn()?;
cmd.wait()?;
let mut json = String::new();
config_file.read_to_string(&mut json)?;
Ok(json)
}
fn avaliable_components(instance: &mut VisualStudio) -> std::io::Result<()> {
let json = components_json(instance)?;
// FIXME: Handle Json being empty.
// The most likely reason for this is that the VS installer was running.
let components = serde_json::from_str::<VsConfig>(&json)?.components;
let sdk: Option<Sdk> = components
.into_iter()
.filter_map(|id| Sdk::from_id(&id))
.max();
if let Some(sdk) = sdk {
instance.sdk = Some(sdk);
Ok(())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Sdk not found",
))
}
}
#[derive(Debug, Clone)]
struct VisualStudio {
display_name: String,
//id: String,
version: u16,
product_id: Product,
install_path: PathBuf,
//channel_id: String,
setup_path: PathBuf,
msvc: Option<Msvc>,
sdk: Option<Sdk>,
}
impl VisualStudio {
fn clear_components(&mut self) {
self.msvc = None;
self.sdk = None;
}
fn components(&self) -> impl Iterator<Item = &str> {
self.msvc
.iter()
.map(|m| m.id())
.chain(self.sdk.iter().map(|s| s.id()))
}
}
const PRODUCT_COMMUNITY: &str = "Microsoft.VisualStudio.Product.Community";
const PRODUCT_BUILD_TOOLS: &str = "Microsoft.VisualStudio.Product.BuildTools";
const PRODUCT_PROFESSIONAL: &str = "Microsoft.VisualStudio.Product.Professional";
const PRODUCT_ENTERPRISE: &str = "Microsoft.VisualStudio.Product.Enterprise";
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Product {
BuildTools = 1,
Enterprise = 2,
Professional = 3,
Community = 4,
}
impl Product {
fn from_str(product: &str) -> Option<Self> {
Some(match product {
PRODUCT_COMMUNITY => Product::Community,
PRODUCT_PROFESSIONAL => Product::Professional,
PRODUCT_ENTERPRISE => Product::Enterprise,
PRODUCT_BUILD_TOOLS => Product::BuildTools,
_ => return None,
})
}
fn as_str(self) -> &'static str {
match self {
Self::BuildTools => PRODUCT_BUILD_TOOLS,
Self::Community => PRODUCT_COMMUNITY,
Self::Professional => PRODUCT_PROFESSIONAL,
Self::Enterprise => PRODUCT_ENTERPRISE,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Sdk {
version: u32,
id: String,
}
impl Sdk {
fn from_id(id: &str) -> Option<Self> {
// SDK components can be recognised by the following regex:
// Microsoft.VisualStudio.Component.Windows[10|11]SDK.[0-9]+
let parts: Vec<&str> = id.split('.').collect();
if parts.len() == 5
&& parts[..3] == ["Microsoft", "VisualStudio", "Component"]
&& ["Windows10SDK", "Windows11SDK"].contains(&parts[3])
&& let Ok(version) = parts[4].parse::<u32>()
{
Some(Self {
version,
id: id.into(),
})
} else {
None
}
}
fn id(&self) -> &str {
&self.id
}
}
const MSVC_X86_X64: &str = "Microsoft.VisualStudio.Component.VC.Tools.x86.x64";
const MSVC_ARM64: &str = "Microsoft.VisualStudio.Component.VC.Tools.ARM64";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Msvc {
X86X64,
Arm64,
}
impl Msvc {
/// Convert from a component Id.
fn from_id(id: &str) -> Option<Self> {
match id {
MSVC_X86_X64 => Some(Self::X86X64),
MSVC_ARM64 => Some(Self::Arm64),
_ => None,
}
}
/// Convert from a rust arch to an Msvc toolchain.
fn from_rust_arch(arch: &str) -> Self {
if arch == "aarch64" {
Self::Arm64
} else {
Self::X86X64
}
}
/// The Visual Studio component identifier.
const fn id(self) -> &'static str {
match self {
Self::X86X64 => MSVC_X86_X64,
Self::Arm64 => MSVC_ARM64,
}
}
}