105 lines
3.0 KiB
Rust
105 lines
3.0 KiB
Rust
/// ファイル一覧を取得(除外ディレクトリをスキップ)
|
|
#[tauri::command]
|
|
fn read_dir_recursive(
|
|
path: String,
|
|
exclude_dirs: Vec<String>,
|
|
max_files: usize,
|
|
) -> Result<Vec<FileEntry>, String> {
|
|
let mut results = Vec::new();
|
|
collect_files(
|
|
std::path::Path::new(&path),
|
|
&path,
|
|
&exclude_dirs,
|
|
&mut results,
|
|
max_files,
|
|
);
|
|
Ok(results)
|
|
}
|
|
|
|
#[derive(serde::Serialize)]
|
|
struct FileEntry {
|
|
path: String, // ルートからの相対パス
|
|
abs_path: String, // 絶対パス(ファイル読み込み用)
|
|
size: u64,
|
|
}
|
|
|
|
fn collect_files(
|
|
dir: &std::path::Path,
|
|
root: &str,
|
|
exclude_dirs: &[String],
|
|
results: &mut Vec<FileEntry>,
|
|
max_files: usize,
|
|
) {
|
|
if results.len() >= max_files {
|
|
return;
|
|
}
|
|
let Ok(entries) = std::fs::read_dir(dir) else {
|
|
return;
|
|
};
|
|
for entry in entries.flatten() {
|
|
if results.len() >= max_files {
|
|
break;
|
|
}
|
|
let path = entry.path();
|
|
let name = entry.file_name().to_string_lossy().to_string();
|
|
|
|
if path.is_dir() {
|
|
if exclude_dirs.contains(&name) {
|
|
continue;
|
|
}
|
|
collect_files(&path, root, exclude_dirs, results, max_files);
|
|
} else if path.is_file() {
|
|
let abs = path.to_string_lossy().to_string();
|
|
let rel = abs.strip_prefix(root).unwrap_or(&abs).trim_start_matches(['/', '\\']).to_string();
|
|
let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
|
|
results.push(FileEntry { path: rel, abs_path: abs, size });
|
|
}
|
|
}
|
|
}
|
|
|
|
/// ファイル内容を読み込む
|
|
#[tauri::command]
|
|
fn read_file(abs_path: String) -> Result<String, String> {
|
|
std::fs::read_to_string(&abs_path).map_err(|e| e.to_string())
|
|
}
|
|
|
|
/// 修正済み内容をファイルに書き戻す
|
|
#[tauri::command]
|
|
fn write_file(abs_path: String, content: String) -> Result<(), String> {
|
|
std::fs::write(&abs_path, content).map_err(|e| e.to_string())
|
|
}
|
|
|
|
/// git status / diff / commit を実行
|
|
#[tauri::command]
|
|
fn git_command(cwd: String, args: Vec<String>) -> Result<String, String> {
|
|
let output = std::process::Command::new("git")
|
|
.current_dir(&cwd)
|
|
.args(&args)
|
|
.output()
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
if output.status.success() {
|
|
Ok(stdout)
|
|
} else {
|
|
Err(if stderr.is_empty() { stdout } else { stderr })
|
|
}
|
|
}
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
pub fn run() {
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_dialog::init())
|
|
.plugin(tauri_plugin_fs::init())
|
|
.plugin(tauri_plugin_shell::init())
|
|
.invoke_handler(tauri::generate_handler![
|
|
read_dir_recursive,
|
|
read_file,
|
|
write_file,
|
|
git_command,
|
|
])
|
|
.run(tauri::generate_context!())
|
|
.expect("error while running tauri application");
|
|
}
|