Skip to content

Commit

Permalink
feat: prepare git and gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Aug 2, 2024
2 parents 456edaa + bf78192 commit a51faa3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "psp"
version = "0.0.1"
version = "0.0.2"
edition = "2021"
authors = ["matteoguadrini"]

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

```console
psp # Press Enter
Welcome to PSP (Python Scaffolding Projects): 0.0.1
Welcome to PSP (Python Scaffolding Projects): 0.0.2
> Name of Python project: test
> Do you want start git repository? Yes
Project `test` created
```

> This project is WIP
Expand Down Expand Up @@ -34,7 +36,7 @@ cd psp && cargo build && sudo cp -var target/debug/psp /usr/bin/psp
## Features

- [x] Scaffolding file and folder structures for your Python project
- [ ] Prepare git and gitignore
- [x] Prepare git and gitignore
- [ ] Prepare unit test files (also with pytest)
- [ ] Prepare virtual environment
- [ ] Install dependencies
Expand Down
85 changes: 81 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use inquire::Text;
use inquire::{Confirm, Text};
use std::{
fs::{create_dir_all, File},
io::Write,
Expand All @@ -7,7 +7,7 @@ use std::{
};

// Constants
const VERSION: &str = "0.0.1";
const VERSION: &str = "0.0.2";

// Utility functions

Expand Down Expand Up @@ -56,13 +56,28 @@ fn prompt_text(question: &str, default: &str, help: &str) -> String {
answer.unwrap().to_string()
}

fn prompt_confirm(question: &str, default: bool, help: &str) -> bool {
let answer = if help != "None" {
Confirm::new(question)
.with_default(default)
.with_help_message(help)
.prompt()
} else {
Confirm::new(question).with_default(default).prompt()
};
answer.unwrap()
}

// Core functions

// Project name
fn prj_name() -> String {
let name = prompt_text("Name of Python project:", "prj", "None");
let root = format!("{name}");
let package = format!("{}", name.to_lowercase());
let project = format!("{root}/{package}");
// Make directories structure
let dir_ret = make_dirs(format!("{name}/{name}").as_str());
let dir_ret = make_dirs(format!("{project}").as_str());
match dir_ret {
Err(e) => {
eprintln!("error: {}", e);
Expand All @@ -72,7 +87,7 @@ fn prj_name() -> String {
}
// Make file structures
let file_ret = make_file(
format!("{name}/{name}/__init__.py").as_str(),
format!("{project}/__init__.py").as_str(),
"#! /usr/env python3\n\n".to_string(),
);
match file_ret {
Expand All @@ -85,13 +100,75 @@ fn prj_name() -> String {
name
}

fn prj_git(name: &str) {
let confirm = prompt_confirm("Do you want start git repository?", true, "None");
if confirm {
let output = std::process::Command::new("git")
.arg("init")
.current_dir(name)
.output()
.expect("error: something wrong with `git init`");
// Check if command exit successfully
if !output.status.success() {
exit(5)
}
// Create .gitignore file
let file_ret = make_file(
format!("{name}/.gitignore").as_str(),
"### Python ###\n\
__pycache__/\n\
*.py[cod]\n\
*$py.class\n\
build/\n\
develop-eggs/\n\
dist/\n\
downloads/\n\
eggs/\n\
.eggs/\n\
lib/\n\
lib64/\n\
parts/\n\
sdist/\n\
var/\n\
wheels/\n\
share/python-wheels/\n\
*.egg-info/\n\
.installed.cfg\n\
*.egg\n\
# Environments\n\
.env\n\
.venv\n\
env/\n\
venv/\n\
ENV/\n\
env.bak/\n\
venv.bak/\n\
# Sphinx documentation\n\
docs/_build/\n\
# mkdocs documentation\n\
/site\n"
.to_string(),
);
match file_ret {
Err(e) => {
eprintln!("error: {}", e);
exit(5);
}
Ok(_) => (),
}
}
}

fn main() {
// Print welcome screen and version
println!("Welcome to PSP (Python Scaffolding Projects): {VERSION}");
// Check if Python 3 is installed
check_tool("/usr/bin/python3");
// Create project structure by name
let name = prj_name();
// Start git
check_tool("/usr/bin/git");
prj_git(&name);
// Finish scaffolding process
println!("Project `{name}` created")
}

0 comments on commit a51faa3

Please sign in to comment.