Skip to content

Commit 2badbd2

Browse files
authored
refactor: force semver versions, change updater should_install sig (#4215)
1 parent cb807e1 commit 2badbd2

File tree

15 files changed

+100
-55
lines changed

15 files changed

+100
-55
lines changed

.changes/package-info-version.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** `PackageInfo::version` is now a `semver::Version` instead of a `String`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**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.

core/tauri-codegen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ walkdir = "2"
2525
brotli = { version = "3", optional = true, default-features = false, features = [ "std" ] }
2626
regex = { version = "1.5.6", optional = true }
2727
uuid = { version = "1", features = [ "v4" ] }
28+
semver = "1"
2829

2930
[target."cfg(windows)".dependencies]
3031
ico = "0.1"

core/tauri-codegen/src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::ffi::OsStr;
65
use std::path::{Path, PathBuf};
6+
use std::{ffi::OsStr, str::FromStr};
77

88
use proc_macro2::TokenStream;
99
use quote::quote;
@@ -220,14 +220,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
220220
quote!(env!("CARGO_PKG_NAME").to_string())
221221
};
222222
let package_version = if let Some(version) = &config.package.version {
223+
semver::Version::from_str(version)?;
223224
quote!(#version.to_string())
224225
} else {
225226
quote!(env!("CARGO_PKG_VERSION").to_string())
226227
};
227228
let package_info = quote!(
228229
#root::PackageInfo {
229230
name: #package_name,
230-
version: #package_version,
231+
version: #package_version.parse().unwrap(),
231232
authors: env!("CARGO_PKG_AUTHORS"),
232233
description: env!("CARGO_PKG_DESCRIPTION"),
233234
}

core/tauri-codegen/src/embedded_assets.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ pub enum EmbeddedAssetsError {
5555

5656
#[error("OUT_DIR env var is not set, do you have a build script?")]
5757
OutDir,
58+
59+
#[error("version error: {0}")]
60+
Version(#[from] semver::Error),
5861
}
5962

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

core/tauri-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ json-patch = "0.2"
3333
glob = { version = "0.3.0", optional = true }
3434
walkdir = { version = "2", optional = true }
3535
memchr = "2.4"
36+
semver = "1"
3637

3738
[target."cfg(target_os = \"linux\")".dependencies]
3839
heck = "0.4"

core/tauri-utils/src/config.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use heck::ToKebabCase;
1515
#[cfg(feature = "schema")]
1616
use schemars::JsonSchema;
17+
use semver::Version;
1718
use serde::{
1819
de::{Deserializer, Error as DeError, Visitor},
1920
Deserialize, Serialize, Serializer,
@@ -27,6 +28,7 @@ use std::{
2728
fmt::{self, Display},
2829
fs::read_to_string,
2930
path::PathBuf,
31+
str::FromStr,
3032
};
3133

3234
/// Items to help with parsing content into a [`Config`].
@@ -2182,13 +2184,25 @@ impl<'d> serde::Deserialize<'d> for PackageVersion {
21822184
.get("version")
21832185
.ok_or_else(|| DeError::custom("JSON must contain a `version` field"))?
21842186
.as_str()
2185-
.ok_or_else(|| DeError::custom("`version` must be a string"))?;
2186-
Ok(PackageVersion(version.into()))
2187+
.ok_or_else(|| {
2188+
DeError::custom(format!("`{} > version` must be a string", path.display()))
2189+
})?;
2190+
Ok(PackageVersion(
2191+
Version::from_str(version)
2192+
.map_err(|_| DeError::custom("`package > version` must be a semver string"))?
2193+
.to_string(),
2194+
))
21872195
} else {
2188-
Err(DeError::custom("value is not a path to a JSON object"))
2196+
Err(DeError::custom(
2197+
"`package > version` value is not a path to a JSON object",
2198+
))
21892199
}
21902200
} else {
2191-
Ok(PackageVersion(value.into()))
2201+
Ok(PackageVersion(
2202+
Version::from_str(value)
2203+
.map_err(|_| DeError::custom("`package > version` must be a semver string"))?
2204+
.to_string(),
2205+
))
21922206
}
21932207
}
21942208
}

core/tauri-utils/src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::io::BufRead;
88

99
/// Read a line breaking in both \n and \r.
1010
///
11-
/// Adapted from https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line
11+
/// Adapted from <https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line>.
1212
pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::Result<usize> {
1313
let mut read = 0;
1414
loop {

core/tauri-utils/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use std::fmt::Display;
99

10+
use semver::Version;
1011
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1112

1213
pub mod assets;
@@ -27,7 +28,7 @@ pub struct PackageInfo {
2728
/// App name
2829
pub name: String,
2930
/// App version
30-
pub version: String,
31+
pub version: Version,
3132
/// The crate authors.
3233
pub authors: &'static str,
3334
/// The crate description.

core/tauri/src/api/cli.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ pub fn get_matches(cli: &CliConfig, package_info: &PackageInfo) -> crate::api::R
102102
.description()
103103
.unwrap_or(&package_info.description.to_string())
104104
.to_string();
105-
let app = get_app(package_info, &package_info.name, Some(&about), cli);
105+
let version = &*package_info.version.to_string();
106+
let app = get_app(package_info, version, &package_info.name, Some(&about), cli);
106107
match app.try_get_matches() {
107108
Ok(matches) => Ok(get_matches_internal(cli, &matches)),
108109
Err(e) => match ErrorExt::kind(&e) {
@@ -178,13 +179,14 @@ fn map_matches(config: &CliConfig, matches: &ArgMatches, cli_matches: &mut Match
178179

179180
fn get_app<'a>(
180181
package_info: &'a PackageInfo,
182+
version: &'a str,
181183
command_name: &'a str,
182184
about: Option<&'a String>,
183185
config: &'a CliConfig,
184186
) -> App<'a> {
185187
let mut app = App::new(command_name)
186188
.author(package_info.authors)
187-
.version(&*package_info.version);
189+
.version(version);
188190

189191
if let Some(about) = about {
190192
app = app.about(&**about);
@@ -210,6 +212,7 @@ fn get_app<'a>(
210212
for (subcommand_name, subcommand) in subcommands {
211213
let clap_subcommand = get_app(
212214
package_info,
215+
version,
213216
subcommand_name,
214217
subcommand.description(),
215218
subcommand,

0 commit comments

Comments
 (0)