use std::{fs, path::PathBuf};
use crate::phighlighter::PLocation;
use crate::plectureparser::find_common_start::remove_common_start;
pub struct PRessourceFile{
p_relative_ressource_file: PathBuf,
p_absolute_ressource_file: PathBuf,
p_relative_output_ressource_file: PathBuf,
p_absolute_output_ressource_file: PathBuf,
p_absolute_output_ressource_directory: PathBuf,
p_location_lecture_file: PLocation,
p_absolute_lecture_output_dir: PathBuf,
}
impl PRessourceFile{
pub fn new(relative_ressource_file: &PathBuf, location_lecture_file: &PLocation, absolute_lecture_output_dir: &PathBuf) -> Self{
let mut other = PRessourceFile{
p_relative_ressource_file: relative_ressource_file.clone(),
p_absolute_ressource_file: Default::default(),
p_relative_output_ressource_file: Default::default(),
p_absolute_output_ressource_file: Default::default(),
p_absolute_output_ressource_directory: Default::default(),
p_location_lecture_file: location_lecture_file.clone(),
p_absolute_lecture_output_dir: absolute_lecture_output_dir.clone(),
};
other.make_ressource_path();
return other;
}
fn make_ressource_path(&mut self){
self.p_absolute_ressource_file = match self.p_location_lecture_file.get_filename().canonicalize(){
Ok(absolute_path) => match absolute_path.parent(){
Some(parent_path) => parent_path.join(&self.p_relative_ressource_file),
None => panic!("PRessourceFile::make_ressource_path : Cannot get parent of absolute path of {:?}", self.p_location_lecture_file)
},
Err(err) => panic!("PRessourceFile::make_ressource_path : Cannot get absolute path of {:?}\n\tError {}", self.p_location_lecture_file, err)
};
let end_source_file: PathBuf = remove_common_start(&self.p_absolute_ressource_file, &self.p_absolute_lecture_output_dir);
self.p_relative_output_ressource_file = PathBuf::from("ressource").join(&end_source_file);
self.p_absolute_output_ressource_file = self.p_absolute_lecture_output_dir.join(&self.p_relative_output_ressource_file);
self.p_absolute_output_ressource_directory = PathBuf::from(self.p_absolute_output_ressource_file.parent().unwrap());
}
pub fn create_dir_all(&self){
match fs::create_dir_all(&self.p_absolute_output_ressource_directory) {
Ok(_) => {},
Err(err) => panic!("PRessourceFile::create_dir_all : cannot create output directory {:?}\n\tError {}", self.p_absolute_output_ressource_directory, err)
};
}
pub fn copy_to_output_dir(&self){
if !self.p_absolute_output_ressource_directory.exists() {
self.create_dir_all();
}
match fs::copy(&self.p_absolute_ressource_file, &self.p_absolute_output_ressource_file){
Ok(_) => {},
Err(err) => panic!("PRessourceFile::copy_to_output_dir : cannot copy file {:?}\n\tto {:?}\n\tDefined in the lecture at {}\n\tError {}", self.p_absolute_ressource_file, self.p_absolute_output_ressource_file, self.p_location_lecture_file, err)
};
}
pub fn get_absolute_input_file(&self) -> &PathBuf{
&self.p_absolute_ressource_file
}
pub fn get_relative_output_file(&self) -> &PathBuf{
&self.p_relative_output_ressource_file
}
pub fn get_absolute_output_file(&self) -> &PathBuf{
&self.p_absolute_output_ressource_file
}
}
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn test_copy_ressource_file(){
fs::create_dir_all(&PathBuf::from("target/test_base_lecture")).unwrap();
let location = PLocation::new(&PathBuf::from("tests/index_copy_ressource.md"), 3, 1);
let ressource_file = PRessourceFile::new(&PathBuf::from("Examples/CopyRessource/main.cpp"), &location, &PathBuf::from("target/test_base_lecture").canonicalize().unwrap());
assert_eq!(ressource_file.get_relative_output_file(), &PathBuf::from("ressource/tests/Examples/CopyRessource/main.cpp"));
ressource_file.copy_to_output_dir();
}
}