From 40b69155e5bafca382a57084239ba8b1bc06d63f Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Fri, 22 Dec 2023 23:21:25 +0800 Subject: [PATCH] wip --- locales/readme.yml | 20 ++++++++++++- src/template/README.md | 3 ++ src/utils/create_project.rs | 6 +++- src/utils/directory2md.rs | 57 +++++++++++++++++++++---------------- 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/locales/readme.yml b/locales/readme.yml index aee832f..0138142 100644 --- a/locales/readme.yml +++ b/locales/readme.yml @@ -394,4 +394,22 @@ src_app_response_rs: uk: Стандартизувати відповідь th: มาตรฐานตอบกลับ el: Τυποποίηση απόκρισης - da: Standardiser svar \ No newline at end of file + da: Standardiser svar +project_dir_description: + en: Project directory description + zh_CN: 项目目录说明 + zh_TW: 項目目錄說明 + fr: Description du répertoire du projet + ja: プロジェクトディレクトリの説明 + es: Descripción del directorio del proyecto + de: Projektverzeichnisbeschreibung + ru: Описание каталога проекта + it: Descrizione della directory di progetto + pt: Descrição do diretório do projeto + ko: 프로젝트 디렉토리 설명 + no: Prosjektkatalogbeskrivelse + is: Lýsing verkefnamöppu + uk: Опис каталогу проекту + th: คําอธิบายไดเร็กทอรี่โปรเจ็กต์ + el: Περιγραφή καταλόγου έργου + da: Projektbiblioteksbeskrivelse \ No newline at end of file diff --git a/src/template/README.md b/src/template/README.md index e69de29..e460008 100644 --- a/src/template/README.md +++ b/src/template/README.md @@ -0,0 +1,3 @@ + +# +{{directory_contents}} \ No newline at end of file diff --git a/src/utils/create_project.rs b/src/utils/create_project.rs index 2f0cdd4..79edc36 100644 --- a/src/utils/create_project.rs +++ b/src/utils/create_project.rs @@ -187,6 +187,7 @@ pub fn write_project_file( data["database_connection_failed"] = handlebars::JsonValue::String(t!("database_connection_failed")); data["user_does_not_exist"] = handlebars::JsonValue::String(t!("user_does_not_exist")); + let mut dependencies = data["dependencies"].clone(); handle_dependencies( &mut dependencies, @@ -469,10 +470,13 @@ pub fn write_project_file( } templates.append(&mut db_templates); } + templates.append(vec![("src/README.md", include_str!("../template/README.md"))].as_mut()); + + let directory_contents = write_directory_contents_to_markdown(&project_path.join("README.md"))?; + data["directory_contents"] = handlebars::JsonValue::String(directory_contents); for (file_name, template) in &templates { render_and_write_to_file(&handlebars, template, &data, project_path.join(file_name))?; } - write_directory_contents_to_markdown(&project_path.join("README.md"))?; Ok(()) } diff --git a/src/utils/directory2md.rs b/src/utils/directory2md.rs index fe0a25d..57be209 100644 --- a/src/utils/directory2md.rs +++ b/src/utils/directory2md.rs @@ -1,8 +1,10 @@ +use anyhow::anyhow; +use anyhow::Result; use once_cell::sync::Lazy; use rust_i18n::t; use std::collections::HashMap; -use std::fs::{self, File}; -use std::io::{Result, Write}; +use std::fmt::Write; +use std::fs::{self}; use std::path::Path; use walkdir::WalkDir; @@ -36,20 +38,25 @@ static PATH_DESCRIPTIONS: Lazy> = Lazy::new(|| { m }); -pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result<()> { - let mut file = File::create(output_file)?; +pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result { + let mut output = String::new(); let project_name = output_file .parent() - .unwrap() + .ok_or(anyhow!("Parent directory not found."))? .file_name() - .unwrap_or_default() + .ok_or(anyhow!("Project name not found."))? .to_string_lossy(); - writeln!(file, "# {}", project_name)?; - for entry in WalkDir::new(output_file.parent().unwrap()) - .into_iter() - .filter_map(|e| e.ok()) - .filter(|e| e.file_name().to_string_lossy() != "README.md") + writeln!(output, "# {}", project_name)?; + + for entry in WalkDir::new( + output_file + .parent() + .ok_or(anyhow!("Parent directory not found."))?, + ) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| e.file_name().to_string_lossy() != "README.md") { let depth = entry.depth(); let indent = " ".repeat(depth.saturating_sub(1)); @@ -57,33 +64,33 @@ pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result<()> { let metadata = fs::metadata(path)?; if let Some(file_name) = path.file_name() { let file_name_str = file_name.to_string_lossy(); - let full_path = path.to_string_lossy().into_owned(); - let full_path = full_path.trim_start_matches(&*project_name); + let full_path = path + .strip_prefix( + output_file + .parent() + .ok_or(anyhow!("Parent directory not found."))?, + )? + .to_string_lossy() + .into_owned(); dbg!(&full_path); - let description = PATH_DESCRIPTIONS.get(full_path); + let description = PATH_DESCRIPTIONS.get(&*full_path); let description = description .map(|s| format!(" ({})", s)) .unwrap_or_default(); if metadata.is_dir() { writeln!( - file, + output, "{}- **{}:** {} {}", - indent, - t!("dir"), - file_name_str, - description + indent, "dir", file_name_str, description )?; } else { writeln!( - file, + output, "{}- *{}:* {} {}", - indent, - t!("file"), - file_name_str, - description + indent, "file", file_name_str, description )?; } } } - Ok(()) + Ok(output) }