2018-03-23 13:28:15 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Build hooks to spit out version+build time info
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use built;
|
|
|
|
|
|
|
|
use reqwest;
|
2018-03-23 13:28:15 +03:00
|
|
|
|
2018-08-17 20:51:50 +03:00
|
|
|
use flate2::read::GzDecoder;
|
2018-03-23 13:28:15 +03:00
|
|
|
use std::env;
|
2018-08-17 20:51:50 +03:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::path::{self, Path, PathBuf};
|
2018-08-21 19:52:37 +03:00
|
|
|
use std::process::Command;
|
|
|
|
|
2018-08-17 20:51:50 +03:00
|
|
|
use tar::Archive;
|
|
|
|
|
|
|
|
const WEB_WALLET_TAG: &str = "0.3.0.1";
|
2018-03-23 13:28:15 +03:00
|
|
|
|
|
|
|
fn main() {
|
2018-08-21 19:52:37 +03:00
|
|
|
// Setting up git hooks in the project: rustfmt and so on.
|
|
|
|
let git_hooks = format!(
|
|
|
|
"git config core.hooksPath {}",
|
|
|
|
PathBuf::from("./.hooks").to_str().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
Command::new("cmd")
|
|
|
|
.args(&["/C", &git_hooks])
|
|
|
|
.output()
|
|
|
|
.expect("failed to execute git config for hooks");
|
|
|
|
} else {
|
|
|
|
Command::new("sh")
|
|
|
|
.args(&["-c", &git_hooks])
|
|
|
|
.output()
|
|
|
|
.expect("failed to execute git config for hooks");
|
|
|
|
}
|
|
|
|
|
2018-08-17 20:51:50 +03:00
|
|
|
// build and versioning information
|
2018-03-23 13:28:15 +03:00
|
|
|
let mut opts = built::Options::default();
|
|
|
|
opts.set_dependencies(true);
|
2018-10-26 03:44:50 +03:00
|
|
|
// don't fail the build if something's missing, may just be cargo release
|
|
|
|
let _ = built::write_built_file_with_opts(
|
2018-03-23 13:28:15 +03:00
|
|
|
&opts,
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
format!("{}{}", env::var("OUT_DIR").unwrap(), "/built.rs"),
|
2018-10-26 03:44:50 +03:00
|
|
|
);
|
2018-08-17 20:51:50 +03:00
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
let web_wallet_install = install_web_wallet();
|
|
|
|
match web_wallet_install {
|
|
|
|
Ok(true) => {}
|
|
|
|
_ => println!(
|
|
|
|
"WARNING : Web wallet could not be installed due to {:?}",
|
|
|
|
web_wallet_install
|
|
|
|
),
|
|
|
|
}
|
2018-08-17 20:51:50 +03:00
|
|
|
}
|
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
fn download_and_decompress(target_file: &str) -> Result<bool, Box<std::error::Error>> {
|
2018-08-17 20:51:50 +03:00
|
|
|
let req_path = format!("https://github.com/mimblewimble/grin-web-wallet/releases/download/{}/grin-web-wallet.tar.gz", WEB_WALLET_TAG);
|
2018-12-12 18:33:30 +03:00
|
|
|
let mut resp = reqwest::get(&req_path)?;
|
2018-08-17 20:51:50 +03:00
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
if !resp.status().is_success() {
|
|
|
|
return Ok(false);
|
2018-08-17 20:51:50 +03:00
|
|
|
}
|
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
// read response
|
|
|
|
let mut out: Vec<u8> = vec![];
|
|
|
|
resp.read_to_end(&mut out)?;
|
2018-08-17 20:51:50 +03:00
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
// Gunzip
|
|
|
|
let mut d = GzDecoder::new(&out[..]);
|
|
|
|
let mut decomp: Vec<u8> = vec![];
|
|
|
|
d.read_to_end(&mut decomp)?;
|
2018-08-17 20:51:50 +03:00
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
// write temp file
|
|
|
|
let mut buffer = File::create(target_file.clone())?;
|
|
|
|
buffer.write_all(&decomp)?;
|
|
|
|
buffer.flush()?;
|
|
|
|
|
|
|
|
Ok(true)
|
2018-08-17 20:51:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Download and unzip tagged web-wallet build
|
2018-12-12 18:33:30 +03:00
|
|
|
fn install_web_wallet() -> Result<bool, Box<std::error::Error>> {
|
2018-08-17 20:51:50 +03:00
|
|
|
let target_file = format!(
|
|
|
|
"{}/grin-web-wallet-{}.tar",
|
2018-12-12 18:33:30 +03:00
|
|
|
env::var("OUT_DIR")?,
|
2018-08-17 20:51:50 +03:00
|
|
|
WEB_WALLET_TAG
|
|
|
|
);
|
2018-12-12 18:33:30 +03:00
|
|
|
let out_dir = env::var("OUT_DIR")?;
|
2018-08-17 20:51:50 +03:00
|
|
|
let mut out_path = PathBuf::from(&out_dir);
|
|
|
|
out_path.pop();
|
|
|
|
out_path.pop();
|
|
|
|
out_path.pop();
|
|
|
|
|
|
|
|
// only re-download if needed
|
2018-12-12 18:33:30 +03:00
|
|
|
println!("{}", target_file);
|
2018-08-17 20:51:50 +03:00
|
|
|
if !Path::new(&target_file).is_file() {
|
2018-12-12 18:33:30 +03:00
|
|
|
let success = download_and_decompress(&target_file)?;
|
|
|
|
if !success {
|
|
|
|
return Ok(false); // could not download and decompress
|
|
|
|
}
|
2018-08-17 20:51:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove old version
|
|
|
|
let mut remove_path = out_path.clone();
|
|
|
|
remove_path.push("grin-wallet");
|
|
|
|
let _ = fs::remove_dir_all(remove_path);
|
|
|
|
|
|
|
|
// Untar
|
2018-12-12 18:33:30 +03:00
|
|
|
let file = File::open(target_file)?;
|
2018-08-17 20:51:50 +03:00
|
|
|
let mut a = Archive::new(file);
|
|
|
|
|
2018-12-12 18:33:30 +03:00
|
|
|
for file in a.entries()? {
|
|
|
|
let mut file = file?;
|
2018-08-17 20:51:50 +03:00
|
|
|
let h = file.header().clone();
|
2018-12-12 18:33:30 +03:00
|
|
|
let path = h.path()?.clone().into_owned();
|
2018-08-17 20:51:50 +03:00
|
|
|
let is_dir = path.to_str().unwrap().ends_with(path::MAIN_SEPARATOR);
|
2018-12-12 18:33:30 +03:00
|
|
|
let path = path.strip_prefix("dist")?;
|
2018-08-17 20:51:50 +03:00
|
|
|
let mut final_path = out_path.clone();
|
|
|
|
final_path.push(path);
|
|
|
|
|
|
|
|
let mut tmp: Vec<u8> = vec![];
|
2018-12-12 18:33:30 +03:00
|
|
|
file.read_to_end(&mut tmp)?;
|
2018-08-17 20:51:50 +03:00
|
|
|
if is_dir {
|
2018-12-12 18:33:30 +03:00
|
|
|
fs::create_dir_all(final_path)?;
|
2018-08-17 20:51:50 +03:00
|
|
|
} else {
|
2018-12-12 18:33:30 +03:00
|
|
|
let mut buffer = File::create(final_path)?;
|
|
|
|
buffer.write_all(&tmp)?;
|
|
|
|
buffer.flush()?;
|
2018-08-17 20:51:50 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 18:33:30 +03:00
|
|
|
|
|
|
|
Ok(true)
|
2018-03-23 13:28:15 +03:00
|
|
|
}
|