feat: commit code

This commit is contained in:
mrxiaozhuox committed 2022-08-22 11:00:31 -07:00
1 parent 0e8414b40a
commit 6c1374edb3
6 files changed
+146 -82

No files matched your search

Generated
+11
View File
@@ -808,6 +808,15 @@ dependencies = [
"syn",
]
[[package]]
name = "erased-serde"
version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "003000e712ad0f95857bd4d2ef8d1890069e06554101697d12050668b2f6f020"
dependencies = [
"serde",
]
[[package]]
name = "euclid"
version = "0.22.7"
@@ -1591,6 +1600,7 @@ checksum = "10277581090f5cb7ecf814bc611152ce4db6dc8deffcaa08e24ed4c5197d9186"
dependencies = [
"bstr",
"cc",
"erased-serde",
"futures-core",
"futures-task",
"futures-util",
@@ -1600,6 +1610,7 @@ dependencies = [
"once_cell",
"pkg-config",
"rustc-hash",
"serde",
]
[[package]]
+1 -1
View File
@@ -60,7 +60,7 @@ dioxus-rsx-interpreter = { git = "https://github.com/dioxuslabs/dioxus/" }
proc-macro2 = { version = "1.0", features = ["span-locations"] }
# plugin packages
mlua = { version = "0.8.1", features = ["lua54", "vendored", "async"] }
mlua = { version = "0.8.1", features = ["lua54", "vendored", "async", "serialize"] }
[[bin]]
path = "src/main.rs"
+3 -1
View File
@@ -34,7 +34,7 @@ impl Build {
.clone()
});
let _ = plugin_manager.on_build_event(&crate_config, &platform);
let _ = plugin_manager.on_build_start(&crate_config, &platform);
match platform.as_str() {
"web" => {
@@ -65,6 +65,8 @@ impl Build {
)?;
file.write_all(temp.as_bytes())?;
let _ = plugin_manager.on_build_finish(&crate_config, &platform);
Ok(())
}
}
+15
View File
@@ -1,4 +1,5 @@
use mlua::{FromLua, Function, ToLua};
use serde::Serialize;
pub mod command;
pub mod dirs;
@@ -47,6 +48,11 @@ impl<'lua> FromLua<'lua> for PluginInfo<'lua> {
if let Ok(v) = tab.get::<_, Function>("on_init") {
res.on_init = Some(v);
}
if let Ok(v) = tab.get::<_, PluginBuildInfo>("build") {
res.build = v;
}
}
Ok(res)
@@ -113,4 +119,13 @@ impl<'lua> ToLua<'lua> for PluginBuildInfo<'lua> {
Ok(mlua::Value::Table(res))
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct PluginServeInfo<'lua> {
pub interval: i32,
pub on_start: Option<Function<'lua>>,
pub on_interval: Option<Function<'lua>>,
}
+80 -33
View File
@@ -3,11 +3,14 @@ use std::{
path::PathBuf,
};
use mlua::{AsChunk, Lua, Table};
use mlua::{AsChunk, Lua, Table, ToLuaMulti};
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::{tools::{app_path, clone_repo}, CrateConfig};
use crate::{
tools::{app_path, clone_repo},
CrateConfig,
};
use self::{
interface::PluginInfo,
@@ -70,19 +73,28 @@ impl PluginManager {
let mut file = std::fs::File::open(init_file).unwrap();
let mut buffer = String::new();
file.read_to_string(&mut buffer).unwrap();
let info = lua.load(&buffer).eval::<PluginInfo>().unwrap();
let _ = manager.set(index, info.clone());
let dir_name_str = plugin_dir.name().unwrap().to_string();
lua.globals().set("current_dir_name", dir_name_str).unwrap();
let info = lua.load(&buffer).eval::<PluginInfo>();
match info {
Ok(info) => {
let _ = manager.set(index, info.clone());
// call `on_init` if file "dcp.json" not exists
let dcp_file = plugin_dir.join("dcp.json");
if !dcp_file.is_file() {
init_list.push((index, dcp_file, info));
let dir_name_str = plugin_dir.name().unwrap().to_string();
lua.globals().set("current_dir_name", dir_name_str).unwrap();
// call `on_init` if file "dcp.json" not exists
let dcp_file = plugin_dir.join("dcp.json");
if !dcp_file.is_file() {
init_list.push((index, dcp_file, info));
}
index += 1;
}
Err(_e) => {
let dir_name = plugin_dir.file_name().unwrap().to_str().unwrap();
log::error!("Plugin '{dir_name}' load failed.");
}
}
index += 1;
}
}
}
@@ -109,33 +121,68 @@ impl PluginManager {
Some(Self { lua })
}
// pub fn load_all_plugins(&self) -> anyhow::Result<()> {
// let lua = &self.lua;
// let manager = lua.globals().get::<_, Table>("manager")?;
// for i in 1..(manager.len()? as i32 + 1) {
// let _ = manager.get::<i32, PluginInfo>(i)?;
// let code = format!("manager[{i}].on_load()");
// lua.load(&code).exec()?;
// }
// Ok(())
// }
pub fn on_build_event(&self, crate_config: &CrateConfig, platform: &str) -> anyhow::Result<()> {
pub fn on_build_start(&self, crate_config: &CrateConfig, platform: &str) -> anyhow::Result<()> {
let lua = &self.lua;
let manager = lua.globals().get::<_, Table>("manager")?;
let info = json!({
"name": crate_config.dioxus_config.application.name,
"platform": platform,
"out_dir": crate_config.out_dir.to_str().unwrap(),
"asset_dir": crate_config.asset_dir.to_str().unwrap(),
});
let args = lua.create_table()?;
args.set("name", crate_config.dioxus_config.application.name.clone())?;
args.set("platform", platform)?;
args.set("out_dir", crate_config.out_dir.to_str().unwrap())?;
args.set("asset_dir", crate_config.asset_dir.to_str().unwrap())?;
for i in 1..(manager.len()? as i32 + 1) {
let _ = manager.get::<i32, PluginInfo>(i)?;
let code = format!("manager[{i}].build.on_start({})", info.to_string());
lua.load(&code).exec()?;
let info = manager.get::<i32, PluginInfo>(i)?;
if let Some(func) = info.build.on_start {
func.call::<Table, ()>(args.clone())?;
}
}
Ok(())
}
pub fn on_build_finish(
&self,
crate_config: &CrateConfig,
platform: &str,
) -> anyhow::Result<()> {
let lua = &self.lua;
let manager = lua.globals().get::<_, Table>("manager")?;
let args = lua.create_table()?;
args.set("name", crate_config.dioxus_config.application.name.clone())?;
args.set("platform", platform)?;
args.set("out_dir", crate_config.out_dir.to_str().unwrap())?;
args.set("asset_dir", crate_config.asset_dir.to_str().unwrap())?;
for i in 1..(manager.len()? as i32 + 1) {
let info = manager.get::<i32, PluginInfo>(i)?;
if let Some(func) = info.build.on_finish {
func.call::<Table, ()>(args.clone())?;
}
}
Ok(())
}
pub fn call_event<'lua>(&self, event: &str, args: impl ToLuaMulti<'lua>) -> anyhow::Result<()> {
let lua = &self.lua;
let manager = lua.globals().get::<_, Table>("manager")?;
for i in 1..(manager.len()? as i32 + 1) {
let info = manager.get::<_, PluginInfo>(i)?;
let func = match event {
"on_init" => info.on_init,
"build.on_start" => info.build.on_start,
_ => None,
};
if let Some(func) = func {
func.call(args)?;
}
}
Ok(())
+36 -47
View File
@@ -151,58 +151,47 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> {
} else {
*last = last.split_at(end.column).0;
}
if let Some(last) = lines.last_mut() {
// if there is only one line the start index of last line will be the start of the rsx!, not the start of the line
if start.line == end.line {
*last = last
.split_at(end.column - start.column)
.0;
} else {
*last = last.split_at(end.column).0;
}
}
let rsx = lines.join("\n");
messages.push(SetRsxMessage {
location: hr,
new_text: rsx,
});
}
let rsx = lines.join("\n");
messages.push(SetRsxMessage {
location: hr,
new_text: rsx,
});
}
}
}
} else {
// if this is a new file, rebuild the project
*last_file_rebuild = FileMap::new(crate_dir.clone());
}
} else {
// if this is a new file, rebuild the project
*last_file_rebuild = FileMap::new(crate_dir.clone());
}
}
if needs_rebuild {
match build_manager.rebuild() {
Ok(res) => {
print_console_info(
port,
&config,
PrettierOptions {
changed: evt.paths,
warnings: res.warnings,
elapsed_time: res.elapsed_time,
},
);
}
Err(err) => {
log::error!("{}", err);
}
}
}
if !messages.is_empty() {
let _ = hot_reload_tx.send(SetManyRsxMessage(messages));
}
}
last_update_time = chrono::Local::now().timestamp();
if needs_rebuild {
match build_manager.rebuild() {
Ok(res) => {
print_console_info(
port,
&config,
PrettierOptions {
changed: evt.paths,
warnings: res.warnings,
elapsed_time: res.elapsed_time,
},
);
}
Err(err) => {
log::error!("{}", err);
}
}
}
if !messages.is_empty() {
let _ = hot_reload_tx.send(SetManyRsxMessage(messages));
}
}
},
notify::Config::default(),
)
last_update_time = chrono::Local::now().timestamp();
}
})
.unwrap();
for sub_path in allow_watch_path {
@@ -332,11 +321,11 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> {
},
);
}
Err(e) => log::error!("{}", e),
}
}
},
notify::Config::default(),
)
}
})
.unwrap();
for sub_path in allow_watch_path {
@@ -610,4 +599,4 @@ async fn ws_handler(
reload_watcher.await.unwrap();
})
}
}