Skip to content

Commit 07d3555

Browse files
committed
Separate theme download from validation.
1 parent 73556a9 commit 07d3555

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

src/main.rs

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ async fn server(
963963
app
964964
}
965965

966-
fn load_or_download_themes(root_path: &str, url: &str, validate: bool) -> HashMap<String, Theme> {
966+
fn load_or_download_themes(root_path: &str, url: &str) -> HashMap<String, Theme> {
967967
let mut themes = theme::load_themes(root_path);
968968

969969
if themes.len() == 0 {
@@ -978,20 +978,18 @@ fn load_or_download_themes(root_path: &str, url: &str, validate: bool) -> HashMa
978978
}
979979

980980
if response == "y" {
981-
if let Err(e) = download_themes(root_path, url, validate) {
981+
if let Err(e) = download_themes(root_path, url) {
982982
panic!("Failed to fetch themes: {}", e);
983983
}
984984

985-
if !validate {
986-
themes = theme::load_themes(root_path);
987-
}
985+
themes = theme::load_themes(root_path);
988986
}
989987
}
990988

991989
themes
992990
}
993991

994-
fn download_themes(root_path: &str, url: &str, validate: bool) -> Result<()> {
992+
fn download_themes(root_path: &str, url: &str) -> Result<()> {
995993
let themes_dir = &Path::new(root_path).join("themes");
996994
let mut tempfile = tempfile::tempfile()?;
997995
let mut response = get(url)?;
@@ -1021,46 +1019,6 @@ fn download_themes(root_path: &str, url: &str, validate: bool) -> Result<()> {
10211019
}
10221020
}
10231021

1024-
if validate {
1025-
log::info!("Validating themes...");
1026-
1027-
let valid_themes_filename = "valid_themes.txt";
1028-
let mut valid_themes_file = File::create(Path::new(root_path).join(valid_themes_filename))?;
1029-
1030-
for path in &match fs::read_dir(themes_dir) {
1031-
Ok(paths) => paths.map(|r| r.unwrap()).collect(),
1032-
_ => vec![],
1033-
} {
1034-
let theme = path.file_name();
1035-
let theme = theme.to_str().unwrap();
1036-
if !path.file_type().unwrap().is_dir() || theme.starts_with(".") {
1037-
continue;
1038-
}
1039-
1040-
let mut empty_site = Site::empty(&theme);
1041-
let templates = site::load_templates(root_path, &empty_site.domain, &empty_site.config);
1042-
if let Err(e) = templates {
1043-
log::warn!("Failed to load theme templates {}: {}", theme, e);
1044-
continue;
1045-
}
1046-
empty_site.tera = Arc::new(RwLock::new(templates.unwrap()));
1047-
if let Err(e) = render_and_build_response(
1048-
&empty_site,
1049-
Section::<PostSectionFilter>::from_resource(
1050-
&get_default_index("index"),
1051-
&empty_site,
1052-
)?,
1053-
) {
1054-
log::warn!("Failed to render theme {}: {}", theme, e);
1055-
continue;
1056-
}
1057-
1058-
writeln!(valid_themes_file, "{}", theme).unwrap();
1059-
}
1060-
1061-
log::info!("Valid themes saved to {}", valid_themes_filename);
1062-
}
1063-
10641022
Ok(())
10651023
}
10661024

@@ -1098,11 +1056,49 @@ fn load_or_create_sites(
10981056
}
10991057
}
11001058

1059+
fn validate_themes(root_path: &str, valid_themes_filename: &str) -> Result<()> {
1060+
let mut valid_themes_file = File::create(Path::new(root_path).join(valid_themes_filename))?;
1061+
let themes_dir = &Path::new(root_path).join("themes");
1062+
1063+
for path in &match fs::read_dir(themes_dir) {
1064+
Ok(paths) => paths.map(|r| r.unwrap()).collect(),
1065+
_ => vec![],
1066+
} {
1067+
let theme = path.file_name();
1068+
let theme = theme.to_str().unwrap();
1069+
if !path.file_type().unwrap().is_dir() || theme.starts_with(".") {
1070+
continue;
1071+
}
1072+
1073+
let mut empty_site = Site::empty(&theme);
1074+
let templates = site::load_templates(root_path, &empty_site.domain, &empty_site.config);
1075+
if let Err(e) = templates {
1076+
log::warn!("Failed to load theme templates {}: {}", theme, e);
1077+
continue;
1078+
}
1079+
empty_site.tera = Arc::new(RwLock::new(templates.unwrap()));
1080+
if let Err(e) = render_and_build_response(
1081+
&empty_site,
1082+
Section::<PostSectionFilter>::from_resource(&get_default_index("index"), &empty_site)?,
1083+
) {
1084+
log::warn!("Failed to render theme {}: {}", theme, e);
1085+
continue;
1086+
}
1087+
1088+
log::info!("Theme OK: {}", theme);
1089+
1090+
writeln!(valid_themes_file, "{}", theme).unwrap();
1091+
}
1092+
1093+
Ok(())
1094+
}
1095+
11011096
#[async_std::main]
11021097
async fn main() -> Result<(), std::io::Error> {
11031098
const DEFAULT_ADDR: &str = "0.0.0.0";
11041099
const DEFAULT_PORT: u32 = 4884;
11051100
const DEFAULT_ROOT_PATH: &str = "./";
1101+
const VALID_THEMES_FILENAME: &str = "valid_themes.txt";
11061102

11071103
let args = Cli::parse();
11081104

@@ -1121,10 +1117,12 @@ async fn main() -> Result<(), std::io::Error> {
11211117
let themes = load_or_download_themes(
11221118
&DEFAULT_ROOT_PATH,
11231119
&args.themes_url.unwrap_or(DEFAULT_THEMES_URL.to_string()),
1124-
args.validate_themes,
11251120
);
11261121

11271122
if args.validate_themes {
1123+
log::info!("Validating themes...");
1124+
validate_themes(DEFAULT_ROOT_PATH, VALID_THEMES_FILENAME).expect("Theme validation failed");
1125+
log::info!("Valid themes saved to {}. Exiting!", VALID_THEMES_FILENAME);
11281126
return Ok(());
11291127
}
11301128

@@ -1315,7 +1313,6 @@ mod tests {
13151313
download_themes(
13161314
&root_path,
13171315
"https://github.com/servus-social/themes/releases/latest/download/test-themes.zip",
1318-
false,
13191316
)?;
13201317

13211318
Ok(())

0 commit comments

Comments
 (0)