Skip to content

Commit

Permalink
refactor: make GitSteps a dedicated step (topgrade-rs#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC authored Mar 9, 2024
1 parent 12116c3 commit 2c1ce3d
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 267 deletions.
9 changes: 1 addition & 8 deletions src/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(dead_code)]
use crate::executor::RunType;
use crate::git::Git;
use crate::sudo::Sudo;
use crate::utils::{require_option, REQUIRE_SUDO};
use crate::{config::Config, executor::Executor};
Expand All @@ -12,7 +11,6 @@ use std::sync::Mutex;
pub struct ExecutionContext<'a> {
run_type: RunType,
sudo: Option<Sudo>,
git: &'a Git,
config: &'a Config,
/// Name of a tmux session to execute commands in, if any.
/// This is used in `./steps/remote/ssh.rs`, where we want to run `topgrade` in a new
Expand All @@ -23,12 +21,11 @@ pub struct ExecutionContext<'a> {
}

impl<'a> ExecutionContext<'a> {
pub fn new(run_type: RunType, sudo: Option<Sudo>, git: &'a Git, config: &'a Config) -> Self {
pub fn new(run_type: RunType, sudo: Option<Sudo>, config: &'a Config) -> Self {
let under_ssh = var("SSH_CLIENT").is_ok() || var("SSH_TTY").is_ok();
Self {
run_type,
sudo,
git,
config,
tmux_session: Mutex::new(None),
under_ssh,
Expand All @@ -44,10 +41,6 @@ impl<'a> ExecutionContext<'a> {
self.run_type
}

pub fn git(&self) -> &Git {
self.git
}

pub fn sudo(&self) -> &Option<Sudo> {
&self.sudo
}
Expand Down
67 changes: 3 additions & 64 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod utils;
pub(crate) static HOME_DIR: Lazy<PathBuf> = Lazy::new(|| home::home_dir().expect("No home directory"));
#[cfg(unix)]
pub(crate) static XDG_DIRS: Lazy<Xdg> = Lazy::new(|| Xdg::new().expect("No home directory"));

#[cfg(windows)]
pub(crate) static WINDOWS_DIRS: Lazy<Windows> = Lazy::new(|| Windows::new().expect("No home directory"));

Expand Down Expand Up @@ -122,8 +123,6 @@ fn run() -> Result<()> {
}
}

let git = git::Git::new();
let mut git_repos = git::Repositories::new(&git);
let powershell = powershell::Powershell::new();
let should_run_powershell = powershell.profile().is_some() && config.should_run(Step::Powershell);
let emacs = emacs::Emacs::new();
Expand All @@ -132,7 +131,7 @@ fn run() -> Result<()> {

let sudo = config.sudo_command().map_or_else(sudo::Sudo::detect, sudo::Sudo::new);
let run_type = executor::RunType::new(config.dry_run());
let ctx = execution_context::ExecutionContext::new(run_type, sudo, &git, &config);
let ctx = execution_context::ExecutionContext::new(run_type, sudo, &config);
let mut runner = runner::Runner::new(&ctx);

// If
Expand Down Expand Up @@ -404,67 +403,7 @@ fn run() -> Result<()> {
})?;
runner.execute(Step::Bob, "Bob", || generic::run_bob(&ctx))?;
runner.execute(Step::Certbot, "Certbot", || generic::run_certbot(&ctx))?;

if config.use_predefined_git_repos() {
if config.should_run(Step::Emacs) {
if !emacs.is_doom() {
if let Some(directory) = emacs.directory() {
git_repos.insert_if_repo(directory);
}
}
git_repos.insert_if_repo(HOME_DIR.join(".doom.d"));
}

if config.should_run(Step::Vim) {
git_repos.insert_if_repo(HOME_DIR.join(".vim"));
git_repos.insert_if_repo(HOME_DIR.join(".config/nvim"));
}

git_repos.insert_if_repo(HOME_DIR.join(".ideavimrc"));
git_repos.insert_if_repo(HOME_DIR.join(".intellimacs"));

if config.should_run(Step::Rcm) {
git_repos.insert_if_repo(HOME_DIR.join(".dotfiles"));
}

#[cfg(unix)]
{
git_repos.insert_if_repo(zsh::zshrc());
if config.should_run(Step::Tmux) {
git_repos.insert_if_repo(HOME_DIR.join(".tmux"));
}
git_repos.insert_if_repo(HOME_DIR.join(".config/fish"));
git_repos.insert_if_repo(XDG_DIRS.config_dir().join("openbox"));
git_repos.insert_if_repo(XDG_DIRS.config_dir().join("bspwm"));
git_repos.insert_if_repo(XDG_DIRS.config_dir().join("i3"));
git_repos.insert_if_repo(XDG_DIRS.config_dir().join("sway"));
}

#[cfg(windows)]
git_repos.insert_if_repo(
WINDOWS_DIRS
.cache_dir()
.join("Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState"),
);

#[cfg(windows)]
windows::insert_startup_scripts(&mut git_repos).ok();

if let Some(profile) = powershell.profile() {
git_repos.insert_if_repo(profile);
}
}

if config.should_run(Step::GitRepos) {
if let Some(custom_git_repos) = config.git_repos() {
for git_repo in custom_git_repos {
git_repos.glob_insert(git_repo);
}
}
runner.execute(Step::GitRepos, "Git repositories", || {
git.multi_pull_step(&git_repos, &ctx)
})?;
}
runner.execute(Step::GitRepos, "Git Repositories", || git::run_git_pull(&ctx))?;

if should_run_powershell {
runner.execute(Step::Powershell, "Powershell Modules Update", || {
Expand Down
Loading

0 comments on commit 2c1ce3d

Please sign in to comment.