-
Notifications
You must be signed in to change notification settings - Fork 257
Swap platform_dirs for directories for config paths #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,109 @@ | ||||||||||||
| use std::{fs, path::Path}; | ||||||||||||
|
|
||||||||||||
| use directories::ProjectDirs; | ||||||||||||
| use platform_dirs::AppDirs; | ||||||||||||
|
|
||||||||||||
| use crate::data::config::Config; | ||||||||||||
|
|
||||||||||||
| const APP_NAME: &str = "Psst"; | ||||||||||||
|
|
||||||||||||
| pub fn perform_migration() { | ||||||||||||
| let old_app_dirs = AppDirs::new(Some(APP_NAME), false); | ||||||||||||
| let new_project_dirs = ProjectDirs::from("", "", APP_NAME); | ||||||||||||
|
|
||||||||||||
| if let (Some(old_dirs), Some(new_dirs)) = (old_app_dirs, new_project_dirs) { | ||||||||||||
| migrate_path( | ||||||||||||
| &old_dirs.config_dir, | ||||||||||||
| new_dirs.config_dir(), | ||||||||||||
| &["config.json"], | ||||||||||||
| ); | ||||||||||||
| migrate_path( | ||||||||||||
| &old_dirs.cache_dir, | ||||||||||||
| new_dirs.cache_dir(), | ||||||||||||
| &[ | ||||||||||||
| "tracks", | ||||||||||||
| "track", | ||||||||||||
| "episodes", | ||||||||||||
| "episode", | ||||||||||||
| "audio", | ||||||||||||
| "keys", | ||||||||||||
| "key", | ||||||||||||
| "lyrics", | ||||||||||||
| "images", | ||||||||||||
| "artist-info", | ||||||||||||
| "related-artists", | ||||||||||||
| "album", | ||||||||||||
| "show", | ||||||||||||
| "user-info", | ||||||||||||
| ], | ||||||||||||
|
Comment on lines
+37
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Country code is cached by |
||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if let Some(active_cache_dir) = Config::cache_dir() { | ||||||||||||
| rename_cache_subdirectories(&active_cache_dir); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn migrate_path(old_dir: &Path, new_dir: &Path, items_to_move: &[&str]) { | ||||||||||||
| if old_dir == new_dir || !old_dir.exists() { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // New path is a subdirectory of the old path (e.g. .../Psst/config vs .../Psst). | ||||||||||||
| // We must move specific items into the new subdirectory to avoid recursion. | ||||||||||||
| if new_dir.starts_with(old_dir) { | ||||||||||||
| log::info!( | ||||||||||||
| "migrating content from {:?} into subdirectory {:?}", | ||||||||||||
| old_dir, | ||||||||||||
| new_dir | ||||||||||||
| ); | ||||||||||||
| if let Err(err) = fs::create_dir_all(new_dir) { | ||||||||||||
| log::error!("failed to create directory {:?}: {}", new_dir, err); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| for &item in items_to_move { | ||||||||||||
| let old_item = old_dir.join(item); | ||||||||||||
| let new_item = new_dir.join(item); | ||||||||||||
| move_if_exists(&old_item, &new_item); | ||||||||||||
| } | ||||||||||||
| } else if !new_dir.exists() { | ||||||||||||
| log::info!("migrating directory from {:?} to {:?}", old_dir, new_dir); | ||||||||||||
| if let Some(parent) = new_dir.parent() { | ||||||||||||
| let _ = fs::create_dir_all(parent); | ||||||||||||
| } | ||||||||||||
| if let Err(err) = fs::rename(old_dir, new_dir) { | ||||||||||||
| log::error!("failed to migrate directory: {}", err); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn rename_cache_subdirectories(cache_dir: &Path) { | ||||||||||||
| if !cache_dir.exists() { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| let renames = [ | ||||||||||||
| ("track", "tracks"), | ||||||||||||
| ("episode", "episodes"), | ||||||||||||
| ("show", "shows"), | ||||||||||||
| ("album", "albums"), | ||||||||||||
| ("artist", "artists"), | ||||||||||||
| ("key", "keys"), | ||||||||||||
| ]; | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming is a good idea |
||||||||||||
|
|
||||||||||||
| for (old_name, new_name) in renames { | ||||||||||||
| let old_path = cache_dir.join(old_name); | ||||||||||||
| let new_path = cache_dir.join(new_name); | ||||||||||||
| move_if_exists(&old_path, &new_path); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn move_if_exists(from: &Path, to: &Path) { | ||||||||||||
| if from.exists() && !to.exists() { | ||||||||||||
| log::info!("moving {:?} to {:?}", from, to); | ||||||||||||
| if let Err(err) = fs::rename(from, to) { | ||||||||||||
| log::error!("failed to move {:?}: {}", from, err); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ pub mod config; | |
| mod ctx; | ||
| mod find; | ||
| mod id; | ||
| mod migration; | ||
| mod nav; | ||
| mod playback; | ||
| mod playlist; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the file could be renamed, just in case. What if there is going to be a migration of another kind in the future, how are we going to differentiate it to this migration? IMO we can afford to call this file something verbose like
migration_paths_to_use_directories.rssince we aim to remove this some time in the future eventually.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Hmm, I renamed legacy which might be enough? Everything in that module should eventually go away.