use crate::generators::create_file;
use crate::writers;
use crate::writers::{
add_new_controller_to_existing_module_in_main_rs, add_new_controller_to_main_rs,
write_to_controller_name_html, write_to_controllers_mod, write_to_new_delete_controller,
write_to_new_get_controller, write_to_previous_create_controller,
};
use eyre::Error;
use std::fs;
use std::fs::create_dir;
pub fn create_delete_controller_in_existing_folder(controller_name: String) -> Result<(), Error> {
println!("Do you want to add this controller to the file for that controller? (y/n): ");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input == "y" {
println!("What is the name of the model you want to create a controller for?: ");
println!("In order to work out of the box, ensure the model already exists.");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
let current_dir = fs::read_dir("./src/controllers").unwrap();
let mut has_controller = false;
for entry in current_dir {
let entry = entry.unwrap();
let file_name = entry.file_name();
let file_name = file_name.to_str().unwrap();
if file_name == input {
has_controller = true;
}
}
if !has_controller {
println!("There is no controller with that name. Do you want to create a new controller with that name? (y/n): ");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input == "y" {
write_to_new_delete_controller(controller_name.clone()).unwrap_or_else(|why| {
println!("Failed to write to controller: {:?}", why.to_string());
});
Ok(())
} else {
println!("Please run the command again and enter a valid controller name.");
std::process::exit(0);
}
} else {
write_to_previous_create_controller(input.to_string(), controller_name.clone())
.unwrap_or_else(|why| {
println!("Failed to write to controller: {:?}", why.to_string());
});
create_file(&format!("./views/pages/{}.html.tera", controller_name)).unwrap_or_else(
|why| {
println!("Failed to create file: {:?}", why.to_string());
},
);
write_to_controller_name_html(controller_name.clone().as_str())
.expect("Failed to write to controllerName.html.tera");
add_new_controller_to_existing_module_in_main_rs(
input,
controller_name.clone().as_str(),
)
.unwrap_or_else(|why| {
println!("Failed to add to controller in main.rs: {:?}", why.kind());
});
Ok(())
}
} else {
write_to_new_get_controller(controller_name.clone()).unwrap_or_else(|why| {
println!("Failed to write to controller: {:?}", why.to_string());
});
Ok(())
}
}
pub fn create_delete_controller_in_new_folder(controller_name: String) -> Result<(), Error> {
let current_dir = fs::read_dir(".").unwrap();
let mut has_rustyroad_toml = false;
for entry in current_dir {
let entry = entry.unwrap();
let file_name = entry.file_name();
let file_name = file_name.to_str().unwrap();
if file_name == "rustyroad.toml" {
has_rustyroad_toml = true;
}
}
if !has_rustyroad_toml {
println!(
"This is not a rustyroad project. Please run this command in a rustyroad project."
);
return Ok(());
}
create_dir(format!("./src/controllers/{}", &controller_name)).unwrap_or_else(|why| {
println!("Failed to create directory: {:?}", why.to_string());
});
let full_file_name = "./src/controllers/page".to_string();
write_to_controllers_mod(&full_file_name, controller_name.clone()).unwrap_or_else(|why| {
println!("Failed to write to controllers/mod: {:?}", why.to_string());
});
create_file(&format!("./src/controllers/{}/page", controller_name)).unwrap_or_else(|why| {
println!("Failed to create file: {:?}", why.to_string());
});
let mut components = Vec::new();
components.push(controller_name.clone().to_string());
writers::write_to_module(
&format!("./src/controllers/{}/page", &controller_name),
components,
)
.unwrap_or_else(|why| {
println!("Failed to write to page: {:?}", why.to_string());
});
create_file(&format!(
"./src/controllers/{}/{}.rs",
controller_name, controller_name
))
.unwrap_or_else(|why| {
println!("Failed to create file: {:?}", why.to_string());
});
write_to_new_delete_controller(controller_name.clone()).unwrap_or_else(|why| {
println!(
"Failed to write to controllerName.rs: {:?}",
why.to_string()
);
});
create_file(&format!("./views/pages/{}.html.tera", controller_name)).unwrap_or_else(|why| {
println!("Failed to create file: {:?}", why.to_string());
});
write_to_controller_name_html(controller_name.clone().as_str())
.expect("Failed to write to controllerName.html.tera");
add_new_controller_to_main_rs(None, None, controller_name.clone().as_str())
.expect("Failed to add to controller in main.rs");
Ok(())
}