mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-12-10 06:32:35 +00:00
config_tools: refactor configurator for web page cache issue
refactor configurator for web page cache issue Tracked-On: #7356 Signed-off-by: Weiyi Feng <weiyix.feng@intel.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
use std::borrow::Borrow;
|
||||
use std::fs;
|
||||
use std::ops::Add;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@@ -7,6 +6,12 @@ use serde::{Deserialize, Serialize};
|
||||
use glob::{glob_with, MatchOptions};
|
||||
use itertools::Itertools;
|
||||
|
||||
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
|
||||
#[repr(u16)]
|
||||
#[non_exhaustive]
|
||||
@@ -178,8 +183,8 @@ impl Configurator {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_history(&mut self, history_type: HistoryType, path: &Path) {
|
||||
let path_string: String = path.to_string_lossy().parse().unwrap();
|
||||
pub fn add_history(&mut self, history_type: HistoryType, history_path: &Path) {
|
||||
let path_string: String = history_path.to_string_lossy().parse().unwrap();
|
||||
match history_type {
|
||||
HistoryType::WorkingFolder => {
|
||||
self.config_data.history.working_folder.insert(0, path_string);
|
||||
@@ -266,8 +271,8 @@ pub fn get_history(history_type: HistoryType) -> Result<String, ()> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn add_history(history_type: HistoryType, path: String) -> Result<(), &'static str> {
|
||||
let path_buf = Path::new(&path);
|
||||
pub fn add_history(history_type: HistoryType, history_path: String) -> Result<(), &'static str> {
|
||||
let path_buf = Path::new(&history_path);
|
||||
if !(path_buf.is_dir() || path_buf.is_file()) {
|
||||
return Err("Not a validate dir or file path.");
|
||||
}
|
||||
@@ -313,4 +318,69 @@ pub fn get_home() -> Result<String, ()> {
|
||||
Ok(path.to_str().unwrap().to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct DirEntry {
|
||||
path: String,
|
||||
children: Option<Vec<DirEntry>>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn acrn_read(file_path: &str) -> Result<String, String> {
|
||||
let mut file = File::open(file_path).map_err(|e| e.to_string())?;
|
||||
let mut contents = String::new();
|
||||
file
|
||||
.read_to_string(&mut contents)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn acrn_write(file_path: &str, contents: &str) -> Result<(), String> {
|
||||
let mut file = File::create(file_path).map_err(|e| e.to_string())?;
|
||||
file
|
||||
.write_all(contents.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn acrn_is_file(path: &str) -> bool {
|
||||
fs::metadata(path)
|
||||
.map(|metadata| metadata.is_file())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn acrn_create_dir(path: &str) -> Result<(), String> {
|
||||
fs::create_dir(path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn read_dir<P: AsRef<Path>>(
|
||||
path: P,
|
||||
recursive: bool,
|
||||
) -> io::Result<Vec<DirEntry>> {
|
||||
let path = path.as_ref();
|
||||
let mut entries = Vec::new();
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path().to_str().unwrap().to_string();
|
||||
let children = if recursive && entry.file_type()?.is_dir() {
|
||||
Some(read_dir(&path, true)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
entries.push(DirEntry { path, children });
|
||||
}
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn acrn_read_dir(
|
||||
path: &str,
|
||||
recursive: bool,
|
||||
) -> Result<Vec<DirEntry>, String> {
|
||||
read_dir(path, recursive).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
use serde::Serialize;
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_read_text_file(path: &str) -> Result<String, String> {
|
||||
let mut file = File::open(path).map_err(|e| e.to_string())?;
|
||||
let mut contents = String::new();
|
||||
file
|
||||
.read_to_string(&mut contents)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_write_text_file(path: &str, contents: &str) -> Result<(), String> {
|
||||
let mut file = File::create(path).map_err(|e| e.to_string())?;
|
||||
file
|
||||
.write_all(contents.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_read_binary_file(path: &str) -> Result<Vec<u8>, String> {
|
||||
let mut file = File::open(path).map_err(|e| e.to_string())?;
|
||||
let mut contents = Vec::new();
|
||||
file.read_to_end(&mut contents).map_err(|e| e.to_string())?;
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_write_binary_file(path: &str, contents: &[u8]) -> Result<(), String> {
|
||||
let mut file = File::create(path).map_err(|e| e.to_string())?;
|
||||
file.write_all(contents).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct DirEntry {
|
||||
path: String,
|
||||
children: Option<Vec<DirEntry>>,
|
||||
}
|
||||
|
||||
fn read_dir<P: AsRef<Path>>(
|
||||
path: P,
|
||||
recursive: bool,
|
||||
) -> io::Result<Vec<DirEntry>> {
|
||||
let path = path.as_ref();
|
||||
let mut entries = Vec::new();
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path().to_str().unwrap().to_string();
|
||||
let children = if recursive && entry.file_type()?.is_dir() {
|
||||
Some(read_dir(&path, true)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
entries.push(DirEntry { path, children });
|
||||
}
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_read_dir(
|
||||
path: &str,
|
||||
recursive: bool,
|
||||
) -> Result<Vec<DirEntry>, String> {
|
||||
read_dir(path, recursive).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_rename(from: &str, to: &str) -> Result<(), String> {
|
||||
fs::rename(from, to).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_delete_file(path: &str) -> Result<(), String> {
|
||||
fs::remove_file(path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_delete_dir(path: &str) -> Result<(), String> {
|
||||
fs::remove_dir_all(path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_create_dir(path: &str) -> Result<(), String> {
|
||||
fs::create_dir(path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_is_file(path: &str) -> bool {
|
||||
fs::metadata(path).map(|m| m.is_file()).unwrap_or(false)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fs_is_dir(path: &str) -> bool {
|
||||
fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false)
|
||||
}
|
||||
@@ -3,78 +3,24 @@ all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
mod filesystem;
|
||||
mod configurator;
|
||||
|
||||
use log::*;
|
||||
|
||||
use tauri::{api::cli::get_matches, utils::config::CliConfig, PackageInfo, RunEvent};
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
// Init context
|
||||
let context = tauri::generate_context!();
|
||||
|
||||
// Build app instance and run
|
||||
let app = tauri::Builder::default()
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
filesystem::fs_rename,
|
||||
filesystem::fs_read_dir,
|
||||
filesystem::fs_read_text_file,
|
||||
filesystem::fs_read_binary_file,
|
||||
filesystem::fs_write_text_file,
|
||||
filesystem::fs_write_binary_file,
|
||||
filesystem::fs_read_dir,
|
||||
filesystem::fs_delete_file,
|
||||
filesystem::fs_delete_dir,
|
||||
filesystem::fs_create_dir,
|
||||
filesystem::fs_is_file,
|
||||
filesystem::fs_is_dir,
|
||||
configurator::get_history,
|
||||
configurator::add_history,
|
||||
configurator::set_working_folder,
|
||||
configurator::write_board,
|
||||
configurator::force_reset,
|
||||
configurator::get_home
|
||||
])
|
||||
.setup(|app| {
|
||||
// Handle cli cmdline
|
||||
let app_config = app.config();
|
||||
let cli_config = app_config.tauri.cli.as_ref().unwrap();
|
||||
let package_info = app.package_info();
|
||||
handle_cli_arg_data(&cli_config, &package_info);
|
||||
Ok(())
|
||||
})
|
||||
.build(context)
|
||||
configurator::get_home,
|
||||
configurator::acrn_read,
|
||||
configurator::acrn_write,
|
||||
configurator::acrn_is_file,
|
||||
configurator::acrn_read_dir,
|
||||
configurator::acrn_create_dir,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
app.run(|_app, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
info!("Received Exit event");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
fn handle_cli_arg_data(cli_config: &CliConfig, pkg_info: &PackageInfo) {
|
||||
match get_matches(cli_config, pkg_info) {
|
||||
Ok(matches) => {
|
||||
let mut exit_flag = false;
|
||||
if let Some(arg_data) = matches.args.get("help") {
|
||||
println!("{}", arg_data.value.as_str().unwrap_or("No help available"));
|
||||
exit_flag = true;
|
||||
}
|
||||
if let Some(arg_data) = matches.args.get("version") {
|
||||
println!("{}", arg_data.value.as_str().unwrap_or("No version data available"));
|
||||
exit_flag = true
|
||||
}
|
||||
if exit_flag {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("{}", e.to_string());
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user