Skip to content

Commit

Permalink
refactor: use trait object for progress bar wrappers (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Dec 14, 2023
1 parent 29d301d commit 05968b8
Show file tree
Hide file tree
Showing 22 changed files with 268 additions and 253 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ jobs:
ref: ${{ github.event.pull_request.head.ref }}
- uses: Swatinem/rust-cache@v2
with:
shared-key: test
shared-key: unit
save-if: ${{ github.ref_name == 'main' }}
cache-all-crates: true
- uses: taiki-e/install-action@v2
with:
tool: nextest,just,cargo-deny,cargo-msrv,cargo-machete
Expand Down Expand Up @@ -66,12 +65,11 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0

#- run: rustup toolchain install nightly --component llvm-tools-preview --profile minimal
- uses: Swatinem/rust-cache@v2
with:
shared-key: test
save-if: false
shared-key: coverage
save-if: ${{ github.ref_name == 'main' }}
- run: sudo apt-get update; sudo apt-get install zsh fish direnv shfmt
- run: npm i -g markdown-magic
- uses: taiki-e/install-action@v2
Expand Down
2 changes: 1 addition & 1 deletion e2e/test_poetry
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail
source "$(dirname "$0")/assert.sh"

if [ "${TEST_ALL:-}" != 1 ]; then
exit
exit
fi

rm -rf "$RTX_DATA_DIR/cache/poetry"
Expand Down
2 changes: 1 addition & 1 deletion e2e/test_python
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail
source "$(dirname "$0")/assert.sh"

if [ "${TEST_ALL:-}" != 1 ]; then
exit
exit
fi

export RTX_EXPERIMENTAL=1
Expand Down
8 changes: 4 additions & 4 deletions src/cli/plugins/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ impl PluginsUninstall {
match config.get_or_create_plugin(plugin_name) {
plugin if plugin.is_installed() => {
let mut pr = mpr.add();
plugin.decorate_progress_bar(&mut pr, None);
plugin.uninstall(&pr)?;
plugin.decorate_progress_bar(pr.as_mut(), None);
plugin.uninstall(pr.as_ref())?;
if self.purge {
plugin.purge(&pr)?;
plugin.purge(pr.as_ref())?;
}
pr.finish_with_message("uninstalled");
pr.finish_with_message("uninstalled".into());
}
_ => mpr.suspend(|| {
warn!(
Expand Down
6 changes: 3 additions & 3 deletions src/cli/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ impl Prune {
for (p, tv) in to_delete {
let mut pr = mpr.add();
if self.dry_run {
pr.set_prefix(format!("{} {} ", pr.prefix(), style("[dryrun]").bold()));
pr.set_prefix(&format!("{} {} ", pr.prefix(), style("[dryrun]").bold()));
}
if self.dry_run || settings.yes || prompt::confirm(&format!("remove {} ?", &tv))? {
p.decorate_progress_bar(&mut pr, Some(&tv));
p.uninstall_version(&tv, &pr, self.dry_run)?;
p.decorate_progress_bar(pr.as_mut(), Some(&tv));
p.uninstall_version(&tv, pr.as_ref(), self.dry_run)?;
pr.finish();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ impl Uninstall {
}

let mut pr = mpr.add();
plugin.decorate_progress_bar(&mut pr, Some(&tv));
if let Err(err) = plugin.uninstall_version(&tv, &pr, self.dry_run) {
plugin.decorate_progress_bar(pr.as_mut(), Some(&tv));
if let Err(err) = plugin.uninstall_version(&tv, pr.as_ref(), self.dry_run) {
pr.error(err.to_string());
return Err(eyre!(err).wrap_err(format!("failed to uninstall {tv}")));
}
if self.dry_run {
pr.finish_with_message("uninstalled (dry-run)");
pr.finish_with_message("uninstalled (dry-run)".into());
} else {
pr.finish_with_message("uninstalled");
pr.finish_with_message("uninstalled".into());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::runtime_symlinks;
use crate::shims;
use crate::toolset::{InstallOptions, ToolVersion, ToolsetBuilder};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::progress_report::ProgressReport;
use crate::ui::progress_report::SingleReport;

/// Upgrades outdated tool versions
#[derive(Debug, clap::Args)]
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Upgrade {
ts.install_versions(config, new_versions, &mpr, &opts)?;
for (tool, tv) in to_remove {
let mut pr = mpr.add();
self.uninstall_old_version(tool.clone(), &tv, &mut pr)?;
self.uninstall_old_version(tool.clone(), &tv, pr.as_mut())?;
}

let ts = ToolsetBuilder::new().with_args(&self.tool).build(config)?;
Expand All @@ -108,7 +108,7 @@ impl Upgrade {
&self,
tool: Arc<dyn Plugin>,
tv: &ToolVersion,
pr: &mut ProgressReport,
pr: &mut dyn SingleReport,
) -> Result<()> {
tool.decorate_progress_bar(pr, Some(tv));
match tool.uninstall_version(tv, pr, self.dry_run) {
Expand Down
10 changes: 5 additions & 5 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use eyre::Context;
use crate::env;
use crate::errors::Error::ScriptFailed;
use crate::file::display_path;
use crate::ui::progress_report::ProgressReport;
use crate::ui::progress_report::SingleReport;

/// Create a command with any number of of positional arguments, which may be
/// different types (anything that implements
Expand Down Expand Up @@ -90,7 +90,7 @@ where

pub struct CmdLineRunner<'a> {
cmd: Command,
pr: Option<&'a ProgressReport>,
pr: Option<&'a dyn SingleReport>,
stdin: Option<String>,
}

Expand Down Expand Up @@ -171,7 +171,7 @@ impl<'a> CmdLineRunner<'a> {
self
}

pub fn with_pr(mut self, pr: &'a ProgressReport) -> Self {
pub fn with_pr(mut self, pr: &'a dyn SingleReport) -> Self {
self.pr = Some(pr);
self
}
Expand Down Expand Up @@ -270,15 +270,15 @@ impl<'a> CmdLineRunner<'a> {
fn on_stdout(&self, line: &str) {
if !line.trim().is_empty() {
if let Some(pr) = self.pr {
pr.set_message(line)
pr.set_message(line.into())
}
}
}

fn on_stderr(&self, line: &str) {
if !line.trim().is_empty() {
match self.pr {
Some(pr) => pr.println(line),
Some(pr) => pr.println(line.into()),
None => eprintln!("{}", line),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/install_context.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::toolset::{ToolVersion, Toolset};
use crate::ui::progress_report::ProgressReport;
use crate::ui::progress_report::SingleReport;

pub struct InstallContext<'a> {
pub ts: &'a Toolset,
pub tv: ToolVersion,
pub pr: ProgressReport,
pub pr: Box<dyn SingleReport>,
pub raw: bool,
pub force: bool,
}
10 changes: 5 additions & 5 deletions src/plugins/core/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::install_context::InstallContext;
use crate::plugins::core::CorePlugin;
use crate::plugins::{Plugin, HTTP};
use crate::toolset::{ToolVersion, ToolVersionRequest};
use crate::ui::progress_report::ProgressReport;
use crate::ui::progress_report::SingleReport;
use crate::{file, http};

#[derive(Debug)]
Expand Down Expand Up @@ -48,14 +48,14 @@ impl BunPlugin {
}

fn test_bun(&self, ctx: &InstallContext) -> Result<()> {
ctx.pr.set_message("bun -v");
ctx.pr.set_message("bun -v".into());
CmdLineRunner::new(self.bun_bin(&ctx.tv))
.with_pr(&ctx.pr)
.with_pr(ctx.pr.as_ref())
.arg("-v")
.execute()
}

fn download(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<PathBuf> {
fn download(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<PathBuf> {
let http = http::Client::new()?;
let url = format!(
"https://github.com/oven-sh/bun/releases/download/bun-v{}/bun-{}-{}.zip",
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Plugin for BunPlugin {
ToolVersionRequest::Version { .. }
));

let tarball_path = self.download(&ctx.tv, &ctx.pr)?;
let tarball_path = self.download(&ctx.tv, ctx.pr.as_ref())?;
self.install(ctx, &tarball_path)?;
self.verify(ctx)?;

Expand Down
18 changes: 9 additions & 9 deletions src/plugins/core/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::install_context::InstallContext;
use crate::plugins::core::CorePlugin;
use crate::plugins::{Plugin, HTTP};
use crate::toolset::{ToolVersion, ToolVersionRequest, Toolset};
use crate::ui::progress_report::ProgressReport;
use crate::ui::progress_report::SingleReport;
use crate::{file, http};

#[derive(Debug)]
Expand Down Expand Up @@ -51,15 +51,15 @@ impl DenoPlugin {
tv.install_path().join("bin/deno")
}

fn test_deno(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<()> {
pr.set_message("deno -V");
fn test_deno(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<()> {
pr.set_message("deno -V".into());
CmdLineRunner::new(self.deno_bin(tv))
.with_pr(pr)
.arg("-V")
.execute()
}

fn download(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<PathBuf> {
fn download(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<PathBuf> {
let http = http::Client::new()?;
let url = format!(
"https://github.com/denoland/deno/releases/download/v{}/deno-{}-{}.zip",
Expand All @@ -78,7 +78,7 @@ impl DenoPlugin {
Ok(tarball_path)
}

fn install(&self, tv: &ToolVersion, pr: &ProgressReport, tarball_path: &Path) -> Result<()> {
fn install(&self, tv: &ToolVersion, pr: &dyn SingleReport, tarball_path: &Path) -> Result<()> {
pr.set_message(format!("installing {}", tarball_path.display()));
file::remove_all(tv.install_path())?;
file::create_dir_all(tv.install_path().join("bin"))?;
Expand All @@ -88,7 +88,7 @@ impl DenoPlugin {
Ok(())
}

fn verify(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<()> {
fn verify(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<()> {
self.test_deno(tv, pr)
}
}
Expand All @@ -115,9 +115,9 @@ impl Plugin for DenoPlugin {
ToolVersionRequest::Version { .. }
));

let tarball_path = self.download(&ctx.tv, &ctx.pr)?;
self.install(&ctx.tv, &ctx.pr, &tarball_path)?;
self.verify(&ctx.tv, &ctx.pr)?;
let tarball_path = self.download(&ctx.tv, ctx.pr.as_ref())?;
self.install(&ctx.tv, ctx.pr.as_ref(), &tarball_path)?;
self.verify(&ctx.tv, ctx.pr.as_ref())?;

Ok(())
}
Expand Down
20 changes: 10 additions & 10 deletions src/plugins/core/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::install_context::InstallContext;
use crate::plugins::core::CorePlugin;
use crate::plugins::Plugin;
use crate::toolset::{ToolVersion, Toolset};
use crate::ui::progress_report::ProgressReport;
use crate::ui::progress_report::SingleReport;
use crate::{cmd, env, file, hash, http};

#[derive(Debug)]
Expand Down Expand Up @@ -57,7 +57,7 @@ impl GoPlugin {
tv.install_path().join("packages")
}

fn install_default_packages(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<()> {
fn install_default_packages(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<()> {
let body = file::read_to_string(&*env::RTX_GO_DEFAULT_PACKAGES_FILE).unwrap_or_default();
for package in body.lines() {
let package = package.split('#').next().unwrap_or_default().trim();
Expand Down Expand Up @@ -87,15 +87,15 @@ impl GoPlugin {
Ok(())
}

fn test_go(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<()> {
pr.set_message("go version");
fn test_go(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<()> {
pr.set_message("go version".into());
CmdLineRunner::new(self.go_bin(tv))
.with_pr(pr)
.arg("version")
.execute()
}

fn download(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<PathBuf> {
fn download(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<PathBuf> {
let http = http::Client::new()?;
let filename = format!("go{}.{}-{}.tar.gz", tv.version, platform(), arch());
let tarball_url = format!("{}/{}", &*env::RTX_GO_DOWNLOAD_MIRROR, &filename);
Expand All @@ -118,7 +118,7 @@ impl GoPlugin {
Ok(())
}

fn install(&self, tv: &ToolVersion, pr: &ProgressReport, tarball_path: &Path) -> Result<()> {
fn install(&self, tv: &ToolVersion, pr: &dyn SingleReport, tarball_path: &Path) -> Result<()> {
let tarball = tarball_path
.file_name()
.unwrap_or_default()
Expand All @@ -128,7 +128,7 @@ impl GoPlugin {
Ok(())
}

fn verify(&self, tv: &ToolVersion, pr: &ProgressReport) -> Result<()> {
fn verify(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<()> {
self.test_go(tv, pr)?;
self.install_default_packages(tv, pr)
}
Expand All @@ -150,9 +150,9 @@ impl Plugin for GoPlugin {
}

fn install_version_impl(&self, ctx: &InstallContext) -> Result<()> {
let tarball_path = self.download(&ctx.tv, &ctx.pr)?;
self.install(&ctx.tv, &ctx.pr, &tarball_path)?;
self.verify(&ctx.tv, &ctx.pr)?;
let tarball_path = self.download(&ctx.tv, ctx.pr.as_ref())?;
self.install(&ctx.tv, ctx.pr.as_ref(), &tarball_path)?;
self.verify(&ctx.tv, ctx.pr.as_ref())?;

Ok(())
}
Expand Down
Loading

0 comments on commit 05968b8

Please sign in to comment.