Skip to content

Commit ba0206d

Browse files
authored
feat(core): allow swapping the assets implementation (#9141)
1 parent db1ea98 commit ba0206d

File tree

11 files changed

+83
-42
lines changed

11 files changed

+83
-42
lines changed

.changes/codegen-set-assets.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-macros": patch:feat
3+
"tauri-codegen": patch:feat
4+
---
5+
6+
The `Context` codegen now accepts a `assets` input to define a custom `tauri::Assets` implementation.

.changes/context-assets-unbox.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:breaking
3+
---
4+
5+
`Context::assets` now returns `&dyn Assets` instead of `Box<&dyn Assets>`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:breaking
3+
---
4+
5+
The `Context` type no longer uses the `<A: Assets>` generic so the assets implementation can be swapped with `Context::assets_mut`.

core/tauri-build/src/codegen/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl CodegenContext {
128128
// outside the tauri crate, making the ::tauri root valid.
129129
root: quote::quote!(::tauri),
130130
capabilities: self.capabilities,
131+
assets: None,
131132
})?;
132133

133134
// get the full output file path

core/tauri-codegen/src/context.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use proc_macro2::TokenStream;
1212
use quote::quote;
1313
use sha2::{Digest, Sha256};
1414

15+
use syn::Expr;
1516
use tauri_utils::acl::capability::{Capability, CapabilityFile};
1617
use tauri_utils::acl::manifest::Manifest;
1718
use tauri_utils::acl::resolved::Resolved;
@@ -36,6 +37,8 @@ pub struct ContextData {
3637
pub root: TokenStream,
3738
/// Additional capabilities to include.
3839
pub capabilities: Option<Vec<PathBuf>>,
40+
/// The custom assets implementation
41+
pub assets: Option<Expr>,
3942
}
4043

4144
fn inject_script_hashes(document: &NodeRef, key: &AssetKey, csp_hashes: &mut CspHashes) {
@@ -132,6 +135,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
132135
config_parent,
133136
root,
134137
capabilities: additional_capabilities,
138+
assets,
135139
} = data;
136140

137141
let target = std::env::var("TARGET")
@@ -163,10 +167,13 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
163167
options = options.with_csp();
164168
}
165169

166-
let assets = if dev && config.build.dev_url.is_some() {
167-
Default::default()
170+
let assets = if let Some(assets) = assets {
171+
quote!(#assets)
172+
} else if dev && config.build.dev_url.is_some() {
173+
let assets = EmbeddedAssets::default();
174+
quote!(#assets)
168175
} else {
169-
match &config.build.frontend_dist {
176+
let assets = match &config.build.frontend_dist {
170177
Some(url) => match url {
171178
FrontendDist::Url(_url) => Default::default(),
172179
FrontendDist::Directory(path) => {
@@ -190,7 +197,8 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
190197
_ => unimplemented!(),
191198
},
192199
None => Default::default(),
193-
}
200+
};
201+
quote!(#assets)
194202
};
195203

196204
let out_dir = {

core/tauri-macros/src/context.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) struct ContextItems {
1717
config_file: PathBuf,
1818
root: syn::Path,
1919
capabilities: Option<Vec<PathBuf>>,
20+
assets: Option<Expr>,
2021
}
2122

2223
impl Parse for ContextItems {
@@ -29,6 +30,7 @@ impl Parse for ContextItems {
2930

3031
let mut root = None;
3132
let mut capabilities = None;
33+
let mut assets = None;
3234
let config_file = input.parse::<LitStr>().ok().map(|raw| {
3335
let _ = input.parse::<Token![,]>();
3436
let path = PathBuf::from(raw.value());
@@ -57,32 +59,44 @@ impl Parse for ContextItems {
5759
root.replace(p);
5860
}
5961
Meta::NameValue(v) => {
60-
if *v.path.require_ident()? == "capabilities" {
61-
if let Expr::Array(array) = v.value {
62-
capabilities.replace(
63-
array
64-
.elems
65-
.into_iter()
66-
.map(|e| {
67-
if let Expr::Lit(ExprLit {
68-
attrs: _,
69-
lit: Lit::Str(s),
70-
}) = e
71-
{
72-
Ok(s.value().into())
73-
} else {
74-
Err(syn::Error::new(
75-
input.span(),
76-
"unexpected expression for capability",
77-
))
78-
}
79-
})
80-
.collect::<Result<Vec<_>, syn::Error>>()?,
81-
);
82-
} else {
62+
let ident = v.path.require_ident()?;
63+
match ident.to_string().as_str() {
64+
"capabilities" => {
65+
if let Expr::Array(array) = v.value {
66+
capabilities.replace(
67+
array
68+
.elems
69+
.into_iter()
70+
.map(|e| {
71+
if let Expr::Lit(ExprLit {
72+
attrs: _,
73+
lit: Lit::Str(s),
74+
}) = e
75+
{
76+
Ok(s.value().into())
77+
} else {
78+
Err(syn::Error::new(
79+
input.span(),
80+
"unexpected expression for capability",
81+
))
82+
}
83+
})
84+
.collect::<Result<Vec<_>, syn::Error>>()?,
85+
);
86+
} else {
87+
return Err(syn::Error::new(
88+
input.span(),
89+
"unexpected value for capabilities",
90+
));
91+
}
92+
}
93+
"assets" => {
94+
assets.replace(v.value);
95+
}
96+
name => {
8397
return Err(syn::Error::new(
8498
input.span(),
85-
"unexpected value for capabilities",
99+
format!("unknown attribute {name}"),
86100
));
87101
}
88102
}
@@ -113,6 +127,7 @@ impl Parse for ContextItems {
113127
}
114128
}),
115129
capabilities,
130+
assets,
116131
})
117132
}
118133
}
@@ -126,6 +141,7 @@ pub(crate) fn generate_context(context: ContextItems) -> TokenStream {
126141
config_parent,
127142
root: context.root.to_token_stream(),
128143
capabilities: context.capabilities,
144+
assets: context.assets,
129145
})
130146
.and_then(|data| context_codegen(data).map_err(|e| e.to_string()));
131147

core/tauri/src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
},
2020
sealed::{ManagerBase, RuntimeOrDispatch},
2121
utils::config::Config,
22-
utils::{assets::Assets, Env},
22+
utils::Env,
2323
webview::PageLoadPayload,
2424
Context, DeviceEventFilter, EventLoopMessage, Manager, Monitor, Runtime, Scopes, StateManager,
2525
Theme, Webview, WebviewWindowBuilder, Window,
@@ -1581,7 +1581,7 @@ tauri::Builder::default()
15811581
feature = "tracing",
15821582
tracing::instrument(name = "app::build", skip_all)
15831583
)]
1584-
pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
1584+
pub fn build(mut self, context: Context) -> crate::Result<App<R>> {
15851585
#[cfg(target_os = "macos")]
15861586
if self.menu.is_none() && self.enable_macos_default_menu {
15871587
self.menu = Some(Box::new(|app_handle| {
@@ -1749,7 +1749,7 @@ tauri::Builder::default()
17491749
}
17501750

17511751
/// Runs the configured Tauri application.
1752-
pub fn run<A: Assets>(self, context: Context<A>) -> crate::Result<()> {
1752+
pub fn run(self, context: Context) -> crate::Result<()> {
17531753
self.build(context)?.run(|_, _| {});
17541754
Ok(())
17551755
}

core/tauri/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ pub fn dev() -> bool {
343343
/// # Stability
344344
/// This is the output of the [`generate_context`] macro, and is not considered part of the stable API.
345345
/// Unless you know what you are doing and are prepared for this type to have breaking changes, do not create it yourself.
346-
pub struct Context<A: Assets> {
346+
pub struct Context {
347347
pub(crate) config: Config,
348-
pub(crate) assets: Box<A>,
348+
pub(crate) assets: Box<dyn Assets>,
349349
pub(crate) default_window_icon: Option<image::Image<'static>>,
350350
pub(crate) app_icon: Option<Vec<u8>>,
351351
#[cfg(all(desktop, feature = "tray-icon"))]
@@ -356,7 +356,7 @@ pub struct Context<A: Assets> {
356356
pub(crate) runtime_authority: RuntimeAuthority,
357357
}
358358

359-
impl<A: Assets> fmt::Debug for Context<A> {
359+
impl fmt::Debug for Context {
360360
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
361361
let mut d = f.debug_struct("Context");
362362
d.field("config", &self.config)
@@ -372,7 +372,7 @@ impl<A: Assets> fmt::Debug for Context<A> {
372372
}
373373
}
374374

375-
impl<A: Assets> Context<A> {
375+
impl Context {
376376
/// The config the application was prepared with.
377377
#[inline(always)]
378378
pub fn config(&self) -> &Config {
@@ -387,13 +387,13 @@ impl<A: Assets> Context<A> {
387387

388388
/// The assets to be served directly by Tauri.
389389
#[inline(always)]
390-
pub fn assets(&self) -> &A {
391-
&self.assets
390+
pub fn assets(&self) -> &dyn Assets {
391+
self.assets.as_ref()
392392
}
393393

394394
/// A mutable reference to the assets to be served directly by Tauri.
395395
#[inline(always)]
396-
pub fn assets_mut(&mut self) -> &mut A {
396+
pub fn assets_mut(&mut self) -> &mut Box<dyn Assets> {
397397
&mut self.assets
398398
}
399399

@@ -459,7 +459,7 @@ impl<A: Assets> Context<A> {
459459
#[allow(clippy::too_many_arguments)]
460460
pub fn new(
461461
config: Config,
462-
assets: Box<A>,
462+
assets: Box<dyn Assets>,
463463
default_window_icon: Option<image::Image<'static>>,
464464
app_icon: Option<Vec<u8>>,
465465
package_info: PackageInfo,

core/tauri/src/manager/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<R: Runtime> fmt::Debug for AppManager<R> {
216216
impl<R: Runtime> AppManager<R> {
217217
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
218218
pub(crate) fn with_handlers(
219-
#[allow(unused_mut)] mut context: Context<impl Assets>,
219+
#[allow(unused_mut)] mut context: Context,
220220
plugins: PluginStore<R>,
221221
invoke_handler: Box<InvokeHandler<R>>,
222222
on_page_load: Option<Arc<OnPageLoad<R>>>,

core/tauri/src/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn noop_assets() -> NoopAsset {
9494
}
9595

9696
/// Creates a new [`crate::Context`] for testing.
97-
pub fn mock_context<A: Assets>(assets: A) -> crate::Context<A> {
97+
pub fn mock_context<A: Assets>(assets: A) -> crate::Context {
9898
Context {
9999
config: Config {
100100
schema: None,

0 commit comments

Comments
 (0)