Ver código fonte

feat(cli): check and notify about updates on `tauri dev`, closes #3789 (#3960)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Ashish Shekar 3 anos atrás
pai
commit
a649aad7ad

+ 6 - 0
.changes/cli-dev-update.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+Notify CLI update when running `tauri dev`.

+ 11 - 11
.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

+ 2 - 2
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",

+ 32 - 0
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(&current_version)?;
+
+  let upstream_version = crate::info::cli_upstream_version()?;
+  let upstream = semver::Version::parse(&upstream_version)?;
+  if upstream.gt(&current) {
+    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<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
   let mut default_gitignore = std::env::temp_dir();
   default_gitignore.push(".tauri-dev");

+ 38 - 1
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<VersionMetadata> {
+  serde_json::from_str::<VersionMetadata>(include_str!("../metadata.json")).map_err(Into::into)
+}
+
+#[cfg(not(debug_assertions))]
+pub(crate) fn cli_current_version() -> Result<String> {
+  version_metadata().map(|meta| meta.js_cli.version)
+}
+
+#[cfg(not(debug_assertions))]
+pub(crate) fn cli_upstream_version() -> Result<String> {
+  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::<VersionMetadata>(&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<String> {
   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::<VersionMetadata>(include_str!("../metadata.json"))?;
+  let metadata = version_metadata()?;
   VersionBlock::new(
     "Node.js",
     get_version("node", &[])