Skip to content

Commit 60d68de

Browse files
committedSep 12, 2024
fix filesystem api
1 parent 91cfbf3 commit 60d68de

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed
 

Diff for: ‎.github/workflows/build.yml

-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ jobs:
7272
steps:
7373
- name: Download
7474
uses: actions/download-artifact@v2
75-
- name: look file stucture
76-
run: |
77-
ls -R
7875
- name: zip windows package win32-x64
7976
uses: TheDoctor0/zip-release@v0.2.1
8077
with:

Diff for: ‎src/bee/lua_filesystem.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ fn copy_file(_: &Lua, (source, destination): (LuaFilePath, LuaFilePath)) -> LuaR
236236

237237
fn absolute(_: &Lua, path: LuaFilePath) -> LuaResult<LuaFilePath> {
238238
let canonical_path = std::fs::canonicalize(&path.path)?;
239-
240239
let full_path_str = canonical_path.to_str().unwrap_or("").to_string();
241240

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

362+
fn pairs(lua: &Lua, path: LuaFilePath) -> LuaResult<(mlua::Function, mlua::Table, mlua::Value)> {
363+
let table = lua.create_table()?;
364+
if let Ok(_) = std::fs::exists(&path.path) {
365+
for entry in std::fs::read_dir(&path.path)? {
366+
let entry = entry?;
367+
let path = entry.path();
368+
let path = LuaFilePath::new(path.to_str().unwrap_or("").to_string());
369+
let file_type = match std::fs::symlink_metadata(&path.path) {
370+
Ok(metadata) => {
371+
if metadata.file_type().is_symlink() {
372+
"symlink".to_string()
373+
} else if metadata.file_type().is_file() {
374+
"file".to_string()
375+
} else if metadata.file_type().is_dir() {
376+
"directory".to_string()
377+
} else {
378+
"unknown".to_string()
379+
}
380+
}
381+
Err(_) => "unknown".to_string(),
382+
};
383+
let file_status = SymlinkStatus::new(file_type);
384+
table.set(path.clone(), file_status)?;
385+
}
386+
}
387+
let next = lua.globals().get::<mlua::Function>("next").unwrap();
388+
Ok((next, table, mlua::Nil))
389+
}
390+
363391
pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
364392
let exports = lua.create_table()?;
365393

@@ -397,17 +425,7 @@ pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
397425
)?;
398426
exports.set(
399427
"pairs",
400-
lua.create_function(|lua, path: LuaFilePath| -> LuaResult<_> {
401-
let table = lua.create_table()?;
402-
for entry in std::fs::read_dir(&path.path)? {
403-
let entry = entry?;
404-
let path = entry.path();
405-
let path = LuaFilePath::new(path.to_str().unwrap_or("").to_string());
406-
table.set(path.clone(), true)?;
407-
}
408-
let next = lua.globals().get::<mlua::Function>("next").unwrap();
409-
Ok((next, table, mlua::Nil))
410-
})?,
428+
lua.create_function(pairs)?,
411429
)?;
412430
exports.set("fullpath", lua.create_function(full_path)?)?;
413431
exports.set("symlink_status", lua.create_function(symlink_status)?)?;

Diff for: ‎src/bee/lua_filewatch.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use mlua::prelude::LuaResult;
22
use mlua::prelude::*;
33
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
44
// use std::path::PathBuf;
5-
use std::sync::mpsc::{channel, Receiver, Sender};
5+
use std::sync::mpsc::{channel, Receiver};
66

77
use super::lua_filesystem::LuaFilePath;
88
// use std::time::Duration;
@@ -43,11 +43,18 @@ impl LuaFileWatch {
4343
if let Some(rx) = &self.receiver {
4444
if let Ok(event) = rx.try_recv() {
4545
let path = event.paths[0].to_str().unwrap().to_string();
46+
if let Some(filter) = &self.filter {
47+
if let Ok(result) = filter.call::<bool>(path.clone()) {
48+
if !result {
49+
return Ok((mlua::Nil, mlua::Nil));
50+
}
51+
}
52+
}
4653
let kind = match event.kind {
4754
notify::EventKind::Create(_) => "create",
4855
notify::EventKind::Modify(_) => "modify",
4956
notify::EventKind::Remove(_) => "remove",
50-
_ => "unknown"
57+
_ => "unknown",
5158
};
5259

5360
return Ok((kind.into_lua(lua).unwrap(), path.into_lua(lua).unwrap()));

Diff for: ‎src/codestyle/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use mlua::Lua;
22

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

8+
#[allow(unused)]
79
pub fn fake_code_style(lua: &Lua) -> mlua::Result<mlua::Table> {
810
let table = lua.create_table()?;
911
table.set("format", lua.create_function(not_implement)?)?;

Diff for: ‎src/lua_preload.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::bee;
2+
#[allow(unused)]
23
use crate::codestyle::fake_code_style;
34
use crate::lua_seri;
5+
#[allow(unused)]
46
use crate::override_lua;
57
use mlua::{lua_State, prelude::*};
68

Diff for: ‎src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ mod codestyle;
77

88
#[macro_use]
99
extern crate lazy_static;
10-
1110
use std::{env, path};
12-
1311
use mlua::prelude::*;
1412

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

0 commit comments

Comments
 (0)