Skip to content

Commit

Permalink
Add --init-templates and --start-theme Flags (#86)
Browse files Browse the repository at this point in the history
fix #70 

---------

Signed-off-by: Akash <[email protected]>
Co-authored-by: Bruno Rocha <[email protected]>
  • Loading branch information
SkySingh04 and rochacbruno authored Oct 27, 2024
1 parent 8984168 commit 83229f0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Cli {
#[arg(long)]
pub watch: bool,

/// Address to bind the server (defaults to localhost:8000)
#[arg(long, default_value = "localhost:8000")]
pub bind: String,

Expand All @@ -29,4 +30,12 @@ pub struct Cli {
/// Print debug messages
#[arg(long)]
pub debug: bool,

/// Initialize templates in the project
#[arg(long)]
pub init_templates: bool,

/// Initialize a theme with templates and static assets
#[arg(long)]
pub start_theme: bool,
}
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use clap::Parser;
use env_logger::{Builder, Env};

use log::{error, info};
use std::{path::PathBuf, sync::Arc};
use std::{fs, path::PathBuf, sync::Arc};

mod cli;
mod config;
mod content;
mod embedded;
mod markdown;
mod server;
mod templates;
mod site;
mod tera_functions;

Expand All @@ -18,8 +18,8 @@ fn main() {
let input_folder = args.input_folder;
let output_folder = Arc::new(args.output_folder);
let serve = args.serve;

let watch = args.watch;

let config_path = if args.config.starts_with('.') || args.config.starts_with('/') {
PathBuf::new().join(args.config)
} else {
Expand All @@ -33,6 +33,22 @@ fn main() {
error!("Logger already initialized: {}", e);
}

// Handle `init_templates` flag
if args.init_templates {
templates::initialize_templates(&input_folder);
info!("Initialized templates.");
return; // Exit early if only initializing templates
}

// Handle `start_theme` flag
if args.start_theme {
templates::initialize_templates(&input_folder);
templates::initialize_theme_assets(&input_folder);
info!("Initialized templates and theme assets.");
return; // Exit early if only initializing theme
}

// Generate site content
site::generate(
&config_path,
&input_folder,
Expand Down
42 changes: 42 additions & 0 deletions src/templates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use log::error;
use std::fs;
use std::path::PathBuf;
use crate::embedded::{Templates, generate_static};

/// Creates the `templates/` folder and writes embedded templates to it.
pub fn initialize_templates(input_folder: &PathBuf) {
let templates_path = input_folder.join("templates");
if let Err(e) = fs::create_dir_all(&templates_path) {
error!("Failed to create templates directory: {}", e);
return;
}

for name in Templates::iter() {
if let Some(template) = Templates::get(name.as_ref()) {
if let Ok(template_str) = std::str::from_utf8(template.data.as_ref()) {
let template_path = templates_path.join(name.as_ref());
if let Err(e) = fs::write(&template_path, template_str) {
error!("Failed to write template '{}': {}", name.as_ref(), e);
}
} else {
error!("Failed to parse template '{}' as UTF-8", name.as_ref());
}
} else {
error!("Template '{}' not found in embedded resources", name.as_ref());
}
}
}

/// Uses `generate_static` to populate the `static/` folder with embedded content.
pub fn initialize_theme_assets(input_folder: &PathBuf) {
let static_path = input_folder.join("static");

if !static_path.exists() {
if let Err(e) = fs::create_dir_all(&static_path) {
error!("Failed to create static assets directory: {}", e);
return;
}
}

generate_static(&static_path);
}

0 comments on commit 83229f0

Please sign in to comment.