use anyhow::{Ok, anyhow};
use serde::Deserialize;
use crate::{
actions::{ExecuteAction, common::Common},
file_utils::{file_read_content, file_replace_content},
};
#[derive(Debug, Deserialize, Default, Clone, PartialEq, Eq)]
pub struct ModifyFileAction {
#[serde(flatten)]
pub common: Common,
pub(self) r#type: ModifyType,
pub content: Option<String>,
pub selector: String,
}
#[derive(Debug, Deserialize, Default, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum ModifyType {
#[default]
Append,
Prepend,
Replace,
}
impl ModifyFileAction {
pub fn content(&self) -> String {
self.content.clone().unwrap_or_default()
}
pub fn modify_content(&self, source_text: String) -> anyhow::Result<String> {
let locations: Vec<(usize, &str)> =
source_text.match_indices(&self.selector).collect();
if locations.len() == 0 {
return Err(anyhow!("No selector matches in target file!"));
}
if locations.len() > 1 {
info!("Found {} matches", locations.len());
} else {
info!("Found {} match", locations.len());
}
let mut output = source_text.clone();
let start_length = source_text.len();
for (index, selected) in locations {
let modified_index = index + output.len().abs_diff(start_length);
match &self.r#type {
ModifyType::Append => {
output.insert_str(
(modified_index + selected.len()).max(0),
&self.content(),
);
}
ModifyType::Prepend => {
output.insert_str(modified_index, &self.content());
}
ModifyType::Replace => {
debug!(
"replacing from {} to {} (total chars {})",
modified_index,
(modified_index + selected.len()).max(0),
output.len()
);
if modified_index > output.len() {
output.insert_str(output.len(), &self.content());
} else {
output.replace_range(
modified_index..(modified_index + selected.len()),
&self.content(),
);
}
}
}
}
match &self.r#type {
ModifyType::Append => {
info!("Appended to all matches");
}
ModifyType::Prepend => {
info!("Prepended to all matches");
}
ModifyType::Replace => {
info!("Replaced all matches");
}
}
Ok(output)
}
}
impl ExecuteAction for ModifyFileAction {
fn execute(
&self,
context: &crate::context::ActionContext,
brick: &crate::brick::Brick,
cwd: &std::path::Path,
) -> anyhow::Result<()> {
let mut files: Vec<String> = brick
.files()
.iter()
.map(|brick_file| brick_file.name().to_string())
.collect();
files.extend(self.common.sources.clone().into_iter());
for file in files {
let target_path = cwd.join(file);
if !target_path.exists() {
return Err(anyhow!("Target file does not exist!"));
}
info!("Modifying file '{}'", target_path.display());
let content = file_read_content(context, &target_path)?;
file_replace_content(context, &target_path, &self.modify_content(content)?)?;
}
Ok(())
}
}