Files
vscode/cli/src/commands/version.rs
T
Connor Peet c536595a7f cli: fallback to system installs in the standalone CLI
The standalone CLI should detect and fall back to using and
system-installed VS Code instance, rather than trying to download zips
and manage its own VS Code instances.

There are three approaches used for discovery:

- On Windows, we can easily and quickly read the register to find
  installed versions based on their app ID.
- On macOS, we initially look in `/Applications` and fall back to the
  slow `system_profiler` command to list app .app's if that fails.
- On Linux, we just look in the PATH. I believe all Linux installers
  (snap, dep, rpm) automatically add VS Code to the user's PATH.

Failing this, the user can also manually specify their installation dir,
using the command `code version use stable --install-dir /path/to/vscode`.

Fixes #164159
2022-10-20 10:54:13 -07:00

67 lines
2.0 KiB
Rust

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
use std::path::{Path, PathBuf};
use crate::{
desktop::{prompt_to_install, CodeVersionManager, RequestedVersion},
log,
util::{
errors::{AnyError, NoInstallInUserProvidedPath},
prereqs::PreReqChecker,
},
};
use super::{args::UseVersionArgs, CommandContext};
pub async fn switch_to(ctx: CommandContext, args: UseVersionArgs) -> Result<i32, AnyError> {
let platform = PreReqChecker::new().verify().await?;
let vm = CodeVersionManager::new(ctx.log.clone(), &ctx.paths, platform);
let version = RequestedVersion::try_from(args.name.as_str())?;
let maybe_path = match args.install_dir {
Some(d) => Some(
CodeVersionManager::get_entrypoint_for_install_dir(&PathBuf::from(&d))
.await
.ok_or(NoInstallInUserProvidedPath(d))?,
),
None => vm.try_get_entrypoint(&version).await,
};
match maybe_path {
Some(p) => {
vm.set_preferred_version(version.clone(), p.clone()).await?;
print_now_using(&ctx.log, &version, &p);
Ok(0)
}
None => {
prompt_to_install(&version);
Ok(1)
}
}
}
pub async fn show(ctx: CommandContext) -> Result<i32, AnyError> {
let platform = PreReqChecker::new().verify().await?;
let vm = CodeVersionManager::new(ctx.log.clone(), &ctx.paths, platform);
let version = vm.get_preferred_version();
println!("Current quality: {}", version);
match vm.try_get_entrypoint(&version).await {
Some(p) => println!("Installation path: {}", p.display()),
None => println!("No existing installation found"),
}
Ok(0)
}
fn print_now_using(log: &log::Logger, version: &RequestedVersion, path: &Path) {
log.result(&format!(
"Now using VS Code {} from {}",
version,
path.display()
));
}