Skip to content

Commit

Permalink
feat(cli): check and notify about updates on tauri dev, closes #3789 (
Browse files Browse the repository at this point in the history
#3960)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
codekidX and lucasfernog committed Apr 25, 2022
1 parent 4a40506 commit a649aad
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-dev-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Notify CLI update when running `tauri dev`.
22 changes: 11 additions & 11 deletions .github/workflows/publish-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down
39 changes: 38 additions & 1 deletion tooling/cli/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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", &[])
Expand Down

0 comments on commit a649aad

Please sign in to comment.