aboutsummaryrefslogtreecommitdiffstats
path: root/components/compositing/build.rs
blob: 828bf622fcbfe35844a94e4299c1599a70afa325 (plain) (blame)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

fn main() {
    let lockfile_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
        .join("..")
        .join("..")
        .join("Cargo.lock");
    let revision_file_path =
        Path::new(&env::var_os("OUT_DIR").unwrap()).join("webrender_revision.rs");

    let mut lockfile = String::new();
    File::open(lockfile_path)
        .expect("Cannot open lockfile")
        .read_to_string(&mut lockfile)
        .expect("Failed to read lockfile");

    match toml::from_str::<toml::value::Table>(&lockfile) {
        Ok(result) => {
            let packages = result
                .get("package")
                .expect("Cargo lockfile should contain package list");

            match *packages {
                toml::Value::Array(ref arr) => {
                    let source = arr
                        .iter()
                        .find(|pkg| {
                            pkg.get("name").and_then(|name| name.as_str()).unwrap_or("") ==
                                "webrender"
                        })
                        .and_then(|pkg| pkg.get("source").and_then(|source| source.as_str()))
                        .unwrap_or("unknown");

                    let parsed: Vec<&str> = source.split('#').collect();
                    let revision = if parsed.len() > 1 { parsed[1] } else { source };

                    let mut revision_module_file = File::create(revision_file_path).unwrap();
                    write!(&mut revision_module_file, "\"{}\"", revision).unwrap();
                },
                _ => panic!("Cannot find package definitions in lockfile"),
            }
        },
        Err(e) => panic!("{}", e),
    }
}