@@ -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:: { self , UnityEditorInstall } , errors, io_utils, package:: { self , MinimalPackage } , template:: SurfaceTemplate } ;
5
+ use crate :: { app:: { self , AppState } , editor:: { self , UnityEditorInstall } , errors, io_utils, package:: { self , MinimalPackage } , template:: { EditorVersionPackageList , SurfaceTemplate } } ;
6
6
7
7
#[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
8
8
#[ serde( rename_all = "camelCase" ) ]
@@ -38,6 +38,8 @@ pub fn generate_project(app: &tauri::AppHandle, app_state: &tauri::State<'_, App
38
38
return Err ( errors:: str_error ( format ! ( "Project already exists at {}" , package_cache_dir_out. display( ) ) . as_str ( ) ) ) ;
39
39
}
40
40
41
+ let cached_editor_packages = crate :: template:: read_editor_version_packages ( & app, & template_info. editor_version . version . clone ( ) ) ?;
42
+
41
43
let package_cache_dir = io_utils:: get_cache_appended_dir ( app, "new_project_package" ) ?;
42
44
if template_info. is_empty {
43
45
// make a new project
@@ -60,15 +62,15 @@ pub fn generate_project(app: &tauri::AppHandle, app_state: &tauri::State<'_, App
60
62
61
63
let packages_dir = package_cache_dir_out
62
64
. join ( "Packages" ) ;
63
- modify_package_json ( & packages_dir, & template_info. packages , & package_cache_dir_out) ?;
65
+ modify_package_json ( & packages_dir, & template_info. packages , & package_cache_dir_out, & cached_editor_packages ) ?;
64
66
} else {
65
67
let packages_dir = package_cache_dir
66
68
. join ( "package" )
67
69
. join ( "ProjectData~" )
68
70
. join ( "Packages" ) ;
69
71
70
72
unpack_package_into_cache ( & package_cache_dir, & template_info) ?;
71
- modify_package_json ( & packages_dir, & template_info. packages , & package_cache_dir_out) ?;
73
+ modify_package_json ( & packages_dir, & template_info. packages , & package_cache_dir_out, & cached_editor_packages ) ?;
72
74
}
73
75
74
76
// create project directory
@@ -117,6 +119,27 @@ pub fn generate_project(app: &tauri::AppHandle, app_state: &tauri::State<'_, App
117
119
std:: fs:: write ( & editor_version_path, editor_version) ?;
118
120
119
121
create_gitignore ( package_cache_dir_out) ?;
122
+
123
+ // create package-lock file
124
+ let package_lock_path = package_cache_dir_out
125
+ . join ( "Packages" )
126
+ . join ( "packages-lock" )
127
+ . with_extension ( "json" ) ;
128
+
129
+ let lock_dependencies = template_info. packages
130
+ . iter ( )
131
+ . filter ( |x| cached_editor_packages. packages . get ( & x. name ) . is_some ( ) )
132
+ . collect :: < Vec < _ > > ( ) ;
133
+ let mut serde_hash_map = serde_json:: Map :: new ( ) ;
134
+ for dep in lock_dependencies. iter ( ) {
135
+ let value = cached_editor_packages. packages . get ( & dep. name ) . unwrap ( ) ;
136
+ serde_hash_map. insert ( dep. name . clone ( ) , serde_json:: to_value ( value) ?) ;
137
+ }
138
+
139
+ let json_str = serde_json:: json!( {
140
+ "dependencies" : serde_hash_map
141
+ } ) ;
142
+ std:: fs:: write ( & package_lock_path, serde_json:: to_string_pretty ( & json_str) ?) ?;
120
143
121
144
Ok ( package_cache_dir_out. clone ( ) )
122
145
}
@@ -126,8 +149,10 @@ pub fn generate_template(app: &tauri::AppHandle, app_state: &tauri::State<AppSta
126
149
let package_cache_dir = io_utils:: get_cache_appended_dir ( app, "new_template_package" ) ?;
127
150
let package_cache_dir_out = io_utils:: get_cache_appended_dir ( app, "new_template_package_output" ) ?;
128
151
152
+ let cached_editor_packages = crate :: template:: read_editor_version_packages ( & app, & template_info. template . editor_version . version . clone ( ) ) ?;
153
+
129
154
unpack_package_into_cache ( & package_cache_dir, & template_info. template ) ?;
130
- modify_package_json ( & package_cache_dir, & template_info. template . packages , & package_cache_dir_out) ?;
155
+ modify_package_json ( & package_cache_dir, & template_info. template . packages , & package_cache_dir_out, & cached_editor_packages ) ?;
131
156
132
157
// modify package.json
133
158
let package_json_path = package_cache_dir
@@ -260,7 +285,7 @@ fn unpack_package_into_cache(output: &PathBuf, template_info: &TemplateInfoForGe
260
285
Ok ( ( ) )
261
286
}
262
287
263
- fn modify_package_json ( json_root : & PathBuf , packages : & Vec < MinimalPackage > , output_path : & PathBuf ) -> Result < ( ) , errors:: AnyError > {
288
+ fn modify_package_json ( json_root : & PathBuf , packages : & Vec < MinimalPackage > , output_path : & PathBuf , cached_editor_packages : & EditorVersionPackageList ) -> Result < ( ) , errors:: AnyError > {
264
289
// modify package.json for dependencies
265
290
std:: fs:: create_dir_all ( & json_root) ?;
266
291
@@ -304,6 +329,23 @@ fn modify_package_json(json_root: &PathBuf, packages: &Vec<MinimalPackage>, outp
304
329
println ! ( "Rest package: {:?}" , package) ;
305
330
let name = package. name . clone ( ) ;
306
331
let version = package. version . clone ( ) ;
332
+
333
+ let is_bad = {
334
+ if let Some ( package) = cached_editor_packages. packages . get ( & name) {
335
+ if package. source . as_ref ( ) . is_some_and ( |x| x == "embedded" ) {
336
+ true
337
+ } else {
338
+ false
339
+ }
340
+ } else {
341
+ false
342
+ }
343
+ } ;
344
+
345
+ if is_bad {
346
+ continue ;
347
+ }
348
+
307
349
dependencies. insert ( name, serde_json:: Value :: String ( version) ) ;
308
350
}
309
351
0 commit comments