-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
on: [push] | ||
|
||
name: CI | ||
|
||
jobs: | ||
build_and_test: | ||
name: hot-lib-reloader tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
- uses: actions-rs/cargo@v1 | ||
with: | ||
command: test | ||
args: -- --no-capture |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Cargo.lock | |
DEV.org | ||
*.org | ||
examples/bevy-extended/ | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pub fn recompile(dir: impl AsRef<std::path::Path>) { | ||
std::process::Command::new("cargo") | ||
.arg("build") | ||
.current_dir(dir) | ||
.spawn() | ||
.and_then(|proc| proc.wait_with_output()) | ||
.expect("cargo build failed"); | ||
} | ||
|
||
pub fn modify_file_and_do( | ||
file: impl AsRef<std::path::Path>, | ||
modify_file_fn: impl FnOnce(&str) -> String, | ||
do_fn: impl FnOnce() + std::panic::UnwindSafe, | ||
) { | ||
let file = file.as_ref().canonicalize().expect("cannot find lib file"); | ||
|
||
let content = std::fs::read_to_string(&file).expect("cannot read file"); | ||
let new_content = modify_file_fn(content.as_str()); | ||
std::fs::write(&file, new_content).expect("cannot write lib file"); | ||
|
||
let res = std::panic::catch_unwind(do_fn); | ||
|
||
std::fs::write(&file, content).expect("cannot restore file"); | ||
|
||
res.expect("modify_file_and_do: do_fn panicked"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
mod common; | ||
|
||
#[hot_lib_reloader::hot_module(dylib = "recursive_lib")] | ||
mod hot_lib { | ||
hot_functions_from_file!("./recursive_lib/src/lib.rs"); | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let n = hot_lib::do_more_stuff(Box::new(hot_lib::do_stuff)); | ||
assert_eq!(n, 5); | ||
|
||
common::modify_file_and_do( | ||
"tests/recursive_lib/src/lib.rs", | ||
|content| { | ||
content.replace( | ||
"pub fn do_stuff() -> i32 { 3 }", | ||
"pub fn do_stuff() -> i32 { 5 }", | ||
) | ||
}, | ||
|| { | ||
common::recompile("tests/recursive_lib"); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
|
||
let n = hot_lib::do_more_stuff(Box::new(hot_lib::do_stuff)); | ||
assert_eq!(n, 7); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "recursive_lib" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["rlib", "dylib"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#[rustfmt::skip] | ||
#[no_mangle] | ||
pub fn do_stuff() -> i32 { 3 } | ||
|
||
#[no_mangle] | ||
pub fn do_more_stuff(callback: Box<dyn Fn() -> i32>) -> i32 { | ||
let n = callback(); | ||
n + 2 | ||
} |