sync_code/
lib.rs

1//! Synchronize code blocks between different files.
2//!
3//! # Usage
4//! `Cargo.toml`:
5//! ```toml
6//! [build-dependencies]
7//! sync-code = "0.1.0"
8//! ```
9//!
10//! `build.rs`:
11//! ```rust
12//! fn main() {
13//!     sync_code::Builder::new()
14//!         .add("src/target1.rs", "src/source1.rs")
15//!         .add("src/target2.rs", "src/source2.rs")
16//!         .sync();
17//! }
18//!
19//! ```
20
21mod sync;
22
23use std::path::Path;
24use sync::Sync;
25
26pub struct Builder {
27    table: Vec<Sync>,
28}
29
30impl Builder {
31    pub fn new() -> Self {
32        Builder { table: Vec::new() }
33    }
34
35    pub fn add<P: AsRef<Path>>(mut self, file: P, dep_file: P) -> Self {
36        println!("cargo:rerun-if-changed={}", dep_file.as_ref().display());
37        self.table.push(Sync::new(
38            file.as_ref().to_path_buf(),
39            dep_file.as_ref().to_path_buf(),
40        ));
41        self
42    }
43
44    pub fn sync(&mut self) {
45        println!("Start syncing code ... {}", self.table.len());
46        for sync in &self.table {
47            sync.sync();
48        }
49    }
50}