pynit/src/main.rs

135 lines
3.1 KiB
Rust
Raw Normal View History

2022-12-07 19:49:39 +01:00
use std::{
env,
fs::{self, File},
path::PathBuf,
2022-12-07 20:53:56 +01:00
process::{Command, Stdio},
2022-12-07 19:49:39 +01:00
};
2022-12-07 00:18:33 +01:00
use anyhow::Result;
use clap::Parser;
2023-06-28 12:05:10 +02:00
use license::get_license;
2022-12-07 19:49:39 +01:00
use pyproject::Pyproject;
2022-12-07 00:18:33 +01:00
2023-06-28 12:05:10 +02:00
mod components;
mod license;
2022-12-07 19:49:39 +01:00
mod pyproject;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
enum Layout {
Src,
Flat,
}
2022-12-07 00:18:33 +01:00
#[derive(clap::Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
2022-12-07 19:49:39 +01:00
/// Output will be more verbose
2022-12-07 00:18:33 +01:00
#[arg(short, long, default_value_t = false)]
verbose: bool,
2022-12-07 19:49:39 +01:00
/// Initialize with minimal fields for "pyproject.toml"
2022-12-07 20:53:56 +01:00
#[arg(short, long)]
complete: bool,
2022-12-07 00:18:33 +01:00
2022-12-07 19:49:39 +01:00
/// Initialize folder with a git repository
2022-12-07 20:53:56 +01:00
#[arg(long)]
2022-12-07 19:49:39 +01:00
git: bool,
/// Initialize a new virtual environment with given name in initialized directory
#[arg(long)]
venv: Option<String>,
/// Define a layout for your project (https://setuptools.pypa.io/en/latest/userguide/package_discovery.html)
#[arg(long, value_enum)]
layout: Option<Layout>,
2022-12-07 00:18:33 +01:00
#[command(subcommand)]
subcommands: Subcommands,
}
#[derive(clap::Subcommand)]
enum Subcommands {
New { folder: PathBuf },
Init {},
}
fn main() -> Result<()> {
let args = Args::parse();
match args.subcommands {
2022-12-07 19:49:39 +01:00
Subcommands::New { folder } => {
fs::create_dir(&folder)?;
let folder = folder.canonicalize()?;
2022-12-07 20:53:56 +01:00
initialize_folder(folder, args.complete, args.layout, args.venv, args.git)?;
2022-12-07 19:49:39 +01:00
}
2022-12-07 00:18:33 +01:00
Subcommands::Init {} => {
2022-12-07 19:49:39 +01:00
let folder = env::current_dir()?;
2022-12-07 20:53:56 +01:00
initialize_folder(folder, args.complete, args.layout, args.venv, args.git)?;
2022-12-07 00:18:33 +01:00
}
};
Ok(())
2022-12-02 19:57:24 +01:00
}
2022-12-07 19:49:39 +01:00
fn initialize_folder(
folder: PathBuf,
2022-12-07 20:53:56 +01:00
complete: bool,
2022-12-07 19:49:39 +01:00
layout: Option<Layout>,
venv: Option<String>,
git: bool,
) -> Result<()> {
// todo: avoid clone and maybe find a better way
2022-12-07 20:53:56 +01:00
let mut pypro = Pyproject::new(folder.clone(), complete);
2022-12-07 19:49:39 +01:00
pypro.ask_inputs()?;
2023-06-28 12:05:10 +02:00
let project_name = pypro.get_project_name();
fs::write(
folder.join("LICENSE"),
get_license(pypro.get_license_spdx())?.body,
)?;
2022-12-07 19:49:39 +01:00
pypro.create_file()?;
if let Some(layout) = layout {
let layout_inner_path = match layout {
Layout::Src => {
let inner_path = folder.join(format!("src/{project_name}"));
fs::create_dir_all(&inner_path)?;
inner_path
}
Layout::Flat => {
let inner_path = folder.join(project_name);
fs::create_dir(&inner_path)?;
inner_path
}
};
File::create(layout_inner_path.join("__init__.py"))?;
}
if let Some(venv) = venv {
Command::new("python3")
.args(&["-m", "venv", &venv])
.current_dir(&folder)
2022-12-07 20:53:56 +01:00
.stdout(Stdio::null())
2022-12-07 19:49:39 +01:00
.status()?;
}
if git {
Command::new("git")
.args(&["init"])
.current_dir(&folder)
2022-12-07 20:53:56 +01:00
.stdout(Stdio::null())
2022-12-07 19:49:39 +01:00
.status()?;
fs::write(
folder.join(".gitignore"),
include_bytes!("gitignore_python.txt"),
)?;
}
Ok(())
}