Add a puffin add command (#117)

This needs far better error handling and user-facing feedback, but it
does the basic operation (and includes discovery of the `pyproject.toml`
file, etc.).
This commit is contained in:
Charlie Marsh 2023-10-18 00:51:20 -04:00 committed by GitHub
parent 339553e228
commit 4c87a1d42c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 292 additions and 0 deletions

View file

@ -20,6 +20,7 @@ puffin-installer = { path = "../puffin-installer" }
puffin-interpreter = { path = "../puffin-interpreter" }
puffin-package = { path = "../puffin-package" }
puffin-resolver = { path = "../puffin-resolver" }
puffin-workspace = { path = "../puffin-workspace" }
anyhow = { workspace = true }
bitflags = { workspace = true }

View file

@ -0,0 +1,29 @@
use anyhow::Result;
use tracing::info;
use crate::commands::ExitStatus;
use crate::printer::Printer;
/// Add a dependency to the workspace.
pub(crate) fn add(name: &str, _printer: Printer) -> Result<ExitStatus> {
// Locate the workspace.
let Some(workspace_root) = puffin_workspace::find_pyproject_toml(std::env::current_dir()?)
else {
return Err(anyhow::anyhow!(
"Could not find a `pyproject.toml` file in the current directory or any of its parents"
));
};
info!("Found workspace at: {}", workspace_root.display());
// Parse the manifest.
let mut manifest = puffin_workspace::Workspace::try_from(workspace_root.as_path())?;
// Add the dependency.
manifest.add_dependency(name)?;
// Write the manifest back to disk.
manifest.save(&workspace_root)?;
Ok(ExitStatus::Success)
}

View file

@ -1,6 +1,7 @@
use std::process::ExitCode;
use std::time::Duration;
pub(crate) use add::add;
pub(crate) use clean::clean;
pub(crate) use compile::compile;
pub(crate) use freeze::freeze;
@ -8,6 +9,7 @@ pub(crate) use sync::{sync, SyncFlags};
pub(crate) use uninstall::uninstall;
pub(crate) use venv::venv;
mod add;
mod clean;
mod compile;
mod freeze;

View file

@ -45,6 +45,8 @@ enum Commands {
Uninstall(UninstallArgs),
/// Create a virtual environment.
Venv(VenvArgs),
/// Add a dependency to the workspace.
Add(AddArgs),
}
#[derive(Args)]
@ -83,6 +85,12 @@ struct VenvArgs {
name: PathBuf,
}
#[derive(Args)]
struct AddArgs {
/// The name of the package to add.
name: String,
}
#[tokio::main]
async fn main() -> ExitCode {
let cli = Cli::parse();
@ -153,6 +161,7 @@ async fn main() -> ExitCode {
.await
}
Commands::Venv(args) => commands::venv(&args.name, args.python.as_deref(), printer).await,
Commands::Add(args) => commands::add(&args.name, printer),
};
match result {