-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 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
Showing
6 changed files
with
231 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |