Skip to content

Commit 8fd1baf

Browse files
authored
fix(core): pull package info from tauri.conf.json if set (#1581)
* fix(core): pull package info from tauri.conf.json if set * fix: codegen
1 parent 5f033db commit 8fd1baf

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

.changes/package-info-config.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+
The package info APIs now checks the `package` object on `tauri.conf.json`.

core/tauri-codegen/src/context.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,25 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
5050
quote!(None)
5151
};
5252

53+
let package_name = if let Some(product_name) = &config.package.product_name {
54+
quote!(#product_name.to_string())
55+
} else {
56+
quote!(env!("CARGO_PKG_NAME").to_string())
57+
};
58+
let package_version = if let Some(version) = &config.package.version {
59+
quote!(#version.to_string())
60+
} else {
61+
quote!(env!("CARGO_PKG_VERSION").to_string())
62+
};
63+
5364
// double braces are purposeful to force the code into a block expression
5465
Ok(quote!(#root::Context {
5566
config: #config,
5667
assets: #assets,
5768
default_window_icon: #default_window_icon,
5869
package_info: #root::api::PackageInfo {
59-
name: env!("CARGO_PKG_NAME"),
60-
version: env!("CARGO_PKG_VERSION")
61-
}
70+
name: #package_name,
71+
version: #package_version,
72+
},
6273
}))
6374
}

core/tauri-utils/src/config.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,23 @@ impl Default for BuildConfig {
382382
}
383383
}
384384

385+
/// The package configuration.
386+
#[derive(Debug, Default, PartialEq, Deserialize)]
387+
#[serde(rename_all = "camelCase")]
388+
pub struct PackageConfig {
389+
/// App name.
390+
pub product_name: Option<String>,
391+
/// App version.
392+
pub version: Option<String>,
393+
}
394+
385395
/// The tauri.conf.json mapper.
386396
#[derive(Debug, PartialEq, Deserialize)]
387397
#[serde(rename_all = "camelCase")]
388398
pub struct Config {
399+
/// Package settings.
400+
#[serde(default)]
401+
pub package: PackageConfig,
389402
/// The Tauri configuration.
390403
#[serde(default)]
391404
pub tauri: TauriConfig,
@@ -761,13 +774,23 @@ mod build {
761774
}
762775
}
763776

777+
impl ToTokens for PackageConfig {
778+
fn to_tokens(&self, tokens: &mut TokenStream) {
779+
let product_name = opt_str_lit(self.product_name.as_ref());
780+
let version = opt_str_lit(self.version.as_ref());
781+
782+
literal_struct!(tokens, PackageConfig, product_name, version);
783+
}
784+
}
785+
764786
impl ToTokens for Config {
765787
fn to_tokens(&self, tokens: &mut TokenStream) {
788+
let package = &self.package;
766789
let tauri = &self.tauri;
767790
let build = &self.build;
768791
let plugins = &self.plugins;
769792

770-
literal_struct!(tokens, Config, tauri, build, plugins);
793+
literal_struct!(tokens, Config, package, tauri, build, plugins);
771794
}
772795
}
773796
}

core/tauri/src/api/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ pub type Result<T> = std::result::Result<T, Error>;
6060
#[derive(Debug, Clone)]
6161
pub struct PackageInfo {
6262
/// App name.
63-
pub name: &'static str,
63+
pub name: String,
6464
/// App version.
65-
pub version: &'static str,
65+
pub version: String,
6666
}
6767

6868
// Not public API

core/tauri/src/updater/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pub(crate) async fn check_update_with_dialog<M: Params>(
403403
// check updates
404404
match self::core::builder()
405405
.urls(&endpoints[..])
406-
.current_version(package_info.version)
406+
.current_version(&package_info.version)
407407
.build()
408408
.await
409409
{
@@ -414,7 +414,7 @@ pub(crate) async fn check_update_with_dialog<M: Params>(
414414
if updater.should_update && updater_config.dialog {
415415
let body = updater.body.clone().unwrap_or_else(|| String::from(""));
416416
let dialog =
417-
prompt_for_install(&updater.clone(), package_info.name, &body.clone(), pubkey).await;
417+
prompt_for_install(&updater.clone(), &package_info.name, &body.clone(), pubkey).await;
418418

419419
if dialog.is_err() {
420420
send_status_update(
@@ -468,7 +468,7 @@ pub(crate) fn listener<M: Params>(
468468

469469
match self::core::builder()
470470
.urls(&endpoints[..])
471-
.current_version(package_info.version)
471+
.current_version(&package_info.version)
472472
.build()
473473
.await
474474
{

0 commit comments

Comments
 (0)