Skip to content

Commit

Permalink
Add migration support for TOML config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduerr committed Sep 23, 2024
1 parent 53beead commit 2b83315
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 299 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Notable changes to the `alacritty_terminal` crate are documented in its
### Added

- Support relative path imports from config files
- `alacritty migrate` support for TOML configuration changes

### Changed

Expand Down
48 changes: 36 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion alacritty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ libc = "0.2"
log = { version = "0.4", features = ["std", "serde"] }
notify = "6.1.1"
parking_lot = "0.12.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.9.25"
tempfile = "3.12.0"
toml = "0.8.2"
toml_edit = "0.22.21"
unicode-width = "0.1"
winit = { version = "0.30.4", default-features = false, features = ["rwh_06", "serde"] }

Expand Down
36 changes: 24 additions & 12 deletions alacritty/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,31 +301,43 @@ pub fn imports(
let mut import_paths = Vec::new();

for import in imports {
let mut path = match import {
let path = match import {
Value::String(path) => PathBuf::from(path),
_ => {
import_paths.push(Err("Invalid import element type: expected path string".into()));
continue;
},
};

// Resolve paths relative to user's home directory.
if let (Ok(stripped), Some(home_dir)) = (path.strip_prefix("~/"), home::home_dir()) {
path = home_dir.join(stripped);
}

if path.is_relative() {
if let Some(base_path) = base_path.parent() {
path = base_path.join(path)
}
}
let normalized = normalize_import(base_path, path);

import_paths.push(Ok(path));
import_paths.push(Ok(normalized));
}

Ok(import_paths)
}

/// Normalize import paths.
pub fn normalize_import<P>(base_config_path: &Path, import_path: P) -> PathBuf
where
P: Into<PathBuf>,
{
let mut import_path = import_path.into();

// Resolve paths relative to user's home directory.
if let (Ok(stripped), Some(home_dir)) = (import_path.strip_prefix("~/"), home::home_dir()) {
import_path = home_dir.join(stripped);
}

if import_path.is_relative() {
if let Some(base_config_dir) = base_config_path.parent() {
import_path = base_config_dir.join(import_path)
}
}

import_path
}

/// Prune the nulls from the YAML to ensure TOML compatibility.
fn prune_yaml_nulls(value: &mut serde_yaml::Value, warn_pruned: bool) {
fn walk(value: &mut serde_yaml::Value, warn_pruned: bool) -> bool {
Expand Down
Loading

0 comments on commit 2b83315

Please sign in to comment.