core: Refactor the messaging components.
This commit is contained in:
parent
6730d8d10b
commit
4d218bed74
115
src/main.rs
115
src/main.rs
|
@ -149,6 +149,14 @@ fn header_message(message: &str) {
|
||||||
info!("{}", message);
|
info!("{}", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn success_message(message: &str) {
|
||||||
|
info!("{} {}", "✔".green(), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn caution_message(message: &str) {
|
||||||
|
info!("{} {}", "♼".yellow(), message);
|
||||||
|
}
|
||||||
|
|
||||||
fn fatal_error(error: Box<dyn std::error::Error>, message: &str) -> ! {
|
fn fatal_error(error: Box<dyn std::error::Error>, message: &str) -> ! {
|
||||||
error!("{} {}", "✘".red(), message);
|
error!("{} {}", "✘".red(), message);
|
||||||
error!("Exact cause: {}", error);
|
error!("Exact cause: {}", error);
|
||||||
|
@ -163,20 +171,23 @@ fn serve_site(args: &Args) {
|
||||||
// Check if build directory is available.
|
// Check if build directory is available.
|
||||||
let build_directory = project_directory.clone().join("build");
|
let build_directory = project_directory.clone().join("build");
|
||||||
if !build_directory.exists() || !build_directory.is_dir() {
|
if !build_directory.exists() || !build_directory.is_dir() {
|
||||||
info!("{} This project needs a build first!", "♼".yellow(),);
|
caution_message("This project needs a build first!");
|
||||||
|
|
||||||
header_message("Attempting to build the site...");
|
header_message("Attempting to build the site...");
|
||||||
build_site(args);
|
build_site(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("{} Found a built site to serve!", "✔".green(),);
|
success_message("Found a built site to serve!");
|
||||||
|
|
||||||
let port = match args.flag_port {
|
let port = match args.flag_port {
|
||||||
x if x <= u16::min_value() || x >= u16::max_value() => 8000,
|
x if x <= u16::min_value() || x >= u16::max_value() => 8000,
|
||||||
x => x,
|
x => x,
|
||||||
};
|
};
|
||||||
|
|
||||||
header_message(&format!("Serving project at local port {}", port.to_string().cyan()));
|
header_message(&format!(
|
||||||
|
"Serving project at local port {}",
|
||||||
|
port.to_string().cyan()
|
||||||
|
));
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -185,7 +196,10 @@ fn serve_site(args: &Args) {
|
||||||
})
|
})
|
||||||
.bind(format!("127.0.0.1:{}", port))
|
.bind(format!("127.0.0.1:{}", port))
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
let error_message = format!("The server could not to bind to port \"{}\". Try using a different port.", port.to_string().red());
|
let error_message = format!(
|
||||||
|
"The server could not to bind to port \"{}\". Try using a different port.",
|
||||||
|
port.to_string().red()
|
||||||
|
);
|
||||||
fatal_error(Box::new(err), &error_message);
|
fatal_error(Box::new(err), &error_message);
|
||||||
})
|
})
|
||||||
.run()
|
.run()
|
||||||
|
@ -209,11 +223,10 @@ fn build_site(args: &Args) {
|
||||||
|
|
||||||
let project_manifest_details = read_to_string(project_manifest).unwrap();
|
let project_manifest_details = read_to_string(project_manifest).unwrap();
|
||||||
let project_site: Site = toml::from_str(&project_manifest_details).unwrap();
|
let project_site: Site = toml::from_str(&project_manifest_details).unwrap();
|
||||||
info!(
|
success_message(&format!(
|
||||||
"{} Found a project manifest for the \"{}\" site!",
|
"Found a project manifest for the \"{}\" site!",
|
||||||
"✔".green(),
|
|
||||||
&project_site.name
|
&project_site.name
|
||||||
);
|
));
|
||||||
|
|
||||||
// Build a list of the pages to build out.
|
// Build a list of the pages to build out.
|
||||||
header_message("Searching for pages...");
|
header_message("Searching for pages...");
|
||||||
|
@ -255,7 +268,7 @@ fn build_site(args: &Args) {
|
||||||
None => format!("Rookeries :: {}", &slug).to_string(),
|
None => format!("Rookeries :: {}", &slug).to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("{} Compiling \"{}\" page", "✔".green(), &slug.green());
|
success_message(&format!("Compiling \"{}\" page", &slug.green()));
|
||||||
|
|
||||||
Page {
|
Page {
|
||||||
title,
|
title,
|
||||||
|
@ -270,13 +283,13 @@ fn build_site(args: &Args) {
|
||||||
header_message("Preparing build directory...");
|
header_message("Preparing build directory...");
|
||||||
let build_directory = project_directory.clone().join("build");
|
let build_directory = project_directory.clone().join("build");
|
||||||
if build_directory.exists() && build_directory.is_dir() {
|
if build_directory.exists() && build_directory.is_dir() {
|
||||||
info!("{} Recreating build directory...", "♼".yellow());
|
caution_message("Recreating build directory...");
|
||||||
remove_dir_all(&build_directory).expect("Failure!");
|
remove_dir_all(&build_directory).expect("Failure!");
|
||||||
create_dir(&build_directory).expect("Failure!");
|
create_dir(&build_directory).expect("Failure!");
|
||||||
info!("{} Created a build directory.", "✔".green());
|
success_message("Created a build directory.");
|
||||||
} else if !build_directory.exists() {
|
} else if !build_directory.exists() {
|
||||||
create_dir(&build_directory).expect("Failure!");
|
create_dir(&build_directory).expect("Failure!");
|
||||||
info!("{} Created a build directory.", "✔".green());
|
success_message("Created a build directory.");
|
||||||
} else {
|
} else {
|
||||||
error!("{} Can not create a build directory!!!", "✘".red());
|
error!("{} Can not create a build directory!!!", "✘".red());
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
|
@ -307,7 +320,7 @@ fn build_site(args: &Args) {
|
||||||
let render_page_str = render_engine.render("index", &ctx).unwrap();
|
let render_page_str = render_engine.render("index", &ctx).unwrap();
|
||||||
|
|
||||||
write(build_directory.join("index.html"), render_page_str).expect("Failed to write index.html");
|
write(build_directory.join("index.html"), render_page_str).expect("Failed to write index.html");
|
||||||
info!("{} Created the index HTML page.", "✔".green());
|
success_message("Created the index HTML page.");
|
||||||
|
|
||||||
header_message("Creating the pages...");
|
header_message("Creating the pages...");
|
||||||
for page in &site_source_pages {
|
for page in &site_source_pages {
|
||||||
|
@ -319,11 +332,10 @@ fn build_site(args: &Args) {
|
||||||
render_page_str,
|
render_page_str,
|
||||||
)
|
)
|
||||||
.expect("Failed to write index.html");
|
.expect("Failed to write index.html");
|
||||||
info!(
|
success_message(&format!(
|
||||||
"{} Created the \"{}\" HTML page.",
|
"Created the \"{}\" HTML page.",
|
||||||
"✔".green(),
|
|
||||||
&page.slug.green()
|
&page.slug.green()
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the API representations.
|
// Create the API representations.
|
||||||
|
@ -333,19 +345,13 @@ fn build_site(args: &Args) {
|
||||||
|
|
||||||
let site_json = serde_json::to_string_pretty(&project_site).unwrap();
|
let site_json = serde_json::to_string_pretty(&project_site).unwrap();
|
||||||
write(api_path.join("site.json"), site_json).expect("Failed to write site.json");
|
write(api_path.join("site.json"), site_json).expect("Failed to write site.json");
|
||||||
info!(
|
success_message("Created the API representation of the site.");
|
||||||
"{} Created the API representation of the site.",
|
|
||||||
"✔".green()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create a status.json that is really a build metadata artifact.
|
// Create a status.json that is really a build metadata artifact.
|
||||||
// TODO: Clean up status setup.
|
// TODO: Clean up status setup.
|
||||||
let status = serde_json::to_string_pretty(&status).unwrap();
|
let status = serde_json::to_string_pretty(&status).unwrap();
|
||||||
write(api_path.join("_build.json"), status).expect("Failed to write the build info.");
|
write(api_path.join("_build.json"), status).expect("Failed to write the build info.");
|
||||||
info!(
|
success_message("Created the API representation of the status.");
|
||||||
"{} Created the API representation of the status.",
|
|
||||||
"✔".green()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create all the page API reps.
|
// Create all the page API reps.
|
||||||
let api_pages_path = &api_path.join("pages");
|
let api_pages_path = &api_path.join("pages");
|
||||||
|
@ -355,11 +361,10 @@ fn build_site(args: &Args) {
|
||||||
let page_json_filename = format!("{}.json", &page.slug);
|
let page_json_filename = format!("{}.json", &page.slug);
|
||||||
write(api_pages_path.join(&page_json_filename), page_json)
|
write(api_pages_path.join(&page_json_filename), page_json)
|
||||||
.expect("Failed to write the page json");
|
.expect("Failed to write the page json");
|
||||||
info!(
|
success_message(&format!(
|
||||||
"{} Created the API representation of the \"{}\" page.",
|
"Created the API representation of the \"{}\" page.",
|
||||||
"✔".green(),
|
|
||||||
&page.slug.green()
|
&page.slug.green()
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bring in the static file directory.
|
// Bring in the static file directory.
|
||||||
|
@ -369,12 +374,11 @@ fn build_site(args: &Args) {
|
||||||
.canonicalize()
|
.canonicalize()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let assets_target_dir = Path::new(&build_directory).join("static");
|
let assets_target_dir = Path::new(&build_directory).join("static");
|
||||||
info!("");
|
header_message(&format!(
|
||||||
info!(
|
|
||||||
"Copying static assets from \"{}\" to \"{}\"",
|
"Copying static assets from \"{}\" to \"{}\"",
|
||||||
&assets_source_dir.display().to_string().green(),
|
&assets_source_dir.display().to_string().green(),
|
||||||
&assets_target_dir.display().to_string().green()
|
&assets_target_dir.display().to_string().green()
|
||||||
);
|
));
|
||||||
match copy_dir::copy_dir(&assets_source_dir, &assets_target_dir) {
|
match copy_dir::copy_dir(&assets_source_dir, &assets_target_dir) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
error!(
|
||||||
|
@ -390,7 +394,7 @@ fn build_site(args: &Args) {
|
||||||
error!("{} Could not copy over the static assets.", "✘".red());
|
error!("{} Could not copy over the static assets.", "✘".red());
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
} else {
|
} else {
|
||||||
info!("{} Copied over assets!", "✔".green());
|
success_message("Copied over assets!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -438,11 +442,10 @@ fn initialize_site(args: &Args) {
|
||||||
if !project_directory.exists() {
|
if !project_directory.exists() {
|
||||||
create_dir(&project_directory).expect("Could not create the project directory.");
|
create_dir(&project_directory).expect("Could not create the project directory.");
|
||||||
let project_dir = &project_directory.canonicalize().unwrap();
|
let project_dir = &project_directory.canonicalize().unwrap();
|
||||||
info!(
|
success_message(&format!(
|
||||||
"{} Created a new project in {}",
|
"Created a new project in {}",
|
||||||
"✔".green(),
|
&project_dir.display().to_string().green()
|
||||||
format!("{}", &project_dir.display()).green()
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
let project_directory = project_directory
|
let project_directory = project_directory
|
||||||
.canonicalize()
|
.canonicalize()
|
||||||
|
@ -461,7 +464,7 @@ fn initialize_site(args: &Args) {
|
||||||
let project_manifest = project_directory.join("site.toml");
|
let project_manifest = project_directory.join("site.toml");
|
||||||
match &project_manifest.exists() {
|
match &project_manifest.exists() {
|
||||||
true => {
|
true => {
|
||||||
info!("{} Found existing site.toml. Skipping.", "♼".yellow());
|
caution_message("Found existing site.toml. Skipping.");
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
// TODO: Support an interactive setup of the Rookeries site.
|
// TODO: Support an interactive setup of the Rookeries site.
|
||||||
|
@ -483,7 +486,7 @@ fn initialize_site(args: &Args) {
|
||||||
let site_toml = format!("{}\n\n{}", site_toml_header, site_toml_content);
|
let site_toml = format!("{}\n\n{}", site_toml_header, site_toml_content);
|
||||||
|
|
||||||
write(&project_manifest, site_toml).expect("Failed to write site.toml");
|
write(&project_manifest, site_toml).expect("Failed to write site.toml");
|
||||||
info!("{} Created a site.toml!", "✔".green());
|
success_message("Created a site.toml!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -501,7 +504,7 @@ fn initialize_site(args: &Args) {
|
||||||
let site_template_path = &project_directory.join("template");
|
let site_template_path = &project_directory.join("template");
|
||||||
if site_template_path.exists() {
|
if site_template_path.exists() {
|
||||||
// TODO: Figure out override.
|
// TODO: Figure out override.
|
||||||
info!("{} Found existing templates. Skipping.", "♼".yellow());
|
caution_message("Found existing templates. Skipping.");
|
||||||
} else {
|
} else {
|
||||||
create_dir(&site_template_path).expect("Failed to create template directory");
|
create_dir(&site_template_path).expect("Failed to create template directory");
|
||||||
// TODO: Allow for more files.
|
// TODO: Allow for more files.
|
||||||
|
@ -516,19 +519,14 @@ fn initialize_site(args: &Args) {
|
||||||
header_message("Add template static assets to site");
|
header_message("Add template static assets to site");
|
||||||
let site_static_path = &project_directory.join("static");
|
let site_static_path = &project_directory.join("static");
|
||||||
if site_static_path.exists() {
|
if site_static_path.exists() {
|
||||||
info!(
|
caution_message("Found existing static assets. Skipping.");
|
||||||
"{} Found existing static assets. Skipping.",
|
|
||||||
"♼".yellow()
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
match copy_dir::copy_dir(&template_path, &site_static_path) {
|
match copy_dir::copy_dir(&template_path, &site_static_path) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
fatal_error(
|
||||||
"{} Could not copy over the template static assets because of: {}",
|
Box::new(e),
|
||||||
"✘".red(),
|
"Could not copy over the template static assets.",
|
||||||
e
|
|
||||||
);
|
);
|
||||||
std::process::exit(2);
|
|
||||||
}
|
}
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
if v.len() > 0 {
|
if v.len() > 0 {
|
||||||
|
@ -539,7 +537,7 @@ fn initialize_site(args: &Args) {
|
||||||
);
|
);
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
} else {
|
} else {
|
||||||
info!("{} Copied over the template assets!", "✔".green());
|
caution_message("Copied over the template assets!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -551,11 +549,10 @@ fn initialize_site(args: &Args) {
|
||||||
let sample_page_path = project_directory.join(&page_file_name);
|
let sample_page_path = project_directory.join(&page_file_name);
|
||||||
match &sample_page_path.exists() {
|
match &sample_page_path.exists() {
|
||||||
true => {
|
true => {
|
||||||
info!(
|
caution_message(&format!(
|
||||||
"{} Found existing {}. Skipping.",
|
"Found existing {}. Skipping.",
|
||||||
"♼".yellow(),
|
|
||||||
&page_file_name.yellow()
|
&page_file_name.yellow()
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
copy_file(
|
copy_file(
|
||||||
|
@ -575,7 +572,7 @@ fn initialize_site(args: &Args) {
|
||||||
let site_plugins_path = &project_directory.join("plugins");
|
let site_plugins_path = &project_directory.join("plugins");
|
||||||
if site_plugins_path.exists() {
|
if site_plugins_path.exists() {
|
||||||
// TODO: Figure out override.
|
// TODO: Figure out override.
|
||||||
info!("{} Found existing plugins. Skipping.", "♼".yellow());
|
caution_message("Found existing plugins. Skipping.");
|
||||||
} else {
|
} else {
|
||||||
create_dir(&site_plugins_path).expect("Failed to create plugin directory");
|
create_dir(&site_plugins_path).expect("Failed to create plugin directory");
|
||||||
let plugin_list = vec!["hello-world", "dark-mode-switch"];
|
let plugin_list = vec!["hello-world", "dark-mode-switch"];
|
||||||
|
@ -587,11 +584,7 @@ fn initialize_site(args: &Args) {
|
||||||
&plugin_file_name,
|
&plugin_file_name,
|
||||||
"plugin",
|
"plugin",
|
||||||
);
|
);
|
||||||
info!(
|
success_message(&format!("Copied over the {} plugin!", &plugin.green()));
|
||||||
"{} Copied over the {} plugin!",
|
|
||||||
"✔".green(),
|
|
||||||
&plugin.green()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue