always ensure the dirs exist (#2245)
Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -52,14 +52,22 @@ async fn set_state(app: tauri::AppHandle, state: Option<ProjectState>) -> Result
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_app_settings_file_path(app: &tauri::AppHandle) -> Result<PathBuf, InvokeError> {
|
async fn get_app_settings_file_path(app: &tauri::AppHandle) -> Result<PathBuf, InvokeError> {
|
||||||
let app_config_dir = app.path().app_config_dir()?;
|
let app_config_dir = app.path().app_config_dir()?;
|
||||||
|
|
||||||
|
// Ensure this directory exists.
|
||||||
|
if !app_config_dir.exists() {
|
||||||
|
tokio::fs::create_dir_all(&app_config_dir)
|
||||||
|
.await
|
||||||
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(app_config_dir.join(SETTINGS_FILE_NAME))
|
Ok(app_config_dir.join(SETTINGS_FILE_NAME))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn read_app_settings_file(app: tauri::AppHandle) -> Result<Configuration, InvokeError> {
|
async fn read_app_settings_file(app: tauri::AppHandle) -> Result<Configuration, InvokeError> {
|
||||||
let mut settings_path = get_app_settings_file_path(&app)?;
|
let mut settings_path = get_app_settings_file_path(&app).await?;
|
||||||
let mut needs_migration = false;
|
let mut needs_migration = false;
|
||||||
|
|
||||||
// Check if this file exists.
|
// Check if this file exists.
|
||||||
@ -104,7 +112,7 @@ async fn read_app_settings_file(app: tauri::AppHandle) -> Result<Configuration,
|
|||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn write_app_settings_file(app: tauri::AppHandle, configuration: Configuration) -> Result<(), InvokeError> {
|
async fn write_app_settings_file(app: tauri::AppHandle, configuration: Configuration) -> Result<(), InvokeError> {
|
||||||
let settings_path = get_app_settings_file_path(&app)?;
|
let settings_path = get_app_settings_file_path(&app).await?;
|
||||||
let contents = toml::to_string_pretty(&configuration).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
let contents = toml::to_string_pretty(&configuration).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
||||||
tokio::fs::write(settings_path, contents.as_bytes())
|
tokio::fs::write(settings_path, contents.as_bytes())
|
||||||
.await
|
.await
|
||||||
@ -113,13 +121,19 @@ async fn write_app_settings_file(app: tauri::AppHandle, configuration: Configura
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_project_settings_file_path(app_settings: Configuration, project_name: &str) -> Result<PathBuf, InvokeError> {
|
async fn get_project_settings_file_path(
|
||||||
Ok(app_settings
|
app_settings: Configuration,
|
||||||
.settings
|
project_name: &str,
|
||||||
.project
|
) -> Result<PathBuf, InvokeError> {
|
||||||
.directory
|
let project_dir = app_settings.settings.project.directory.join(project_name);
|
||||||
.join(project_name)
|
|
||||||
.join(PROJECT_SETTINGS_FILE_NAME))
|
if !project_dir.exists() {
|
||||||
|
tokio::fs::create_dir_all(&project_dir)
|
||||||
|
.await
|
||||||
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(project_dir.join(PROJECT_SETTINGS_FILE_NAME))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@ -127,7 +141,7 @@ async fn read_project_settings_file(
|
|||||||
app_settings: Configuration,
|
app_settings: Configuration,
|
||||||
project_name: &str,
|
project_name: &str,
|
||||||
) -> Result<ProjectConfiguration, InvokeError> {
|
) -> Result<ProjectConfiguration, InvokeError> {
|
||||||
let settings_path = get_project_settings_file_path(app_settings, project_name)?;
|
let settings_path = get_project_settings_file_path(app_settings, project_name).await?;
|
||||||
|
|
||||||
// Check if this file exists.
|
// Check if this file exists.
|
||||||
if !settings_path.exists() {
|
if !settings_path.exists() {
|
||||||
@ -149,7 +163,7 @@ async fn write_project_settings_file(
|
|||||||
project_name: &str,
|
project_name: &str,
|
||||||
configuration: ProjectConfiguration,
|
configuration: ProjectConfiguration,
|
||||||
) -> Result<(), InvokeError> {
|
) -> Result<(), InvokeError> {
|
||||||
let settings_path = get_project_settings_file_path(app_settings, project_name)?;
|
let settings_path = get_project_settings_file_path(app_settings, project_name).await?;
|
||||||
let contents = toml::to_string_pretty(&configuration).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
let contents = toml::to_string_pretty(&configuration).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
||||||
tokio::fs::write(settings_path, contents.as_bytes())
|
tokio::fs::write(settings_path, contents.as_bytes())
|
||||||
.await
|
.await
|
||||||
|
Reference in New Issue
Block a user