-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e5999a
commit 6914f33
Showing
69 changed files
with
214 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::utils; | ||
|
||
pub fn init_repository(dir: &Path) -> Result<()> { | ||
if !dir.join(".git").exists() { | ||
// Temporary fix to work around bug in libgit2 when creating a | ||
// directory in the root of a posix filesystem. | ||
// See: https://github.com/libgit2/libgit2/issues/5130 | ||
utils::create_dir_all(dir)?; | ||
git2::Repository::init(dir)?; | ||
write_ignore_file(dir)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn write_ignore_file(project_path: &Path) -> Result<()> { | ||
let fp_ignore = project_path.join(".gitignore"); | ||
let mut fp_ignore_file = File::create(fp_ignore)?; | ||
fp_ignore_file.write_all(b"/target\n/migration/target")?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::ffi::{OsStr, OsString}; | ||
use std::path::Path; | ||
use std::{env, slice}; | ||
|
||
use anyhow::{Context, Result}; | ||
use rust_i18n::t; | ||
|
||
use crate::printer::{self, success, warning}; | ||
use crate::namer; | ||
use crate::NewCmd; | ||
|
||
pub fn create(new_cmd: &NewCmd) -> Result<()> { | ||
check_name(&new_cmd.project_name)?; | ||
let project_name = &new_cmd.project_name; | ||
let project_path = Path::new(project_name); | ||
if project_path.exists() { | ||
anyhow::bail!(t!( | ||
"error_project_path_exist", | ||
path = project_path.to_string_lossy() | ||
)) | ||
} | ||
|
||
check_path(project_path)?; | ||
crate::templates::classic::generate(new_cmd)?; | ||
after_print_info(project_name); | ||
Ok(()) | ||
} | ||
|
||
fn after_print_info(project_name: &String) { | ||
println!(); // a new line | ||
success(t!("create_info", project_name = project_name).replace(r"\n", "\n")); | ||
success(t!("create_success").replace(r"\n", "\n")); | ||
success(t!("rust_version_tip")); | ||
println!(); // a new line | ||
} | ||
|
||
fn check_name(name: &str) -> Result<()> { | ||
namer::validate_package_name(name, "package name")?; | ||
|
||
if namer::is_keyword(name) { | ||
anyhow::bail!(t!("error_is_keyword", name = name)); | ||
} | ||
if namer::is_conflicting_artifact_name(name) { | ||
warning(t!("error_is_conflicting_artifact_name", name = name).replace(r"\n", "\n")); | ||
} | ||
if name == "test" { | ||
anyhow::bail!(t!("error_equal_test").replace(r"\n", "\n")) | ||
} | ||
if ["core", "std", "alloc", "proc_macro", "proc-macro"].contains(&name) { | ||
warning(t!("error_part_of_standard_library", name = name,).replace(r"\n", "\n")); | ||
} | ||
if namer::is_windows_reserved(name) { | ||
if cfg!(windows) { | ||
anyhow::bail!(t!("error_is_windows_reserved", name = name),); | ||
} else { | ||
warning(t!("warning_is_windows_reserved", name = name).replace(r"\n", "\n")); | ||
} | ||
} | ||
if namer::is_non_ascii_name(name) { | ||
warning(t!("warning_is_non_ascii_name", name = name).replace(r"\n", "\n")); | ||
} | ||
Ok(()) | ||
} | ||
fn check_path(path: &Path) -> Result<()> { | ||
// warn if the path contains characters that will break `env::join_paths` | ||
if join_paths(slice::from_ref(&OsStr::new(path)), "").is_err() { | ||
let path = path.to_string_lossy(); | ||
printer::warning(t!("warning_invalid_path", path = path)); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn join_paths<T: AsRef<OsStr>>(paths: &[T], env: &str) -> Result<OsString> { | ||
env::join_paths(paths.iter()).with_context(|| { | ||
let mut message = t!("erroe_join_paths", env = env).replace(r"\n", "\n"); | ||
for path in paths { | ||
use std::fmt::Write; | ||
write!(&mut message, "\n {:?}", Path::new(path)).unwrap(); | ||
} | ||
message | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.