Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ darling = "0.21"
deadpool-redis = { version = "0.21", default-features = false }
derive_builder = "0.20"
derive_more = "2"
dialoguer = "0.11.0"
digest = "0.10"
email_address = "0.2.9"
fake = "4"
Expand Down
1 change: 1 addition & 0 deletions cot-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ clap-verbosity-flag = { workspace = true, features = ["tracing"] }
darling.workspace = true
cot.workspace = true
cot_codegen = { workspace = true, features = ["symbol-resolver"] }
dialoguer.workspace = true
glob.workspace = true
heck.workspace = true
hex.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions cot-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub struct ProjectNewArgs {
pub name: Option<String>,
#[command(flatten)]
pub source: CotSourceArgs,
/// Skip interactive prompts and use defaults
#[arg(short, long)]
pub yes: bool,
}

#[derive(Debug, Subcommand)]
Expand Down
39 changes: 38 additions & 1 deletion cot-cli/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,48 @@ use std::path::PathBuf;

use anyhow::Context;
use clap::CommandFactory;
use dialoguer::Confirm;

use crate::args::{
Cli, CompletionsArgs, ManpagesArgs, MigrationListArgs, MigrationMakeArgs, ProjectNewArgs,
};
use crate::migration_generator::{MigrationGeneratorOptions, list_migrations, make_migrations};
use crate::new_project::{CotSource, new_project};
use crate::utils::{StatusType, print_status_msg};

pub fn handle_new_project(
ProjectNewArgs { path, name, source }: ProjectNewArgs,
ProjectNewArgs {
path,
name,
source,
yes,
}: ProjectNewArgs,
) -> anyhow::Result<()> {
let path = path
.canonicalize()
.with_context(|| format!("unable to canonicalize path: {}", path.display()))?;

if path.exists() && path.join("Cargo.toml").exists() {
if !yes {
let confirmed = Confirm::new()
.with_prompt(format!(
"Directory '{}' is not empty. Overwrite?",
path.display()
))
.default(false)
.interact()?;
if !confirmed {
print_status_msg(StatusType::Error, "Aborted: directory not empty");
std::process::exit(0);
}
} else {
print_status_msg(
StatusType::Warning,
&format!("Overwriting existing files in `{}`", path.display()),
);
}
}

let project_name = match name {
None => {
let dir_name = path
Expand All @@ -29,6 +61,10 @@ pub fn handle_new_project(
} else {
CotSource::PublishedCrate
};
print_status_msg(
StatusType::Creating,
&format!("Creating project '{}' in: {}", project_name, path.display()),
);
new_project(&path, &project_name, &cot_source).with_context(|| "unable to create project")
}

Expand Down Expand Up @@ -96,6 +132,7 @@ mod tests {
use_git: false,
cot_path: None,
},
yes: false,
};

let result = handle_new_project(args);
Expand Down
34 changes: 21 additions & 13 deletions cot-cli/src/new_project.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use anyhow::Context;
use heck::ToPascalCase;
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};
Expand Down Expand Up @@ -60,31 +61,38 @@ pub fn new_project(
&format!("Cot project `{project_name}`"),
);

if path.exists() {
anyhow::bail!("destination `{}` already exists", path.display());
if !path.exists() {
std::fs::create_dir_all(path)
.with_context(|| format!("failed to create directory `{}`", path.display()))?;
}

let project_struct_name = format!("{}Project", project_name.to_pascal_case());
let app_name = format!("{}App", project_name.to_pascal_case());
let cot_source = cot_source.as_cargo_toml_source();
let dev_secret_key = generate_secret_key();

for (file_name, content) in PROJECT_FILES {
for (template_name, content) in PROJECT_FILES {
// Cargo reads and parses all files that are named "Cargo.toml" in a repository,
// so we need a different name so that it doesn't fail on build.
let file_name = file_name.replace(".template", "");

let file_path = path.join(file_name);
trace!("Writing file: {:?}", file_path);
let file_name = template_name.replace(".template", "");
let file_path = path.join(&file_name);

if file_path.exists() {
print_status_msg(
StatusType::Warning,
&format!("Overwriting `{}`", file_path.display()),
);
} else {
trace!("Writing file: {:?}", file_path);
}

std::fs::create_dir_all(
file_path
.parent()
.expect("joined path should always have a parent"),
)?;
if let Some(dir) = file_path.parent() {
std::fs::create_dir_all(dir)
.with_context(|| format!("failed to create directory `{}`", dir.display()))?;
}

std::fs::write(
file_path,
&file_path,
content
.replace("{{ project_name }}", project_name)
.replace("{{ project_struct_name }}", &project_struct_name)
Expand Down
1 change: 1 addition & 0 deletions cot-cli/tests/new_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn new_project_compile_test() {
&project_path,
"my_project",
&CotSource::Path(&cot_workspace_path),
false,
)
.unwrap();

Expand Down
Loading