diff --git a/API.md b/API.md index e3d6a6c..f9e780c 100644 --- a/API.md +++ b/API.md @@ -5,13 +5,14 @@ Usage: ``` - pyflow new + pyflow new ``` Arguments: - * [] `--src` - Creates `src` folder instead of `` folder + * [x] `--name` - Creates project with the given `` inside `` folder, so these identifiers can be different. + * [] `--src` - Creates `src` folder instead of `` folder. * [] `init` - Create a `pyproject.toml` from [] requirements.txt, [] pipfile, [] setup.py, etc. To be run inside project folder with these files. diff --git a/src/actions/new.rs b/src/actions/new.rs index f99b2a1..fed82d3 100644 --- a/src/actions/new.rs +++ b/src/actions/new.rs @@ -32,8 +32,8 @@ Problem creating the project. This may be due to a permissions problem. If on linux, please try again with `sudo`. "#}; -pub fn new(name: &str) { - if new_internal(name).is_err() { +pub fn new(path: &str, name: &str) { + if new_internal(path, name).is_err() { abort(NEW_ERROR_MESSAGE); } success(&format!("Created a new Python project named {}", name)) @@ -41,18 +41,19 @@ pub fn new(name: &str) { // TODO: Join this function after refactoring /// Create a template directory for a python project. -fn new_internal(name: &str) -> Result<(), Box> { +fn new_internal(path: &str, name: &str) -> Result<(), Box> { + let normalized_name = name.replace("-", "_"); if !PathBuf::from(name).exists() { - fs::create_dir_all(&format!("{}/{}", name, name.replace("-", "_")))?; - fs::File::create(&format!("{}/{}/__init__.py", name, name.replace("-", "_")))?; - fs::File::create(&format!("{}/README.md", name))?; - fs::File::create(&format!("{}/.gitignore", name))?; + fs::create_dir_all(&format!("{}/{}", path, normalized_name))?; + fs::File::create(&format!("{}/{}/__init__.py", path, normalized_name))?; + fs::File::create(&format!("{}/README.md", path))?; + fs::File::create(&format!("{}/.gitignore", path))?; } let readme_init = &format!("# {}\n\n{}", name, "(A description)"); - fs::write(&format!("{}/.gitignore", name), GITIGNORE_INIT)?; - fs::write(&format!("{}/README.md", name), readme_init)?; + fs::write(&format!("{}/.gitignore", path), GITIGNORE_INIT)?; + fs::write(&format!("{}/README.md", path), readme_init)?; let cfg = Config { name: Some(name.to_string()), @@ -61,9 +62,9 @@ fn new_internal(name: &str) -> Result<(), Box> { ..Default::default() }; - cfg.write_file(&PathBuf::from(format!("{}/pyproject.toml", name))); + cfg.write_file(&PathBuf::from(format!("{}/pyproject.toml", path))); - if commands::git_init(Path::new(name)).is_err() { + if commands::git_init(Path::new(path)).is_err() { util::print_color( "Unable to initialize a git repo for your project", Color::Yellow, // Dark diff --git a/src/cli_options.rs b/src/cli_options.rs index cd060ee..d7564ab 100644 --- a/src/cli_options.rs +++ b/src/cli_options.rs @@ -15,11 +15,13 @@ pub struct Opt { #[derive(StructOpt, Debug)] pub enum SubCommand { - /// Create a project folder with the basics + /// Create a project folder with the basics file structure #[structopt(name = "new")] New { - #[structopt(name = "name")] - name: String, // holds the project name. + #[structopt(name = "path")] + path: String, // holds the project folder. + #[structopt(long)] + name: Option, // holds the project name. }, /// Add packages to `pyproject.toml` and sync an environment diff --git a/src/main.rs b/src/main.rs index f5e836d..c14cbd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,7 +94,13 @@ fn main() { match &subcmd { // Actions requires nothing to know about the project - SubCommand::New { name } => actions::new(name), + SubCommand::New { path, name } => { + // if name is not provided, use the directory name + match name { + Some(name) => actions::new(path, name), + None => actions::new(path, path) + } + }, SubCommand::Init => actions::init(CFG_FILENAME), SubCommand::Reset {} => actions::reset(), SubCommand::Clear {} => actions::clear(&pyflow_path, &dep_cache_path, &script_env_path), diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs new file mode 100644 index 0000000..e69de29