Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Dec 22, 2023
1 parent f033eba commit 40b6915
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
20 changes: 19 additions & 1 deletion locales/readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,22 @@ src_app_response_rs:
uk: Стандартизувати відповідь
th: มาตรฐานตอบกลับ
el: Τυποποίηση απόκρισης
da: Standardiser svar
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
3 changes: 3 additions & 0 deletions src/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

#
{{directory_contents}}
6 changes: 5 additions & 1 deletion src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}

Expand Down
57 changes: 32 additions & 25 deletions src/utils/directory2md.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -36,54 +38,59 @@ static PATH_DESCRIPTIONS: Lazy<HashMap<String, String>> = 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<String> {
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));
let path = entry.path();
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)
}

0 comments on commit 40b6915

Please sign in to comment.