33// SPDX-License-Identifier: MIT
44
55use crate :: config:: Config ;
6+ use crate :: platform:: Target ;
67use json_patch:: merge;
78use serde:: de:: DeserializeOwned ;
89use serde_json:: Value ;
@@ -47,47 +48,29 @@ impl ConfigFormat {
4748 }
4849 }
4950
50- fn into_platform_file_name ( self ) -> & ' static str {
51+ fn into_platform_file_name ( self , target : Target ) -> & ' static str {
5152 match self {
52- Self :: Json => {
53- if cfg ! ( target_os = "macos" ) {
54- "tauri.macos.conf.json"
55- } else if cfg ! ( windows) {
56- "tauri.windows.conf.json"
57- } else if cfg ! ( target_os = "android" ) {
58- "tauri.android.conf.json"
59- } else if cfg ! ( target_os = "ios" ) {
60- "tauri.ios.conf.json"
61- } else {
62- "tauri.linux.conf.json"
63- }
64- }
65- Self :: Json5 => {
66- if cfg ! ( target_os = "macos" ) {
67- "tauri.macos.conf.json5"
68- } else if cfg ! ( windows) {
69- "tauri.windows.conf.json5"
70- } else if cfg ! ( target_os = "android" ) {
71- "tauri.android.conf.json"
72- } else if cfg ! ( target_os = "ios" ) {
73- "tauri.ios.conf.json"
74- } else {
75- "tauri.linux.conf.json5"
76- }
77- }
78- Self :: Toml => {
79- if cfg ! ( target_os = "macos" ) {
80- "Tauri.macos.toml"
81- } else if cfg ! ( windows) {
82- "Tauri.windows.toml"
83- } else if cfg ! ( target_os = "android" ) {
84- "tauri.android.toml"
85- } else if cfg ! ( target_os = "ios" ) {
86- "tauri.ios.toml"
87- } else {
88- "Tauri.linux.toml"
89- }
90- }
53+ Self :: Json => match target {
54+ Target :: Darwin => "tauri.macos.conf.json" ,
55+ Target :: Windows => "tauri.windows.conf.json" ,
56+ Target :: Linux => "tauri.linux.conf.json" ,
57+ Target :: Android => "tauri.android.conf.json" ,
58+ Target :: Ios => "tauri.ios.conf.json" ,
59+ } ,
60+ Self :: Json5 => match target {
61+ Target :: Darwin => "tauri.macos.conf.json5" ,
62+ Target :: Windows => "tauri.windows.conf.json5" ,
63+ Target :: Linux => "tauri.linux.conf.json5" ,
64+ Target :: Android => "tauri.android.conf.json5" ,
65+ Target :: Ios => "tauri.ios.conf.json5" ,
66+ } ,
67+ Self :: Toml => match target {
68+ Target :: Darwin => "Tauri.macos.toml" ,
69+ Target :: Windows => "Tauri.windows.toml" ,
70+ Target :: Linux => "Tauri.linux.toml" ,
71+ Target :: Android => "Tauri.android.toml" ,
72+ Target :: Ios => "Tauri.ios.toml" ,
73+ } ,
9174 }
9275 }
9376}
@@ -154,28 +137,28 @@ pub enum ConfigError {
154137}
155138
156139/// Determines if the given folder has a configuration file.
157- pub fn folder_has_configuration_file ( folder : & Path ) -> bool {
140+ pub fn folder_has_configuration_file ( target : Target , folder : & Path ) -> bool {
158141 folder. join ( ConfigFormat :: Json . into_file_name ( ) ) . exists ( )
159142 || folder. join ( ConfigFormat :: Json5 . into_file_name ( ) ) . exists ( )
160143 || folder. join ( ConfigFormat :: Toml . into_file_name ( ) ) . exists ( )
161144 // platform file names
162- || folder. join ( ConfigFormat :: Json . into_platform_file_name ( ) ) . exists ( )
163- || folder. join ( ConfigFormat :: Json5 . into_platform_file_name ( ) ) . exists ( )
164- || folder. join ( ConfigFormat :: Toml . into_platform_file_name ( ) ) . exists ( )
145+ || folder. join ( ConfigFormat :: Json . into_platform_file_name ( target ) ) . exists ( )
146+ || folder. join ( ConfigFormat :: Json5 . into_platform_file_name ( target ) ) . exists ( )
147+ || folder. join ( ConfigFormat :: Toml . into_platform_file_name ( target ) ) . exists ( )
165148}
166149
167150/// Determines if the given file path represents a Tauri configuration file.
168- pub fn is_configuration_file ( path : & Path ) -> bool {
151+ pub fn is_configuration_file ( target : Target , path : & Path ) -> bool {
169152 path
170153 . file_name ( )
171154 . map ( |file_name| {
172155 file_name == OsStr :: new ( ConfigFormat :: Json . into_file_name ( ) )
173156 || file_name == OsStr :: new ( ConfigFormat :: Json5 . into_file_name ( ) )
174157 || file_name == OsStr :: new ( ConfigFormat :: Toml . into_file_name ( ) )
175158 // platform file names
176- || file_name == OsStr :: new ( ConfigFormat :: Json . into_platform_file_name ( ) )
177- || file_name == OsStr :: new ( ConfigFormat :: Json5 . into_platform_file_name ( ) )
178- || file_name == OsStr :: new ( ConfigFormat :: Toml . into_platform_file_name ( ) )
159+ || file_name == OsStr :: new ( ConfigFormat :: Json . into_platform_file_name ( target ) )
160+ || file_name == OsStr :: new ( ConfigFormat :: Json5 . into_platform_file_name ( target ) )
161+ || file_name == OsStr :: new ( ConfigFormat :: Toml . into_platform_file_name ( target ) )
179162 } )
180163 . unwrap_or_default ( )
181164}
@@ -192,9 +175,9 @@ pub fn is_configuration_file(path: &Path) -> bool {
192175/// Merging the configurations using [JSON Merge Patch (RFC 7396)].
193176///
194177/// [JSON Merge Patch (RFC 7396)]: https://datatracker.ietf.org/doc/html/rfc7396.
195- pub fn read_from ( root_dir : PathBuf ) -> Result < Value , ConfigError > {
196- let mut config: Value = parse_value ( root_dir. join ( "tauri.conf.json" ) ) ?. 0 ;
197- if let Some ( ( platform_config, _) ) = read_platform ( root_dir) ? {
178+ pub fn read_from ( target : Target , root_dir : PathBuf ) -> Result < Value , ConfigError > {
179+ let mut config: Value = parse_value ( target , root_dir. join ( "tauri.conf.json" ) ) ?. 0 ;
180+ if let Some ( ( platform_config, _) ) = read_platform ( target , root_dir) ? {
198181 merge ( & mut config, & platform_config) ;
199182 }
200183 Ok ( config)
@@ -203,10 +186,13 @@ pub fn read_from(root_dir: PathBuf) -> Result<Value, ConfigError> {
203186/// Reads the platform-specific configuration file from the given root directory if it exists.
204187///
205188/// Check [`read_from`] for more information.
206- pub fn read_platform ( root_dir : PathBuf ) -> Result < Option < ( Value , PathBuf ) > , ConfigError > {
207- let platform_config_path = root_dir. join ( ConfigFormat :: Json . into_platform_file_name ( ) ) ;
208- if does_supported_file_name_exist ( & platform_config_path) {
209- let ( platform_config, path) : ( Value , PathBuf ) = parse_value ( platform_config_path) ?;
189+ pub fn read_platform (
190+ target : Target ,
191+ root_dir : PathBuf ,
192+ ) -> Result < Option < ( Value , PathBuf ) > , ConfigError > {
193+ let platform_config_path = root_dir. join ( ConfigFormat :: Json . into_platform_file_name ( target) ) ;
194+ if does_supported_file_name_exist ( target, & platform_config_path) {
195+ let ( platform_config, path) : ( Value , PathBuf ) = parse_value ( target, platform_config_path) ?;
210196 Ok ( Some ( ( platform_config, path) ) )
211197 } else {
212198 Ok ( None )
@@ -217,16 +203,16 @@ pub fn read_platform(root_dir: PathBuf) -> Result<Option<(Value, PathBuf)>, Conf
217203///
218204/// The passed path is expected to be the path to the "default" configuration format, in this case
219205/// JSON with `.json`.
220- pub fn does_supported_file_name_exist ( path : impl Into < PathBuf > ) -> bool {
206+ pub fn does_supported_file_name_exist ( target : Target , path : impl Into < PathBuf > ) -> bool {
221207 let path = path. into ( ) ;
222208 let source_file_name = path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
223209 let lookup_platform_config = ENABLED_FORMATS
224210 . iter ( )
225- . any ( |format| source_file_name == format. into_platform_file_name ( ) ) ;
211+ . any ( |format| source_file_name == format. into_platform_file_name ( target ) ) ;
226212 ENABLED_FORMATS . iter ( ) . any ( |format| {
227213 path
228214 . with_file_name ( if lookup_platform_config {
229- format. into_platform_file_name ( )
215+ format. into_platform_file_name ( target )
230216 } else {
231217 format. into_file_name ( )
232218 } )
@@ -248,31 +234,37 @@ pub fn does_supported_file_name_exist(path: impl Into<PathBuf>) -> bool {
248234/// a. Parse it with `toml`
249235/// b. Return error if all above steps failed
250236/// 4. Return error if all above steps failed
251- pub fn parse ( path : impl Into < PathBuf > ) -> Result < ( Config , PathBuf ) , ConfigError > {
252- do_parse ( path. into ( ) )
237+ pub fn parse ( target : Target , path : impl Into < PathBuf > ) -> Result < ( Config , PathBuf ) , ConfigError > {
238+ do_parse ( target , path. into ( ) )
253239}
254240
255241/// See [`parse`] for specifics, returns a JSON [`Value`] instead of [`Config`].
256- pub fn parse_value ( path : impl Into < PathBuf > ) -> Result < ( Value , PathBuf ) , ConfigError > {
257- do_parse ( path. into ( ) )
242+ pub fn parse_value (
243+ target : Target ,
244+ path : impl Into < PathBuf > ,
245+ ) -> Result < ( Value , PathBuf ) , ConfigError > {
246+ do_parse ( target, path. into ( ) )
258247}
259248
260- fn do_parse < D : DeserializeOwned > ( path : PathBuf ) -> Result < ( D , PathBuf ) , ConfigError > {
249+ fn do_parse < D : DeserializeOwned > (
250+ target : Target ,
251+ path : PathBuf ,
252+ ) -> Result < ( D , PathBuf ) , ConfigError > {
261253 let file_name = path
262254 . file_name ( )
263255 . map ( OsStr :: to_string_lossy)
264256 . unwrap_or_default ( ) ;
265257 let lookup_platform_config = ENABLED_FORMATS
266258 . iter ( )
267- . any ( |format| file_name == format. into_platform_file_name ( ) ) ;
259+ . any ( |format| file_name == format. into_platform_file_name ( target ) ) ;
268260
269261 let json5 = path. with_file_name ( if lookup_platform_config {
270- ConfigFormat :: Json5 . into_platform_file_name ( )
262+ ConfigFormat :: Json5 . into_platform_file_name ( target )
271263 } else {
272264 ConfigFormat :: Json5 . into_file_name ( )
273265 } ) ;
274266 let toml = path. with_file_name ( if lookup_platform_config {
275- ConfigFormat :: Toml . into_platform_file_name ( )
267+ ConfigFormat :: Toml . into_platform_file_name ( target )
276268 } else {
277269 ConfigFormat :: Toml . into_file_name ( )
278270 } ) ;
0 commit comments