aboutsummaryrefslogtreecommitdiffstats
path: root/components/compositing/build.rs
diff options
context:
space:
mode:
authorOJ Kwon <kwon.ohjoong@gmail.com>2018-03-19 13:44:51 -0700
committerOJ Kwon <kwon.ohjoong@gmail.com>2018-03-22 23:20:25 -0700
commitf9230975f54c3777219ba09d784557b3841f7acc (patch)
tree60b4f94057d0b4007ea5091f17531a171bc879b1 /components/compositing/build.rs
parent0671fcb5c7d2a8a0a3178937a55f5b31d452db9c (diff)
downloadservo-f9230975f54c3777219ba09d784557b3841f7acc.tar.gz
servo-f9230975f54c3777219ba09d784557b3841f7acc.zip
refactor(build): generate revision via build.rs
Diffstat (limited to 'components/compositing/build.rs')
-rw-r--r--components/compositing/build.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/components/compositing/build.rs b/components/compositing/build.rs
new file mode 100644
index 00000000000..d5c18728dc9
--- /dev/null
+++ b/components/compositing/build.rs
@@ -0,0 +1,65 @@
+/* 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 http://mozilla.org/MPL/2.0/. */
+
+extern crate toml;
+
+use std::env;
+use std::fs::File;
+use std::io::{Read, Write};
+
+const WEBRENDER_REVISION_TEMPLATE: &'static str =
+"/* 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 http://mozilla.org/MPL/2.0/. */
+
+/// auto-generated by cargo. do not manually modify.
+pub const REVISION: &'static str = ";
+
+fn main() {
+ let current_path = env::current_dir().expect("Failed to access path to lockfile");
+ let lockfile_path = current_path.join("..").join("..").join("Cargo.lock");
+ let revision_file_path = current_path.join("webrender_revision.rs");
+
+ let mut existing_revision_exported = String::new();
+ match File::open(&revision_file_path) {
+ Ok(mut f) => {
+ f.read_to_string(&mut existing_revision_exported).unwrap_or_default();
+ },
+ Err(_) => ()
+ }
+
+ 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 };
+
+ if let Some(_) = existing_revision_exported.find(revision) {
+ return
+ }
+
+ let revision_contents = format!("{}\"{}\";", WEBRENDER_REVISION_TEMPLATE, revision);
+ let mut revision_module_file = File::create(&revision_file_path).unwrap();
+ write!(&mut revision_module_file, "{}", revision_contents).unwrap();
+ },
+ _ => panic!("Cannot find package definitions in lockfile")
+ }
+ },
+ Err(e) => panic!(e)
+ }
+}