Skip to content

Commit

Permalink
fix(core): pull package info from tauri.conf.json if set (#1581)
Browse files Browse the repository at this point in the history
* fix(core): pull package info from tauri.conf.json if set

* fix: codegen
  • Loading branch information
lucasfernog committed Apr 22, 2021
1 parent 5f033db commit 8fd1baf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changes/package-info-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

The package info APIs now checks the `package` object on `tauri.conf.json`.
17 changes: 14 additions & 3 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,25 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
quote!(None)
};

let package_name = if let Some(product_name) = &config.package.product_name {
quote!(#product_name.to_string())
} else {
quote!(env!("CARGO_PKG_NAME").to_string())
};
let package_version = if let Some(version) = &config.package.version {
quote!(#version.to_string())
} else {
quote!(env!("CARGO_PKG_VERSION").to_string())
};

// double braces are purposeful to force the code into a block expression
Ok(quote!(#root::Context {
config: #config,
assets: #assets,
default_window_icon: #default_window_icon,
package_info: #root::api::PackageInfo {
name: env!("CARGO_PKG_NAME"),
version: env!("CARGO_PKG_VERSION")
}
name: #package_name,
version: #package_version,
},
}))
}
25 changes: 24 additions & 1 deletion core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,23 @@ impl Default for BuildConfig {
}
}

/// The package configuration.
#[derive(Debug, Default, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PackageConfig {
/// App name.
pub product_name: Option<String>,
/// App version.
pub version: Option<String>,
}

/// The tauri.conf.json mapper.
#[derive(Debug, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
/// Package settings.
#[serde(default)]
pub package: PackageConfig,
/// The Tauri configuration.
#[serde(default)]
pub tauri: TauriConfig,
Expand Down Expand Up @@ -761,13 +774,23 @@ mod build {
}
}

impl ToTokens for PackageConfig {
fn to_tokens(&self, tokens: &mut TokenStream) {
let product_name = opt_str_lit(self.product_name.as_ref());
let version = opt_str_lit(self.version.as_ref());

literal_struct!(tokens, PackageConfig, product_name, version);
}
}

impl ToTokens for Config {
fn to_tokens(&self, tokens: &mut TokenStream) {
let package = &self.package;
let tauri = &self.tauri;
let build = &self.build;
let plugins = &self.plugins;

literal_struct!(tokens, Config, tauri, build, plugins);
literal_struct!(tokens, Config, package, tauri, build, plugins);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct PackageInfo {
/// App name.
pub name: &'static str,
pub name: String,
/// App version.
pub version: &'static str,
pub version: String,
}

// Not public API
Expand Down
6 changes: 3 additions & 3 deletions core/tauri/src/updater/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ pub(crate) async fn check_update_with_dialog<M: Params>(
// check updates
match self::core::builder()
.urls(&endpoints[..])
.current_version(package_info.version)
.current_version(&package_info.version)
.build()
.await
{
Expand All @@ -414,7 +414,7 @@ pub(crate) async fn check_update_with_dialog<M: Params>(
if updater.should_update && updater_config.dialog {
let body = updater.body.clone().unwrap_or_else(|| String::from(""));
let dialog =
prompt_for_install(&updater.clone(), package_info.name, &body.clone(), pubkey).await;
prompt_for_install(&updater.clone(), &package_info.name, &body.clone(), pubkey).await;

if dialog.is_err() {
send_status_update(
Expand Down Expand Up @@ -468,7 +468,7 @@ pub(crate) fn listener<M: Params>(

match self::core::builder()
.urls(&endpoints[..])
.current_version(package_info.version)
.current_version(&package_info.version)
.build()
.await
{
Expand Down

0 comments on commit 8fd1baf

Please sign in to comment.