Skip to content

Commit

Permalink
refactor: paths object
Browse files Browse the repository at this point in the history
  • Loading branch information
beeb committed Aug 22, 2024
1 parent 0286a7c commit 7fd47c9
Show file tree
Hide file tree
Showing 13 changed files with 1,884 additions and 1,908 deletions.
35 changes: 17 additions & 18 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::fs;

use super::Result;
use crate::{
config::{add_to_config, get_config_path, read_soldeer_config},
config::{add_to_config, read_soldeer_config, Paths},
install::{ensure_dependencies_dir, install_dependency, Progress},
lock::add_to_lockfile,
registry::get_latest_forge_std,
remappings::add_to_remappings,
remappings::{edit_remappings, RemappingsAction},
utils::remove_forge_lib,
PROJECT_ROOT,
};
use clap::Parser;
use cliclack::{
Expand All @@ -25,36 +24,36 @@ pub struct Init {
pub clean: bool,
}

pub(crate) async fn init_command(cmd: Init) -> Result<()> {
pub(crate) async fn init_command(paths: &Paths, cmd: Init) -> Result<()> {
if cmd.clean {
remark("Flag `--clean` was set, removing `lib` dir and submodules")?;
remove_forge_lib().await?;
remove_forge_lib(&paths.root).await?;
}

let config_path = get_config_path()?;
let config = read_soldeer_config(Some(&config_path))?;
let config = read_soldeer_config(&paths.config)?;
success("Done reading config")?;
ensure_dependencies_dir()?;
ensure_dependencies_dir(&paths.dependencies)?;
let dependency = get_latest_forge_std().await?;
let multi = multi_progress(format!("Installing {dependency}"));
let progress = Progress::new(&multi, 1);
progress.start_all();
let lock = install_dependency(&dependency, None, None, false, progress.clone()).await.map_err(
|e| {
multi.error(&e);
e
},
)?;
let lock =
install_dependency(&dependency, None, &paths.dependencies, None, false, progress.clone())
.await
.map_err(|e| {
multi.error(&e);
e
})?;
progress.stop_all();
multi.stop();
add_to_config(&dependency, &config_path)?;
add_to_config(&dependency, &paths.config)?;
success("Dependency added to config")?;
add_to_lockfile(lock)?;
add_to_lockfile(lock, &paths.lock)?;
success("Dependency added to lockfile")?;
add_to_remappings(dependency, &config, &config_path).await?;
edit_remappings(&RemappingsAction::Add(dependency), &config, paths)?;
success("Dependency added to remappings")?;

let gitignore_path = PROJECT_ROOT.join(".gitignore");
let gitignore_path = paths.root.join(".gitignore");
if gitignore_path.exists() {
let mut gitignore = fs::read_to_string(&gitignore_path)?;
if !gitignore.contains("dependencies") {
Expand Down
46 changes: 26 additions & 20 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::{validate_dependency, Result};
use crate::{
config::{add_to_config, get_config_path, read_config_deps, read_soldeer_config, Dependency},
config::{add_to_config, read_config_deps, read_soldeer_config, Dependency, Paths},
errors::{InstallError, LockError},
install::{ensure_dependencies_dir, install_dependencies, install_dependency, Progress},
lock::{add_to_lockfile, generate_lockfile_contents, read_lockfile},
remappings::{add_to_remappings, update_remappings},
DEPENDENCY_DIR, LOCK_FILE,
remappings::{edit_remappings, RemappingsAction},
};
use clap::Parser;
use cliclack::{
Expand Down Expand Up @@ -62,35 +61,36 @@ pub struct Install {
pub clean: bool,
}

pub(crate) async fn install_command(cmd: Install) -> Result<()> {
let config_path = get_config_path()?;
let mut config = read_soldeer_config(Some(&config_path))?;
pub(crate) async fn install_command(paths: &Paths, cmd: Install) -> Result<()> {
let mut config = read_soldeer_config(&paths.config)?;
if cmd.regenerate_remappings {
config.remappings_regenerate = true;
}
if cmd.recursive_deps {
config.recursive_deps = true;
}
success("Done reading config")?;
ensure_dependencies_dir()?;
let dependencies: Vec<Dependency> = read_config_deps(Some(&config_path))?;
ensure_dependencies_dir(&paths.dependencies)?;
let dependencies: Vec<Dependency> = read_config_deps(&paths.config)?;
match cmd.dependency {
None => {
let (locks, lockfile_content) = read_lockfile()?;
let (locks, lockfile_content) = read_lockfile(&paths.lock)?;
success("Done reading lockfile")?;
if cmd.clean {
remark("Flag `--clean` was set, re-installing all dependencies")?;
fs::remove_dir_all(DEPENDENCY_DIR.as_path()).map_err(|e| {
InstallError::IOError { path: DEPENDENCY_DIR.to_path_buf(), source: e }
fs::remove_dir_all(&paths.dependencies).map_err(|e| InstallError::IOError {
path: paths.dependencies.clone(),
source: e,
})?;
ensure_dependencies_dir()?;
ensure_dependencies_dir(&paths.dependencies)?;
}
let multi = multi_progress("Installing dependencies");
let progress = Progress::new(&multi, dependencies.len() as u64);
progress.start_all();
let new_locks = install_dependencies(
&dependencies,
&locks,
&paths.dependencies,
config.recursive_deps,
progress.clone(),
)
Expand All @@ -101,9 +101,9 @@ pub(crate) async fn install_command(cmd: Install) -> Result<()> {
if !lockfile_content.is_empty() && new_lockfile_content != lockfile_content {
warning("Warning: the lock file is out of sync with the dependencies. Consider running `soldeer update` to re-generate the lockfile.")?;
} else if lockfile_content.is_empty() {
fs::write(LOCK_FILE.as_path(), new_lockfile_content).map_err(LockError::IOError)?;
fs::write(&paths.lock, new_lockfile_content).map_err(LockError::IOError)?;
}
update_remappings(&config, &config_path).await?;
edit_remappings(&RemappingsAction::None, &config, paths)?;
success("Updated remappings")?;
}
Some(dependency) => {
Expand All @@ -118,9 +118,15 @@ pub(crate) async fn install_command(cmd: Install) -> Result<()> {
let multi = multi_progress(format!("Installing {dep}"));
let progress = Progress::new(&multi, 1);
progress.start_all();
let lock =
install_dependency(&dep, None, None, config.recursive_deps, progress.clone())
.await?;
let lock = install_dependency(
&dep,
None,
&paths.dependencies,
None,
config.recursive_deps,
progress.clone(),
)
.await?;
progress.stop_all();
multi.stop();
// for GIT deps, we need to add the commit hash before adding them to the
Expand All @@ -129,11 +135,11 @@ pub(crate) async fn install_command(cmd: Install) -> Result<()> {
git_dep.rev =
Some(lock.as_git().expect("lock entry should be of type git").rev.clone());
}
add_to_config(&dep, &config_path)?;
add_to_config(&dep, &paths.config)?;
success("Dependency added to config")?;
add_to_lockfile(lock)?;
add_to_lockfile(lock, &paths.lock)?;
success("Dependency added to lockfile")?;
add_to_remappings(dep, &config, &config_path).await?;
edit_remappings(&RemappingsAction::Add(dep), &config, paths)?;
success("Dependency added to remappings")?;
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/commands/uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::Result;
use crate::{
config::{delete_from_config, get_config_path, read_soldeer_config},
config::{delete_from_config, read_soldeer_config, Paths},
download::delete_dependency_files_sync,
lock::remove_lock,
remappings::remove_from_remappings,
remappings::{edit_remappings, RemappingsAction},
SoldeerError,
};
use clap::Parser;
Expand All @@ -17,24 +17,23 @@ pub struct Uninstall {
pub dependency: String,
}

pub(crate) fn uninstall_command(cmd: &Uninstall) -> Result<()> {
let config_path = get_config_path()?;
let config = read_soldeer_config(Some(&config_path))?;
pub(crate) fn uninstall_command(paths: &Paths, cmd: &Uninstall) -> Result<()> {
let config = read_soldeer_config(&paths.config)?;
success("Done reading config")?;

// delete from the config file and return the dependency
let dependency = delete_from_config(&cmd.dependency, &config_path)?;
let dependency = delete_from_config(&cmd.dependency, &paths.config)?;
success("Dependency removed from config file")?;

// deleting the files
delete_dependency_files_sync(&dependency)
delete_dependency_files_sync(&dependency, &paths.dependencies)
.map_err(|e| SoldeerError::DownloadError { dep: dependency.to_string(), source: e })?;
success("Dependency removed from disk")?;

remove_lock(&dependency)?;
remove_lock(&dependency, &paths.lock)?;
success("Dependency removed from lockfile")?;

remove_from_remappings(dependency, &config, &config_path)?;
edit_remappings(&RemappingsAction::Remove(dependency), &config, paths)?;
success("Dependency removed from remappings")?;

Ok(())
Expand Down
30 changes: 17 additions & 13 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::fs;

use super::Result;
use crate::{
config::{get_config_path, read_config_deps, read_soldeer_config, Dependency},
config::{read_config_deps, read_soldeer_config, Dependency, Paths},
errors::LockError,
install::{ensure_dependencies_dir, Progress},
lock::{generate_lockfile_contents, read_lockfile},
remappings::update_remappings,
remappings::{edit_remappings, RemappingsAction},
update::update_dependencies,
LOCK_FILE,
};
use clap::Parser;
use cliclack::{log::success, multi_progress};
Expand All @@ -30,33 +29,38 @@ pub struct Update {
// TODO: add a parameter for a dependency name, where we would only update that particular
// dependency

pub(crate) async fn update_command(cmd: Update) -> Result<()> {
let config_path = get_config_path()?;
let mut config = read_soldeer_config(Some(&config_path))?;
pub(crate) async fn update_command(paths: &Paths, cmd: Update) -> Result<()> {
let mut config = read_soldeer_config(&paths.config)?;
if cmd.regenerate_remappings {
config.remappings_regenerate = true;
}
if cmd.recursive_deps {
config.recursive_deps = true;
}
success("Done reading config")?;
ensure_dependencies_dir()?;
let dependencies: Vec<Dependency> = read_config_deps(Some(&config_path))?;
let (locks, _) = read_lockfile()?;
ensure_dependencies_dir(&paths.dependencies)?;
let dependencies: Vec<Dependency> = read_config_deps(&paths.config)?;
let (locks, _) = read_lockfile(&paths.lock)?;
success("Done reading lockfile")?;
let multi = multi_progress("Updating dependencies");
let progress = Progress::new(&multi, dependencies.len() as u64);
progress.start_all();
let new_locks =
update_dependencies(&dependencies, &locks, config.recursive_deps, progress.clone()).await?;
let new_locks = update_dependencies(
&dependencies,
&locks,
&paths.dependencies,
config.recursive_deps,
progress.clone(),
)
.await?;
progress.stop_all();
multi.stop();

let new_lockfile_content = generate_lockfile_contents(new_locks);
fs::write(LOCK_FILE.as_path(), new_lockfile_content).map_err(LockError::IOError)?;
fs::write(&paths.lock, new_lockfile_content).map_err(LockError::IOError)?;
success("Updated lockfile")?;

update_remappings(&config, &config_path).await?;
edit_remappings(&RemappingsAction::None, &config, paths)?;
success("Updated remappings")?;
Ok(())
}
Loading

0 comments on commit 7fd47c9

Please sign in to comment.