Skip to content

Commit

Permalink
wrap up generator
Browse files Browse the repository at this point in the history
  • Loading branch information
elcharitas committed Sep 29, 2024
1 parent bb1f2e1 commit 86ece37
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

21 changes: 21 additions & 0 deletions crates/Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ dialoguer = "0.10"
serde = "1.0"
anyhow = "1"
console = "^0.15.0"
lazy_static = "1"
ramhorns = "1"
exitcode = "^1.1.2"
tracing = "^0.1.34"
tracing-tree = { version = "0.2.1" }
tracing-subscriber = { version = "0.3.1", features = ["env-filter"] }
include_dir = "0.7.4"


[features]
Expand Down
63 changes: 55 additions & 8 deletions crates/cli/src/bin/cmd/new.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use anyhow::Result;
use clap::{Arg, ArgMatches, Command};
use convert_case::Casing;
use include_dir::Dir;
use lazy_static::lazy_static;

lazy_static! {
static ref TEMPLATES: Vec<&'static str> = vec!["basic", "ramhorns", "vercel"];
static ref TEMPLATES_DIR: Vec<Dir<'static>> = vec![
include_dir::include_dir!("./starter-templates/with-basic"),
include_dir::include_dir!("./starter-templates/with-ramhorns"),
include_dir::include_dir!("./starter-templates/with-vercel")
];
}

/// `ngyn new` command
///
Expand Down Expand Up @@ -38,21 +50,56 @@ pub fn command() -> Command {
)
}

pub fn run(matches: &ArgMatches, subcommand_matches: &ArgMatches) -> Result<cargo_ngyn::CmdExit> {
pub fn run(_matches: &ArgMatches, subcommand_matches: &ArgMatches) -> Result<cargo_ngyn::CmdExit> {
if let Some(name) = subcommand_matches.get_one::<String>("name") {
println!("Creating new project: {}", name);
} else {
let name = dialoguer::Input::<String>::new()
.with_prompt("Name of the project to create")
.interact()?;
let force = dialoguer::Confirm::new()
.with_prompt("Force the creation of the project")
.default(false)
.interact()?;
let name = name.to_case(convert_case::Case::Snake);

let template = dialoguer::Select::new()
.with_prompt("Use a template to create the project")
.default(0)
.interact()?;
.with_prompt("Use a template to create the project")
.items(&TEMPLATES)
.default(0)
.interact()?;

let cwd = std::env::current_dir()?;
let project_dir = cwd.join(&name);

let force = if project_dir.exists() {
dialoguer::Confirm::new()
.with_prompt("Project directory already exists. Overwrite?")
.interact()?
} else {
true
};

if !force {
return Ok(cargo_ngyn::CmdExit {
code: exitcode::OK,
message: Some("Project directory already exists. Exiting...".to_string()),
});
}

// safely remove current project directory and create a new one
if project_dir.exists() {
std::fs::remove_dir_all(&project_dir)?;
} else {
std::fs::create_dir_all(&project_dir)?;
}

let template_dir = &TEMPLATES_DIR[template];
let template_files = template_dir.files();

for file in template_files {
let file_path = project_dir.join(file.path());
let file_content = file.contents_utf8().unwrap();

std::fs::create_dir_all(file_path.parent().unwrap())?;
std::fs::write(file_path, file_content)?;
}
}

Ok(cargo_ngyn::CmdExit {
Expand Down

0 comments on commit 86ece37

Please sign in to comment.