use crate::{cli::UpdateCommand, utils::SWAY_GIT_TAG};
use anyhow::{anyhow, Result};
use forc_pkg::{self as pkg, lock, Lock, Manifest};
use forc_util::{find_manifest_dir, lock_path};
use std::{fs, path::PathBuf};
pub async fn update(command: UpdateCommand) -> Result<()> {
let UpdateCommand {
path,
check,
target_dependency: _,
..
} = command;
let this_dir = match path {
Some(path) => PathBuf::from(path),
None => std::env::current_dir()?,
};
let manifest_dir = match find_manifest_dir(&this_dir) {
Some(dir) => dir,
None => {
return Err(anyhow!(
"No manifest file found in this directory or any parent directories of it: {:?}",
this_dir
))
}
};
let manifest = Manifest::from_dir(&manifest_dir, SWAY_GIT_TAG)?;
let lock_path = lock_path(&manifest_dir);
let old_lock = Lock::from_path(&lock_path).ok().unwrap_or_default();
let offline = false;
let new_plan = pkg::BuildPlan::new(&manifest_dir, SWAY_GIT_TAG, offline)?;
let new_lock = Lock::from_graph(new_plan.graph());
let diff = new_lock.diff(&old_lock);
lock::print_diff(&manifest.project.name, &diff);
if !check {
let string = toml::ser::to_string_pretty(&new_lock)
.map_err(|e| anyhow!("failed to serialize lock file: {}", e))?;
fs::write(&lock_path, &string).map_err(|e| anyhow!("failed to write lock file: {}", e))?;
println!(" Created new lock file at {}", lock_path.display());
} else {
println!(" `--check` enabled: `Forc.lock` was not changed");
}
Ok(())
}