Skip to content

Commit

Permalink
Generate new library name on each compile
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mw committed Feb 2, 2018
1 parent 2105cad commit 4f7be34
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
30 changes: 30 additions & 0 deletions demo/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
let lib_name = "reloadable";

let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let cargo_toml_path = Path::new(&cargo_manifest_dir).join("Cargo.toml");

let mut cargo_toml_src = File::open(&cargo_toml_path).unwrap();
let mut data = String::new();
cargo_toml_src.read_to_string(&mut data).unwrap();
drop(cargo_toml_src);

let search = format!("name = \"{}", lib_name);
let line = data.lines().find(|l| l.contains(&search));

if let Some(line) = line {
let epoch_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let replace = format!("name = \"{}_{}\"", lib_name, epoch_time.as_secs());

let mut cargo_toml_dst = File::create(&cargo_toml_path).unwrap();
cargo_toml_dst
.write(data.replace(line, &replace).as_bytes())
.unwrap();
}
}
52 changes: 46 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,15 @@ impl<Host> Reloadable<Host> {
///
/// [`live_reload!`]: macro.live_reload.html
pub fn new<P: AsRef<Path>>(path: P, host: Host) -> Result<Self, Error> {
let sym = AppSym::new(&path)?;
let sym = AppSym::new(find_latest_path(&path))?;

let size = (unsafe { &**sym.api }.size)();
let (tx, rx) = channel();
let mut watcher = notify::watcher(tx, Duration::from_secs(1))?;

let mut new_path = PathBuf::new();
new_path.push(path);

watcher.watch(
new_path.parent().unwrap(),
notify::RecursiveMode::NonRecursive,
Expand Down Expand Up @@ -313,10 +316,8 @@ impl<Host> Reloadable<Host> {
while let Ok(evt) = self.rx.try_recv() {
use notify::DebouncedEvent::*;
match evt {
NoticeWrite(ref path) |
Write(ref path) |
Create(ref path) => {
if *path == self.path {
NoticeWrite(ref path) | Write(ref path) | Create(ref path) => {
if *path == find_latest_path(&self.path) {
should_reload = true;
}
}
Expand Down Expand Up @@ -344,8 +345,11 @@ impl<Host> Reloadable<Host> {
if let Some(AppSym { ref mut api, .. }) = self.sym {
(unsafe { &***api }.unload)(&mut self.host, Self::get_state_ptr(&mut self.state));
}

let path = find_latest_path(&self.path);

self.sym = None;
let sym = AppSym::new(&self.path)?;
let sym = AppSym::new(path)?;
// @Avoid reallocating if unnecessary
self.realloc_buffer((unsafe { &**sym.api }.size)());
(unsafe { &**sym.api }.reload)(&mut self.host, Self::get_state_ptr(&mut self.state));
Expand Down Expand Up @@ -532,3 +536,39 @@ macro_rules! live_reload {
};
}
}

fn find_latest_path<P: AsRef<Path>>(path: P) -> PathBuf {
let path = path.as_ref();

let paths = {
let file_stem = path.file_stem();
let ext = path.extension();

std::fs::read_dir(path.parent().unwrap())
.unwrap()
.filter(|e| e.is_ok())
.map(|e| e.unwrap().path())
.filter(|path| {
let file_stem2 = path.file_stem();
let ext2 = path.extension();

match (file_stem, ext, file_stem2, ext2) {
(Some(file_stem), Some(ext), Some(file_stem2), Some(ext2)) => {
file_stem2
.to_str()
.unwrap()
.starts_with(&format!("{}_", file_stem.to_str().unwrap()))
&& ext == ext2
}
_ => false,
}
})
.collect::<std::vec::Vec<PathBuf>>()
};

if paths.len() == 0 {
path.to_path_buf()
} else {
paths.iter().max_by_key(|p| p.file_stem()).unwrap().clone()
}
}

0 comments on commit 4f7be34

Please sign in to comment.