@@ -2,7 +2,7 @@ use std::{collections::HashMap, path::PathBuf};
2
2
3
3
use flate2:: { read:: GzDecoder , write:: GzEncoder } ;
4
4
5
- use crate :: { app:: { self , AppState } , editor:: UnityEditorInstall , errors, io_utils, package:: MinimalPackage , template:: SurfaceTemplate } ;
5
+ use crate :: { app:: { self , AppState } , editor:: UnityEditorInstall , errors, io_utils, package:: { self , MinimalPackage } , template:: SurfaceTemplate } ;
6
6
7
7
#[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
8
8
#[ serde( rename_all = "camelCase" ) ]
@@ -39,7 +39,7 @@ pub fn generate_project(app: &tauri::AppHandle, project_info: &ProjectInfoForGen
39
39
40
40
let package_cache_dir = io_utils:: get_cache_appended_dir ( app, "new_project_package" ) ;
41
41
unpack_package_into_cache ( & package_cache_dir, & template_info) ?;
42
- modify_package_json ( & package_cache_dir, & template_info. packages ) ?;
42
+ modify_package_json ( & package_cache_dir, & template_info. packages , & package_cache_dir_out ) ?;
43
43
44
44
// create project directory
45
45
std:: fs:: create_dir ( & package_cache_dir_out) ?;
@@ -88,8 +88,10 @@ pub fn generate_project(app: &tauri::AppHandle, project_info: &ProjectInfoForGen
88
88
// generate a new template file
89
89
pub fn generate_template ( app : & tauri:: AppHandle , app_state : & tauri:: State < AppState > , template_info : & NewTemplateInfo ) -> Result < PathBuf , errors:: AnyError > {
90
90
let package_cache_dir = io_utils:: get_cache_appended_dir ( app, "new_template_package" ) ;
91
+ let package_cache_dir_out = io_utils:: get_cache_appended_dir ( app, "new_template_package_output" ) ;
92
+
91
93
unpack_package_into_cache ( & package_cache_dir, & template_info. template ) ?;
92
- modify_package_json ( & package_cache_dir, & template_info. template . packages ) ?;
94
+ modify_package_json ( & package_cache_dir, & template_info. template . packages , & package_cache_dir_out ) ?;
93
95
94
96
// modify package.json
95
97
let package_json_path = package_cache_dir
@@ -115,8 +117,6 @@ pub fn generate_template(app: &tauri::AppHandle, app_state: &tauri::State<AppSta
115
117
} ) ;
116
118
println ! ( "Writing new package.json: {}" , package_json_path. display( ) ) ;
117
119
std:: fs:: write ( & package_json_path, serde_json:: to_string_pretty ( & new_package_json) ?) ?;
118
-
119
- let package_cache_dir_out = io_utils:: get_cache_appended_dir ( app, "new_template_package_output" ) ;
120
120
121
121
// copy contents from package to output
122
122
let data_root = PathBuf :: from ( "package" ) ;
@@ -217,7 +217,7 @@ fn unpack_package_into_cache(output: &PathBuf, template_info: &TemplateInfoForGe
217
217
Ok ( ( ) )
218
218
}
219
219
220
- fn modify_package_json ( package_cache_dir : & PathBuf , packages : & Vec < MinimalPackage > ) -> Result < ( ) , errors:: AnyError > {
220
+ fn modify_package_json ( package_cache_dir : & PathBuf , packages : & Vec < MinimalPackage > , output_path : & PathBuf ) -> Result < ( ) , errors:: AnyError > {
221
221
// modify package.json for dependencies
222
222
let packages_dir = package_cache_dir
223
223
. join ( "package" )
@@ -235,18 +235,68 @@ fn modify_package_json(package_cache_dir: &PathBuf, packages: &Vec<MinimalPackag
235
235
std:: fs:: remove_file ( & packages_lock_json) ?;
236
236
}
237
237
238
+ // read any local packages
239
+ let local_packages = packages
240
+ . iter ( )
241
+ . filter ( |x| x. _type == package:: PackageType :: Local )
242
+ . collect :: < Vec < _ > > ( ) ;
243
+
244
+ let rest_packages = packages
245
+ . iter ( )
246
+ . filter ( |x| x. _type != package:: PackageType :: Local )
247
+ . collect :: < Vec < _ > > ( ) ;
248
+
249
+ println ! ( "Local packages: {:?}" , local_packages) ;
250
+ println ! ( "Rest packages: {:?}" , rest_packages) ;
251
+
238
252
let manifest_json_contents = std:: fs:: read_to_string ( & manifest_json) ?;
239
253
let mut manifest_json_contents: HashMap < String , serde_json:: Value >
240
254
= serde_json:: from_str ( & manifest_json_contents) ?;
241
255
242
256
let mut dependencies: serde_json:: Map < String , serde_json:: Value > = serde_json:: Map :: new ( ) ;
243
257
244
258
// override the dependencies
245
- for package in packages. iter ( ) {
259
+ for package in rest_packages. iter ( ) {
260
+ println ! ( "Rest package: {:?}" , package) ;
246
261
let name = package. name . clone ( ) ;
247
262
let version = package. version . clone ( ) ;
248
263
dependencies. insert ( name, serde_json:: Value :: String ( version) ) ;
249
264
}
265
+
266
+ // local ones now
267
+ for package in local_packages. iter ( ) {
268
+ println ! ( "Local package: {:?}" , package) ;
269
+ // get local path from this project to the json path in name
270
+ let package_json_path = std:: path:: Path :: new ( & package. name ) . to_path_buf ( ) ;
271
+ let package_json_path_parent = package_json_path
272
+ . parent ( )
273
+ . ok_or ( errors:: str_error ( "Failed to get parent" ) ) ?;
274
+ let relative_path = io_utils:: diff_paths ( & package_json_path_parent, & output_path. join ( "Packages" ) ) ;
275
+
276
+ let relative_path = match relative_path {
277
+ Some ( path) => path. to_path_buf ( ) ,
278
+ None => package_json_path. clone ( )
279
+ } ;
280
+
281
+ let json = std:: fs:: read_to_string ( & package_json_path) ?;
282
+ let json: serde_json:: Value = serde_json:: from_str ( & json) ?;
283
+ let name = json. as_object ( )
284
+ . ok_or ( errors:: str_error ( & format ! ( "Failed to get json object from {}" , package_json_path. display( ) ) ) ) ?
285
+ . get ( "name" )
286
+ . ok_or ( errors:: str_error ( & format ! ( "Failed to get name from {}" , package_json_path. display( ) ) ) ) ?
287
+ . as_str ( )
288
+ . ok_or ( errors:: str_error ( & format ! ( "Failed to get name from {}" , package_json_path. display( ) ) ) ) ?
289
+ . to_string ( ) ;
290
+
291
+ let name = name. clone ( ) ;
292
+ let version = format ! ( "file:{}" , relative_path
293
+ . to_str( )
294
+ . ok_or( errors:: str_error( "Failed to get str" ) ) ?
295
+ . replace( "\\ " , "/" )
296
+ ) ;
297
+ dependencies. insert ( name, serde_json:: Value :: String ( version) ) ;
298
+ }
299
+
250
300
manifest_json_contents. insert ( "dependencies" . to_string ( ) , serde_json:: Value :: Object ( dependencies) ) ;
251
301
252
302
// save to disk
0 commit comments