Skip to content

Commit

Permalink
Only update the lock file if something has changed (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimush authored Oct 31, 2023
1 parent d2a6918 commit 73af2f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
44 changes: 25 additions & 19 deletions src/cli/command_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::info;
use log::{debug, info};

use crate::{
api::LockMode,
Expand Down Expand Up @@ -62,40 +62,46 @@ pub fn do_lock(

let lock_file_path = root.join(lock_file_name);

let lockfile = match (lock_mode, lock_file_path.exists()) {
let (old_lock, lockfile) = match (lock_mode, lock_file_path.exists()) {
(LockMode::Locked, false) => return Err("Lock file does not exist".into()),

(LockMode::Locked, true) => {
let lockfile = LockFile::from_file(&lock_file_path)?;
let resolver = LockFileModuleResolver::new(cache, lockfile, true);
log::debug!("Verifying lockfile...");
fetch::lock(&module_descriptor, &resolver)?
let old_lock = LockFile::from_file(&lock_file_path)?;
let resolver = LockFileModuleResolver::new(cache, &old_lock, true);
debug!("Verifying lockfile...");
let lockfile = fetch::lock(&module_descriptor, &resolver)?;
(Some(old_lock), lockfile)
}

(LockMode::Update, false) => {
log::debug!("Generating lockfile...");
fetch::lock(&module_descriptor, &cache)?
debug!("Generating lockfile...");
(None, fetch::lock(&module_descriptor, &cache)?)
}

(LockMode::Update, true) => {
let lockfile = LockFile::from_file(&lock_file_path)?;
let resolver = LockFileModuleResolver::new(cache, lockfile, false);
log::debug!("Updating lockfile...");
fetch::lock(&module_descriptor, &resolver)?
let old_lock = LockFile::from_file(&lock_file_path)?;
let resolver = LockFileModuleResolver::new(cache, &old_lock, false);
debug!("Updating lockfile...");
let lockfile = fetch::lock(&module_descriptor, &resolver)?;
(Some(old_lock), lockfile)
}

(LockMode::Recreate, _) => {
log::debug!("Generating lockfile...");
fetch::lock(&module_descriptor, &cache)?
debug!("Generating lockfile...");
(None, fetch::lock(&module_descriptor, &cache)?)
}
};

log::debug!("Generated lockfile: {:?}", lockfile);
let value_toml = toml::Value::try_from(&lockfile)?;
debug!("Generated lockfile: {:?}", lockfile);

let lock_file_path = root.join(lock_file_name);
std::fs::write(&lock_file_path, toml::to_string_pretty(&value_toml)?)?;
log::info!("Wrote lockfile to {}", lock_file_path.display());
if old_lock.is_some_and(|old_lock| old_lock == lockfile) {
debug!("Lockfile is up to date");
} else {
let value_toml = toml::Value::try_from(&lockfile)?;
let lock_file_path = root.join(lock_file_name);
std::fs::write(&lock_file_path, toml::to_string_pretty(&value_toml)?)?;
info!("Wrote lockfile to {}", lock_file_path.display());
}

Ok(lockfile)
}
Expand Down
10 changes: 5 additions & 5 deletions src/resolver/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::model::protofetch::{lock::LockFile, Coordinate, DependencyName, Revis

use super::{ModuleResolver, ResolvedModule};

pub struct LockFileModuleResolver<R> {
pub struct LockFileModuleResolver<'a, R> {
inner: R,
lock_file: LockFile,
lock_file: &'a LockFile,
locked: bool,
}

impl<R> LockFileModuleResolver<R> {
pub fn new(inner: R, lock_file: LockFile, locked: bool) -> Self {
impl<'a, R> LockFileModuleResolver<'a, R> {
pub fn new(inner: R, lock_file: &'a LockFile, locked: bool) -> Self {
Self {
inner,
lock_file,
Expand All @@ -21,7 +21,7 @@ impl<R> LockFileModuleResolver<R> {
}
}

impl<R> ModuleResolver for LockFileModuleResolver<R>
impl<'a, R> ModuleResolver for LockFileModuleResolver<'a, R>
where
R: ModuleResolver,
{
Expand Down

0 comments on commit 73af2f7

Please sign in to comment.