Add new 'deps add' command

This makes it easier to add new dependencies, without having to
  manually edit the `aiken.toml` file.

  The command is accessible via two different paths:

  - aiken deps add

  or simply

  - aiken add

  for this is quite common to find at the top-level of the command-line,
  and, we still want to keep commands for managing dependencies grouped
  under a command sub-group and not all at the top-level. So we're
  merely promoting that one for visibility.
This commit is contained in:
KtorZ
2023-01-14 23:00:44 +01:00
parent d4f905b1db
commit 38df9a586e
8 changed files with 135 additions and 20 deletions

View File

@@ -0,0 +1,71 @@
use aiken_project::{
config::{Config, Dependency, Platform},
error::Warning,
package_name::PackageName,
pretty,
};
use miette::IntoDiagnostic;
use owo_colors::OwoColorize;
use std::{path::PathBuf, process, str::FromStr};
#[derive(clap::Args)]
/// Add a new project package as dependency
pub struct Args {
/// Package name, in the form of {owner}/{repository}.
///
/// For example → 'add aiken-lang/stdlib'
///
/// Note that by default, this assumes the package is located
/// on Github.
package: String,
/// The package version, as a git commit hash, a tag or a branch name.
#[clap(long)]
version: String,
}
pub fn exec(args: Args) -> miette::Result<()> {
let root = PathBuf::from(".");
let dependency = Dependency {
name: PackageName::from_str(&args.package)?,
version: args.version,
source: Platform::Github,
};
let config = match Config::load(&root) {
Ok(config) => config,
Err(e) => {
e.report();
process::exit(1);
}
};
println!(
"{} {}",
pretty::pad_left("Adding".to_string(), 13, " ")
.bold()
.purple(),
dependency.name.bright_blue(),
);
match config.insert(&dependency, false) {
Some(config) => {
config.save(&root).into_diagnostic()?;
println!(
"{} version = {}",
pretty::pad_left("Added".to_string(), 13, " ")
.bold()
.purple(),
dependency.version.yellow()
);
Ok(())
}
None => {
let warning = Warning::DependencyAlreadyExists {
name: dependency.name,
};
warning.report();
process::exit(1)
}
}
}

View File

@@ -1,3 +1,4 @@
pub mod add;
pub mod clear_cache;
use clap::Subcommand;
@@ -6,12 +7,16 @@ use clap::Subcommand;
#[derive(Subcommand)]
#[clap(setting(clap::AppSettings::DeriveDisplayOrder))]
pub enum Cmd {
/// Add a new dependency
Add(add::Args),
/// Clear the system-wide dependencies cache
ClearCache,
}
pub fn exec(cmd: Cmd) -> miette::Result<()> {
match cmd {
Cmd::Add(args) => add::exec(args),
Cmd::ClearCache => clear_cache::exec(),
}
}

View File

@@ -29,6 +29,8 @@ pub fn exec(args: Args) -> miette::Result<()> {
}
fn create_project(args: Args, package_name: &PackageName) -> miette::Result<()> {
package_name.restrict().into_diagnostic()?;
let root = PathBuf::from(&package_name.repo);
if root.exists() {

View File

@@ -1,4 +1,8 @@
use aiken::cmd::{build, check, deps, docs, fmt, lsp, new, tx, uplc};
use aiken::cmd::{
build, check,
deps::{self, add},
docs, fmt, lsp, new, tx, uplc,
};
use clap::Parser;
/// Aiken: a smart-contract language and toolchain for Cardano
@@ -12,6 +16,7 @@ pub enum Cmd {
Build(build::Args),
Check(check::Args),
Docs(docs::Args),
Add(add::Args),
#[clap(subcommand)]
Deps(deps::Cmd),
@@ -38,9 +43,10 @@ fn main() -> miette::Result<()> {
Cmd::New(args) => new::exec(args),
Cmd::Fmt(args) => fmt::exec(args),
Cmd::Build(args) => build::exec(args),
Cmd::Docs(args) => docs::exec(args),
Cmd::Deps(args) => deps::exec(args),
Cmd::Check(args) => check::exec(args),
Cmd::Docs(args) => docs::exec(args),
Cmd::Add(args) => add::exec(args),
Cmd::Deps(args) => deps::exec(args),
Cmd::Lsp(args) => lsp::exec(args),
Cmd::Tx(sub_cmd) => tx::exec(sub_cmd),
Cmd::Uplc(sub_cmd) => uplc::exec(sub_cmd),