use crate::cli::UpdateCommand;
use anyhow::{anyhow, Result};
use forc_pkg::{self as pkg, lock, Lock};
use forc_util::lock_path;
use pkg::manifest::ManifestFile;
use std::{fs, path::PathBuf};
use tracing::info;
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 = ManifestFile::from_dir(&this_dir)?;
let lock_path = lock_path(manifest.dir());
let old_lock = Lock::from_path(&lock_path).ok().unwrap_or_default();
let offline = false;
let member_manifests = manifest.member_manifests()?;
let ipfs_node = command.ipfs_node.unwrap_or_default();
let new_plan = pkg::BuildPlan::from_manifests(&member_manifests, offline, ipfs_node)?;
let new_lock = Lock::from_graph(new_plan.graph());
let diff = new_lock.diff(&old_lock);
let member_names = member_manifests
.values()
.map(|manifest| manifest.project.name.clone())
.collect();
lock::print_diff(&member_names, &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))?;
info!(" Created new lock file at {}", lock_path.display());
} else {
info!(" `--check` enabled: `Forc.lock` was not changed");
}
Ok(())
}