Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: project creation

+40 -32
+1
Cargo.lock
··· 1770 1770 "clap", 1771 1771 "colored", 1772 1772 "env_logger", 1773 + "flate2", 1773 1774 "futures", 1774 1775 "futures-util", 1775 1776 "http-body-util",
+1
crates/cli/Cargo.toml
··· 37 37 tar = "0.4.43" 38 38 toml_edit = "0.22.23" 39 39 local-ip-address = "0.6.3" 40 + flate2 = "1.0.35"
+38 -32
crates/cli/src/init.rs
··· 4 4 }; 5 5 6 6 use colored::Colorize; 7 + use flate2::read::GzDecoder; 7 8 use inquire::{validator::Validation, Confirm, Select, Text}; 8 9 use rand::seq::IndexedRandom; 9 10 use spinach::{Color, Spinner}; 10 11 use toml_edit::DocumentMut; 11 - use tracing::{debug, info}; 12 + use tracing::{debug, error, info}; 12 13 13 14 mod names; 14 15 mod render_config; ··· 228 229 info!(name: "SKIP_FORMAT", " Need a hand? Find us at {}.", "https://maudit.org/chat".bold().bright_magenta().underline()); 229 230 } 230 231 231 - fn download_and_unpack_template(template: &str, project_path: &Path) -> Result<(), String> { 232 + fn download_and_unpack_template( 233 + template: &str, 234 + project_path: &Path, 235 + ) -> Result<(), Box<dyn std::error::Error>> { 232 236 let tarball = ureq::get(REPO_TAR_URL) 233 237 .call() 234 238 .map_err(|e| format!("Failed to download template: {}", e))?; 235 239 236 240 if !tarball.status().is_success() { 237 - return Err("Failed to download template".to_string()); 241 + return Err("Failed to download template".into()); 238 242 } 239 243 240 - let (_, body) = tarball.into_parts(); 241 - let archive = body.into_reader(); 244 + let (_, mut body) = tarball.into_parts(); 245 + 246 + let archive = GzDecoder::new(body.as_reader()); 242 247 243 248 // Uncomment to test with a local tarball 244 249 //let archive = std::fs::File::open("project.tar").unwrap(); 245 250 246 251 let mut archive = tar::Archive::new(archive); 247 252 248 - for file in archive.entries().unwrap() { 249 - let mut file = file.unwrap(); 250 - let path = file.path().unwrap(); 251 - 252 - if path.starts_with(format!("examples/{}", template)) { 253 - let path = path.strip_prefix(format!("examples/{}", template)).unwrap(); 254 - let path = project_path.join(path); 253 + let example_path = format!("examples/{}", template); 254 + for entry in archive.entries()? { 255 + let mut entry = entry?; 256 + let path = entry.path()?.to_string_lossy().to_string(); 255 257 256 - file.unpack(path).unwrap(); 258 + if let Some(index) = path.find(&example_path).map(|i| i + example_path.len() + 1) { 259 + let dest_path = project_path.join(&path[index..]); 260 + entry.unpack(dest_path)?; 257 261 } 258 262 } 259 263 260 264 // Edit the Cargo.toml file 261 265 let cargo_toml_path = project_path.join("Cargo.toml"); 266 + match std::fs::read_to_string(&cargo_toml_path) { 267 + Ok(content) => { 268 + let mut cargo_toml = content.parse::<DocumentMut>().expect("invalid doc"); 262 269 263 - let cargo_toml_content = std::fs::read_to_string(&cargo_toml_path).unwrap(); 264 - let mut cargo_toml = cargo_toml_content 265 - .parse::<DocumentMut>() 266 - .expect("invalid doc"); 270 + if let Some(project_name) = project_path.file_name().and_then(|name| name.to_str()) { 271 + cargo_toml["package"]["name"] = toml_edit::value(project_name); 267 272 268 - let project_name = project_path 269 - .components() 270 - .last() 271 - .unwrap() 272 - .as_os_str() 273 - .to_str() 274 - .unwrap(); 273 + if let toml_edit::Item::Value(v) = 274 + &cargo_toml["package"]["metadata"]["maudit"]["intended_version"] 275 + { 276 + cargo_toml["dependencies"]["maudit"] = toml_edit::value(v.clone()); 277 + } 275 278 276 - cargo_toml["package"]["name"] = toml_edit::value(project_name); 279 + cargo_toml["package"]["metadata"] = toml_edit::Item::None; 277 280 278 - let maudit_intended_version = &cargo_toml["package"]["metadata"]["maudit"]["intended_version"]; 279 - 280 - // If the template is using the workspace version, remove the `workspace = true` property 281 - if let toml_edit::Item::Value(v) = maudit_intended_version { 282 - cargo_toml["dependencies"]["maudit"] = toml_edit::value(v); 281 + if let Err(e) = std::fs::write(&cargo_toml_path, cargo_toml.to_string()) { 282 + error!("Failed to write Cargo.toml file: {}", e); 283 + } 284 + } else { 285 + error!("Failed to determine project name from path"); 286 + } 287 + } 288 + Err(e) => { 289 + error!("Failed to read Cargo.toml file: {}", e); 290 + } 283 291 } 284 - 285 - std::fs::write(&cargo_toml_path, cargo_toml.to_string()).unwrap(); 286 292 287 293 Ok(()) 288 294 }