Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Created April 11, 2020 01:30
Show Gist options
  • Save KodrAus/98443b5c706266b99b78fdda273eb936 to your computer and use it in GitHub Desktop.
Save KodrAus/98443b5c706266b99b78fdda273eb936 to your computer and use it in GitHub Desktop.
Find `msbuild` using `vswhere`
use std::path::PathBuf;
use std::process::Command;
use std::str;
let mut msbuild = PathBuf::from("msbuild");
let check_msbuild = Command::new(msbuild)
.arg("--version")
.output()
.is_ok();
if !check_msbuild {
let vswhere = {
let mut path = PathBuf::from(std::env::var("ProgramFiles(x86)").expect("failed to find `vswhere`"));
path.push("Microsoft Visual Studio");
path.push("Installer");
path.push("vswhere.exe");
path
};
let msbuild_paths = Command::new(vswhere)
.args(&[
"-latest",
"-requires",
"Microsoft.Component.MSBuild",
"-find",
"MSBuild\\**\\Bin\\MSBuild.exe"
] as &[_])
.output()
.expect("failed to run vswhere");
let found_msbuild_path = str::from_utf8(&msbuild_paths.stdout)
.expect("invalid vswhere output")
.lines()
.next()
.expect("no vswhere output");
msbuild = PathBuf::from(found_msbuild_path);
} else {
panic!("on the path!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment