forked from DioxusLabs/dioxus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdioxus_config.rs
More file actions
304 lines (271 loc) · 9.9 KB
/
dioxus_config.rs
File metadata and controls
304 lines (271 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use crate::config::component::ComponentConfig;
use super::*;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub(crate) struct DioxusConfig {
#[serde(default)]
pub(crate) application: ApplicationConfig,
#[serde(default)]
pub(crate) web: WebConfig,
#[serde(default)]
pub(crate) bundle: BundleConfig,
#[serde(default)]
pub(crate) components: ComponentConfig,
/// Unified permissions configuration.
/// Permissions declared here are automatically mapped to platform-specific
/// identifiers (AndroidManifest.xml, Info.plist, etc.)
#[serde(default)]
pub(crate) permissions: PermissionsConfig,
/// Unified deep linking configuration.
/// URL schemes and universal links declared here are mapped to platform-specific
/// configurations. Use `[ios]`, `[android]`, `[macos]` sections for overrides.
#[serde(default)]
pub(crate) deep_links: DeepLinkConfig,
/// Unified background mode configuration.
/// Background capabilities declared here are mapped to platform-specific
/// configurations. Use `[ios]`, `[android]` sections for overrides.
#[serde(default)]
pub(crate) background: BackgroundConfig,
/// iOS-specific configuration.
#[serde(default)]
pub(crate) ios: IosConfig,
/// Android-specific configuration.
#[serde(default)]
pub(crate) android: AndroidConfig,
/// macOS-specific configuration.
#[serde(default)]
pub(crate) macos: MacosConfig,
/// Windows-specific configuration.
#[serde(default)]
pub(crate) windows: WindowsConfig,
/// Linux-specific configuration.
#[serde(default)]
pub(crate) linux: LinuxConfig,
/// Custom renderer configuration for projects that use `dioxus-core` with their own renderer.
///
/// When present, this overrides the default renderer autodetection and feature injection.
/// Existing Dioxus projects (without this section) are unaffected.
///
/// ```toml
/// [renderer]
/// name = "my-renderer"
/// default_platform = "desktop"
///
/// [renderer.features]
/// desktop = []
/// web = ["my-web"]
/// ios = ["my-mobile"]
/// android = ["my-mobile"]
/// ```
#[serde(default)]
pub(crate) renderer: RendererConfig,
}
/// Configuration for custom (non-dioxus) renderers.
///
/// Projects that use `dioxus-core` directly with their own renderer can use this section
/// to declare platform-to-feature mappings so `dx serve`, `dx build`, and `dx bundle` work
/// without pulling in dioxus's built-in renderers.
#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
pub(crate) struct RendererConfig {
/// Display name for the renderer (shown in TUI).
#[serde(default)]
pub(crate) name: Option<String>,
/// Default platform when none is specified on the CLI.
///
/// Must be one of: `"web"`, `"macos"`, `"windows"`, `"linux"`, `"ios"`, `"android"`,
/// `"server"`, `"liveview"`.
#[serde(default)]
pub(crate) default_platform: Option<String>,
/// Map from platform name to cargo features to enable.
///
/// Keys are platform identifiers (e.g., `"desktop"`, `"web"`, `"ios"`).
/// Values are lists of cargo feature names to pass via `--features`.
/// An empty list means "build with default features, don't inject any extra".
#[serde(default)]
pub(crate) features: HashMap<String, Vec<String>>,
}
impl RendererConfig {
/// Returns `true` if a custom renderer is configured.
pub(crate) fn is_custom(&self) -> bool {
self.name.is_some() || !self.features.is_empty()
}
/// Look up custom features for a platform, trying each key in order.
///
/// This allows fallback chains like `["macos", "desktop"]` so platform-specific
/// keys take priority over generic ones.
pub(crate) fn features_for_platform(&self, keys: &[&str]) -> Option<Vec<String>> {
for key in keys {
if let Some(feats) = self.features.get(*key) {
return Some(feats.clone());
}
}
None
}
}
/// Platform identifier for bundle resolution.
/// This is separate from the CLI's Platform enum which includes Server and Unknown variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BundlePlatform {
Ios,
Android,
MacOS,
Windows,
Linux,
Web,
}
impl From<crate::BundleFormat> for BundlePlatform {
fn from(format: crate::BundleFormat) -> Self {
match format {
crate::BundleFormat::Ios => BundlePlatform::Ios,
crate::BundleFormat::Android => BundlePlatform::Android,
crate::BundleFormat::MacOS => BundlePlatform::MacOS,
crate::BundleFormat::Windows => BundlePlatform::Windows,
crate::BundleFormat::Linux => BundlePlatform::Linux,
crate::BundleFormat::Web | crate::BundleFormat::Server => BundlePlatform::Web,
}
}
}
impl DioxusConfig {
/// Get the resolved bundle identifier for a specific platform.
/// Platform-specific identifiers override the base bundle identifier.
pub fn resolved_identifier(&self, platform: BundlePlatform) -> Option<&str> {
let platform_override = match platform {
BundlePlatform::Ios => self.ios.identifier.as_deref(),
BundlePlatform::Android => self.android.identifier.as_deref(),
BundlePlatform::MacOS => self.macos.identifier.as_deref(),
BundlePlatform::Windows => self.windows.identifier.as_deref(),
BundlePlatform::Linux => self.linux.identifier.as_deref(),
BundlePlatform::Web => None,
};
platform_override.or(self.bundle.identifier.as_deref())
}
}
impl Default for DioxusConfig {
fn default() -> Self {
Self {
application: ApplicationConfig {
asset_dir: None,
out_dir: None,
public_dir: Some("public".into()),
tailwind_input: None,
tailwind_output: None,
ios_info_plist: None,
android_manifest: None,
android_main_activity: None,
android_min_sdk_version: None,
macos_info_plist: None,
ios_entitlements: None,
macos_entitlements: None,
},
web: WebConfig {
app: WebAppConfig {
title: default_title(),
base_path: None,
},
proxy: vec![],
watcher: Default::default(),
resource: WebResourceConfig {
dev: WebDevResourceConfig {
style: vec![],
script: vec![],
},
style: Some(vec![]),
script: Some(vec![]),
},
https: WebHttpsConfig {
enabled: None,
mkcert: None,
key_path: None,
cert_path: None,
},
pre_compress: false,
wasm_opt: Default::default(),
},
bundle: BundleConfig::default(),
components: ComponentConfig::default(),
permissions: PermissionsConfig::default(),
deep_links: DeepLinkConfig::default(),
background: BackgroundConfig::default(),
ios: IosConfig::default(),
android: AndroidConfig::default(),
macos: MacosConfig::default(),
windows: WindowsConfig::default(),
linux: LinuxConfig::default(),
renderer: RendererConfig::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn static_dir_defaults_to_public() {
let config = DioxusConfig::default();
assert_eq!(
config.application.public_dir,
Some(std::path::PathBuf::from("public"))
);
}
#[test]
fn static_dir_can_be_overridden() {
let source = r#"
[application]
public_dir = "public2"
"#;
let config: DioxusConfig = toml::from_str(source).expect("parse config");
assert_eq!(
config.application.public_dir.as_deref(),
Some(std::path::Path::new("public2"))
);
}
#[test]
fn static_dir_can_be_disabled() {
let source = r#"
[application]
public_dir = ""
"#;
let config: DioxusConfig = toml::from_str(source).expect("parse config");
assert_eq!(config.application.public_dir.as_deref(), None);
}
#[test]
fn renderer_config_absent_is_not_custom() {
let config = DioxusConfig::default();
assert!(!config.renderer.is_custom());
assert!(config.renderer.features.is_empty());
}
#[test]
fn renderer_config_parses_from_toml() {
let source = r#"
[renderer]
name = "tanzo"
default_platform = "desktop"
[renderer.features]
desktop = []
web = ["tanzo-web"]
ios = ["tanzo-mobile"]
android = ["tanzo-mobile"]
"#;
let config: DioxusConfig = toml::from_str(source).expect("parse config");
assert!(config.renderer.is_custom());
assert_eq!(config.renderer.name.as_deref(), Some("tanzo"));
assert_eq!(config.renderer.default_platform.as_deref(), Some("desktop"));
assert_eq!(
config.renderer.features_for_platform(&["desktop"]),
Some(vec![])
);
assert_eq!(
config.renderer.features_for_platform(&["web"]),
Some(vec!["tanzo-web".to_string()])
);
assert_eq!(
config.renderer.features_for_platform(&["macos", "desktop"]),
Some(vec![])
);
assert_eq!(
config.renderer.features_for_platform(&["nonexistent"]),
None
);
}
}