refactor: build on Windows without admin privilege.

This commit is contained in:
Flux Xu 2020-09-19 12:28:02 -04:00
parent cde8dceda9
commit 595bf69a43

View file

@ -9,29 +9,30 @@ use cfg_if::cfg_if;
cfg_if! { cfg_if! {
if #[cfg(windows)] { if #[cfg(windows)] {
use std::env::var; use std::env::var;
use std::fs::File; use std::fs::{File, create_dir};
use std::io::copy; use std::io::copy;
use std::path::PathBuf; use std::path::PathBuf;
pub fn setup() { pub fn setup() {
let node_full_version = let node_full_version =
String::from_utf8(Command::new("node").arg("-v").output().unwrap().stdout).unwrap(); String::from_utf8(Command::new("node").arg("-v").output().unwrap().stdout).unwrap();
let node_exec_path = String::from_utf8(
Command::new("node") let dev_dir: PathBuf = [
.arg("-e") &var("HOMEDRIVE").expect("Get env HOMEDRIVE failed"),
.arg("console.log(process.execPath)") &var("HOMEPATH").expect("Get env HOMEDRIVE failed"),
.output() ".napi-rs"
.unwrap() ].iter().collect();
.stdout,
) match create_dir(&dev_dir) {
.unwrap(); Ok(_) => {},
let node_exec_path = node_exec_path.trim_end(); Err(err) => {
let mut node_lib_file_dir = PathBuf::from(node_exec_path) if err.kind() != std::io::ErrorKind::AlreadyExists {
.parent() panic!("create ~/.napi-rs folder failed: {}", err)
.unwrap() }
.to_path_buf(); },
let node_lib_dir = PathBuf::from(&node_lib_file_dir); }
node_lib_file_dir.push(format!("node-{}.lib", node_full_version.trim_end()));
let node_lib_file_dir = dev_dir.join(format!("node-{}.lib", node_full_version.trim_end()));
if !node_lib_file_dir.exists() { if !node_lib_file_dir.exists() {
let lib_file_download_url = format!( let lib_file_download_url = format!(
"https://nodejs.org/dist/{}/win-x64/node.lib", "https://nodejs.org/dist/{}/win-x64/node.lib",
@ -46,7 +47,7 @@ cfg_if! {
"cargo:rustc-link-lib={}", "cargo:rustc-link-lib={}",
&node_lib_file_dir.file_stem().unwrap().to_str().unwrap() &node_lib_file_dir.file_stem().unwrap().to_str().unwrap()
); );
println!("cargo:rustc-link-search={}", node_lib_dir.to_str().unwrap()); println!("cargo:rustc-link-search={}", dev_dir.to_str().unwrap());
// Link `win_delay_load_hook.obj` for windows electron // Link `win_delay_load_hook.obj` for windows electron
let node_runtime_env = "npm_config_runtime"; let node_runtime_env = "npm_config_runtime";
println!("cargo:rerun-if-env-changed={}", node_runtime_env); println!("cargo:rerun-if-env-changed={}", node_runtime_env);