2021-05-23 20:08:30 +00:00
|
|
|
/*
|
|
|
|
* This file is part of espanso.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2021 Federico Terzi
|
|
|
|
*
|
|
|
|
* espanso is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* espanso is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-10-05 20:06:53 +00:00
|
|
|
#[allow(unused_imports)]
|
2021-05-23 20:08:30 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2021-10-05 20:06:53 +00:00
|
|
|
#[allow(unused_imports)]
|
2021-05-23 20:08:30 +00:00
|
|
|
#[macro_use]
|
|
|
|
#[cfg(test)]
|
|
|
|
extern crate include_dir;
|
|
|
|
|
2021-10-05 20:06:53 +00:00
|
|
|
#[allow(unused_imports)]
|
2021-05-23 20:08:30 +00:00
|
|
|
#[macro_use]
|
|
|
|
#[cfg(test)]
|
|
|
|
extern crate test_case;
|
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2021-05-23 20:08:30 +00:00
|
|
|
use anyhow::Result;
|
2021-05-29 11:08:32 +00:00
|
|
|
use fs_extra::dir::CopyOptions;
|
|
|
|
use tempdir::TempDir;
|
2021-05-23 20:08:30 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-05-24 18:58:18 +00:00
|
|
|
mod convert;
|
|
|
|
mod load;
|
2021-05-29 11:08:32 +00:00
|
|
|
mod render;
|
2021-05-24 18:58:18 +00:00
|
|
|
|
2021-05-23 20:08:30 +00:00
|
|
|
// TODO: implement
|
|
|
|
// Use yaml-rust with "preserve-order" = true
|
|
|
|
// Strategy:
|
|
|
|
// 1. Backup the current config directory in a zip archive (also with the packages)
|
|
|
|
// 2. Create a temporary directory alonside the legacy one called "espanso-new"
|
|
|
|
// 3. Convert all the files and write the output into "espanso-new"
|
|
|
|
// 4. Rename the legacy dir to "espanso-old"
|
|
|
|
// 5. Rename new dir to "espanso"
|
|
|
|
// 6. If the legacy directory was a symlink, try to recreate it (ask the user first)
|
|
|
|
|
|
|
|
// TODO: before attempting the migration strategy, check if the current
|
|
|
|
// espanso config directory is a symlink and, if so, attempt to remap
|
|
|
|
// the symlink with the new dir (after asking the user)
|
|
|
|
// This is necessary because in order to be safe, the migration strategy
|
|
|
|
// creates the new config on a new temporary directory and then "swaps"
|
|
|
|
// the old with the new one
|
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
// TODO: test also with non-lowercase file names
|
2021-05-23 20:08:30 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
pub fn migrate(config_dir: &Path, packages_dir: &Path, output_dir: &Path) -> Result<()> {
|
|
|
|
if !config_dir.is_dir() {
|
|
|
|
return Err(MigrationError::InvalidConfigDir.into());
|
|
|
|
}
|
2021-05-24 18:58:18 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
let working_dir = TempDir::new("espanso-migration")?;
|
|
|
|
|
|
|
|
fs_extra::dir::copy(
|
|
|
|
config_dir,
|
|
|
|
working_dir.path(),
|
|
|
|
&CopyOptions {
|
|
|
|
content_only: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// If packages are located within the config_dir, no need to copy them in
|
|
|
|
// the working directory
|
|
|
|
if packages_dir.parent() != Some(config_dir) {
|
|
|
|
fs_extra::dir::copy(packages_dir, working_dir.path(), &CopyOptions::new())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the output directory
|
|
|
|
if output_dir.exists() {
|
|
|
|
return Err(MigrationError::OutputDirAlreadyPresent.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fs::create_dir_all(output_dir)?;
|
2021-05-24 18:58:18 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
// Convert the configurations
|
|
|
|
let legacy_files = load::load(working_dir.path())?;
|
|
|
|
let converted_files = convert::convert(legacy_files);
|
|
|
|
let rendered_files = render::render(converted_files)?;
|
2021-05-25 20:07:26 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
for (file, content) in rendered_files {
|
|
|
|
let target = output_dir.join(file);
|
|
|
|
|
|
|
|
if let Some(parent) = target.parent() {
|
|
|
|
if !parent.is_dir() {
|
|
|
|
std::fs::create_dir_all(parent)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fs::write(target, content)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy all non-YAML directories
|
|
|
|
for entry in std::fs::read_dir(working_dir.path())? {
|
|
|
|
let entry = entry?;
|
|
|
|
let path = entry.path();
|
|
|
|
if path.is_dir() {
|
|
|
|
let dir_name = path.file_name();
|
|
|
|
if let Some(name) = dir_name.map(|s| s.to_string_lossy().to_string().to_lowercase()) {
|
|
|
|
if name != "user" && name != "packages" {
|
|
|
|
fs_extra::dir::copy(path, output_dir, &CopyOptions::new())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum MigrationError {
|
|
|
|
#[error("invalid config directory")]
|
|
|
|
InvalidConfigDir,
|
|
|
|
|
|
|
|
#[error("output directory already present")]
|
|
|
|
OutputDirAlreadyPresent,
|
|
|
|
}
|
2021-05-24 18:58:18 +00:00
|
|
|
|
2021-05-23 20:08:30 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-05-29 11:08:32 +00:00
|
|
|
use std::{collections::HashMap, fs::create_dir_all, path::Path};
|
2021-05-24 18:58:18 +00:00
|
|
|
|
2021-05-25 20:07:26 +00:00
|
|
|
use super::*;
|
2021-05-23 20:08:30 +00:00
|
|
|
use include_dir::{include_dir, Dir};
|
2021-05-27 20:06:39 +00:00
|
|
|
use tempdir::TempDir;
|
2021-05-25 20:07:26 +00:00
|
|
|
use test_case::test_case;
|
2021-05-23 20:08:30 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
use pretty_assertions::assert_eq as assert_peq;
|
|
|
|
use yaml_rust::{yaml::Hash, Yaml};
|
2021-05-27 20:06:39 +00:00
|
|
|
|
|
|
|
fn run_with_temp_dir(test_data: &Dir, action: impl FnOnce(&Path, &Path)) {
|
2021-05-29 11:08:32 +00:00
|
|
|
let tmp_dir = TempDir::new("espanso-migrate").unwrap();
|
2021-05-27 20:06:39 +00:00
|
|
|
let tmp_path = tmp_dir.path();
|
|
|
|
let legacy_path = tmp_dir.path().join("legacy");
|
|
|
|
let expected_path = tmp_dir.path().join("expected");
|
|
|
|
|
|
|
|
for entry in test_data.find("**/*").unwrap() {
|
|
|
|
let entry_path = entry.path();
|
|
|
|
|
|
|
|
let entry_path_str = entry_path.to_string_lossy().to_string();
|
|
|
|
if entry_path_str.is_empty() {
|
|
|
|
continue;
|
2021-05-29 11:08:32 +00:00
|
|
|
}
|
2021-05-27 20:06:39 +00:00
|
|
|
|
|
|
|
let target = tmp_path.join(entry_path);
|
|
|
|
|
|
|
|
if entry_path.extension().is_none() {
|
|
|
|
create_dir_all(target).unwrap();
|
|
|
|
} else {
|
|
|
|
std::fs::write(target, test_data.get_file(entry_path).unwrap().contents()).unwrap();
|
2021-05-29 11:08:32 +00:00
|
|
|
}
|
2021-05-27 20:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
action(&legacy_path, &expected_path);
|
|
|
|
}
|
|
|
|
|
2021-05-27 21:16:53 +00:00
|
|
|
fn to_sorted_list<T>(hash: HashMap<String, T>) -> Vec<(String, T)> {
|
|
|
|
let mut tuples: Vec<(String, T)> = hash.into_iter().map(|(k, v)| (k, v)).collect();
|
|
|
|
tuples.sort_by_key(|(k, _)| k.clone());
|
|
|
|
tuples
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:14:42 +00:00
|
|
|
fn to_sorted_hash(hash: &Hash) -> Vec<(String, &Yaml)> {
|
2021-05-29 11:08:32 +00:00
|
|
|
let mut tuples: Vec<(String, &Yaml)> = hash
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| (k.as_str().unwrap().to_string(), v))
|
|
|
|
.collect();
|
2021-05-29 08:14:42 +00:00
|
|
|
tuples.sort_by_key(|(k, _)| k.clone());
|
|
|
|
tuples
|
|
|
|
}
|
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
fn list_files_in_dir(dir: &Path) -> Vec<String> {
|
|
|
|
let prefix = dir.to_string_lossy().to_string();
|
|
|
|
fs_extra::dir::get_dir_content(&dir)
|
|
|
|
.unwrap()
|
|
|
|
.files
|
|
|
|
.into_iter()
|
|
|
|
.map(|file| file.trim_start_matches(&prefix).to_string())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:06:39 +00:00
|
|
|
static SIMPLE_CASE: Dir = include_dir!("test/simple");
|
2021-05-23 20:08:30 +00:00
|
|
|
static BASE_CASE: Dir = include_dir!("test/base");
|
2021-05-29 08:14:42 +00:00
|
|
|
static ALL_PARAMS_CASE: Dir = include_dir!("test/all_params");
|
2021-05-29 11:08:32 +00:00
|
|
|
static OTHER_DIRS_CASE: Dir = include_dir!("test/other_dirs");
|
2021-11-07 13:21:59 +00:00
|
|
|
static FORM_SYNTAX: Dir = include_dir!("test/form_syntax");
|
2021-05-23 20:08:30 +00:00
|
|
|
|
2021-10-05 20:06:53 +00:00
|
|
|
#[allow(clippy::unused_unit)]
|
2021-05-27 20:06:39 +00:00
|
|
|
#[test_case(&SIMPLE_CASE; "simple case")]
|
2021-05-23 20:08:30 +00:00
|
|
|
#[test_case(&BASE_CASE; "base case")]
|
2021-05-29 08:14:42 +00:00
|
|
|
#[test_case(&ALL_PARAMS_CASE; "all config parameters case")]
|
2021-05-29 11:08:32 +00:00
|
|
|
#[test_case(&OTHER_DIRS_CASE; "other directories case")]
|
2021-11-07 13:21:59 +00:00
|
|
|
#[test_case(&FORM_SYNTAX; "form syntax")]
|
2021-05-23 20:08:30 +00:00
|
|
|
fn test_migration(test_data: &Dir) {
|
2021-05-27 20:06:39 +00:00
|
|
|
run_with_temp_dir(test_data, |legacy, expected| {
|
2021-05-29 11:08:32 +00:00
|
|
|
let tmp_out_dir = TempDir::new("espanso-migrate-out").unwrap();
|
|
|
|
let output_dir = tmp_out_dir.path().join("out");
|
2021-05-27 20:06:39 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
migrate(legacy, &legacy.join("packages"), &output_dir).unwrap();
|
2021-05-27 20:06:39 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
let converted_files = load::load(&output_dir).unwrap();
|
2021-05-27 20:06:39 +00:00
|
|
|
|
2021-05-29 11:08:32 +00:00
|
|
|
// Verify configuration content
|
|
|
|
let expected_files = load::load(expected).unwrap();
|
|
|
|
assert_eq!(converted_files.len(), expected_files.len());
|
|
|
|
for (file, converted) in to_sorted_list(converted_files) {
|
|
|
|
assert_peq!(
|
|
|
|
to_sorted_hash(&converted),
|
2021-10-05 20:06:53 +00:00
|
|
|
to_sorted_hash(expected_files.get(&file).unwrap())
|
2021-05-29 11:08:32 +00:00
|
|
|
);
|
2021-05-27 20:06:39 +00:00
|
|
|
}
|
2021-05-29 11:08:32 +00:00
|
|
|
|
|
|
|
// Verify file structure
|
|
|
|
assert_peq!(list_files_in_dir(expected), list_files_in_dir(&output_dir));
|
2021-05-27 20:06:39 +00:00
|
|
|
});
|
2021-05-23 20:08:30 +00:00
|
|
|
}
|
|
|
|
}
|