From a649aad7ad26d4578699370d6e63d80edeca1f97 Mon Sep 17 00:00:00 2001 From: Ashish Shekar Date: Mon, 25 Apr 2022 20:01:25 +0530 Subject: [PATCH] feat(cli): check and notify about updates on `tauri dev`, closes #3789 (#3960) Co-authored-by: Lucas Nogueira --- .changes/cli-dev-update.md | 6 +++++ .github/workflows/publish-cli.yml | 22 ++++++++--------- tooling/cli/node/package.json | 4 ++-- tooling/cli/src/dev.rs | 32 +++++++++++++++++++++++++ tooling/cli/src/info.rs | 39 ++++++++++++++++++++++++++++++- 5 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 .changes/cli-dev-update.md diff --git a/.changes/cli-dev-update.md b/.changes/cli-dev-update.md new file mode 100644 index 000000000..1d06aa4af --- /dev/null +++ b/.changes/cli-dev-update.md @@ -0,0 +1,6 @@ +--- +"cli.rs": patch +"cli.js": patch +--- + +Notify CLI update when running `tauri dev`. diff --git a/.github/workflows/publish-cli.yml b/.github/workflows/publish-cli.yml index 2deb75489..9831f2989 100644 --- a/.github/workflows/publish-cli.yml +++ b/.github/workflows/publish-cli.yml @@ -22,14 +22,14 @@ jobs: target: x86_64-apple-darwin architecture: x64 build: | - yarn build + yarn build:release strip -x *.node - host: windows-latest - build: yarn build + build: yarn build:release target: x86_64-pc-windows-msvc architecture: x64 - host: windows-latest - build: yarn build --target i686-pc-windows-msvc + build: yarn build:release --target i686-pc-windows-msvc target: i686-pc-windows-msvc architecture: x64 - host: ubuntu-18.04 @@ -39,16 +39,16 @@ jobs: set -e && rustup target add x86_64-unknown-linux-gnu && cd tooling/cli/node - yarn build --target x86_64-unknown-linux-gnu --zig --zig-abi-suffix 2.12 && + yarn build:release --target x86_64-unknown-linux-gnu --zig --zig-abi-suffix 2.12 && llvm-strip -x *.node - host: ubuntu-18.04 target: x86_64-unknown-linux-musl docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine - build: set -e && cd tooling/cli/node && yarn build && strip *.node + build: set -e && cd tooling/cli/node && yarn build:release && strip *.node - host: macos-latest target: aarch64-apple-darwin build: | - yarn build --target=aarch64-apple-darwin + yarn build:release --target=aarch64-apple-darwin strip -x *.node - host: ubuntu-18.04 architecture: x64 @@ -57,7 +57,7 @@ jobs: sudo apt-get update sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y build: | - yarn build --target=aarch64-unknown-linux-gnu + yarn build:release --target=aarch64-unknown-linux-gnu aarch64-linux-gnu-strip *.node - host: ubuntu-18.04 architecture: x64 @@ -66,7 +66,7 @@ jobs: sudo apt-get update sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y build: | - yarn build --target=armv7-unknown-linux-gnueabihf + yarn build:release --target=armv7-unknown-linux-gnueabihf arm-linux-gnueabihf-strip *.node - host: ubuntu-18.04 architecture: x64 @@ -76,12 +76,12 @@ jobs: set -e && rustup target add aarch64-unknown-linux-musl && cd tooling/cli/node && - yarn build --target aarch64-unknown-linux-musl && + yarn build:release --target aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node #- host: windows-latest # architecture: x64 # target: aarch64-pc-windows-msvc - # build: yarn build --target aarch64-pc-windows-msvc + # build: yarn build:release --target aarch64-pc-windows-msvc name: stable - ${{ matrix.settings.target }} - node@16 runs-on: ${{ matrix.settings.host }} steps: @@ -173,7 +173,7 @@ jobs: # freebsd-version # cd ./tooling/cli/node/ # yarn install --ignore-scripts --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000 - # yarn build + # yarn build:release # strip -x *.node # rm -rf node_modules # rm -rf ../target diff --git a/tooling/cli/node/package.json b/tooling/cli/node/package.json index 460b40a10..81eff1fee 100644 --- a/tooling/cli/node/package.json +++ b/tooling/cli/node/package.json @@ -52,8 +52,8 @@ }, "scripts": { "artifacts": "napi artifacts", - "build": "napi build --platform --release", - "build:debug": "napi build --platform", + "build:release": "napi build --platform --release", + "build": "napi build --platform", "prepublishOnly": "napi prepublish -t npm", "test": "jest --runInBand --forceExit --no-cache", "version": "napi version", diff --git a/tooling/cli/src/dev.rs b/tooling/cli/src/dev.rs index a929abc20..26217c874 100644 --- a/tooling/cli/src/dev.rs +++ b/tooling/cli/src/dev.rs @@ -78,6 +78,21 @@ pub fn command(options: Options) -> Result<()> { fn command_internal(options: Options) -> Result<()> { let logger = Logger::new("tauri:dev"); + #[cfg(not(debug_assertions))] + match check_for_updates() { + Ok((msg, sleep)) => { + if sleep { + logger.log(msg); + std::thread::sleep(std::time::Duration::from_secs(3)); + } else { + logger.log(msg); + } + } + Err(e) => { + logger.log(e.to_string()); + } + }; + let tauri_path = tauri_dir(); set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?; let merge_config = if let Some(config) = &options.config { @@ -281,6 +296,23 @@ fn command_internal(options: Options) -> Result<()> { } } +#[cfg(not(debug_assertions))] +fn check_for_updates() -> Result<(String, bool)> { + let current_version = crate::info::cli_current_version()?; + let current = semver::Version::parse(¤t_version)?; + + let upstream_version = crate::info::cli_upstream_version()?; + let upstream = semver::Version::parse(&upstream_version)?; + if upstream.gt(¤t) { + let message = format!( + "🚀 A new version of Tauri CLI is avaliable! [{}]", + upstream.to_string() + ); + return Ok((message, true)); + } + Ok(("🎉 Tauri CLI is up-to-date!".into(), false)) +} + fn lookup(dir: &Path, mut f: F) { let mut default_gitignore = std::env::temp_dir(); default_gitignore.push(".tauri-dev"); diff --git a/tooling/cli/src/info.rs b/tooling/cli/src/info.rs index 51ac3510b..8f92e4a19 100644 --- a/tooling/cli/src/info.rs +++ b/tooling/cli/src/info.rs @@ -85,6 +85,43 @@ enum PackageManager { #[clap(about = "Shows information about Tauri dependencies and project configuration")] pub struct Options; +fn version_metadata() -> Result { + serde_json::from_str::(include_str!("../metadata.json")).map_err(Into::into) +} + +#[cfg(not(debug_assertions))] +pub(crate) fn cli_current_version() -> Result { + version_metadata().map(|meta| meta.js_cli.version) +} + +#[cfg(not(debug_assertions))] +pub(crate) fn cli_upstream_version() -> Result { + let upstream_metadata = match ureq::get( + "https://raw.githubusercontent.com/tauri-apps/tauri/dev/tooling/cli/metadata.json", + ) + .call() + { + Ok(r) => r, + Err(ureq::Error::Status(code, _response)) => { + let message = format!("Unable to find updates at the moment. Code: {}", code); + return Err(anyhow::Error::msg(message)); + } + Err(ureq::Error::Transport(transport)) => { + let message = format!( + "Unable to find updates at the moment. Error: {:?}", + transport.kind() + ); + return Err(anyhow::Error::msg(message)); + } + }; + + upstream_metadata + .into_string() + .and_then(|meta_str| Ok(serde_json::from_str::(&meta_str))) + .and_then(|json| Ok(json.unwrap().js_cli.version)) + .map_err(|e| anyhow::Error::new(e)) +} + fn crate_latest_version(name: &str) -> Option { let url = format!("https://docs.rs/crate/{}/", name); match ureq::get(&url).call() { @@ -582,7 +619,7 @@ pub fn command(_options: Options) -> Result<()> { .unwrap_or_default(); panic::set_hook(hook); - let metadata = serde_json::from_str::(include_str!("../metadata.json"))?; + let metadata = version_metadata()?; VersionBlock::new( "Node.js", get_version("node", &[])