(args: &[String], cwd: &Path)
| 285 | } |
| 286 | |
| 287 | pub(crate) fn new_dlc_command(args: &[String], cwd: &Path) -> Result<(), String> { |
| 288 | let Some(raw_name) = parse_flag_value(args, "--name") else { |
| 289 | return Err("missing required flag `--name`".to_string()); |
| 290 | }; |
| 291 | let dlc_name = validate_dlc_name(&raw_name)?; |
| 292 | |
| 293 | let project_dir = if let Some(raw_project) = parse_flag_value(args, "--path") { |
| 294 | resolve_local_path(&raw_project, cwd) |
| 295 | } else { |
| 296 | find_project_root(cwd).ok_or_else(|| { |
| 297 | "could not find project.toml. Run from a project directory or pass --path <project_dir>." |
| 298 | .to_string() |
| 299 | })? |
| 300 | }; |
| 301 | let project_dir = project_dir.canonicalize().unwrap_or(project_dir); |
| 302 | if !project_dir.join("project.toml").exists() { |
| 303 | return Err(format!( |
| 304 | "invalid --path `{}` for new_dlc. Use project root (directory containing project.toml).", |
| 305 | project_dir.display() |
| 306 | )); |
| 307 | } |
| 308 | |
| 309 | let dlc_root = project_dir.join("dlcs").join(&dlc_name); |
| 310 | if dlc_root.exists() { |
| 311 | return Err(format!("dlc already exists: {}", dlc_root.display())); |
| 312 | } |
| 313 | |
| 314 | fs::create_dir_all(dlc_root.join("scenes")) |
| 315 | .map_err(|err| format!("failed to create dlc scenes dir: {err}"))?; |
| 316 | fs::create_dir_all(dlc_root.join("scripts")) |
| 317 | .map_err(|err| format!("failed to create dlc scripts dir: {err}"))?; |
| 318 | fs::create_dir_all(dlc_root.join("materials")) |
| 319 | .map_err(|err| format!("failed to create dlc materials dir: {err}"))?; |
| 320 | fs::create_dir_all(dlc_root.join("meshes")) |
| 321 | .map_err(|err| format!("failed to create dlc meshes dir: {err}"))?; |
| 322 | |
| 323 | let script_path = dlc_root.join("scripts").join("script.rs"); |
| 324 | write_new_file(&script_path, &default_script_empty_rs())?; |
| 325 | |
| 326 | let scene_path = dlc_root.join("scenes").join("main.scn"); |
| 327 | let scene = format!( |
| 328 | "$root = @main\n\n[main]\nscript = \"dlc://{dlc_name}/scripts/script.rs\"\n[Node2D]\n position = (0, 0)\n[/Node2D]\n[/main]\n" |
| 329 | ); |
| 330 | write_new_file(&scene_path, &scene)?; |
| 331 | update_workspace_vscode_linked_projects(&workspace_root(), &project_dir)?; |
| 332 | update_project_vscode_linked_projects(&project_dir)?; |
| 333 | |
| 334 | println!( |
| 335 | "created dlc `{}` at {}", |
| 336 | dlc_name, |
| 337 | normalize_powershell_path(&dlc_root) |
| 338 | ); |
| 339 | println!("reference scene with: dlc://{}/scenes/main.scn", dlc_name); |
| 340 | println!( |
| 341 | "reference script with: dlc://{}/scripts/script.rs", |
| 342 | dlc_name |
| 343 | ); |
| 344 | maybe_open_file_in_editor(args, &scene_path)?; |
no test coverage detected