forked from prefabcode/proxygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
67 lines (55 loc) · 2.02 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
extern crate ease;
use std::env;
use std::path::Path;
use std::fs::OpenOptions;
use std::io::Read;
use std::io::Write;
use std::io::Seek;
use std::io::SeekFrom;
use std::cmp::Ordering;
use ease::{Url, Request};
#[derive(Debug, PartialEq)]
enum VersionStatus {
OutOfDate,
UpToDate,
}
fn get_allcards_version_status() -> VersionStatus {
let out_dir = env::var("OUT_DIR").unwrap();
let allcards_path = Path::new(&out_dir).join("AllCards.json");
if OpenOptions::new().read(true).open(&allcards_path).is_err() {
return VersionStatus::OutOfDate;
}
let version_path = Path::new(&out_dir).join("version.json");
let mut version_file =
OpenOptions::new().create(true).read(true).write(true).open(&version_path).unwrap();
let mut local_version_body = String::new();
version_file.read_to_string(&mut local_version_body).unwrap();
let version_url = Url::parse("http://mtgjson.com/json/version.json").unwrap();
let remote_version_body = Request::new(version_url).get().unwrap().body;
match local_version_body.cmp(&remote_version_body) {
Ordering::Equal => VersionStatus::UpToDate,
_ => {
version_file.seek(SeekFrom::Start(0)).unwrap();
write!(version_file, "{}", remote_version_body).unwrap();
VersionStatus::OutOfDate
}
}
}
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let allcards_path = Path::new(&out_dir).join("AllCards.json");
match get_allcards_version_status() {
VersionStatus::UpToDate => {}
VersionStatus::OutOfDate => {
let mut allcards_file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&allcards_path)
.unwrap();
let allcards_url = Url::parse("http://mtgjson.com/json/AllCards.json").unwrap();
let allcards_body = Request::new(allcards_url).get().unwrap().body;
write!(allcards_file, "{}", allcards_body).unwrap();
}
}
}