use std::io::Write;
use clap::Args;
use futures::future::join_all;
use tokio::runtime::Runtime;
use crate::debugln;
use crate::error::*;
use crate::sess::{Session, SessionIo};
#[derive(Args, Debug)]
pub struct PathArgs {
#[arg(num_args(1..))]
pub name: Vec<String>,
#[arg(long)]
pub checkout: bool,
}
pub fn run(sess: &Session, args: &PathArgs) -> Result<()> {
let ids = args
.name
.iter()
.map(|n| Ok((n, sess.dependency_with_name(&n.to_lowercase())?)))
.collect::<Result<Vec<_>>>()?;
let io = SessionIo::new(sess);
let paths = ids
.iter()
.map(|&(_, id)| io.get_package_path(id))
.collect::<Vec<_>>();
if args.checkout || !paths.iter().all(|p| p.exists()) {
debugln!("main: obtain checkouts {:?}", ids);
let rt = Runtime::new()?;
let _checkouts = rt
.block_on(join_all(
ids.iter()
.map(|&(_, id)| io.checkout(id, false, &[]))
.collect::<Vec<_>>(),
))
.into_iter()
.collect::<Result<Vec<_>>>()?;
debugln!("main: checkouts {:#?}", _checkouts);
}
for c in paths {
if let Some(s) = c.to_str() {
let _ = writeln!(std::io::stdout(), "{}", s);
}
}
Ok(())
}