@@ -2,15 +2,16 @@ 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:: { self , MinimalPackage } , template:: SurfaceTemplate } ;
5
+ use crate :: { app:: { self , AppState } , editor:: { self , 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" ) ]
9
9
pub struct TemplateInfoForGeneration {
10
10
template : Option < SurfaceTemplate > ,
11
11
editor_version : UnityEditorInstall ,
12
12
packages : Vec < MinimalPackage > ,
13
- selected_files : Vec < PathBuf >
13
+ selected_files : Vec < PathBuf > ,
14
+ is_empty : bool
14
15
}
15
16
16
17
#[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
@@ -31,39 +32,74 @@ pub struct NewTemplateInfo {
31
32
}
32
33
33
34
// generate a new project from a template + info
34
- pub fn generate_project ( app : & tauri:: AppHandle , project_info : & ProjectInfoForGeneration , template_info : & TemplateInfoForGeneration ) -> Result < PathBuf , errors:: AnyError > {
35
+ pub fn generate_project ( app : & tauri:: AppHandle , app_state : & tauri :: State < ' _ , AppState > , project_info : & ProjectInfoForGeneration , template_info : & TemplateInfoForGeneration ) -> Result < PathBuf , errors:: AnyError > {
35
36
let package_cache_dir_out = & project_info. path . join ( project_info. name . clone ( ) ) ;
36
37
if package_cache_dir_out. exists ( ) {
37
38
return Err ( errors:: str_error ( format ! ( "Project already exists at {}" , package_cache_dir_out. display( ) ) . as_str ( ) ) ) ;
38
39
}
39
-
40
+
40
41
let package_cache_dir = io_utils:: get_cache_appended_dir ( app, "new_project_package" ) ;
41
- unpack_package_into_cache ( & package_cache_dir, & template_info) ?;
42
- modify_package_json ( & package_cache_dir, & template_info. packages , & package_cache_dir_out) ?;
42
+ if template_info. is_empty {
43
+ // make a new project
44
+ // copy entire project over
45
+ // modify manifest
46
+
47
+ let editor_version = & template_info. editor_version . version ;
48
+ let package_cache_dir_out_str = package_cache_dir_out
49
+ . to_str ( )
50
+ . ok_or ( errors:: str_error ( "Failed to convert path to string" ) ) ?
51
+ . to_string ( ) ;
52
+ // let tmp_path = package_cache_dir
53
+ // .join("project")
54
+ // .join("ProjectData~")
55
+ // .to_str()
56
+ // .ok_or(errors::str_error("Failed to convert path to string"))?
57
+ // .to_string();
58
+ let args = vec ! [ "-createProject" . to_string( ) , package_cache_dir_out_str, "-quit" . to_string( ) ] ;
59
+ editor:: open ( editor_version. clone ( ) , args, & app_state, true ) ?;
60
+
61
+ let packages_dir = package_cache_dir_out
62
+ . join ( "Packages" ) ;
63
+ modify_package_json ( & packages_dir, & template_info. packages , & package_cache_dir_out) ?;
64
+ } else {
65
+ let packages_dir = package_cache_dir
66
+ . join ( "package" )
67
+ . join ( "ProjectData~" )
68
+ . join ( "Packages" ) ;
69
+
70
+ unpack_package_into_cache ( & package_cache_dir, & template_info) ?;
71
+ modify_package_json ( & packages_dir, & template_info. packages , & package_cache_dir_out) ?;
72
+ }
43
73
44
74
// create project directory
45
- std:: fs:: create_dir ( & package_cache_dir_out) ?;
46
- std:: fs:: create_dir ( & package_cache_dir_out. join ( "Assets" ) ) ?;
75
+ std:: fs:: create_dir_all ( & package_cache_dir_out. join ( "Assets" ) ) ?;
76
+ std:: fs:: create_dir_all ( & package_cache_dir_out. join ( "ProjectSettings" ) ) ?;
77
+ std:: fs:: create_dir_all ( & package_cache_dir_out. join ( "Packages" ) ) ?;
47
78
48
79
// copy contents from ProjectData~ to output
49
80
let project_data_root = PathBuf :: from ( "package" ) . join ( "ProjectData~" ) ;
50
- for file in template_info . selected_files . iter ( ) . filter ( |x| x . starts_with ( & project_data_root ) ) {
51
- let trimmed_file = file . strip_prefix ( & project_data_root )
52
- . map_err ( |_| errors :: str_error ( "Failed to strip prefix" ) ) ? ;
53
- let from = package_cache_dir . join ( file ) ;
54
- let dest = package_cache_dir_out . join ( trimmed_file ) ;
55
- println ! ( "Copying {} to {}" , from. display ( ) , dest . display ( ) ) ;
56
-
57
- if dest . extension ( ) . is_none ( ) {
58
- println ! ( "Creating directory dest: {}" , dest . display ( ) ) ;
81
+
82
+ if !template_info . is_empty {
83
+ for file in template_info . selected_files . iter ( ) . filter ( |x| x . starts_with ( & project_data_root ) ) {
84
+ let trimmed_file = file . strip_prefix ( & project_data_root )
85
+ . map_err ( |_| errors :: str_error ( "Failed to strip prefix" ) ) ? ;
86
+ let from = package_cache_dir . join ( file ) ;
87
+ let dest = package_cache_dir_out . join ( trimmed_file ) ;
88
+ println ! ( "Copying {} to {}" , from . display ( ) , dest . display ( ) ) ;
89
+
59
90
std:: fs:: create_dir_all ( & dest) ?;
60
- } else {
61
- println ! ( "Creating directory parent: {} for {}" , dest. parent( ) . unwrap( ) . display( ) , dest. display( ) ) ;
62
- std:: fs:: create_dir_all ( dest. parent ( ) . unwrap ( ) ) ?;
63
- if let Err ( err) = std:: fs:: copy ( & from, & dest) {
64
- println ! ( "Failed to copy from {} to {}: {}" , from. display( ) , dest. display( ) , err) ;
91
+
92
+ if dest. extension ( ) . is_none ( ) {
93
+ println ! ( "Creating directory dest: {}" , dest. display( ) ) ;
94
+ // std::fs::create_dir_all(&dest)?;
95
+ } else {
96
+ println ! ( "Creating directory parent: {} for {}" , dest. parent( ) . unwrap( ) . display( ) , dest. display( ) ) ;
97
+ // std::fs::create_dir_all(dest.parent().unwrap())?;
98
+ if let Err ( err) = std:: fs:: copy ( & from, & dest) {
99
+ println ! ( "Failed to copy from {} to {}: {}" , from. display( ) , dest. display( ) , err) ;
100
+ }
65
101
}
66
- }
102
+ }
67
103
}
68
104
69
105
// remove cache
@@ -201,6 +237,13 @@ pub fn generate_template(app: &tauri::AppHandle, app_state: &tauri::State<AppSta
201
237
}
202
238
203
239
fn unpack_package_into_cache ( output : & PathBuf , template_info : & TemplateInfoForGeneration ) -> Result < ( ) , errors:: AnyError > {
240
+ if template_info. template . is_none ( ) {
241
+ let package_path = output. join ( "package" ) ;
242
+ std:: fs:: create_dir_all ( & package_path) ?;
243
+
244
+ return Ok ( ( ) ) ;
245
+ }
246
+
204
247
let template = template_info. template
205
248
. as_ref ( )
206
249
. ok_or ( errors:: str_error ( "Template not found" ) ) ?
@@ -217,17 +260,15 @@ fn unpack_package_into_cache(output: &PathBuf, template_info: &TemplateInfoForGe
217
260
Ok ( ( ) )
218
261
}
219
262
220
- fn modify_package_json ( package_cache_dir : & PathBuf , packages : & Vec < MinimalPackage > , output_path : & PathBuf ) -> Result < ( ) , errors:: AnyError > {
263
+ fn modify_package_json ( json_root : & PathBuf , packages : & Vec < MinimalPackage > , output_path : & PathBuf ) -> Result < ( ) , errors:: AnyError > {
221
264
// modify package.json for dependencies
222
- let packages_dir = package_cache_dir
223
- . join ( "package" )
224
- . join ( "ProjectData~" )
225
- . join ( "Packages" ) ;
226
- let manifest_json = packages_dir
265
+ std:: fs:: create_dir_all ( & json_root) ?;
266
+
267
+ let manifest_json = json_root
227
268
. join ( "manifest" )
228
269
. with_extension ( "json" ) ;
229
270
230
- let packages_lock_json = packages_dir
271
+ let packages_lock_json = json_root
231
272
. join ( "packages-lock" )
232
273
. with_extension ( "json" ) ;
233
274
@@ -249,7 +290,13 @@ fn modify_package_json(package_cache_dir: &PathBuf, packages: &Vec<MinimalPackag
249
290
println ! ( "Local packages: {:?}" , local_packages) ;
250
291
println ! ( "Rest packages: {:?}" , rest_packages) ;
251
292
252
- let manifest_json_contents = std:: fs:: read_to_string ( & manifest_json) ?;
293
+ let manifest_json_contents = {
294
+ if !manifest_json. exists ( ) {
295
+ "{}" . to_string ( )
296
+ } else {
297
+ std:: fs:: read_to_string ( & manifest_json) ?
298
+ }
299
+ } ;
253
300
let mut manifest_json_contents: HashMap < String , serde_json:: Value >
254
301
= serde_json:: from_str ( & manifest_json_contents) ?;
255
302
@@ -309,8 +356,8 @@ fn modify_package_json(package_cache_dir: &PathBuf, packages: &Vec<MinimalPackag
309
356
// commands
310
357
311
358
#[ tauri:: command]
312
- pub async fn cmd_generate_project ( app : tauri:: AppHandle , project_info : ProjectInfoForGeneration , template_info : TemplateInfoForGeneration ) -> Result < PathBuf , errors:: AnyError > {
313
- let output = generate_project ( & app, & project_info, & template_info) ?;
359
+ pub async fn cmd_generate_project ( app : tauri:: AppHandle , app_state : tauri :: State < ' _ , AppState > , project_info : ProjectInfoForGeneration , template_info : TemplateInfoForGeneration ) -> Result < PathBuf , errors:: AnyError > {
360
+ let output = generate_project ( & app, & app_state , & project_info, & template_info) ?;
314
361
Ok ( output)
315
362
}
316
363
0 commit comments