Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chrono = { version = "0.4.41", features = ["serde"] }
clap = { version = "4.5.38", features = ["derive"] }
clap-markdown = "0.1.5"
ctrlc = "3.4.7"
derive_more = { version = "2.1.1", features = ["full"] }
dirs = "6.0.0"
inquire = "0.7.5"
open = "5.3.3"
Expand All @@ -30,3 +31,4 @@ spinners = "4.2.0"
strum = "0.27.1"
strum_macros = "0.27.1"
tempfile = "3.20.0"
thiserror = "2.0.20"
2 changes: 1 addition & 1 deletion scripts/test-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pwd
--title ExampleProject \
--tags "economics, ComPuterScience" \
--color red \
--template HTML \
--template Markdown \
--description "This is an example project" \
--license "gPl-2.0"

Expand Down
86 changes: 13 additions & 73 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::{
error::Error,
fs::{self},
process::Command,
str::FromStr,
};
use std::{collections::HashMap, error::Error, process::Command, str::FromStr};

use clap::Args;
use spinners::{Spinner, Spinners};
use strum::IntoEnumIterator;

use crate::{config, template::Template};
use crate::{
config,
templates::{templater::Templater, CLITemplate, Renderer},
};

/// Options for the `oseda init` command
#[derive(Args, Debug)]
Expand Down Expand Up @@ -41,26 +39,6 @@ pub struct InitOptions {
pub description: Option<String>,
}

// embed all the static markdown template files into binary
const MD_VITE_CONFIG_JS: &str = include_str!("../static/md-templates/vite.config.js");
const MD_INDEX_HTML: &str = include_str!("../static/md-templates/index.html");
const MD_MAIN_JS: &str = include_str!("../static/md-templates/main.js");
const MD_SLIDES: &str = include_str!("../static/md-templates/slides.md");
const MD_CUSTOM_CSS: &str = include_str!("../static/md-templates/custom.css");
const MD_FERRIS: &[u8] = include_bytes!("../static/md-templates/ferris.png");
const MD_GITIGNORE: &str = include_str!("../static/md-templates/.gitignore");
const MD_FAVICON: &[u8] = include_bytes!("../static/md-templates/favicon.png");

// do the same with the html templates
const HTML_VITE_CONFIG_JS: &str = include_str!("../static/html-templates/vite.config.js");
const HTML_INDEX_HTML: &str = include_str!("../static/html-templates/index.html");
const HTML_MAIN_JS: &str = include_str!("../static/html-templates/main.js");
const HTML_SLIDES: &str = include_str!("../static/html-templates/slides.html");
const HTML_CUSTOM_CSS: &str = include_str!("../static/html-templates/custom.css");
const HTML_FERRIS: &[u8] = include_bytes!("../static/html-templates/ferris.png");
const HTML_GITIGNORE: &str = include_str!("../static/html-templates/.gitignore");
const HTML_FAVICON: &[u8] = include_bytes!("../static/html-templates/favicon.png");

/// Initialize an Oseda project with the provided options
///
/// This command will:
Expand All @@ -77,7 +55,7 @@ const HTML_FAVICON: &[u8] = include_bytes!("../static/html-templates/favicon.png
pub fn init(opts: InitOptions) -> Result<(), Box<dyn Error>> {
let template = match opts.template {
Some(ref arg_template) => {
Template::from_str(arg_template).map_err(|_| "Invalid template".to_string())?
CLITemplate::from_str(arg_template).map_err(|_| "Invalid template".to_string())?
}
None => prompt_template()?,
};
Expand Down Expand Up @@ -132,56 +110,18 @@ pub fn init(opts: InitOptions) -> Result<(), Box<dyn Error>> {

config::write_config(&conf.title, &conf)?;

// 99% sure we'll only ever have to maintain these two template schemas
match template {
Template::Markdown => {
// fs::write(format!("{}/package.json", &conf.title), MD_PACKAGE_JSON)?;
fs::write(format!("{}/vite.config.js", &conf.title), MD_VITE_CONFIG_JS)?;
fs::write(format!("{}/index.html", &conf.title), MD_INDEX_HTML)?;
fs::write(format!("{}/.gitignore", &conf.title), MD_GITIGNORE)?;

std::fs::create_dir_all(format!("{}/src", &conf.title))?;
fs::write(format!("{}/src/main.js", &conf.title), MD_MAIN_JS)?;

std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
fs::write(format!("{}/slides/slides.md", &conf.title), MD_SLIDES)?;
// empty hashmap for now
let mut params: HashMap<String, String> = HashMap::new();
params.insert(String::from("TITLE"), conf.title.clone());

std::fs::create_dir_all(format!("{}/css", &conf.title))?;
fs::write(format!("{}/css/custom.css", &conf.title), MD_CUSTOM_CSS)?;

std::fs::create_dir_all(format!("{}/public", &conf.title))?;
fs::write(format!("{}/public/ferris.png", &conf.title), MD_FERRIS)?;
fs::write(format!("{}/public/favicon.png", &conf.title), MD_FAVICON)?;
}
Template::HTML => {
// fs::write(format!("{}/package.json", &conf.title), HTML_PACKAGE_JSON)?;
fs::write(
format!("{}/vite.config.js", &conf.title),
HTML_VITE_CONFIG_JS,
)?;
fs::write(format!("{}/index.html", &conf.title), HTML_INDEX_HTML)?;
fs::write(format!("{}/.gitignore", &conf.title), HTML_GITIGNORE)?;

std::fs::create_dir_all(format!("{}/src", &conf.title))?;
fs::write(format!("{}/src/main.js", &conf.title), HTML_MAIN_JS)?;

std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
fs::write(format!("{}/slides/slides.html", &conf.title), HTML_SLIDES)?;

std::fs::create_dir_all(format!("{}/css", &conf.title))?;
fs::write(format!("{}/css/custom.css", &conf.title), HTML_CUSTOM_CSS)?;

std::fs::create_dir_all(format!("{}/public", &conf.title))?;
fs::write(format!("{}/public/ferris.png", &conf.title), HTML_FERRIS)?;
fs::write(format!("{}/public/favicon.png", &conf.title), HTML_FAVICON)?;
}
}
let templater = Templater::new(template, params);
templater.write_to_fs(&conf.title)?;

Ok(())
}

fn prompt_template() -> Result<Template, Box<dyn Error>> {
let template_opts: Vec<Template> = Template::iter().collect();
fn prompt_template() -> Result<CLITemplate, Box<dyn Error>> {
let template_opts: Vec<CLITemplate> = CLITemplate::iter().collect();

let chosen_template = inquire::Select::new("Select a template:", template_opts).prompt()?;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod license;
pub mod net;
pub mod puppeteer;
pub mod tags;
pub mod template;
pub mod templates;

/// Oseda Project scafolding CLI
#[derive(Parser)]
Expand Down
9 changes: 0 additions & 9 deletions src/template.rs

This file was deleted.

57 changes: 57 additions & 0 deletions src/templates/html/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::{collections::HashMap, fs};

use crate::templates::{render_template, Renderer, RendererError};

pub const HTML_VITE_CONFIG_JS: &str = include_str!("static/vite.config.js");
pub const HTML_INDEX_HTML: &str = include_str!("static/index.html");
pub const HTML_MAIN_JS: &str = include_str!("static/main.js");
pub const HTML_SLIDES: &str = include_str!("static/slides.html");
pub const HTML_CUSTOM_CSS: &str = include_str!("static/custom.css");
pub const HTML_FERRIS: &[u8] = include_bytes!("static/ferris.png");
pub const HTML_GITIGNORE: &str = include_str!("static/.gitignore");
pub const HTML_FAVICON: &[u8] = include_bytes!("static/favicon.png");

pub struct HtmlRenderer {
pub params: HashMap<String, String>,
}

impl Renderer for HtmlRenderer {
fn write_to_fs(&self, target_dir: &str) -> Result<(), RendererError> {
fs::write(
format!("{}/vite.config.js", target_dir),
render_template(HTML_VITE_CONFIG_JS, &self.params),
)?;
fs::write(
format!("{}/index.html", target_dir),
render_template(HTML_INDEX_HTML, &self.params),
)?;
fs::write(
format!("{}/.gitignore", target_dir),
render_template(HTML_GITIGNORE, &self.params),
)?;

std::fs::create_dir_all(format!("{}/src", target_dir))?;
fs::write(
format!("{}/src/main.js", target_dir),
render_template(HTML_MAIN_JS, &self.params),
)?;

std::fs::create_dir_all(format!("{}/slides", target_dir))?;
fs::write(
format!("{}/slides/slides.html", target_dir),
render_template(HTML_SLIDES, &self.params),
)?;

std::fs::create_dir_all(format!("{}/css", target_dir))?;
fs::write(
format!("{}/css/custom.css", target_dir),
render_template(HTML_CUSTOM_CSS, &self.params),
)?;

std::fs::create_dir_all(format!("{}/public", target_dir))?;
fs::write(format!("{}/public/ferris.png", target_dir), HTML_FERRIS)?;
fs::write(format!("{}/public/favicon.png", target_dir), HTML_FAVICON)?;

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My OSEDA project</title>
<title>{{ TITLE }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="/favicon.png" />
</head>
Expand Down
File renamed without changes.
59 changes: 59 additions & 0 deletions src/templates/md/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::{collections::HashMap, fs};

use crate::templates::{render_template, Renderer, RendererError};

pub const MD_VITE_CONFIG_JS: &str = include_str!("static/vite.config.js");
pub const MD_INDEX_HTML: &str = include_str!("static/index.html");
pub const MD_MAIN_JS: &str = include_str!("static/main.js");
pub const MD_SLIDES: &str = include_str!("static/slides.md");
pub const MD_CUSTOM_CSS: &str = include_str!("static/custom.css");
pub const MD_FERRIS: &[u8] = include_bytes!("static/ferris.png");
pub const MD_GITIGNORE: &str = include_str!("static/.gitignore");
pub const MD_FAVICON: &[u8] = include_bytes!("static/favicon.png");

pub struct MarkdownRenderer {
pub params: HashMap<String, String>,
}

impl Renderer for MarkdownRenderer {
fn write_to_fs(&self, target_dir: &str) -> Result<(), RendererError> {
fs::write(
format!("{}/vite.config.js", target_dir),
render_template(MD_VITE_CONFIG_JS, &self.params),
)?;

fs::write(
format!("{}/index.html", target_dir),
render_template(MD_INDEX_HTML, &self.params),
)?;
fs::write(
format!("{}/.gitignore", target_dir),
render_template(MD_GITIGNORE, &self.params),
)?;

std::fs::create_dir_all(format!("{}/src", target_dir))?;
fs::write(
format!("{}/src/main.js", target_dir),
render_template(MD_MAIN_JS, &self.params),
)?;

std::fs::create_dir_all(format!("{}/slides", target_dir))?;
fs::write(
format!("{}/slides/slides.md", target_dir),
render_template(MD_SLIDES, &self.params),
)?;

std::fs::create_dir_all(format!("{}/css", target_dir))?;
fs::write(
format!("{}/css/custom.css", target_dir),
render_template(MD_CUSTOM_CSS, &self.params),
)?;

// binary files do not need templated
std::fs::create_dir_all(format!("{}/public", target_dir))?;
fs::write(format!("{}/public/ferris.png", target_dir), MD_FERRIS)?;
fs::write(format!("{}/public/favicon.png", target_dir), MD_FAVICON)?;

Ok(())
}
}
File renamed without changes.
File renamed without changes.
Loading
Loading