ui-cli 0.3.8

A CLI to add components to your app.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use std::collections::HashSet;
use std::fs;
use std::path::Path;

use cargo_toml::Manifest;
use toml_edit::{DocumentMut, Item, Value};

use crate::command_init::workspace_utils::{WorkspaceInfo, analyze_workspace};
use crate::shared::cli_error::{CliError, CliResult};
use crate::shared::task_spinner::TaskSpinner;

pub fn process_cargo_deps(cargo_deps: &[String]) -> CliResult<()> {
    let spinner = TaskSpinner::new("Checking dependencies...");

    // Detect workspace to determine how to add dependencies
    let workspace_info = analyze_workspace().ok();

    // Get existing dependencies from the target Cargo.toml
    let existing_deps = get_existing_dependencies(&workspace_info)?;

    // Filter out dependencies that already exist
    let (new_deps, existing_deps_found): (Vec<_>, Vec<_>) =
        cargo_deps.iter().partition(|dep| !existing_deps.contains(*dep));

    if !existing_deps_found.is_empty() {
        let existing_str = existing_deps_found.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", ");
        spinner.set_message(&format!("⏭️  Skipping existing dependencies: [{existing_str}]"));
    }

    if new_deps.is_empty() {
        spinner.finish_with_message("All dependencies already exist in Cargo.toml");
        return Ok(());
    }

    spinner.set_message("Adding new crates to Cargo.toml...");

    // Check if we should use workspace dependencies
    let use_workspace_deps = should_use_workspace_deps(&workspace_info);

    let mut added_deps = Vec::new();

    for dep in &new_deps {
        spinner.set_message(&format!("📦 Adding crate: {dep}"));

        let result = if use_workspace_deps {
            // Safe: use_workspace_deps is only true when workspace_info is Some with valid data
            let Some(info) = workspace_info.as_ref() else {
                return Err(CliError::cargo_operation("Workspace info unavailable"));
            };
            add_workspace_dependency(dep, info)
        } else {
            add_dependency_with_cargo(dep, &workspace_info)
        };

        match result {
            Ok(()) => added_deps.push(dep.as_str()),
            Err(e) => return Err(e),
        }
    }

    let dependencies_str = added_deps.join(", ");
    let finish_message = format!("Successfully added to Cargo.toml: [{dependencies_str}] !");
    spinner.finish_success(&finish_message);

    Ok(())
}

/* ========================================================== */
/*                     ✨ FUNCTIONS ✨                        */
/* ========================================================== */

/// Check if we should use workspace dependencies pattern
fn should_use_workspace_deps(workspace_info: &Option<WorkspaceInfo>) -> bool {
    let Some(info) = workspace_info else {
        return false;
    };

    if !info.is_workspace {
        return false;
    }

    let Some(workspace_root) = &info.workspace_root else {
        return false;
    };

    // Check if workspace root has [workspace.dependencies] section
    let root_cargo_toml = workspace_root.join("Cargo.toml");
    if !root_cargo_toml.exists() {
        return false;
    }

    let Ok(contents) = fs::read_to_string(&root_cargo_toml) else {
        return false;
    };

    let Ok(doc) = contents.parse::<DocumentMut>() else {
        return false;
    };

    // Check if [workspace.dependencies] exists
    doc.get("workspace").and_then(|w| w.get("dependencies")).is_some()
}

/// Add dependency using workspace pattern:
/// 1. Add to [workspace.dependencies] in root Cargo.toml
/// 2. Add dep.workspace = true to member Cargo.toml
fn add_workspace_dependency(dep: &str, info: &WorkspaceInfo) -> CliResult<()> {
    let workspace_root =
        info.workspace_root.as_ref().ok_or_else(|| CliError::cargo_operation("Workspace root not found"))?;

    let member_path = info
        .target_crate_path
        .as_ref()
        .ok_or_else(|| CliError::cargo_operation("Target crate path not found"))?;

    // First, get the latest version from crates.io
    let version = fetch_latest_version(dep)?;

    // Add to workspace root [workspace.dependencies]
    let root_cargo_toml = workspace_root.join("Cargo.toml");
    add_to_workspace_dependencies(&root_cargo_toml, dep, &version)?;

    // Add to member [dependencies] with workspace = true
    let member_cargo_toml = member_path.join("Cargo.toml");
    add_workspace_ref_to_member(&member_cargo_toml, dep)?;

    Ok(())
}

/// Add dependency to [workspace.dependencies] in root Cargo.toml
fn add_to_workspace_dependencies(cargo_toml_path: &Path, dep: &str, version: &str) -> CliResult<()> {
    let contents = fs::read_to_string(cargo_toml_path)?;
    let mut doc: DocumentMut = contents
        .parse()
        .map_err(|e| CliError::cargo_operation(&format!("Failed to parse Cargo.toml: {e}")))?;

    // Get or create [workspace.dependencies]
    let workspace = doc.entry("workspace").or_insert(Item::Table(toml_edit::Table::new()));

    let workspace_table =
        workspace.as_table_mut().ok_or_else(|| CliError::cargo_operation("[workspace] is not a table"))?;

    let deps = workspace_table.entry("dependencies").or_insert(Item::Table(toml_edit::Table::new()));

    let deps_table = deps
        .as_table_mut()
        .ok_or_else(|| CliError::cargo_operation("[workspace.dependencies] is not a table"))?;

    // Check if already exists
    if deps_table.contains_key(dep) {
        return Ok(());
    }

    // Add the dependency with version
    deps_table.insert(dep, Item::Value(Value::String(toml_edit::Formatted::new(version.to_string()))));

    // Write back
    fs::write(cargo_toml_path, doc.to_string())?;

    Ok(())
}

/// Add dep.workspace = true to member's [dependencies]
fn add_workspace_ref_to_member(cargo_toml_path: &Path, dep: &str) -> CliResult<()> {
    let contents = fs::read_to_string(cargo_toml_path)?;
    let mut doc: DocumentMut = contents
        .parse()
        .map_err(|e| CliError::cargo_operation(&format!("Failed to parse member Cargo.toml: {e}")))?;

    // Get or create [dependencies]
    let deps = doc.entry("dependencies").or_insert(Item::Table(toml_edit::Table::new()));

    let deps_table =
        deps.as_table_mut().ok_or_else(|| CliError::cargo_operation("[dependencies] is not a table"))?;

    // Check if already exists
    if deps_table.contains_key(dep) {
        return Ok(());
    }

    // Add dep.workspace = true using dotted key format
    let mut dep_table = toml_edit::Table::new();
    dep_table.set_dotted(true);
    dep_table.insert("workspace", Item::Value(Value::Boolean(toml_edit::Formatted::new(true))));
    deps_table.insert(dep, Item::Table(dep_table));

    // Write back
    fs::write(cargo_toml_path, doc.to_string())?;

    Ok(())
}

/// Fetch the latest version of a crate from crates.io
fn fetch_latest_version(crate_name: &str) -> CliResult<String> {
    // Use cargo search to get the latest version
    let output = std::process::Command::new("cargo")
        .args(["search", crate_name, "--limit", "1"])
        .output()
        .map_err(|_| CliError::cargo_operation("Failed to execute cargo search"))?;

    if !output.status.success() {
        return Err(CliError::cargo_operation(&format!("Failed to search for crate '{crate_name}'")));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse output like: serde = "1.0.219"   # A generic serialization/deserialization framework
    for line in stdout.lines() {
        if line.starts_with(crate_name) {
            // Extract version from format: crate_name = "version"
            if let Some(version_part) = line.split('=').nth(1) {
                let version = version_part
                    .trim()
                    .trim_matches('"')
                    .split_whitespace()
                    .next()
                    .unwrap_or("")
                    .trim_matches('"');

                if !version.is_empty() {
                    return Ok(version.to_string());
                }
            }
        }
    }

    // Fallback: use "*" if we can't determine version
    Ok("*".to_string())
}

/// Fallback: use cargo add command
fn add_dependency_with_cargo(dep: &str, workspace_info: &Option<WorkspaceInfo>) -> CliResult<()> {
    let args = build_cargo_add_args(dep, workspace_info);

    let output = std::process::Command::new("cargo")
        .args(&args)
        .output()
        .map_err(|_| CliError::cargo_operation("Failed to execute cargo add"))?;

    if output.status.success() {
        Ok(())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        Err(CliError::cargo_operation(&format!("Failed to add dependency '{dep}': {stderr}")))
    }
}

/// Build cargo add arguments, adding --package flag for workspaces
fn build_cargo_add_args(dep: &str, workspace_info: &Option<WorkspaceInfo>) -> Vec<String> {
    let mut args = vec!["add".to_string(), dep.to_string()];

    if let Some(info) = workspace_info.as_ref().filter(|i| i.is_workspace)
        && let Some(crate_name) = &info.target_crate
    {
        args.push("--package".to_string());
        args.push(crate_name.clone());
    }

    args
}

/// Check if a crate is already in Cargo.toml dependencies
fn get_existing_dependencies(workspace_info: &Option<WorkspaceInfo>) -> CliResult<HashSet<String>> {
    // Determine which Cargo.toml to check
    let cargo_toml_path = if let Some(info) = workspace_info {
        if let Some(crate_path) = &info.target_crate_path {
            crate_path.join("Cargo.toml")
        } else {
            Path::new("Cargo.toml").to_path_buf()
        }
    } else {
        Path::new("Cargo.toml").to_path_buf()
    };

    if !cargo_toml_path.exists() {
        return Ok(HashSet::new());
    }

    // Read the file directly to avoid workspace resolution issues
    let contents = fs::read_to_string(&cargo_toml_path)?;
    let manifest = Manifest::from_slice(contents.as_bytes())?;

    let mut existing_deps = HashSet::new();

    // Check [dependencies] section
    for dep_name in manifest.dependencies.keys() {
        existing_deps.insert(dep_name.clone());
    }

    // Check [dev-dependencies] section
    for dep_name in manifest.dev_dependencies.keys() {
        existing_deps.insert(dep_name.clone());
    }

    Ok(existing_deps)
}

/* ========================================================== */
/*                        🧪 TESTS 🧪                         */
/* ========================================================== */

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use tempfile::TempDir;

    use super::*;

    #[test]
    fn test_build_cargo_add_args_no_workspace() {
        let args = build_cargo_add_args("serde", &None);
        assert_eq!(args, vec!["add", "serde"]);
    }

    #[test]
    fn test_build_cargo_add_args_single_crate() {
        let info = WorkspaceInfo {
            is_workspace: false,
            workspace_root: None,
            target_crate: Some("my-app".to_string()),
            target_crate_path: None,
            components_base_path: "src/components".to_string(),
        };

        let args = build_cargo_add_args("serde", &Some(info));
        assert_eq!(args, vec!["add", "serde"]);
    }

    #[test]
    fn test_build_cargo_add_args_workspace_with_target() {
        let info = WorkspaceInfo {
            is_workspace: true,
            workspace_root: Some(PathBuf::from("/project")),
            target_crate: Some("frontend".to_string()),
            target_crate_path: Some(PathBuf::from("/project/frontend")),
            components_base_path: "frontend/src/components".to_string(),
        };

        let args = build_cargo_add_args("serde", &Some(info));
        assert_eq!(args, vec!["add", "serde", "--package", "frontend"]);
    }

    #[test]
    fn test_build_cargo_add_args_workspace_no_target() {
        let info = WorkspaceInfo {
            is_workspace: true,
            workspace_root: Some(PathBuf::from("/project")),
            target_crate: None,
            target_crate_path: None,
            components_base_path: "src/components".to_string(),
        };

        let args = build_cargo_add_args("serde", &Some(info));
        assert_eq!(args, vec!["add", "serde"]);
    }

    #[test]
    fn test_should_use_workspace_deps_no_workspace() {
        assert!(!should_use_workspace_deps(&None));
    }

    #[test]
    fn test_should_use_workspace_deps_not_workspace() {
        let info = WorkspaceInfo {
            is_workspace: false,
            workspace_root: None,
            target_crate: Some("app".to_string()),
            target_crate_path: None,
            components_base_path: "src/components".to_string(),
        };
        assert!(!should_use_workspace_deps(&Some(info)));
    }

    #[test]
    fn test_should_use_workspace_deps_with_workspace_dependencies() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace Cargo.toml with [workspace.dependencies]
        fs::write(
            root.join("Cargo.toml"),
            r#"
[workspace]
members = ["app"]

[workspace.dependencies]
leptos = "0.7"
"#,
        )
        .unwrap();

        let info = WorkspaceInfo {
            is_workspace: true,
            workspace_root: Some(root.to_path_buf()),
            target_crate: Some("app".to_string()),
            target_crate_path: Some(root.join("app")),
            components_base_path: "app/src/components".to_string(),
        };

        assert!(should_use_workspace_deps(&Some(info)));
    }

    #[test]
    fn test_should_use_workspace_deps_without_workspace_dependencies() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace Cargo.toml WITHOUT [workspace.dependencies]
        fs::write(
            root.join("Cargo.toml"),
            r#"
[workspace]
members = ["app"]
"#,
        )
        .unwrap();

        let info = WorkspaceInfo {
            is_workspace: true,
            workspace_root: Some(root.to_path_buf()),
            target_crate: Some("app".to_string()),
            target_crate_path: Some(root.join("app")),
            components_base_path: "app/src/components".to_string(),
        };

        assert!(!should_use_workspace_deps(&Some(info)));
    }

    #[test]
    fn test_add_to_workspace_dependencies() {
        let temp = TempDir::new().unwrap();
        let cargo_toml = temp.path().join("Cargo.toml");

        // Create initial Cargo.toml
        fs::write(
            &cargo_toml,
            r#"[workspace]
members = ["app"]

[workspace.dependencies]
leptos = "0.7"
"#,
        )
        .unwrap();

        // Add serde
        add_to_workspace_dependencies(&cargo_toml, "serde", "1.0").unwrap();

        // Verify
        let contents = fs::read_to_string(&cargo_toml).unwrap();
        assert!(contents.contains("serde = \"1.0\""), "Should contain serde dependency: {contents}");
        assert!(contents.contains("leptos = \"0.7\""), "Should preserve existing deps: {contents}");
    }

    #[test]
    fn test_add_workspace_ref_to_member() {
        let temp = TempDir::new().unwrap();
        let cargo_toml = temp.path().join("Cargo.toml");

        // Create initial member Cargo.toml
        fs::write(
            &cargo_toml,
            r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
leptos.workspace = true
"#,
        )
        .unwrap();

        // Add serde.workspace = true
        add_workspace_ref_to_member(&cargo_toml, "serde").unwrap();

        // Verify
        let contents = fs::read_to_string(&cargo_toml).unwrap();
        assert!(contents.contains("serde"), "Should contain serde: {contents}");
        assert!(
            contents.contains("workspace = true") || contents.contains("workspace=true"),
            "Should have workspace = true: {contents}"
        );
    }

    #[test]
    fn test_add_workspace_ref_uses_dotted_format() {
        let temp = TempDir::new().unwrap();
        let cargo_toml = temp.path().join("Cargo.toml");

        // Create initial member Cargo.toml
        fs::write(
            &cargo_toml,
            r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
"#,
        )
        .unwrap();

        // Add validator.workspace = true
        add_workspace_ref_to_member(&cargo_toml, "validator").unwrap();

        // Verify it uses dotted format (validator.workspace = true) not inline ({ workspace = true })
        let contents = fs::read_to_string(&cargo_toml).unwrap();
        assert!(
            contents.contains("validator.workspace = true"),
            "Should use dotted format 'validator.workspace = true', got: {contents}"
        );
        assert!(
            !contents.contains("{ workspace = true }"),
            "Should NOT use inline table format, got: {contents}"
        );
    }

    #[test]
    fn get_existing_deps_returns_empty_when_no_cargo_toml() {
        let temp = TempDir::new().unwrap();
        let info = WorkspaceInfo {
            is_workspace: false,
            workspace_root: None,
            target_crate: None,
            target_crate_path: Some(temp.path().to_path_buf()),
            components_base_path: "src/components".to_string(),
        };
        let result = get_existing_dependencies(&Some(info)).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn get_existing_deps_reads_dependencies_section() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("Cargo.toml"),
            r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
serde = "1.0"
tokio = "1.0"
"#,
        )
        .unwrap();
        let info = WorkspaceInfo {
            is_workspace: false,
            workspace_root: None,
            target_crate: None,
            target_crate_path: Some(temp.path().to_path_buf()),
            components_base_path: "src/components".to_string(),
        };
        let result = get_existing_dependencies(&Some(info)).unwrap();
        assert!(result.contains("serde"));
        assert!(result.contains("tokio"));
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn get_existing_deps_includes_dev_dependencies() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("Cargo.toml"),
            r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
serde = "1.0"

[dev-dependencies]
tempfile = "3.0"
"#,
        )
        .unwrap();
        let info = WorkspaceInfo {
            is_workspace: false,
            workspace_root: None,
            target_crate: None,
            target_crate_path: Some(temp.path().to_path_buf()),
            components_base_path: "src/components".to_string(),
        };
        let result = get_existing_dependencies(&Some(info)).unwrap();
        assert!(result.contains("serde"));
        assert!(result.contains("tempfile"));
    }

    #[test]
    fn test_add_workspace_dependency_full_flow() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace root Cargo.toml
        fs::write(
            root.join("Cargo.toml"),
            r#"[workspace]
members = ["app"]

[workspace.dependencies]
leptos = "0.7"
"#,
        )
        .unwrap();

        // Create app directory and Cargo.toml
        let app_dir = root.join("app");
        fs::create_dir_all(&app_dir).unwrap();
        fs::write(
            app_dir.join("Cargo.toml"),
            r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
leptos.workspace = true
"#,
        )
        .unwrap();

        let _info = WorkspaceInfo {
            is_workspace: true,
            workspace_root: Some(root.to_path_buf()),
            target_crate: Some("app".to_string()),
            target_crate_path: Some(app_dir.clone()),
            components_base_path: "app/src/components".to_string(),
        };

        // Test the individual functions since fetch_latest_version requires network
        add_to_workspace_dependencies(&root.join("Cargo.toml"), "serde", "1.0").unwrap();
        add_workspace_ref_to_member(&app_dir.join("Cargo.toml"), "serde").unwrap();

        // Verify root Cargo.toml
        let root_contents = fs::read_to_string(root.join("Cargo.toml")).unwrap();
        assert!(root_contents.contains("serde = \"1.0\""), "Root should have serde: {root_contents}");

        // Verify app Cargo.toml
        let app_contents = fs::read_to_string(app_dir.join("Cargo.toml")).unwrap();
        assert!(app_contents.contains("serde"), "App should have serde ref: {app_contents}");
    }
}