Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix vscode extensions update step #650

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ fn run() -> Result<()> {
runner.execute(Step::Vcpkg, "vcpkg", || generic::run_vcpkg_update(&ctx))?;
runner.execute(Step::Pipx, "pipx", || generic::run_pipx_update(&ctx))?;
runner.execute(Step::Vscode, "Visual Studio Code extensions", || {
generic::run_vscode_extensions_upgrade(&ctx)
generic::run_vscode_extensions_update(&ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for changing it! Nice!

})?;
runner.execute(Step::Conda, "conda", || generic::run_conda_update(&ctx))?;
runner.execute(Step::Mamba, "mamba", || generic::run_mamba_update(&ctx))?;
Expand Down
53 changes: 37 additions & 16 deletions src/steps/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ use crate::{
terminal::print_warning,
};

#[cfg(target_os = "linux")]
pub fn is_wsl() -> Result<bool> {
let output = Command::new("uname").arg("-r").output_checked_utf8()?.stdout;
debug!("Uname output: {}", output);
Ok(output.contains("microsoft"))
}

#[cfg(not(target_os = "linux"))]
pub fn is_wsl() -> Result<bool> {
Ok(false)
}

pub fn run_cargo_update(ctx: &ExecutionContext) -> Result<()> {
let cargo_dir = env::var_os("CARGO_HOME")
.map(PathBuf::from)
Expand Down Expand Up @@ -325,28 +337,37 @@ pub fn run_vcpkg_update(ctx: &ExecutionContext) -> Result<()> {
command.args(["upgrade", "--no-dry-run"]).status_checked()
}

pub fn run_vscode_extensions_upgrade(ctx: &ExecutionContext) -> Result<()> {
pub fn run_vscode_extensions_update(ctx: &ExecutionContext) -> Result<()> {
// Calling vscode in WSL may install a server instead of updating extensions (https://github.com/topgrade-rs/topgrade/issues/594#issuecomment-1782157367)
if is_wsl()? {
return Err(SkipStep(String::from("Should not run in WSL")).into());
}

let vscode = require("code")?;
print_separator("Visual Studio Code extensions");

// Vscode does not have CLI command to upgrade all extensions (see https://github.com/microsoft/vscode/issues/56578)
// Instead we get the list of installed extensions with `code --list-extensions` command (obtain a line-return separated list of installed extensions)
let extensions = Command::new(&vscode)
.arg("--list-extensions")
// Vscode has update command only since 1.86 version ("january 2024" update), disable the update for prior versions
// Use command `code --version` which returns 3 lines: version, git commit, instruction set. We parse only the first one
let version: Result<Version> = match Command::new("code")
.arg("--version")
.output_checked_utf8()?
.stdout;

// Then we construct the upgrade command: `code --force --install-extension [ext0] --install-extension [ext1] ... --install-extension [extN]`
if !extensions.is_empty() {
let mut command_args = vec!["--force"];
for extension in extensions.split_whitespace() {
command_args.extend(["--install-extension", extension]);
}
.stdout
.lines()
.next()
{
Some(item) => Version::parse(item).map_err(|err| err.into()),
_ => return Err(SkipStep(String::from("Cannot find vscode version")).into()),
};

ctx.run_type().execute(&vscode).args(command_args).status_checked()?;
if !matches!(version, Ok(version) if version >= Version::new(1, 86, 0)) {
return Err(SkipStep(String::from("Too old vscode version to have update extensions command")).into());
}

Ok(())
print_separator("Visual Studio Code extensions");

ctx.run_type()
.execute(vscode)
.arg("--update-extensions")
.status_checked()
}

pub fn run_pipx_update(ctx: &ExecutionContext) -> Result<()> {
Expand Down
7 changes: 1 addition & 6 deletions src/steps/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing::{debug, warn};
use crate::command::CommandExt;
use crate::error::{SkipStep, TopgradeError};
use crate::execution_context::ExecutionContext;
use crate::steps::generic::is_wsl;
use crate::steps::os::archlinux;
use crate::terminal::print_separator;
use crate::utils::{require, require_option, which, PathExt, REQUIRE_SUDO};
Expand Down Expand Up @@ -189,12 +190,6 @@ fn update_bedrock(ctx: &ExecutionContext) -> Result<()> {
Ok(())
}

fn is_wsl() -> Result<bool> {
let output = Command::new("uname").arg("-r").output_checked_utf8()?.stdout;
debug!("Uname output: {}", output);
Ok(output.contains("microsoft"))
}

fn upgrade_alpine_linux(ctx: &ExecutionContext) -> Result<()> {
let apk = require("apk")?;
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
Expand Down