Skip to content

Commit

Permalink
Merge pull request #22 from salvo-rs/add_readme
Browse files Browse the repository at this point in the history
Add readme
  • Loading branch information
fankaiLiu authored Dec 26, 2023
2 parents 49a160f + ac15835 commit 3ef5ea2
Show file tree
Hide file tree
Showing 8 changed files with 1,130 additions and 141 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ unicode-xid = "0.2.4"
rust-i18n = "2"
itertools = "0.12.0"
reqwest = "0.11.22"
tokio = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["full"] }
walkdir = "2.4.0"
once_cell = "1.19.0"
123 changes: 35 additions & 88 deletions locales/after_print_info.yml

Large diffs are not rendered by default.

881 changes: 881 additions & 0 deletions locales/readme.yml

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions src/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# {{introduction}}
{{introduction_text}}
{{#if is_sqlite}}
{{/if}}
``` shell
//{{run_the_project}}
cargo run
//{{run_the_tests}}
cargo test
```
{{#if need_db_conn}}
# {{tip_title}}
- {{password_tip}}
{{# if is_sea_orm_or_sqlx}}
- {{config_tip}}
{{/if}}
# {{orm_title}}
{{#if is_sqlx}}
{{sqlx_website}}
## sqlx_cli
{{sqlx_cli}} https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/README.md
## {{initialization}}
{{#if is_sqlite}}
{{seleted_sqlite}}
{{else}}
- {{initialization_sqlx_cli_not_sqlite}}
{{/if}}
{{/if}}
{{#if is_sea_orm}}
{{sea_orm_website}}
## sea_orm_cli
{{sea_orm_cli_website}}
## {{initialization}}
{{/if}}
{{#if is_sea_orm}}
{{#if is_sqlite}}
{{seleted_sqlite}}
{{else}}
- {{initialization_seaorm_cli_not_sqlite}}
{{/if}}
{{/if}}
{{#if is_diesel}}
{{diesel_website}}
## diesel_cli
{{diesel_cli_website}}
## {{initialization}}
{{#if is_sqlite}}
{{seleted_sqlite}}
{{else}}
- {{initialization_diesel_cli_not_sqlite}}
{{/if}}
{{/if}}
{{#if is_rbatis}}
{{rbatis_website}}
## {{initialization}}
{{#if is_sqlite}}
{{seleted_sqlite}}
{{else}}
- {{initialization_rbatis_cli_not_sqlite}}
{{/if}}
{{/if}}
{{#if is_mongodb}}
{{mongodb_website}}
## {{initialization}}
- {{mongodb_usage_import_user_data}}
{{/if}}
{{/if}}
# {{project_dir_description}}
{{directory_contents}}
# {{about_salvo}}
{{about_salvo_text}}
87 changes: 37 additions & 50 deletions src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
};

use super::{
directory2md::write_directory_contents_to_markdown,
get_selection::{get_user_selected, DbConnectionType, DbType, TemplateType, UserSelected},
print_util, restricted_names, warning,
};
Expand Down Expand Up @@ -41,64 +42,19 @@ pub fn create_project(project: Project) -> Result<()> {
warning(t!("warning_init_git", error = e).replace(r"\n", "\n"));
}
}
after_print_info(project_name, config);
after_print_info(project_name);
}
None => anyhow::bail!("cli quit!"),
}
Ok(())
}

fn after_print_info(project_name: &String, config: UserSelected) {
fn after_print_info(project_name: &String) {
println!(); // a new line

// print success info
success(t!("create_success", project_name = project_name).replace(r"\n", "\n"));

// print success info
success(t!("create_info", project_name = project_name).replace(r"\n", "\n"));
success(t!("create_success").replace(r"\n", "\n"));
println!(); // a new line

match config.db_conn_type {
DbConnectionType::Sqlx => {
success(t!("create_success_sqlx").replace(r"\n", "\n"));
match config.db_type {
DbType::Sqlite => {
success(t!("create_success_sqlx_sqlite").replace(r"\n", "\n"));
}
_ => {
success(t!("create_success_mysql_or_pgsql").replace(r"\n", "\n"));
}
}
}
DbConnectionType::SeaOrm => {
success(t!("create_success_sea_orm").replace(r"\n", "\n"));
match config.db_type {
DbType::Sqlite => {
success(t!("create_success_sqlx_sqlite").replace(r"\n", "\n"));
}
_ => {
success(t!("create_success_mysql_or_pgsql").replace(r"\n", "\n"));
}
}
}
DbConnectionType::Diesel => match config.db_type {
DbType::Sqlite => {
success(t!("create_success_sqlx_diesel").replace(r"\n", "\n"));
}
_ => {
success(t!("create_success_mysql_or_pgsql").replace(r"\n", "\n"));
}
},
DbConnectionType::Rbatis => match config.db_type {
DbType::Mysql | DbType::Postgres | DbType::Mssql => {
success(t!("create_success_rbatis").replace(r"\n", "\n"));
}
_ => {}
},
DbConnectionType::Mongodb => {
success(t!("mongodb_usage_import_user_data").replace(r"\n", "\n"));
success(t!("access_instructions").replace(r"\n", "\n"));
}
_ => {}
}
}

pub fn write_project_file(
Expand Down Expand Up @@ -186,6 +142,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 @@ -471,6 +428,36 @@ pub fn write_project_file(
for (file_name, template) in &templates {
render_and_write_to_file(&handlebars, template, &data, project_path.join(file_name))?;
}

let directory_contents = write_directory_contents_to_markdown(&project_path.join("README.md"))?;
data["directory_contents"] = handlebars::JsonValue::String(directory_contents);
data["project_dir_description"] = handlebars::JsonValue::String(t!("project_dir_description"));
data["introduction"] = handlebars::JsonValue::String(t!("introduction"));
data["introduction_text"] = handlebars::JsonValue::String(t!("introduction_text"));
data["seleted_sqlite"] = handlebars::JsonValue::String(t!("seleted_sqlite"));
data["run_the_project"]=handlebars::JsonValue::String(t!("run_the_project"));
data["run_the_tests"]=handlebars::JsonValue::String(t!("run_the_tests"));
data["sqlx_cli"]=handlebars::JsonValue::String(t!("sqlx_cli"));
data["about_salvo"]=handlebars::JsonValue::String(t!("about_salvo"));
data["about_salvo_text"]=handlebars::JsonValue::String(t!("about_salvo_text"));
data["tip_title"]=handlebars::JsonValue::String(t!("tip_title"));
data["password_tip"]=handlebars::JsonValue::String(t!("password_tip"));
data["config_tip"]=handlebars::JsonValue::String(t!("config_tip"));
data["orm_title"]=handlebars::JsonValue::String(t!("orm_title"));
data["sqlx_website"]=handlebars::JsonValue::String(t!("sqlx_website"));
data["sea_orm_website"]=handlebars::JsonValue::String(t!("sea_orm_website"));
data["diesel_website"]=handlebars::JsonValue::String(t!("diesel_website"));
data["rbatis_website"]=handlebars::JsonValue::String(t!("rbatis_website"));
data["mongodb_website"]=handlebars::JsonValue::String(t!("mongodb_website"));
data["initialization"]=handlebars::JsonValue::String(t!("initialization"));
data["initialization_sqlx_cli_not_sqlite"]=handlebars::JsonValue::String(t!("initialization_sqlx_cli_not_sqlite").replace(r"\n", "\n"));
data["initialization_seaorm_cli_not_sqlite"]=handlebars::JsonValue::String(t!("initialization_seaorm_cli_not_sqlite").replace(r"\n", "\n"));
data["initialization_diesel_cli_not_sqlite"]=handlebars::JsonValue::String(t!("initialization_diesel_cli_not_sqlite").replace(r"\n", "\n"));
data["initialization_rbatis_cli_not_sqlite"]=handlebars::JsonValue::String(t!("initialization_rbatis_cli_not_sqlite").replace(r"\n", "\n"));
data["sea_orm_cli_website"]=handlebars::JsonValue::String(t!("sea_orm_cli_website").replace(r"\n", "\n"));
data["diesel_cli_website"]=handlebars::JsonValue::String(t!("diesel_cli_website").replace(r"\n", "\n"));
data["mongodb_usage_import_user_data"]=handlebars::JsonValue::String(t!("mongodb_usage_import_user_data").replace(r"\n", "\n"));
render_and_write_to_file(&handlebars, include_str!("../template/README.md"), &data, project_path.join("README.md"))?;
Ok(())
}

Expand Down
98 changes: 98 additions & 0 deletions src/utils/directory2md.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use anyhow::anyhow;
use anyhow::Result;
use once_cell::sync::Lazy;
use rust_i18n::t;
use std::collections::HashMap;
use std::fmt::Write;
use std::fs::{self};
use std::path::Path;
use walkdir::WalkDir;

static PATH_DESCRIPTIONS: Lazy<HashMap<String, String>> = Lazy::new(|| {
let mut m = HashMap::new();
m.insert("Cargo.toml".to_string(), t!("cargo_toml"));
m.insert(".env".to_string(), t!("dot_env"));
m.insert("config/config.toml".to_string(), t!("config_config_toml"));
m.insert("migrations".to_string(), t!("migrations"));
m.insert("migration".to_string(), t!("migrations"));
m.insert("config".to_string(), t!("config"));
m.insert("config/certs".to_string(), t!("config_certs"));
m.insert("templates".to_string(), t!("templates"));
m.insert("data".to_string(), t!("data"));
m.insert("assets".to_string(), t!("assets"));
m.insert("src".to_string(), t!("src"));
m.insert("src/app_response.rs".to_string(), t!("src_app_response_rs"));
m.insert("src/routers".to_string(), t!("src_routers"));
m.insert("src/middleware".to_string(), t!("src_middleware"));
m.insert("src/utils".to_string(), t!("src_utils"));
m.insert("src/dtos".to_string(), t!("src_dtos"));
m.insert("src/entities".to_string(), t!("src_entities"));
m.insert("src/models".to_string(), t!("src_entities"));
m.insert("src/services".to_string(), t!("src_services"));
m.insert("src/config.rs".to_string(), t!("src_config_rs"));
m.insert("src/app_error.rs".to_string(), t!("src_app_error_rs"));
m.insert("src/main.rs".to_string(), t!("src_main_rs"));
m
});

pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result<String> {
let mut output = String::new();
let project_name = output_file
.parent()
.ok_or(anyhow!("Parent directory not found."))?
.file_name()
.ok_or(anyhow!("Project name not found."))?
.to_string_lossy();

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
.strip_prefix(
output_file
.parent()
.ok_or(anyhow!("Parent directory not found."))?,
)?
.to_string_lossy()
.into_owned();
let description = PATH_DESCRIPTIONS.get(&*full_path);
let description = description
.map(|s| format!(" ({})", s))
.unwrap_or_default();
if metadata.is_dir() {
writeln!(
output,
"{}- **{}:** {} {}",
indent,
t!("dir"),
file_name_str,
description
)?;
} else {
writeln!(
output,
"{}- *{}:* {} {}",
indent,
t!("file"),
file_name_str,
description
)?;
}
}
}
Ok(output)
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod check_for_updates;
pub mod create_project;
mod directory2md;
pub mod get_selection;
mod print_util;
mod restricted_names;
Expand Down

0 comments on commit 3ef5ea2

Please sign in to comment.