Skip to content

Commit

Permalink
refactor: force semver versions, change updater should_install sig (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 25, 2022
1 parent cb807e1 commit 2badbd2
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changes/package-info-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change:** `PackageInfo::version` is now a `semver::Version` instead of a `String`.
5 changes: 5 additions & 0 deletions .changes/should-install-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change**: `UpdateBuilder::should_update` now takes the current version as a `semver::Version` and a `RemoteRelease` struct, allowing you to check other release fields.
1 change: 1 addition & 0 deletions core/tauri-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ walkdir = "2"
brotli = { version = "3", optional = true, default-features = false, features = [ "std" ] }
regex = { version = "1.5.6", optional = true }
uuid = { version = "1", features = [ "v4" ] }
semver = "1"

[target."cfg(windows)".dependencies]
ico = "0.1"
Expand Down
5 changes: 3 additions & 2 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::{ffi::OsStr, str::FromStr};

use proc_macro2::TokenStream;
use quote::quote;
Expand Down Expand Up @@ -220,14 +220,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
quote!(env!("CARGO_PKG_NAME").to_string())
};
let package_version = if let Some(version) = &config.package.version {
semver::Version::from_str(version)?;
quote!(#version.to_string())
} else {
quote!(env!("CARGO_PKG_VERSION").to_string())
};
let package_info = quote!(
#root::PackageInfo {
name: #package_name,
version: #package_version,
version: #package_version.parse().unwrap(),
authors: env!("CARGO_PKG_AUTHORS"),
description: env!("CARGO_PKG_DESCRIPTION"),
}
Expand Down
5 changes: 4 additions & 1 deletion core/tauri-codegen/src/embedded_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub enum EmbeddedAssetsError {

#[error("OUT_DIR env var is not set, do you have a build script?")]
OutDir,

#[error("version error: {0}")]
Version(#[from] semver::Error),
}

/// Represent a directory of assets that are compressed and embedded.
Expand Down Expand Up @@ -261,7 +264,7 @@ impl EmbeddedAssets {
move |mut state, (prefix, entry)| {
let (key, asset) = Self::compress_file(&prefix, entry.path(), &map, &mut state.csp_hashes)?;
state.assets.insert(key, asset);
Ok(state)
Result::<_, EmbeddedAssetsError>::Ok(state)
},
)?;

Expand Down
1 change: 1 addition & 0 deletions core/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ json-patch = "0.2"
glob = { version = "0.3.0", optional = true }
walkdir = { version = "2", optional = true }
memchr = "2.4"
semver = "1"

[target."cfg(target_os = \"linux\")".dependencies]
heck = "0.4"
Expand Down
22 changes: 18 additions & 4 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use heck::ToKebabCase;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use semver::Version;
use serde::{
de::{Deserializer, Error as DeError, Visitor},
Deserialize, Serialize, Serializer,
Expand All @@ -27,6 +28,7 @@ use std::{
fmt::{self, Display},
fs::read_to_string,
path::PathBuf,
str::FromStr,
};

/// Items to help with parsing content into a [`Config`].
Expand Down Expand Up @@ -2182,13 +2184,25 @@ impl<'d> serde::Deserialize<'d> for PackageVersion {
.get("version")
.ok_or_else(|| DeError::custom("JSON must contain a `version` field"))?
.as_str()
.ok_or_else(|| DeError::custom("`version` must be a string"))?;
Ok(PackageVersion(version.into()))
.ok_or_else(|| {
DeError::custom(format!("`{} > version` must be a string", path.display()))
})?;
Ok(PackageVersion(
Version::from_str(version)
.map_err(|_| DeError::custom("`package > version` must be a semver string"))?
.to_string(),
))
} else {
Err(DeError::custom("value is not a path to a JSON object"))
Err(DeError::custom(
"`package > version` value is not a path to a JSON object",
))
}
} else {
Ok(PackageVersion(value.into()))
Ok(PackageVersion(
Version::from_str(value)
.map_err(|_| DeError::custom("`package > version` must be a semver string"))?
.to_string(),
))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-utils/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::BufRead;

/// Read a line breaking in both \n and \r.
///
/// Adapted from https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line
/// Adapted from <https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line>.
pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::Result<usize> {
let mut read = 0;
loop {
Expand Down
3 changes: 2 additions & 1 deletion core/tauri-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use std::fmt::Display;

use semver::Version;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub mod assets;
Expand All @@ -27,7 +28,7 @@ pub struct PackageInfo {
/// App name
pub name: String,
/// App version
pub version: String,
pub version: Version,
/// The crate authors.
pub authors: &'static str,
/// The crate description.
Expand Down
7 changes: 5 additions & 2 deletions core/tauri/src/api/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ pub fn get_matches(cli: &CliConfig, package_info: &PackageInfo) -> crate::api::R
.description()
.unwrap_or(&package_info.description.to_string())
.to_string();
let app = get_app(package_info, &package_info.name, Some(&about), cli);
let version = &*package_info.version.to_string();
let app = get_app(package_info, version, &package_info.name, Some(&about), cli);
match app.try_get_matches() {
Ok(matches) => Ok(get_matches_internal(cli, &matches)),
Err(e) => match ErrorExt::kind(&e) {
Expand Down Expand Up @@ -178,13 +179,14 @@ fn map_matches(config: &CliConfig, matches: &ArgMatches, cli_matches: &mut Match

fn get_app<'a>(
package_info: &'a PackageInfo,
version: &'a str,
command_name: &'a str,
about: Option<&'a String>,
config: &'a CliConfig,
) -> App<'a> {
let mut app = App::new(command_name)
.author(package_info.authors)
.version(&*package_info.version);
.version(version);

if let Some(about) = about {
app = app.about(&**about);
Expand All @@ -210,6 +212,7 @@ fn get_app<'a>(
for (subcommand_name, subcommand) in subcommands {
let clap_subcommand = get_app(
package_info,
version,
subcommand_name,
subcommand.description(),
subcommand,
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/endpoints/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum Cmd {

impl Cmd {
fn get_app_version<R: Runtime>(context: InvokeContext<R>) -> super::Result<String> {
Ok(context.package_info.version)
Ok(context.package_info.version.to_string())
}

fn get_app_name<R: Runtime>(context: InvokeContext<R>) -> super::Result<String> {
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn mock_context<A: Assets>(assets: A) -> crate::Context<A> {
system_tray_icon: None,
package_info: crate::PackageInfo {
name: "test".into(),
version: "0.1.0".into(),
version: "0.1.0".parse().unwrap(),
authors: "Tauri",
description: "Tauri test",
},
Expand Down
Loading

0 comments on commit 2badbd2

Please sign in to comment.