Skip to content

Commit

Permalink
add version check command (#14880)
Browse files Browse the repository at this point in the history
# Description

This PR supersedes nushell/nushell#14813 by
making it a built-in command instead of checking for the latest version
at some interval when nushell starts.

This is what it looks like.

![image](https://github.com/user-attachments/assets/35629425-b332-4078-aea5-4931cfb0471f)

This example shows the output when the running version was
0.101.1-nightly.10

![image](https://github.com/user-attachments/assets/71216635-fb75-4251-a443-bf0d0b9a1c07)


Description from old PR.
One key functionality that I thought was interesting with this and that
I worked with @hustcer on was to try and make sure it works with
nightlies. So, it should tell you when there's a new nightly version
that is available to download. This way, you can know about it without
checking.

What's key from a nightly perspective is (1) the tags are now semver
compliant and (2) hustcer now updates the Cargo.toml package.version
version number prior to compilation so you can know you're running a
nightly version, and this PR uses that information to know whether to
check the nightly repo or the nushell repo for updates.

This uses the
[update-informer](https://docs.rs/update-informer/latest/update_informer/)
crate. NOTE that this _informs_ you of updates but does not
automatically update. I kind of see this as the first step to eventually
having an auto updater.

There was caching of the version in the old PR since it ran on every
nushell startup. Since this PR makes it a command and therefore always
runs on-demand, I've removed the caching so that it always checks when
you run it.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
  • Loading branch information
fdncred authored Jan 23, 2025
1 parent f0f6b3a commit cdbb3ee
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 10 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ tempfile = "3.15"
titlecase = "3.0"
toml = "0.8"
trash = "5.2"
update-informer = { version = "1.2.0", default-features = false, features = ["github", "native-tls", "ureq"] }
umask = "2.1"
unicode-segmentation = "1.12"
unicode-width = "0.2"
Expand Down
22 changes: 12 additions & 10 deletions crates/nu-command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ tabled = { workspace = true, features = ["ansi"], default-features = false }
titlecase = { workspace = true }
toml = { workspace = true, features = ["preserve_order"] }
unicode-segmentation = { workspace = true }
ureq = { workspace = true, default-features = false, features = ["charset", "gzip", "json"] }
update-informer = { workspace = true, optional = true }
ureq = { workspace = true, default-features = false, features = ["charset", "gzip", "json", "native-tls"], optional = true }
url = { workspace = true }
uu_cp = { workspace = true, optional = true }
uu_mkdir = { workspace = true, optional = true }
Expand Down Expand Up @@ -142,7 +143,7 @@ os = [
# include other features
"js",
"network",
"nu-protocol/os",
"nu-protocol/os",
"nu-utils/os",

# os-dependant dependencies
Expand All @@ -160,29 +161,30 @@ os = [
"which",
]

# The dependencies listed below need 'getrandom'.
# They work with JS (usually with wasm-bindgen) or regular OS support.
# The dependencies listed below need 'getrandom'.
# They work with JS (usually with wasm-bindgen) or regular OS support.
# Hence they are also put under the 'os' feature to avoid repetition.
js = [
"getrandom",
"getrandom/js",
"rand",
"getrandom",
"getrandom/js",
"rand",
"uuid",
]

# These dependencies require networking capabilities, especially the http
# interface requires openssl which is not easy to embed into wasm,
# using rustls could solve this issue.
network = [
"multipart-rs",
"multipart-rs",
"native-tls",
"ureq/native-tls",
"update-informer/native-tls",
"ureq",
"uuid",
]

plugin = [
"nu-parser/plugin",
"os",
"os",
]
sqlite = ["rusqlite"]
trash-support = ["trash"]
Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/default_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
HttpPut,
HttpOptions,
Port,
VersionCheck,
}
bind_command! {
Url,
Expand Down
5 changes: 5 additions & 0 deletions crates/nu-command/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ mod http;
#[cfg(feature = "network")]
mod port;
mod url;
#[cfg(feature = "network")]
mod version_check;

#[cfg(feature = "network")]
pub use self::http::*;
pub use self::url::*;

#[cfg(feature = "network")]
pub use port::SubCommand as Port;

#[cfg(feature = "network")]
pub use version_check::VersionCheck;
156 changes: 156 additions & 0 deletions crates/nu-command/src/network/version_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use nu_engine::command_prelude::*;
use serde::Deserialize;
use update_informer::{
http_client::{GenericHttpClient, HttpClient},
registry, Check, Package, Registry, Result as UpdateResult,
};

#[derive(Clone)]
pub struct VersionCheck;

impl Command for VersionCheck {
fn name(&self) -> &str {
"version check"
}

fn description(&self) -> &str {
"Checks to see if you have the latest version of nushell."
}

fn extra_description(&self) -> &str {
"If you're running nushell nightly, `version check` will check to see if you are running the latest nightly version. If you are running the nushell release, `version check` will check to see if you're running the latest release version."
}

fn signature(&self) -> Signature {
Signature::build("version check")
.category(Category::Platform)
.input_output_types(vec![(Type::Nothing, Type::String)])
}

fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Check if you have the latest version of nushell",
example: "version check",
result: None,
}]
}

fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let version_check = check_for_latest_nushell_version();
Ok(version_check.into_pipeline_data())
}
}

pub struct NuShellNightly;

impl Registry for NuShellNightly {
const NAME: &'static str = "nushell/nightly";

fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
pkg: &Package,
) -> UpdateResult<Option<String>> {
#[derive(Deserialize, Debug)]
struct Response {
tag_name: String,
}

let url = format!("https://api.github.com/repos/{}/releases", pkg);
let versions = http_client
.add_header("Accept", "application/vnd.github.v3+json")
.add_header("User-Agent", "update-informer")
.get::<Vec<Response>>(&url)?;

if let Some(v) = versions.first() {
// The nightly repo tags look like "0.101.1-nightly.4+23dc1b6"
// We want to return the "0.101.1-nightly.4" part because hustcer
// is changing the cargo.toml package.version to be that syntax
let up_through_plus = match v.tag_name.split('+').next() {
Some(v) => v,
None => &v.tag_name,
};
return Ok(Some(up_through_plus.to_string()));
}

Ok(None)
}
}

struct NativeTlsHttpClient;

impl HttpClient for NativeTlsHttpClient {
fn get<T: serde::de::DeserializeOwned>(
url: &str,
timeout: std::time::Duration,
headers: update_informer::http_client::HeaderMap,
) -> update_informer::Result<T> {
let agent = ureq::AgentBuilder::new()
.tls_connector(std::sync::Arc::new(native_tls::TlsConnector::new()?))
.build();

let mut req = agent.get(url).timeout(timeout);

for (header, value) in headers {
req = req.set(header, value);
}

let json = req.call()?.into_json()?;

Ok(json)
}
}

pub fn check_for_latest_nushell_version() -> Value {
let current_version = env!("CARGO_PKG_VERSION").to_string();

let mut rec = Record::new();

if current_version.contains("nightly") {
rec.push("channel", Value::test_string("nightly"));

let nightly_pkg_name = "nushell/nightly";
// The .interval() determines how long the cached check lives. Setting it to std::time::Duration::ZERO
// means that there is essentially no cache and it will check for a new version each time you run nushell.
// Since this is run on demand, there isn't really a need to cache the check.
let informer =
update_informer::new(NuShellNightly, nightly_pkg_name, current_version.clone())
.http_client(NativeTlsHttpClient)
.interval(std::time::Duration::ZERO);

if let Ok(Some(new_version)) = informer.check_version() {
rec.push("current", Value::test_bool(false));
rec.push("latest", Value::test_string(format!("{}", new_version)));
Value::test_record(rec)
} else {
rec.push("current", Value::test_bool(true));
rec.push("latest", Value::test_string(current_version.clone()));
Value::test_record(rec)
}
} else {
rec.push("channel", Value::test_string("release"));

let normal_pkg_name = "nushell/nushell";
// By default, this update request is cached for 24 hours so it won't check for a new version
// each time you run nushell. Since this is run on demand, there isn't really a need to cache the check which
// is why we set the interval to std::time::Duration::ZERO.
let informer =
update_informer::new(registry::GitHub, normal_pkg_name, current_version.clone())
.interval(std::time::Duration::ZERO);

if let Ok(Some(new_version)) = informer.check_version() {
rec.push("current", Value::test_bool(false));
rec.push("latest", Value::test_string(format!("{}", new_version)));
Value::test_record(rec)
} else {
rec.push("current", Value::test_bool(true));
rec.push("latest", Value::test_string(current_version.clone()));
Value::test_record(rec)
}
}
}

0 comments on commit cdbb3ee

Please sign in to comment.