Skip to content

Commit

Permalink
feat: scaffolding Python project files and folders
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Jul 31, 2024
2 parents 9995e62 + 2902db3 commit 456edaa
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 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.1.0"
version = "0.0.1"
edition = "2021"
authors = ["matteoguadrini"]

Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,36 @@

```console
psp # Press Enter
Welcome to PSP (Python Scaffolding Projects): 0.0.1
> Name of Python project: test
```

> This project is WIP
## Installation

To install compiled file into your machine, download it:

```console
# For Linux
sudo curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.1/psp_linux > /usr/bin/psp
sudo chmod +x /usr/bin/psp

# For MacOS
sudo curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.1/psp_macos > /usr/bin/psp
sudo chmod +x /usr/bin/psp
```

Instead, if you compile this project as own, follow this steps:

```console
git clone https://github.com/MatteoGuadrini/psp.git
cd psp && cargo build && sudo cp -var target/debug/psp /usr/bin/psp
```

## Features

- [ ] Scaffold file and folder structures for your Python project
- [x] Scaffolding file and folder structures for your Python project
- [ ] Prepare git and gitignore
- [ ] Prepare unit test files (also with pytest)
- [ ] Prepare virtual environment
Expand All @@ -22,6 +45,7 @@ psp # Press Enter
- [ ] Prepare tox environment
- [ ] Prepare docs folder for sphinx/mkdocs documentation
- [ ] Prepare README, LICENSE, CONTRIBUTING, CODE_OF_CONDUCT and CHANGES files
- [ ] Add _quick_ argument for rapid configuration

## Open source
_psp_ is an open source project. Any contribute, It's welcome.
Expand Down Expand Up @@ -58,6 +82,8 @@ Thanks to Jim Blandy, Jason Orendorff and Nora Tindall for writing the _Program

Thanks to Tim McNamara for writing the _Rust in Action_ book.

Thanks to [Zed IDE](https://zed.dev/) and for license of [RustRover](https://www.jetbrains.com/rust/) offered by Jetbrains.

Special thanks go to my wife, who understood the hours of absence for this development.
Thanks to my children, for the daily inspiration they give me and to make me realize, that life must be simple.

Expand Down
97 changes: 95 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,97 @@
use inquire::Text;
use std::{
fs::{create_dir_all, File},
io::Write,
path::Path,
process::exit,
};

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

// Utility functions

// Function for check if tool is installed
fn check_tool(tool: &str) {
if !Path::new(tool).exists() {
eprintln!("error: the tool {} not installed", tool);
exit(1);
}
}

// Function for creating folders and parents
fn make_dirs(dir: &str) -> std::io::Result<()> {
let result = create_dir_all(dir);
match result {
Err(e) => {
eprintln!("error: {}", e);
exit(2);
}
Ok(_) => Ok(()),
}
}

// Function for creating file with contents
fn make_file(file: &str, content: String) -> std::io::Result<()> {
let mut file = File::create(file)?;
let result = file.write_all(&content.as_bytes());
match result {
Err(e) => {
eprintln!("error: {}", e);
exit(3);
}
Ok(_) => Ok(()),
}
}

// Function for prompt text
fn prompt_text(question: &str, default: &str, help: &str) -> String {
let answer = if default != "None" {
Text::new(question).with_default(default).prompt()
} else if help != "None" {
Text::new(question).with_help_message(help).prompt()
} else {
Text::new(question).prompt()
};
answer.unwrap().to_string()
}

// Core functions

// Project name
fn prj_name() -> String {
let name = prompt_text("Name of Python project:", "prj", "None");
// Make directories structure
let dir_ret = make_dirs(format!("{name}/{name}").as_str());
match dir_ret {
Err(e) => {
eprintln!("error: {}", e);
exit(4);
}
Ok(_) => (),
}
// Make file structures
let file_ret = make_file(
format!("{name}/{name}/__init__.py").as_str(),
"#! /usr/env python3\n\n".to_string(),
);
match file_ret {
Err(e) => {
eprintln!("error: {}", e);
exit(4);
}
Ok(_) => (),
}
name
}

fn main() {
println!("Hello by PSP (Python Scaffolding Projects!)");
println!("This project is a WIP! Be careful!")
// 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();
// Finish scaffolding process
println!("Project `{name}` created")
}

0 comments on commit 456edaa

Please sign in to comment.