1
Fork 0

Separate stuff out.

This commit is contained in:
Bauke 2021-12-14 00:50:38 +01:00
parent 97ccb7539b
commit b8cdc833f3
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
4 changed files with 136 additions and 83 deletions

37
source/copy.rs Normal file
View File

@ -0,0 +1,37 @@
use std::{fs, path::Path, process::Command};
use color_eyre::Result;
pub struct Copy;
impl Copy {
pub fn write(
build_dir: &Path,
public_dir: &Path,
source_dir: &Path,
) -> Result<()> {
let files_to_copy = vec![(
source_dir.join("netlify/_redirects"),
public_dir.join("_redirects"),
)];
for (source, destination) in files_to_copy {
fs::copy(source, destination)?;
}
let dirs_to_copy = vec![
(source_dir.join("js"), &public_dir),
(build_dir.join("userstyles"), &public_dir),
];
for (source, destination) in dirs_to_copy {
Command::new("cp")
.arg("-r")
.arg(source)
.arg(destination)
.output()?;
}
Ok(())
}
}

View File

@ -1,14 +1,31 @@
use std::{fs, path::PathBuf, process::Command};
use std::{
fs,
path::{Path, PathBuf},
};
use askama::Template;
use color_eyre::{install, Result};
mod copy;
mod scss;
mod templates;
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
fn main() -> Result<()> {
install()?;
let build_dir = PathBuf::from("target");
let public_dir = PathBuf::from("public");
let source_dir = PathBuf::from("source");
build_userstyles(&build_dir)?;
templates::Index::write(&public_dir)?;
templates::Userstyles::write(&public_dir)?;
scss::Scss::write(&public_dir, &source_dir)?;
copy::Copy::write(&build_dir, &public_dir, &source_dir)?;
Ok(())
}
fn build_userstyles(build_dir: &Path) -> Result<()> {
for target in userstyles::ALL_USERSTYLES {
let style = userstyles::Userstyle::load(target)?;
let style_name = style.metadata.name.to_lowercase().replace(" ", "-");
@ -26,84 +43,5 @@ fn main() -> color_eyre::Result<()> {
}
}
let public_dir = PathBuf::from("public");
let source_dir = PathBuf::from("source");
let styles = userstyles::ALL_USERSTYLES
.iter()
.map(|target| userstyles::Userstyle::load(target))
.flatten()
.collect::<Vec<_>>();
let templates_to_render: Vec<(Box<dyn Template>, PathBuf)> = vec![
(
Box::new(templates::Index {
page_title: "bauke.xyz".to_string(),
}),
public_dir.join("index.html"),
),
(
Box::new(templates::Userstyles {
page_title: "bauke.xyz".to_string(),
styles,
}),
public_dir.join("userstyles/index.html"),
),
];
for (template, path) in templates_to_render {
fs::create_dir_all(&path.parent().unwrap())?;
let rendered = template.render()?;
fs::write(path, rendered)?;
}
let css_dir = public_dir.join("css");
fs::create_dir_all(&css_dir)?;
let scss_dir = source_dir.join("scss");
let scss_to_render = vec![
(scss_dir.join("index.scss"), css_dir.join("index.css")),
(
scss_dir.join("modern-normalize.scss"),
css_dir.join("modern-normalize.css"),
),
];
for (source, destination) in scss_to_render {
let rendered = rsass::compile_scss_path(
&source,
rsass::output::Format {
style: rsass::output::Style::Expanded,
precision: 5,
},
)?;
fs::write(destination, rendered)?;
}
let files_to_copy = vec![(
source_dir.join("netlify/_redirects"),
public_dir.join("_redirects"),
)];
for (source, destination) in files_to_copy {
fs::copy(source, destination)?;
}
let dirs_to_copy = vec![
(source_dir.join("js"), &public_dir),
(build_dir.join("userstyles"), &public_dir),
];
for (source, destination) in dirs_to_copy {
Command::new("cp")
.arg("-r")
.arg(source)
.arg(destination)
.output()?;
}
Ok(())
}

35
source/scss/mod.rs Normal file
View File

@ -0,0 +1,35 @@
use std::{
fs::{create_dir_all, write},
path::Path,
};
use color_eyre::Result;
use rsass::{
compile_scss_path,
output::{Format, Style},
};
pub struct Scss;
impl Scss {
pub fn write(public_dir: &Path, source_dir: &Path) -> Result<()> {
let css_dir = public_dir.join("css");
create_dir_all(&css_dir)?;
let scss_dir = source_dir.join("scss");
let scss_filenames = vec!["index", "modern-normalize"];
let format = Format {
precision: 5,
style: Style::Compressed,
};
for filename in scss_filenames {
let scss_path = scss_dir.join(format!("{}.scss", filename));
let css = compile_scss_path(&scss_path, format)?;
let css_path = css_dir.join(format!("{}.css", filename));
write(css_path, css)?;
}
Ok(())
}
}

View File

@ -1,4 +1,10 @@
use std::{
fs::{create_dir_all, write},
path::Path,
};
use askama::Template;
use color_eyre::Result;
#[derive(Debug, Template)]
#[template(path = "index.html")]
@ -6,9 +12,46 @@ pub struct Index {
pub page_title: String,
}
impl Index {
pub fn write(public_dir: &Path) -> Result<()> {
let destination = public_dir.join("index.html");
create_dir_all(destination.parent().unwrap())?;
let template = Self {
page_title: "bauke".to_string(),
};
write(destination, template.render()?)?;
Ok(())
}
}
#[derive(Debug, Template)]
#[template(path = "userstyles.html")]
pub struct Userstyles {
pub page_title: String,
pub styles: Vec<userstyles::Userstyle>,
}
impl Userstyles {
pub fn write(public_dir: &Path) -> Result<()> {
let destination = public_dir.join("userstyles/index.html");
create_dir_all(destination.parent().unwrap())?;
let styles = userstyles::ALL_USERSTYLES
.iter()
.map(|target| userstyles::Userstyle::load(target))
.flatten()
.collect();
let template = Self {
page_title: "bauke".to_string(),
styles,
};
write(destination, template.render()?)?;
Ok(())
}
}