diff --git a/src/main.rs b/src/main.rs index ad1f00f..1271a81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,6 +149,14 @@ fn header_message(message: &str) { info!("{}", message); } +fn success_message(message: &str) { + info!("{} {}", "✔".green(), message); +} + +fn caution_message(message: &str) { + info!("{} {}", "♼".yellow(), message); +} + fn fatal_error(error: Box, message: &str) -> ! { error!("{} {}", "✘".red(), message); error!("Exact cause: {}", error); @@ -163,20 +171,23 @@ fn serve_site(args: &Args) { // Check if build directory is available. let build_directory = project_directory.clone().join("build"); 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..."); 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 { - x if x <= u16::min_value() || x >= u16::max_value() => 8000, + x if x <= u16::min_value() || x >= u16::max_value() => 8000, 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 || { App::new() @@ -185,7 +196,10 @@ fn serve_site(args: &Args) { }) .bind(format!("127.0.0.1:{}", port)) .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); }) .run() @@ -209,11 +223,10 @@ fn build_site(args: &Args) { let project_manifest_details = read_to_string(project_manifest).unwrap(); let project_site: Site = toml::from_str(&project_manifest_details).unwrap(); - info!( - "{} Found a project manifest for the \"{}\" site!", - "✔".green(), + success_message(&format!( + "Found a project manifest for the \"{}\" site!", &project_site.name - ); + )); // Build a list of the pages to build out. header_message("Searching for pages..."); @@ -255,7 +268,7 @@ fn build_site(args: &Args) { None => format!("Rookeries :: {}", &slug).to_string(), }; - info!("{} Compiling \"{}\" page", "✔".green(), &slug.green()); + success_message(&format!("Compiling \"{}\" page", &slug.green())); Page { title, @@ -270,13 +283,13 @@ fn build_site(args: &Args) { header_message("Preparing build directory..."); let build_directory = project_directory.clone().join("build"); 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!"); create_dir(&build_directory).expect("Failure!"); - info!("{} Created a build directory.", "✔".green()); + success_message("Created a build directory."); } else if !build_directory.exists() { create_dir(&build_directory).expect("Failure!"); - info!("{} Created a build directory.", "✔".green()); + success_message("Created a build directory."); } else { error!("{} Can not create a build directory!!!", "✘".red()); std::process::exit(2); @@ -307,7 +320,7 @@ fn build_site(args: &Args) { 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"); - info!("{} Created the index HTML page.", "✔".green()); + success_message("Created the index HTML page."); header_message("Creating the pages..."); for page in &site_source_pages { @@ -319,11 +332,10 @@ fn build_site(args: &Args) { render_page_str, ) .expect("Failed to write index.html"); - info!( - "{} Created the \"{}\" HTML page.", - "✔".green(), + success_message(&format!( + "Created the \"{}\" HTML page.", &page.slug.green() - ); + )); } // Create the API representations. @@ -333,19 +345,13 @@ fn build_site(args: &Args) { 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"); - info!( - "{} Created the API representation of the site.", - "✔".green() - ); + success_message("Created the API representation of the site."); // Create a status.json that is really a build metadata artifact. // TODO: Clean up status setup. let status = serde_json::to_string_pretty(&status).unwrap(); write(api_path.join("_build.json"), status).expect("Failed to write the build info."); - info!( - "{} Created the API representation of the status.", - "✔".green() - ); + success_message("Created the API representation of the status."); // Create all the page API reps. 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); write(api_pages_path.join(&page_json_filename), page_json) .expect("Failed to write the page json"); - info!( - "{} Created the API representation of the \"{}\" page.", - "✔".green(), + success_message(&format!( + "Created the API representation of the \"{}\" page.", &page.slug.green() - ); + )); } // Bring in the static file directory. @@ -369,12 +374,11 @@ fn build_site(args: &Args) { .canonicalize() .unwrap(); let assets_target_dir = Path::new(&build_directory).join("static"); - info!(""); - info!( + header_message(&format!( "Copying static assets from \"{}\" to \"{}\"", &assets_source_dir.display().to_string().green(), &assets_target_dir.display().to_string().green() - ); + )); match copy_dir::copy_dir(&assets_source_dir, &assets_target_dir) { Err(e) => { error!( @@ -390,7 +394,7 @@ fn build_site(args: &Args) { error!("{} Could not copy over the static assets.", "✘".red()); std::process::exit(2); } else { - info!("{} Copied over assets!", "✔".green()); + success_message("Copied over assets!"); } } } @@ -438,11 +442,10 @@ fn initialize_site(args: &Args) { if !project_directory.exists() { create_dir(&project_directory).expect("Could not create the project directory."); let project_dir = &project_directory.canonicalize().unwrap(); - info!( - "{} Created a new project in {}", - "✔".green(), - format!("{}", &project_dir.display()).green() - ); + success_message(&format!( + "Created a new project in {}", + &project_dir.display().to_string().green() + )); } let project_directory = project_directory .canonicalize() @@ -461,7 +464,7 @@ fn initialize_site(args: &Args) { let project_manifest = project_directory.join("site.toml"); match &project_manifest.exists() { true => { - info!("{} Found existing site.toml. Skipping.", "♼".yellow()); + caution_message("Found existing site.toml. Skipping."); } false => { // 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); 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"); if site_template_path.exists() { // TODO: Figure out override. - info!("{} Found existing templates. Skipping.", "♼".yellow()); + caution_message("Found existing templates. Skipping."); } else { create_dir(&site_template_path).expect("Failed to create template directory"); // TODO: Allow for more files. @@ -516,19 +519,14 @@ fn initialize_site(args: &Args) { header_message("Add template static assets to site"); let site_static_path = &project_directory.join("static"); if site_static_path.exists() { - info!( - "{} Found existing static assets. Skipping.", - "♼".yellow() - ); + caution_message("Found existing static assets. Skipping."); } else { match copy_dir::copy_dir(&template_path, &site_static_path) { Err(e) => { - error!( - "{} Could not copy over the template static assets because of: {}", - "✘".red(), - e + fatal_error( + Box::new(e), + "Could not copy over the template static assets.", ); - std::process::exit(2); } Ok(v) => { if v.len() > 0 { @@ -539,7 +537,7 @@ fn initialize_site(args: &Args) { ); std::process::exit(2); } 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); match &sample_page_path.exists() { true => { - info!( - "{} Found existing {}. Skipping.", - "♼".yellow(), + caution_message(&format!( + "Found existing {}. Skipping.", &page_file_name.yellow() - ); + )); } false => { copy_file( @@ -575,7 +572,7 @@ fn initialize_site(args: &Args) { let site_plugins_path = &project_directory.join("plugins"); if site_plugins_path.exists() { // TODO: Figure out override. - info!("{} Found existing plugins. Skipping.", "♼".yellow()); + caution_message("Found existing plugins. Skipping."); } else { create_dir(&site_plugins_path).expect("Failed to create plugin directory"); let plugin_list = vec!["hello-world", "dark-mode-switch"]; @@ -587,11 +584,7 @@ fn initialize_site(args: &Args) { &plugin_file_name, "plugin", ); - info!( - "{} Copied over the {} plugin!", - "✔".green(), - &plugin.green() - ); + success_message(&format!("Copied over the {} plugin!", &plugin.green())); } } }