@@ -58,104 +58,12 @@ pub struct Options {
5858}
5959
6060pub fn command ( mut options : Options ) -> Result < ( ) > {
61- let ( merge_config, merge_config_path) = if let Some ( config) = & options. config {
62- if config. starts_with ( '{' ) {
63- ( Some ( config. to_string ( ) ) , None )
64- } else {
65- (
66- Some (
67- std:: fs:: read_to_string ( & config)
68- . with_context ( || "failed to read custom configuration" ) ?,
69- ) ,
70- Some ( config. clone ( ) ) ,
71- )
72- }
73- } else {
74- ( None , None )
75- } ;
76- options. config = merge_config;
77-
78- let tauri_path = tauri_dir ( ) ;
79- set_current_dir ( & tauri_path) . with_context ( || "failed to change current working directory" ) ?;
61+ let mut interface = setup ( & mut options) ?;
8062
8163 let config = get_config ( options. config . as_deref ( ) ) ?;
82-
8364 let config_guard = config. lock ( ) . unwrap ( ) ;
8465 let config_ = config_guard. as_ref ( ) . unwrap ( ) ;
8566
86- let mut interface = AppInterface :: new ( config_) ?;
87-
88- let bundle_identifier_source = match config_. find_bundle_identifier_overwriter ( ) {
89- Some ( source) if source == MERGE_CONFIG_EXTENSION_NAME => merge_config_path. unwrap_or ( source) ,
90- Some ( source) => source,
91- None => "tauri.conf.json" . into ( ) ,
92- } ;
93-
94- if config_. tauri . bundle . identifier == "com.tauri.dev" {
95- error ! (
96- "You must change the bundle identifier in `{} > tauri > bundle > identifier`. The default value `com.tauri.dev` is not allowed as it must be unique across applications." ,
97- bundle_identifier_source
98- ) ;
99- std:: process:: exit ( 1 ) ;
100- }
101-
102- if config_
103- . tauri
104- . bundle
105- . identifier
106- . chars ( )
107- . any ( |ch| !( ch. is_alphanumeric ( ) || ch == '-' || ch == '.' ) )
108- {
109- error ! (
110- "The bundle identifier \" {}\" set in `{} > tauri > bundle > identifier`. The bundle identifier string must contain only alphanumeric characters (A–Z, a–z, and 0–9), hyphens (-), and periods (.)." ,
111- config_. tauri. bundle. identifier,
112- bundle_identifier_source
113- ) ;
114- std:: process:: exit ( 1 ) ;
115- }
116-
117- if let Some ( before_build) = config_. build . before_build_command . clone ( ) {
118- run_hook ( "beforeBuildCommand" , before_build, options. debug ) ?;
119- }
120-
121- if let AppUrl :: Url ( WindowUrl :: App ( web_asset_path) ) = & config_. build . dist_dir {
122- if !web_asset_path. exists ( ) {
123- return Err ( anyhow:: anyhow!(
124- "Unable to find your web assets, did you forget to build your web app? Your distDir is set to \" {:?}\" ." ,
125- web_asset_path
126- ) ) ;
127- }
128- if web_asset_path. canonicalize ( ) ?. file_name ( ) == Some ( std:: ffi:: OsStr :: new ( "src-tauri" ) ) {
129- return Err ( anyhow:: anyhow!(
130- "The configured distDir is the `src-tauri` folder.
131- Please isolate your web assets on a separate folder and update `tauri.conf.json > build > distDir`." ,
132- ) ) ;
133- }
134-
135- let mut out_folders = Vec :: new ( ) ;
136- for folder in & [ "node_modules" , "src-tauri" , "target" ] {
137- if web_asset_path. join ( folder) . is_dir ( ) {
138- out_folders. push ( folder. to_string ( ) ) ;
139- }
140- }
141- if !out_folders. is_empty ( ) {
142- return Err ( anyhow:: anyhow!(
143- "The configured distDir includes the `{:?}` {}. Please isolate your web assets on a separate folder and update `tauri.conf.json > build > distDir`." ,
144- out_folders,
145- if out_folders. len( ) == 1 { "folder" } else { "folders" }
146- )
147- ) ;
148- }
149- }
150-
151- if options. runner . is_none ( ) {
152- options. runner = config_. build . runner . clone ( ) ;
153- }
154-
155- if let Some ( list) = options. features . as_mut ( ) {
156- list. extend ( config_. build . features . clone ( ) . unwrap_or_default ( ) ) ;
157- }
158-
15967 let app_settings = interface. app_settings ( ) ;
16068 let interface_options = options. clone ( ) . into ( ) ;
16169
@@ -310,6 +218,108 @@ pub fn command(mut options: Options) -> Result<()> {
310218 Ok ( ( ) )
311219}
312220
221+ pub fn setup ( options : & mut Options ) -> Result < AppInterface > {
222+ let ( merge_config, merge_config_path) = if let Some ( config) = & options. config {
223+ if config. starts_with ( '{' ) {
224+ ( Some ( config. to_string ( ) ) , None )
225+ } else {
226+ (
227+ Some (
228+ std:: fs:: read_to_string ( & config)
229+ . with_context ( || "failed to read custom configuration" ) ?,
230+ ) ,
231+ Some ( config. clone ( ) ) ,
232+ )
233+ }
234+ } else {
235+ ( None , None )
236+ } ;
237+ options. config = merge_config;
238+
239+ let tauri_path = tauri_dir ( ) ;
240+ set_current_dir ( & tauri_path) . with_context ( || "failed to change current working directory" ) ?;
241+
242+ let config = get_config ( options. config . as_deref ( ) ) ?;
243+
244+ let config_guard = config. lock ( ) . unwrap ( ) ;
245+ let config_ = config_guard. as_ref ( ) . unwrap ( ) ;
246+
247+ let interface = AppInterface :: new ( config_) ?;
248+
249+ let bundle_identifier_source = match config_. find_bundle_identifier_overwriter ( ) {
250+ Some ( source) if source == MERGE_CONFIG_EXTENSION_NAME => merge_config_path. unwrap_or ( source) ,
251+ Some ( source) => source,
252+ None => "tauri.conf.json" . into ( ) ,
253+ } ;
254+
255+ if config_. tauri . bundle . identifier == "com.tauri.dev" {
256+ error ! (
257+ "You must change the bundle identifier in `{} > tauri > bundle > identifier`. The default value `com.tauri.dev` is not allowed as it must be unique across applications." ,
258+ bundle_identifier_source
259+ ) ;
260+ std:: process:: exit ( 1 ) ;
261+ }
262+
263+ if config_
264+ . tauri
265+ . bundle
266+ . identifier
267+ . chars ( )
268+ . any ( |ch| !( ch. is_alphanumeric ( ) || ch == '-' || ch == '.' ) )
269+ {
270+ error ! (
271+ "The bundle identifier \" {}\" set in `{} > tauri > bundle > identifier`. The bundle identifier string must contain only alphanumeric characters (A–Z, a–z, and 0–9), hyphens (-), and periods (.)." ,
272+ config_. tauri. bundle. identifier,
273+ bundle_identifier_source
274+ ) ;
275+ std:: process:: exit ( 1 ) ;
276+ }
277+
278+ if let Some ( before_build) = config_. build . before_build_command . clone ( ) {
279+ run_hook ( "beforeBuildCommand" , before_build, options. debug ) ?;
280+ }
281+
282+ if let AppUrl :: Url ( WindowUrl :: App ( web_asset_path) ) = & config_. build . dist_dir {
283+ if !web_asset_path. exists ( ) {
284+ return Err ( anyhow:: anyhow!(
285+ "Unable to find your web assets, did you forget to build your web app? Your distDir is set to \" {:?}\" ." ,
286+ web_asset_path
287+ ) ) ;
288+ }
289+ if web_asset_path. canonicalize ( ) ?. file_name ( ) == Some ( std:: ffi:: OsStr :: new ( "src-tauri" ) ) {
290+ return Err ( anyhow:: anyhow!(
291+ "The configured distDir is the `src-tauri` folder.
292+ Please isolate your web assets on a separate folder and update `tauri.conf.json > build > distDir`." ,
293+ ) ) ;
294+ }
295+
296+ let mut out_folders = Vec :: new ( ) ;
297+ for folder in & [ "node_modules" , "src-tauri" , "target" ] {
298+ if web_asset_path. join ( folder) . is_dir ( ) {
299+ out_folders. push ( folder. to_string ( ) ) ;
300+ }
301+ }
302+ if !out_folders. is_empty ( ) {
303+ return Err ( anyhow:: anyhow!(
304+ "The configured distDir includes the `{:?}` {}. Please isolate your web assets on a separate folder and update `tauri.conf.json > build > distDir`." ,
305+ out_folders,
306+ if out_folders. len( ) == 1 { "folder" } else { "folders" }
307+ )
308+ ) ;
309+ }
310+ }
311+
312+ if options. runner . is_none ( ) {
313+ options. runner = config_. build . runner . clone ( ) ;
314+ }
315+
316+ if let Some ( list) = options. features . as_mut ( ) {
317+ list. extend ( config_. build . features . clone ( ) . unwrap_or_default ( ) ) ;
318+ }
319+
320+ Ok ( interface)
321+ }
322+
313323fn run_hook ( name : & str , hook : HookCommand , debug : bool ) -> Result < ( ) > {
314324 let ( script, script_cwd) = match hook {
315325 HookCommand :: Script ( s) if s. is_empty ( ) => ( None , None ) ,
0 commit comments