-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scaffolding Python project files and folders
- Loading branch information
Showing
3 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |