Skip to content

Commit

Permalink
feat: fix relative paths for templates, format compare_to (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Jun 29, 2024
1 parent 7edc61b commit 12101b0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
6 changes: 3 additions & 3 deletions example/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ swww_options = [
test = "aaaa"

[templates.name1]
input_path = "example/colors.whatever-extension"
output_path = "example/a/colors-generated.whatever-extension"
input_path = "./colors.whatever-extension"
output_path = "./a/colors-generated.whatever-extension"
colors_to_compare = [
{ name = "black", color = "#000000" },
{ name = "red", color = "#ff0000" },
Expand All @@ -29,7 +29,7 @@ colors_to_compare = [
{ name = "fuchsia", color = "#ff00ff" },
{ name = "purple", color = "#800080" },
]
compare_to = "#aefbd5"
compare_to = "{{colors.primary.default.hex}}"
hook = 'echo "source color {{colors.source_color.default.hex}}, source image {{image}}, compared color {{compared_color}}"'

[config.custom_colors]
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> Result<(), Report> {

setup_logging(log_level)?;

let config: ConfigFile = ConfigFile::read(&args)?;
let (config, config_path) = ConfigFile::read(&args)?;

if config.config.version_check == Some(true) {
check_version();
Expand Down Expand Up @@ -142,6 +142,7 @@ fn main() -> Result<(), Report> {
&default_scheme,
&config.config.custom_keywords,
&args.prefix,
config_path
)?;

if config.config.reload_apps == Some(true) {
Expand Down
14 changes: 7 additions & 7 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ const DEFAULT_CONFIG: &str = r#"
"#;

impl ConfigFile {
pub fn read(args: &Cli) -> Result<ConfigFile, Report> {
pub fn read(args: &Cli) -> Result<(ConfigFile, Option<PathBuf>), Report> {
match &args.config {
Some(config_file) => Ok(Self::read_from_custom_path(config_file)?),
Some(config_file) => Ok((Self::read_from_custom_path(config_file)?, Some(config_file.to_path_buf()))),
None => Ok(Self::read_from_proj_path()?),
}
}
Expand All @@ -105,22 +105,22 @@ impl ConfigFile {
}
}

fn read_from_proj_path() -> Result<ConfigFile, Report> {
fn read_from_proj_path() -> Result<(ConfigFile, Option<PathBuf>), Report> {
if let Some(proj_dirs) = ProjectDirs::from("com", "InioX", "matugen") {
let proj_dir = proj_dirs.config_dir();
let config_file = PathBuf::from(proj_dir).join("config.toml");

if !config_file.exists() {
return Self::read_from_fallback_path();
return Ok((Self::read_from_fallback_path()?, None));
}

let content: String = fs::read_to_string(config_file).unwrap();
let content: String = fs::read_to_string(&config_file).unwrap();
match toml::from_str(&content) {
Ok(res) => Ok(res),
Ok(res) => Ok((res, Some(config_file))),
Err(e) => Err(Report::new(e).suggestion(ERROR_TEXT)),
}
} else {
Ok(Self::read_from_fallback_path()?)
Ok((Self::read_from_fallback_path()?, None))
}
}

Expand Down
44 changes: 36 additions & 8 deletions src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::util::color::color_to_string;
use crate::util::filters::set_lightness;
use crate::util::variables::format_hook_text;

use std::fs::canonicalize;
use std::path::Path;
use std::str;

use std::process::{Command, Stdio};
Expand Down Expand Up @@ -81,6 +83,26 @@ struct ColorVariants {

use super::color::rgb_from_argb;

pub trait StripCanonicalization where Self: AsRef<Path> {
#[cfg(not(target_os = "windows"))]
fn strip_canonicalization(&self) -> PathBuf {
self.as_ref().to_path_buf()
}

#[cfg(target_os = "windows")]
fn strip_canonicalization(&self) -> PathBuf {
const VERBATIM_PREFIX: &str = r#"\\?\"#;
let p = self.as_ref().display().to_string();
if p.starts_with(VERBATIM_PREFIX) {
PathBuf::from(&p[VERBATIM_PREFIX.len()..])
} else {
self.as_ref().to_path_buf()
}
}
}

impl StripCanonicalization for PathBuf {}

pub fn check_string_value(value: &Value) -> Option<&String> {
match value {
Value::String(v) => Some(v),
Expand Down Expand Up @@ -114,6 +136,7 @@ impl Template {
default_scheme: &SchemesEnum,
custom_keywords: &Option<HashMap<String, String>>,
path_prefix: &Option<PathBuf>,
config_path: Option<PathBuf>
) -> Result<(), Report> {
let default_prefix = "@".to_string();

Expand Down Expand Up @@ -156,6 +179,13 @@ impl Template {
let input_path_absolute = template.input_path.try_resolve()?;
let output_path_absolute = template.output_path.try_resolve()?;

let (input_path_absolute, output_path_absolute) = if config_path.is_some() {
let base = std::fs::canonicalize(&config_path.as_ref().unwrap())?;
(template.input_path.try_resolve_in(&base)?.to_path_buf().strip_canonicalization(), template.output_path.try_resolve_in(&base)?.to_path_buf().strip_canonicalization())
} else {
(template.input_path.try_resolve()?.to_path_buf(), template.output_path.try_resolve()?.to_path_buf())
};

if template.hook.is_some() {
format_hook(template, &engine, &mut render_data)?;
}
Expand Down Expand Up @@ -221,8 +251,8 @@ fn export_template(
name: &String,
render_data: &Value,
path_prefix: &Option<PathBuf>,
output_path_absolute: std::borrow::Cow<std::path::Path>,
input_path_absolute: std::borrow::Cow<std::path::Path>,
output_path_absolute: PathBuf,
input_path_absolute: PathBuf,
i: usize,
templates: &HashMap<String, Template>,
) -> Result<(), Report> {
Expand All @@ -242,7 +272,7 @@ fn export_template(
})?;
let out = if path_prefix.is_some() && !cfg!(windows) {
let prefix_path = PathBuf::from(path_prefix.as_ref().unwrap());
rebase(output_path_absolute.as_ref(), &prefix_path, None)
rebase(&output_path_absolute, &prefix_path, None)
.expect("failed to rebase output path")
} else {
output_path_absolute.to_path_buf()
Expand Down Expand Up @@ -286,19 +316,17 @@ fn format_hook(
) -> Result<(), Report> {
let compared_color: Option<String> =
if template.colors_to_compare.is_some() && template.compare_to.is_some() {
let s = engine.compile(template.compare_to.as_ref().unwrap())?;
let compare_to = s.render(&engine, &render_data).to_string()?;
Some(color_to_string(
&template.colors_to_compare.as_ref().unwrap(),
&template.compare_to.as_ref().unwrap(),
&compare_to,
))
} else {
None
};
Ok(
if template.colors_to_compare.is_some() && template.compare_to.is_some() {
color::color_to_string(
&template.colors_to_compare.as_ref().unwrap(),
&template.compare_to.as_ref().unwrap(),
);
let t = engine.compile(template.hook.as_ref().unwrap())?;
let res = format_hook_text(render_data, compared_color.as_ref(), t);
let mut command = shell(&res);
Expand Down
2 changes: 1 addition & 1 deletion src/util/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ pub fn format_hook_text(render_data: &mut Value, compared_color: Option<&String>
.to_string().unwrap();

return data
}
}

0 comments on commit 12101b0

Please sign in to comment.