From bee822fe32328cb70324a2bd35caa77fbbdf8309 Mon Sep 17 00:00:00 2001 From: DataHearth Date: Wed, 7 Dec 2022 19:53:56 +0000 Subject: [PATCH] fix: correct complete flag --- .gitignore | 3 +- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 18 ++++++----- src/pyproject.rs | 8 ++--- 4 files changed, 98 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index c41cc9e..cf162c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target \ No newline at end of file +/target +/my_project \ No newline at end of file diff --git a/README.md b/README.md index b801f19..c4ac4f3 100644 --- a/README.md +++ b/README.md @@ -1 +1,83 @@ # pynit + +`pynit` speed up the process of creating a new python project. It can initialise the project with `git`, +a virtual environment (using the `venv` module) and creating a [basic folder structure](https://setuptools.pypa.io/en/latest/userguide/package_discovery.html) if wanted. + +## Usage + +```bash +Small CLI tool to initialize a python project + +Usage: pynit [OPTIONS] + +Commands: + new + init + help Print this message or the help of the given subcommand(s) + +Options: + -v, --verbose Output will be more verbose + -c, --complete Initialize with minimal fields for "pyproject.toml" + --git Initialize folder with a git repository + --venv Initialize a new virtual environment with given name in initialized directory + --layout Define a layout for your project (https://setuptools.pypa.io/en/latest/userguide/package_discovery.html) [possible values: src, flat] + -h, --help Print help information + -V, --version Print version information +``` + +2 subcommands are available: `new` and `init`. Each of them have the same flags (defined globally). + +### Global flags + +#### --verbose + +Adding this flag will make the `STDOUT` more verbose. + +NOTE: the flag doesn't do anything currently as logging is not yet implemented. + +#### -c/--complete + +`--complete` allows you to control how much questions will be asked during the initialisation of the `pyproject.toml`. By default, only required fields will be asked: `build-sytem` section, `project.name` and `project.version`. + +#### --git + +Initialise a git repository + +#### --venv + +Add a virtual environment to your python project with a given `name`. + +#### --layout + +Add a default popular folder structure to your project. +Two options are available: `flat` and `src`. + +For more information, check out [this setuptools section](https://setuptools.pypa.io/en/latest/userguide/package_discovery.html). + +### new + +`new` acts like `cargo new`. It take one argument which is the project name. Project name will be the folder name and used as default when asking questions to create a basic `pyproject.toml`. + +The directory must NOT exist before creating the project. + +#### Examples + +```bash +$ pynit --git --venv .env --layout flat new my_project +$ exa -a --tree --level=2 my_project +my_project +├── .env +│ ├── ... +├── .git +│ ├── ... +├── .gitignore +├── my_project +│ └── __init__.py +└── pyproject.toml +``` + +### init + +Like `new`, `init` does the same thing. Except it'll initiase the current directory. + +NOTE: if a `.gitignore` and/or a `pyproject.toml` is present, they'll be truncated. diff --git a/src/main.rs b/src/main.rs index 0df4f1d..b3063ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::{ env, fs::{self, File}, path::PathBuf, - process::Command, + process::{Command, Stdio}, }; use anyhow::Result; @@ -25,11 +25,11 @@ struct Args { verbose: bool, /// Initialize with minimal fields for "pyproject.toml" - #[arg(short, long, default_value_t = true)] - minimum: bool, + #[arg(short, long)] + complete: bool, /// Initialize folder with a git repository - #[arg(long, default_value_t = true)] + #[arg(long)] git: bool, /// Initialize a new virtual environment with given name in initialized directory @@ -58,12 +58,12 @@ fn main() -> Result<()> { fs::create_dir(&folder)?; let folder = folder.canonicalize()?; - initialize_folder(folder, args.minimum, args.layout, args.venv, args.git)?; + initialize_folder(folder, args.complete, args.layout, args.venv, args.git)?; } Subcommands::Init {} => { let folder = env::current_dir()?; - initialize_folder(folder, args.minimum, args.layout, args.venv, args.git)?; + initialize_folder(folder, args.complete, args.layout, args.venv, args.git)?; } }; @@ -72,13 +72,13 @@ fn main() -> Result<()> { fn initialize_folder( folder: PathBuf, - minimum: bool, + complete: bool, layout: Option, venv: Option, git: bool, ) -> Result<()> { // todo: avoid clone and maybe find a better way - let mut pypro = Pyproject::new(folder.clone(), minimum); + let mut pypro = Pyproject::new(folder.clone(), complete); pypro.ask_inputs()?; let project_name = pypro.get_project_name(); @@ -107,12 +107,14 @@ fn initialize_folder( Command::new("python3") .args(&["-m", "venv", &venv]) .current_dir(&folder) + .stdout(Stdio::null()) .status()?; } if git { Command::new("git") .args(&["init"]) .current_dir(&folder) + .stdout(Stdio::null()) .status()?; fs::write( folder.join(".gitignore"), diff --git a/src/pyproject.rs b/src/pyproject.rs index ff096a1..c8cc2aa 100644 --- a/src/pyproject.rs +++ b/src/pyproject.rs @@ -13,16 +13,16 @@ pub struct Pyproject { #[serde(skip_serializing)] folder: PathBuf, #[serde(skip_serializing)] - minimum: bool, + complete: bool, pub build_system: BuildSystem, pub project: Project, } impl Pyproject { - pub fn new(folder: PathBuf, minimum: bool) -> Self { + pub fn new(folder: PathBuf, complete: bool) -> Self { Pyproject { folder, - minimum, + complete, build_system: BuildSystem::default(), project: Project::default(), } @@ -72,7 +72,7 @@ impl Pyproject { .default("0.1.0".to_string()) .interact_text()?; - if !self.minimum { + if self.complete { self.project.description = Input::with_theme(&theme) .with_prompt("description") .allow_empty(true)