Skip to content

Commit

Permalink
fix filesystem api
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Sep 12, 2024
1 parent 91cfbf3 commit 60d68de
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ jobs:
steps:
- name: Download
uses: actions/download-artifact@v2
- name: look file stucture
run: |
ls -R
- name: zip windows package win32-x64
uses: TheDoctor0/[email protected]
with:
Expand Down
42 changes: 30 additions & 12 deletions src/bee/lua_filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ fn copy_file(_: &Lua, (source, destination): (LuaFilePath, LuaFilePath)) -> LuaR

fn absolute(_: &Lua, path: LuaFilePath) -> LuaResult<LuaFilePath> {
let canonical_path = std::fs::canonicalize(&path.path)?;

let full_path_str = canonical_path.to_str().unwrap_or("").to_string();

// 移除 \\?\ 前缀
Expand Down Expand Up @@ -360,6 +359,35 @@ fn symlink_status(_: &Lua, path: LuaFilePath) -> LuaResult<SymlinkStatus> {
Ok(SymlinkStatus::new(file_type))
}

fn pairs(lua: &Lua, path: LuaFilePath) -> LuaResult<(mlua::Function, mlua::Table, mlua::Value)> {
let table = lua.create_table()?;
if let Ok(_) = std::fs::exists(&path.path) {
for entry in std::fs::read_dir(&path.path)? {
let entry = entry?;
let path = entry.path();
let path = LuaFilePath::new(path.to_str().unwrap_or("").to_string());
let file_type = match std::fs::symlink_metadata(&path.path) {
Ok(metadata) => {
if metadata.file_type().is_symlink() {
"symlink".to_string()
} else if metadata.file_type().is_file() {
"file".to_string()
} else if metadata.file_type().is_dir() {
"directory".to_string()
} else {
"unknown".to_string()
}
}
Err(_) => "unknown".to_string(),
};
let file_status = SymlinkStatus::new(file_type);
table.set(path.clone(), file_status)?;
}
}
let next = lua.globals().get::<mlua::Function>("next").unwrap();
Ok((next, table, mlua::Nil))
}

pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
let exports = lua.create_table()?;

Expand Down Expand Up @@ -397,17 +425,7 @@ pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
)?;
exports.set(
"pairs",
lua.create_function(|lua, path: LuaFilePath| -> LuaResult<_> {
let table = lua.create_table()?;
for entry in std::fs::read_dir(&path.path)? {
let entry = entry?;
let path = entry.path();
let path = LuaFilePath::new(path.to_str().unwrap_or("").to_string());
table.set(path.clone(), true)?;
}
let next = lua.globals().get::<mlua::Function>("next").unwrap();
Ok((next, table, mlua::Nil))
})?,
lua.create_function(pairs)?,
)?;
exports.set("fullpath", lua.create_function(full_path)?)?;
exports.set("symlink_status", lua.create_function(symlink_status)?)?;
Expand Down
11 changes: 9 additions & 2 deletions src/bee/lua_filewatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use mlua::prelude::LuaResult;
use mlua::prelude::*;
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
// use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::mpsc::{channel, Receiver};

use super::lua_filesystem::LuaFilePath;
// use std::time::Duration;
Expand Down Expand Up @@ -43,11 +43,18 @@ impl LuaFileWatch {
if let Some(rx) = &self.receiver {
if let Ok(event) = rx.try_recv() {
let path = event.paths[0].to_str().unwrap().to_string();
if let Some(filter) = &self.filter {
if let Ok(result) = filter.call::<bool>(path.clone()) {
if !result {
return Ok((mlua::Nil, mlua::Nil));
}
}
}
let kind = match event.kind {
notify::EventKind::Create(_) => "create",
notify::EventKind::Modify(_) => "modify",
notify::EventKind::Remove(_) => "remove",
_ => "unknown"
_ => "unknown",
};

return Ok((kind.into_lua(lua).unwrap(), path.into_lua(lua).unwrap()));
Expand Down
2 changes: 2 additions & 0 deletions src/codestyle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use mlua::Lua;

#[allow(unused)]
fn not_implement(_: &Lua, _: mlua::MultiValue) -> mlua::Result<(bool, String)> {
Ok((false, "not implement".to_string()))
}

#[allow(unused)]
pub fn fake_code_style(lua: &Lua) -> mlua::Result<mlua::Table> {
let table = lua.create_table()?;
table.set("format", lua.create_function(not_implement)?)?;
Expand Down
2 changes: 2 additions & 0 deletions src/lua_preload.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::bee;
#[allow(unused)]
use crate::codestyle::fake_code_style;
use crate::lua_seri;
#[allow(unused)]
use crate::override_lua;
use mlua::{lua_State, prelude::*};

Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ mod codestyle;

#[macro_use]
extern crate lazy_static;

use std::{env, path};

use mlua::prelude::*;

#[tokio::main(flavor = "current_thread")]
Expand Down

0 comments on commit 60d68de

Please sign in to comment.