use std::{str::FromStr, sync::Arc};
use eyre::{Context, Result};
use lux_lib::{
config::Config,
git::shorthand::RemoteGitUrlShorthand,
lua_version::LuaVersion,
operations::Sync,
package::PackageReq,
progress::{MultiProgress, Progress},
project::Project,
tree::Tree,
};
#[derive(Debug, Clone)]
pub enum PackageReqOrGitShorthand {
PackageReq(PackageReq),
GitShorthand(RemoteGitUrlShorthand),
}
impl FromStr for PackageReqOrGitShorthand {
type Err = eyre::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match RemoteGitUrlShorthand::parse_with_prefix(s) {
Ok(shorthand) => Ok(Self::GitShorthand(shorthand)),
Err(_) => Ok(Self::PackageReq(PackageReq::parse(s)?)),
}
}
}
pub fn current_project_or_user_tree(config: &Config) -> Result<Tree> {
let project = Project::current()?;
Ok(match &project {
Some(project) => project.tree(config)?,
None => {
let lua_version = LuaVersion::from(config)?.clone();
config.user_tree(lua_version)?
}
})
}
pub async fn sync_dependencies_if_locked(
project: &Project,
progress: Arc<Progress<MultiProgress>>,
config: &Config,
) -> Result<()> {
Sync::new(project, config)
.progress(progress)
.sync_dependencies()
.await
.wrap_err("syncing dependencies with the project lockfile failed.")?;
Ok(())
}
pub async fn sync_build_dependencies_if_locked(
project: &Project,
progress: Arc<Progress<MultiProgress>>,
config: &Config,
) -> Result<()> {
Sync::new(project, config)
.progress(progress.clone())
.sync_build_dependencies()
.await
.wrap_err("syncing build dependencies with the project lockfile failed.")?;
Ok(())
}
pub async fn sync_test_dependencies_if_locked(
project: &Project,
progress: Arc<Progress<MultiProgress>>,
config: &Config,
) -> Result<()> {
Sync::new(project, config)
.progress(progress.clone())
.sync_test_dependencies()
.await
.wrap_err("syncing test dependencies with the project lockfile failed.")?;
Ok(())
}