diff options
Diffstat (limited to 'winpty')
-rw-r--r-- | winpty/Cargo.toml | 7 | ||||
-rw-r--r-- | winpty/build.rs | 71 | ||||
-rw-r--r-- | winpty/src/windows.rs | 5 |
3 files changed, 71 insertions, 12 deletions
diff --git a/winpty/Cargo.toml b/winpty/Cargo.toml index 1d1529b2..a8611c3d 100644 --- a/winpty/Cargo.toml +++ b/winpty/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Zac Pullar-Strecker <zacmps@gmail.com>"] license = "MIT" description = "Safe rust bindings for winpty" +edition = "2018" [target.'cfg(windows)'.dependencies] winpty-sys = "0.4.3" @@ -13,3 +14,9 @@ widestring = "0.4.0" [target.'cfg(windows)'.dev-dependencies] named_pipe = "0.3" winapi = { version = "0.3", features = ["winnt", "processthreadsapi"] } + +[target.'cfg(windows)'.build-dependencies] +embed-resource = "1.1.4" +tempfile = "3.0.4" +reqwest = "0.9" +zip = "0.5" diff --git a/winpty/build.rs b/winpty/build.rs index a0742a11..2f4c4565 100644 --- a/winpty/build.rs +++ b/winpty/build.rs @@ -1,14 +1,67 @@ -use std::fs::copy; +#[cfg(windows)] +use std::fs::OpenOptions; +#[cfg(windows)] +use std::io; + +#[cfg(windows)] +use std::env; +#[cfg(windows)] +use std::fs::{copy, File}; +#[cfg(windows)] use std::path::Path; +#[cfg(windows)] +use embed_resource; +#[cfg(windows)] +use reqwest; +#[cfg(windows)] +use tempfile; +#[cfg(windows)] +use zip; + +#[cfg(windows)] +const WINPTY_PACKAGE_URL: &str = + "https://github.com/rprichard/winpty/releases/download/0.4.3/winpty-0.4.3-msvc2015.zip"; + fn main() { - // The working directory for `cargo test` is in the deps folder, not the debug/release root - if cfg!(test) && Path::new("target").exists() { - #[cfg(debug_assertions)] - copy("../extra/windows/x86_64/winpty-agent.exe", "target/debug/deps/winpty-agent.exe") - .unwrap(); - #[cfg(not(debug_assertions))] - copy("../extra/windows/x86_64/winpty-agent.exe", "target/release/deps/winpty-agent.exe") - .unwrap(); + #[cfg(windows)] + { + embed_resource::compile("../extra/windows/windows.rc"); + + // Path is relative to target/{profile}/build/alacritty-HASH/out + let file = Path::new(&env::var("OUT_DIR").unwrap()).join("../../../winpty-agent.exe"); + if !file.exists() { + aquire_winpty_agent(&file); + } + + // The working directory for `cargo test` is in the deps folder, not the debug/release root + copy(&file, file.parent().unwrap().join("deps/winpty-agent.exe")).unwrap(); } } + +#[cfg(windows)] +fn aquire_winpty_agent(out_path: &Path) { + let tmp_dir = tempfile::Builder::new().prefix("alacritty_build").tempdir().unwrap(); + + let mut response = reqwest::get(WINPTY_PACKAGE_URL).unwrap(); + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(tmp_dir.path().join("winpty_package.zip")) + .unwrap(); + + io::copy(&mut response, &mut file).unwrap(); + + let mut archive = zip::ZipArchive::new(file).unwrap(); + + let target = match env::var("TARGET").unwrap().split("-").next().unwrap() { + "x86_64" => "x64", + "i386" => "ia32", + _ => panic!("architecture has no winpty binary"), + }; + + let mut winpty_agent = archive.by_name(&format!("{}/bin/winpty-agent.exe", target)).unwrap(); + + io::copy(&mut winpty_agent, &mut File::create(out_path).unwrap()).unwrap(); +} diff --git a/winpty/src/windows.rs b/winpty/src/windows.rs index 064db2da..5746df2a 100644 --- a/winpty/src/windows.rs +++ b/winpty/src/windows.rs @@ -337,10 +337,9 @@ impl<'a> Drop for SpawnConfig<'a> { #[cfg(test)] mod tests { - extern crate named_pipe; - extern crate winapi; + use named_pipe::PipeClient; + use winapi; - use self::named_pipe::PipeClient; use self::winapi::um::processthreadsapi::OpenProcess; use self::winapi::um::winnt::READ_CONTROL; |