Skip to content

Commit

Permalink
more fixes for install-protoc
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Apr 2, 2022
1 parent 3bf2502 commit 4036c5b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions install-protoc.ers
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,44 @@
//! [dependencies]
//! ureq = { version = "2", features = ["rustls"] }
//! zip = "0.6"
//! cfg-if = "1"
//! ```

use std::io::Read;
use std::env::consts::OS;
use std::io::{Cursor, Read};
use std::path::PathBuf;
use zip::ZipArchive;

const URL: &'static str = "https://github.com/protocolbuffers/protobuf/releases/download";
const VERSION: &'static str = "3.19.4";

fn main() {
let os = cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] {
"win32"
} else if #[cfg(target_os = "linux")] {
"linux-x86_64"
} else if #[cfg(target_os = "macos")] {
"osx-x86_64"
} else {
panic!("Unsupported os");
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let os = match OS {
"windows" => "win32",
"linux" => "linux-x86_64",
"macos" => "macos-x86_64",
_ => panic!("Unsupported OS"),
};

let url = format!("{URL}/v{VERSION}/protoc-{VERSION}-{os}.zip");

let mut buf = Vec::new();

let reader = ureq::get(&url)
.call()
.unwrap()
println!("Downloading `protoc` v{VERSION} from github...");

ureq::get(&url)
.call()?
.into_reader()
.read_to_end(&mut buf);
.read_to_end(&mut buf)?;

println!("Download completed.");

let mut archive = zip::ZipArchive::new(std::io::Cursor::new(buf)).unwrap();
let mut archive = ZipArchive::new(Cursor::new(buf))?;

let path = PathBuf::from(std::env!("RUST_SCRIPT_BASE_PATH")).join("protobuf");

archive.extract(&path).unwrap();

println!("Extracted to {}", path.display());

Ok(())
}

0 comments on commit 4036c5b

Please sign in to comment.