@@ -2,21 +2,23 @@ use std::collections::HashMap;
22
33use anyhow:: { bail, Context } ;
44
5- #[ derive( Default , Clone ) ]
5+ #[ derive( Default , Clone , PartialEq , Eq , Debug ) ]
66pub struct Manifest < ' a > {
77 pub before_dev_command : Option < & ' a str > ,
88 pub before_build_command : Option < & ' a str > ,
99 pub dev_path : Option < & ' a str > ,
1010 pub dist_dir : Option < & ' a str > ,
11- pub with_global_tauri : bool ,
11+ pub with_global_tauri : Option < bool > ,
1212 pub files : HashMap < & ' a str , & ' a str > ,
1313}
1414
1515impl < ' a > Manifest < ' a > {
1616 pub fn parse ( s : & ' a str , mobile : bool ) -> Result < Self , anyhow:: Error > {
17- let mut manifest = Manifest :: default ( ) ;
17+ let mut manifest = Self :: default ( ) ;
18+
1819 let mut in_files_section = false ;
1920 let mut in_mobile_section = false ;
21+
2022 for ( i, line) in s. split ( '\n' ) . enumerate ( ) {
2123 let line_number = i + 1 ;
2224
@@ -71,7 +73,7 @@ impl<'a> Manifest<'a> {
7173 "beforeBuildCommand" if replace => manifest. before_build_command = Some ( v) ,
7274 "devPath" if replace => manifest. dev_path = Some ( v) ,
7375 "distDir" if replace => manifest. dist_dir = Some ( v) ,
74- "withGlobalTauri" if replace => manifest. with_global_tauri = v. parse ( ) ?,
76+ "withGlobalTauri" if replace => manifest. with_global_tauri = Some ( v. parse ( ) ?) ,
7577 _ if in_files_section => {
7678 manifest. files . insert ( k, v) ;
7779 }
@@ -81,4 +83,176 @@ impl<'a> Manifest<'a> {
8183 }
8284 Ok ( manifest)
8385 }
86+
87+ pub fn replace_vars ( & self , content : & str ) -> String {
88+ content
89+ . replace (
90+ "{{fragment_before_dev_command}}" ,
91+ self . before_dev_command . unwrap_or_default ( ) ,
92+ )
93+ . replace (
94+ "{{fragment_before_build_command}}" ,
95+ self . before_build_command . unwrap_or_default ( ) ,
96+ )
97+ . replace ( "{{fragment_dev_path}}" , self . dev_path . unwrap_or_default ( ) )
98+ . replace ( "{{fragment_dist_dir}}" , self . dist_dir . unwrap_or_default ( ) )
99+ . replace (
100+ r#""withGlobalTauri": "{{fragment_with_global_tauri}}""# ,
101+ & format ! (
102+ r#""withGlobalTauri": {}"# ,
103+ self . with_global_tauri. unwrap_or( false )
104+ ) ,
105+ )
106+ }
107+ }
108+
109+ #[ cfg( test) ]
110+ mod test {
111+ use super :: * ;
112+
113+ #[ test]
114+ fn it_parses ( ) {
115+ let manifest_file = r#"
116+ # Copyright 2019-2022 Tauri Programme within The Commons Conservancy
117+ # SPDX-License-Identifier: Apache-2.0
118+ # SPDX-License-Identifier: MIT
119+
120+ beforeDevCommand = npm start -- --port 1420
121+ beforeBuildCommand = {{pkg_manager_run_command}} build # this comment should be stripped
122+ devPath = http://localhost:1420
123+
124+ [mobile]
125+ beforeBuildCommand = {{pkg_manager_run_command}} build mobile
126+
127+ [files]
128+ tauri.svg = src/assets/tauri.svg
129+ styles.css = src/styles.css
130+ "# ;
131+
132+ assert_eq ! ( Manifest :: parse( manifest_file, false ) . unwrap( ) , {
133+ let mut files = HashMap :: new( ) ;
134+ files. insert( "tauri.svg" , "src/assets/tauri.svg" ) ;
135+ files. insert( "styles.css" , "src/styles.css" ) ;
136+
137+ Manifest {
138+ before_dev_command: Some ( "npm start -- --port 1420" ) ,
139+ before_build_command: Some ( "{{pkg_manager_run_command}} build" ) ,
140+ dev_path: Some ( "http://localhost:1420" ) ,
141+ dist_dir: None ,
142+ with_global_tauri: None ,
143+ files,
144+ }
145+ } ) ;
146+
147+ assert_eq ! ( Manifest :: parse( manifest_file, true ) . unwrap( ) , {
148+ let mut files = HashMap :: new( ) ;
149+ files. insert( "tauri.svg" , "src/assets/tauri.svg" ) ;
150+ files. insert( "styles.css" , "src/styles.css" ) ;
151+
152+ Manifest {
153+ before_dev_command: Some ( "npm start -- --port 1420" ) ,
154+ before_build_command: Some ( "{{pkg_manager_run_command}} build mobile" ) ,
155+ dev_path: Some ( "http://localhost:1420" ) ,
156+ dist_dir: None ,
157+ with_global_tauri: None ,
158+ files,
159+ }
160+ } ) ;
161+ }
162+
163+ #[ test]
164+ #[ should_panic]
165+ fn it_panics_while_parsing ( ) {
166+ let manifest_file = r#"
167+ # Copyright 2019-2022 Tauri Programme within The Commons Conservancy
168+ # SPDX-License-Identifier: Apache-2.0
169+ # SPDX-License-Identifier: MIT
170+
171+ beforeDevCommand = npm start -- --port 1420
172+ beforeBuildCommand =
173+ devPath = http://localhost:1420
174+
175+ [mobile]
176+ beforeBuildCommand = {{pkg_manager_run_command}} build mobile
177+
178+ [files]
179+ tauri.svg = src/assets/tauri.svg
180+ styles.css = src/styles.css
181+ "# ;
182+
183+ Manifest :: parse ( manifest_file, false ) . unwrap ( ) ;
184+ }
185+
186+ #[ test]
187+ fn later_should_override_former ( ) {
188+ let manifest_file = r#"
189+ # Copyright 2019-2022 Tauri Programme within The Commons Conservancy
190+ # SPDX-License-Identifier: Apache-2.0
191+ # SPDX-License-Identifier: MIT
192+
193+ beforeDevCommand = npm start -- --port 1420
194+ beforeBuildCommand = {{pkg_manager_run_command}} build # this comment should be stripped
195+ devPath = http://localhost:1420
196+ beforeBuildCommand = {{pkg_manager_run_command}} build mobile
197+
198+ [files]
199+ tauri.svg = src/assets/tauri.svg
200+ styles.css = src/styles.css
201+ "# ;
202+
203+ assert_eq ! ( Manifest :: parse( manifest_file, false ) . unwrap( ) , {
204+ let mut files = HashMap :: new( ) ;
205+ files. insert( "tauri.svg" , "src/assets/tauri.svg" ) ;
206+ files. insert( "styles.css" , "src/styles.css" ) ;
207+
208+ Manifest {
209+ before_dev_command: Some ( "npm start -- --port 1420" ) ,
210+ before_build_command: Some ( "{{pkg_manager_run_command}} build mobile" ) ,
211+ dev_path: Some ( "http://localhost:1420" ) ,
212+ dist_dir: None ,
213+ with_global_tauri: None ,
214+ files,
215+ }
216+ } ) ;
217+ }
218+
219+ #[ test]
220+ fn it_replaces_vars ( ) {
221+ let manifest_file = r#"
222+ # Copyright 2019-2022 Tauri Programme within The Commons Conservancy
223+ # SPDX-License-Identifier: Apache-2.0
224+ # SPDX-License-Identifier: MIT
225+
226+ beforeDevCommand = npm start -- --port 1420
227+ beforeBuildCommand = {{pkg_manager_run_command}} build # this comment should be stripped
228+ devPath = http://localhost:1420
229+
230+ [files]
231+ tauri.svg = src/assets/tauri.svg
232+ styles.css = src/styles.css
233+ "# ;
234+
235+ let manifest = Manifest :: parse ( manifest_file, false ) . unwrap ( ) ;
236+
237+ let content = r#"{
238+ "build": {
239+ "beforeDevCommand": "{{fragment_before_dev_command}}",
240+ "beforeBuildCommand": "{{fragment_before_build_command}}",
241+ "devPath": "{{fragment_dev_path}}",
242+ "distDir": "{{fragment_dist_dir}}"
243+ },
244+ }"# ;
245+ assert_eq ! (
246+ manifest. replace_vars( content) . as_str( ) ,
247+ r#"{
248+ "build": {
249+ "beforeDevCommand": "npm start -- --port 1420",
250+ "beforeBuildCommand": "{{pkg_manager_run_command}} build",
251+ "devPath": "http://localhost:1420",
252+ "distDir": ""
253+ },
254+ }"#
255+ . to_string( )
256+ )
257+ }
84258}
0 commit comments