Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build): rerun plugin build when plugins.json changes, wait write #6893

Merged
merged 4 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ dependencies {"
let plugins_json_path = project_dir.join(".tauri").join("plugins.json");
let mut plugins: HashMap<String, mobile::PluginMetadata> = if plugins_json_path.exists() {
let s = read_to_string(&plugins_json_path)?;
println!("cargo:rerun-if-changed={}", plugins_json_path.display());
serde_json::from_str(&s)?
} else {
Default::default()
Expand Down
39 changes: 35 additions & 4 deletions core/tauri-build/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
use std::{
collections::HashMap,
env::{var, var_os},
fs::{copy, create_dir, create_dir_all, read_to_string, remove_dir_all, write},
fs::{copy, create_dir, create_dir_all, read_to_string, remove_dir_all, File},
io::Write,
path::{Path, PathBuf},
thread::sleep,
time::{Duration, SystemTime},
};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
pub(crate) struct PluginMetadata {
pub path: PathBuf,
}
Expand Down Expand Up @@ -77,8 +80,36 @@ impl PluginBuilder {
} else {
Default::default()
};
plugins.insert(pkg_name, PluginMetadata { path: source });
write(&plugins_json_path, serde_json::to_string(&plugins)?)?;

let metadata = PluginMetadata { path: source };
let already_set = plugins
.get(&pkg_name)
.map(|m| m == &metadata)
.unwrap_or(false);
if !already_set {
plugins.insert(pkg_name, metadata);
let mut file = File::create(&plugins_json_path)?;
file.write_all(serde_json::to_string(&plugins)?.as_bytes())?;
file.flush()?;

// wait for the file to be written before moving to the app build script
let now = SystemTime::now()
.checked_sub(Duration::from_millis(10))
.unwrap();
let mut attempts = 0;
while !plugins_json_path
.metadata()
.map(|m| m.modified().unwrap() >= now)
.unwrap_or(false)
{
attempts += 1;
if attempts == 10 {
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should panic instead I believe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, pushed it.

}
sleep(Duration::from_millis(100));
}
}
println!("cargo:rerun-if-changed={}", plugins_json_path.display());
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions examples/api/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod tray;

use serde::Serialize;
use tauri::{window::WindowBuilder, App, AppHandle, RunEvent, WindowUrl};
use tauri_plugin_sample::{PingRequest, SampleExt};
//use tauri_plugin_sample::{PingRequest, SampleExt};

#[derive(Clone, Serialize)]
struct Reply {
Expand All @@ -32,7 +32,7 @@ pub fn run() {
.level(log::LevelFilter::Info)
.build(),
)
.plugin(tauri_plugin_sample::init())
//.plugin(tauri_plugin_sample::init())
.setup(move |app| {
#[cfg(desktop)]
{
Expand Down Expand Up @@ -65,15 +65,6 @@ pub fn run() {
#[cfg(debug_assertions)]
window.open_devtools();

let value = Some("test".to_string());
let response = app.sample().ping(PingRequest {
value: value.clone(),
});
log::info!("got response: {:?}", response);
if let Ok(res) = response {
assert_eq!(res.value, value);
}

#[cfg(desktop)]
std::thread::spawn(|| {
let server = match tiny_http::Server::http("localhost:3003") {
Expand Down