ferrisup 0.2.5

A versatile Rust project bootstrapping tool - start anywhere, scale anywhere
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
use anyhow::{Context, Result};
use colored::Colorize;
use dialoguer::{Confirm, Input, Select};
use std::path::{Path, PathBuf};
use std::fs;
use ferrisup_common::{fs::create_directory, cargo::*};


/// Execute the workspace command to manage Cargo workspaces
pub fn execute(action: Option<&str>, path: Option<&str>) -> Result<()> {
    println!("{}", "FerrisUp Workspace Manager".bold().green());
    
    // Get project path
    let project_dir = if let Some(p) = path {
        PathBuf::from(p)
    } else {
        // Prompt for project path
        let use_current = Confirm::new()
            .with_prompt("Use current directory?")
            .default(true)
            .interact()?;
        
        if use_current {
            std::env::current_dir()?
        } else {
            let path = Input::<String>::new()
                .with_prompt("Enter project path")
                .interact()?;
            
            PathBuf::from(path)
        }
    };
    
    // Get action
    let action_str = if let Some(act) = action {
        act.to_string()
    } else {
        let options = vec!["init", "add", "remove", "list", "optimize"];
        let selection = Select::new()
            .with_prompt("Select workspace action")
            .items(&options)
            .default(0)
            .interact()?;
        
        options[selection].to_string()
    };
    
    // Execute the selected action
    match action_str.as_str() {
        "init" => init_workspace(&project_dir)?,
        "add" => add_crate_to_workspace(&project_dir)?,
        "remove" => remove_crate_from_workspace(&project_dir)?,
        "list" => list_workspace_members(&project_dir)?,
        "optimize" => optimize_workspace(&project_dir)?,
        _ => return Err(anyhow::anyhow!("Invalid action. Use 'init', 'add', 'remove', 'list', or 'optimize'")),
    }
    
    Ok(())
}

/// Initialize a new Cargo workspace
fn init_workspace(project_dir: &Path) -> Result<()> {
    // Check if Cargo.toml exists
    let cargo_toml_path = project_dir.join("Cargo.toml");
    
    let workspace_exists = if cargo_toml_path.exists() {
        let content = read_cargo_toml(project_dir)?;
        content.contains("[workspace]")
    } else {
        false
    };
    
    if workspace_exists {
        println!("{}", "Workspace already initialized!".yellow());
        return Ok(());
    }
    
    // Ask for workspace members
    let default_dirs = vec![
        "client/*".to_string(),
        "server/*".to_string(),
        "ferrisup_common/*".to_string(),
    ];
    
    let mut dirs = if !cargo_toml_path.exists() {
        // New workspace from scratch
        default_dirs
    } else {
        // Convert existing project to workspace
        println!("\n{}", "Converting existing project to workspace".green());
        
        let options = vec![
            "Use default workspace structure (client/*, server/*, ferrisup_common/*)",
            "Discover existing crates",
            "Manually specify members",
        ];
        
        let selection = Select::new()
            .with_prompt("How would you like to initialize the workspace?")
            .items(&options)
            .default(0)
            .interact()?;
        
        match selection {
            0 => default_dirs,
            1 => discover_crates(project_dir)?,
            2 => {
                let input = Input::<String>::new()
                    .with_prompt("Enter comma-separated workspace members (e.g. 'crate1, crate2/*, ferrisup_common/*')")
                    .interact()?;
                
                input.split(',')
                    .map(|s| s.trim().to_string())
                    .collect()
            },
            _ => default_dirs,
        }
    };
    
    // Create the workspace Cargo.toml
    let cargo_content = if !cargo_toml_path.exists() {
        // Create new Cargo.toml
        format!(
            r#"[workspace]
members = [
{}
]

[workspace.dependencies]
# Common dependencies for workspace members
anyhow = "1.0"
serde = {{ version = "1.0", features = ["derive"] }}
log = "0.4"
"#,
            dirs.iter()
                .map(|dir| format!("    \"{}\",", dir))
                .collect::<Vec<String>>()
                .join("\n")
        )
    } else {
        // Modify existing Cargo.toml
        let content = read_cargo_toml(project_dir)?;
        
        // Preserve existing content and add workspace section
        if content.contains("[package]") {
            // Convert an application to a workspace root
            // First, move package section to its own crate
            let package_name = extract_package_name(&content).unwrap_or("app".to_string());
            
            // Create app directory for the existing package
            let app_dir = project_dir.join(&package_name);
            if !app_dir.exists() {
                create_directory(&app_dir)?;
                
                // Move existing src directory to app directory
                let src_dir = project_dir.join("src");
                if src_dir.exists() {
                    let target_dir = app_dir.join("src");
                    fs::rename(&src_dir, &target_dir)?;
                }
                
                // Create app Cargo.toml with the package section
                let app_cargo = app_dir.join("Cargo.toml");
                fs::write(&app_cargo, extract_package_section(&content))?;
                
                println!("{} {}", "Moved existing package to:".green(), app_dir.display());
                
                // Add the new crate to workspace members
                dirs.push(package_name);
            }
            
            // Create new root Cargo.toml
            format!(
                r#"[workspace]
members = [
{}
]

[workspace.dependencies]
# Common dependencies for workspace members
anyhow = "1.0"
serde = {{ version = "1.0", features = ["derive"] }}
log = "0.4"
"#,
                dirs.iter()
                    .map(|dir| format!("    \"{}\",", dir))
                    .collect::<Vec<String>>()
                    .join("\n")
            )
        } else {
            // Just add workspace section to existing Cargo.toml
            format!(
                r#"{}

[workspace]
members = [
{}
]

[workspace.dependencies]
# Common dependencies for workspace members
anyhow = "1.0"
serde = {{ version = "1.0", features = ["derive"] }}
log = "0.4"
"#,
                content,
                dirs.iter()
                    .map(|dir| format!("    \"{}\",", dir))
                    .collect::<Vec<String>>()
                    .join("\n")
            )
        }
    };
    
    // Write the Cargo.toml file
    write_cargo_toml_content(project_dir, &cargo_content)?;
    
    println!("{} {}", "Initialized workspace in:".green(), project_dir.display());
    println!("{} {}", "Workspace members:".green(), dirs.join(", "));
    
    // Create default directories if they don't exist
    for dir in &["client", "server", "ferrisup_common"] {
        let path = project_dir.join(dir);
        if !path.exists() {
            create_directory(&path)?;
            println!("{} {}", "Created directory:".green(), path.display());
        }
    }
    
    Ok(())
}

/// Add a new crate to an existing workspace
fn add_crate_to_workspace(project_dir: &Path) -> Result<()> {
    // Verify it's a workspace
    let cargo_content = read_cargo_toml(project_dir)?;
    if !cargo_content.contains("[workspace]") {
        return Err(anyhow::anyhow!("Not a Cargo workspace (no [workspace] section in Cargo.toml)"));
    }
    
    // Get crate type
    let crate_types = vec!["client", "server", "ferrisup_common", "custom"];
    let selection = Select::new()
        .with_prompt("Select crate type")
        .items(&crate_types)
        .default(0)
        .interact()?;
    
    let crate_type = crate_types[selection];
    
    // Get crate name
    let crate_name = Input::<String>::new()
        .with_prompt("Enter crate name")
        .interact()?;
    
    // Determine crate path based on type
    let crate_path = match crate_type {
        "client" => project_dir.join("../../../client").join(&crate_name),
        "server" => project_dir.join("server").join(&crate_name),
        "ferrisup_common" => project_dir.join("../../../ferrisup_common").join(&crate_name),
        _ => project_dir.join(&crate_name),
    };
    
    // Create crate directory
    create_directory(&crate_path)?;
    
    // Get crate template
    let is_bin = if crate_type == "client" || crate_type == "server" {
        true
    } else {
        Confirm::new()
            .with_prompt("Is this a binary crate? (No for library)")
            .default(false)
            .interact()?
    };
    
    // Create src directory and main.rs/lib.rs
    let src_dir = crate_path.join("src");
    create_directory(&src_dir)?;
    
    if is_bin {
        fs::write(
            src_dir.join("main.rs"),
            "fn main() {\n    println!(\"Hello from {}!\");\n}\n".replace("{}", &crate_name)
        )?;
    } else {
        fs::write(
            src_dir.join("lib.rs"),
            "//! {} library\n\n/// Example function\npub fn hello() -> &'static str {\n    \"Hello from {}!\"\n}\n"
                .replace("{}", &crate_name)
        )?;
    }
    
    // Create Cargo.toml for the crate
    let crate_cargo_content = format!(
        r#"[package]
name = "{}"
version = "0.1.0"
edition = "2021"

[dependencies]
"#,
        if crate_type == "custom" {
            crate_name.clone()
        } else {
            let project_name = project_dir.file_name()
                .and_then(|name| name.to_str())
                .map(|s| s.replace('-', "_"))
                .unwrap_or_else(|| "project".to_string());
            format!("{}-{}", project_name, crate_name)
        }
    );
    
    fs::write(crate_path.join("Cargo.toml"), crate_cargo_content)?;
    
    println!("{} {}", "Created crate:".green(), crate_path.display());
    
    // Update workspace members
    update_workspace_members(project_dir)?;
    
    Ok(())
}

/// Remove a crate from an existing workspace
fn remove_crate_from_workspace(project_dir: &Path) -> Result<()> {
    // Verify it's a workspace
    let cargo_content = read_cargo_toml(project_dir)?;
    if !cargo_content.contains("[workspace]") {
        return Err(anyhow::anyhow!("Not a Cargo workspace (no [workspace] section in Cargo.toml)"));
    }
    
    // List workspace members
    let members = list_workspace_crates(project_dir)?;
    
    if members.is_empty() {
        println!("{}", "No workspace members found".yellow());
        return Ok(());
    }
    
    // Let user select a crate to remove
    let selection = Select::new()
        .with_prompt("Select crate to remove from workspace")
        .items(&members)
        .default(0)
        .interact()?;
    
    let crate_path = members[selection].clone();
    
    // Confirm deletion
    let delete_files = Confirm::new()
        .with_prompt(format!("Also delete {} files?", crate_path))
        .default(false)
        .interact()?;
    
    // Remove files if requested
    if delete_files {
        let full_path = project_dir.join(&crate_path);
        fs::remove_dir_all(&full_path)
            .context(format!("Failed to remove {}", full_path.display()))?;
        
        println!("{} {}", "Deleted crate files:".green(), crate_path);
    }
    
    // Update workspace members
    update_workspace_members(project_dir)?;
    
    println!("{} {}", "Removed crate from workspace:".green(), crate_path);
    
    Ok(())
}

/// List members of an existing workspace
fn list_workspace_members(project_dir: &Path) -> Result<()> {
    // Verify it's a workspace
    let cargo_content = read_cargo_toml(project_dir)?;
    if !cargo_content.contains("[workspace]") {
        return Err(anyhow::anyhow!("Not a Cargo workspace (no [workspace] section in Cargo.toml)"));
    }
    
    // Extract workspace members
    let members = extract_workspace_members(&cargo_content);
    
    println!("\n{}", "Workspace Members:".bold());
    
    if members.is_empty() {
        println!("  No members found");
    } else {
        for (i, member) in members.iter().enumerate() {
            println!("  {}. {}", i + 1, member);
        }
    }
    
    // List actual crates found (resolved)
    let crates = list_workspace_crates(project_dir)?;
    
    println!("\n{}", "Found Crates:".bold());
    
    if crates.is_empty() {
        println!("  No crates found");
    } else {
        for (i, crate_path) in crates.iter().enumerate() {
            println!("  {}. {}", i + 1, crate_path);
        }
    }
    
    Ok(())
}

/// Optimize a workspace by identifying and fixing common issues
fn optimize_workspace(project_dir: &Path) -> Result<()> {
    println!("{}", "Optimizing workspace...".green());
    
    // Verify it's a workspace
    let cargo_content = read_cargo_toml(project_dir)?;
    if !cargo_content.contains("[workspace]") {
        return Err(anyhow::anyhow!("Not a Cargo workspace (no [workspace] section in Cargo.toml)"));
    }
    
    // Check for duplicated dependencies
    let mut improvements = Vec::new();
    improvements.push("Checking for dependency issues...".to_string());
    
    // Update workspace members - ensure all crates are included
    let updated = update_workspace_members(project_dir)?;
    if updated {
        improvements.push("✓ Updated workspace members list".to_string());
    }
    
    // Check if workspace.dependencies exists and add if not
    if !cargo_content.contains("[workspace.dependencies]") {
        // Add workspace.dependencies section header only
        let updated_content = format!(
            r#"{}\n
[workspace.dependencies]
# Common dependencies for workspace members
"#,
            cargo_content
        );
        
        // Write updated Cargo.toml with just the section header
        write_cargo_toml_content(project_dir, &updated_content)?;
        
        // Now add common dependencies using our utility function
        let common_deps = vec![
            ("anyhow".to_string(), "1.0".to_string(), None),
            ("serde".to_string(), "1.0".to_string(), Some(vec!["derive".to_string()])),
            ("log".to_string(), "0.4".to_string(), None)
        ];
        
        let cargo_path = project_dir.join("Cargo.toml");
        update_cargo_with_dependencies(&cargo_path, common_deps, false)?;
        improvements.push("✓ Added [workspace.dependencies] section".to_string());
    }
    
    // Report improvements
    println!("\n{}", "Workspace Optimization Results:".bold());
    for improvement in improvements {
        println!("  {}", improvement);
    }
    
    println!("\n{}", "Workspace optimized successfully!".green());
    
    Ok(())
}

/// Helper function to discover crates in a project directory
fn discover_crates(project_dir: &Path) -> Result<Vec<String>> {
    let mut crates = Vec::new();
    
    // Look for directories with Cargo.toml files
    let walkdir = walkdir::WalkDir::new(project_dir)
        .follow_links(true)
        .max_depth(3) // Don't go too deep
        .into_iter()
        .filter_map(|e| e.ok());
    
    for entry in walkdir {
        let path = entry.path();
        
        // Skip the root directory itself
        if path == project_dir {
            continue;
        }
        
        // Check if it has a Cargo.toml file
        if path.is_dir() && path.join("Cargo.toml").exists() {
            if let Ok(rel_path) = path.strip_prefix(project_dir) {
                let rel_path_str = rel_path.to_string_lossy().to_string();
                crates.push(rel_path_str);
            }
        }
    }
    
    // If nothing found in subdirectories, check for common patterns
    if crates.is_empty() {
        // Check for client/server/ferrisup_common directories
        for dir in &["client", "server", "ferrisup_common"] {
            let dir_path = project_dir.join(dir);
            if dir_path.exists() && dir_path.is_dir() {
                crates.push(format!("{}/*", dir));
            }
        }
    }
    
    Ok(crates)
}

/// Helper function to list actual crates in a workspace
fn list_workspace_crates(project_dir: &Path) -> Result<Vec<String>> {
    let mut crates = Vec::new();
    
    // Extract workspace members
    let cargo_content = read_cargo_toml(project_dir)?;
    let members = extract_workspace_members(&cargo_content);
    
    // Resolve glob patterns and check if each member exists
    for member in members {
        if member.contains('*') {
            // Handle glob pattern
            let parts: Vec<&str> = member.split('*').collect();
            let prefix = parts[0];
            
            let prefix_path = project_dir.join(prefix);
            if prefix_path.exists() && prefix_path.is_dir() {
                if let Ok(entries) = fs::read_dir(&prefix_path) {
                    for entry in entries.filter_map(|e| e.ok()) {
                        if entry.path().is_dir() && entry.path().join("Cargo.toml").exists() {
                            if let Ok(rel_path) = entry.path().strip_prefix(project_dir) {
                                crates.push(rel_path.to_string_lossy().to_string());
                            }
                        }
                    }
                }
            }
        } else {
            // Handle direct path
            let member_path = project_dir.join(&member);
            if member_path.exists() && member_path.is_dir() && member_path.join("Cargo.toml").exists() {
                crates.push(member);
            }
        }
    }
    
    Ok(crates)
}

/// Helper function to extract workspace members from Cargo.toml content
fn extract_workspace_members(cargo_content: &str) -> Vec<String> {
    let mut members = Vec::new();
    
    // Basic parsing of members array
    if let Some(workspace_section) = cargo_content.split("[workspace]").nth(1) {
        if let Some(members_section) = workspace_section.split("members").nth(1) {
            if let Some(members_list) = members_section.split('[').nth(1) {
                if let Some(members_list) = members_list.split(']').next() {
                    for line in members_list.lines() {
                        let line = line.trim();
                        if line.starts_with('"') && line.contains('"') {
                            let member = line
                                .trim_start_matches('"')
                                .split('"')
                                .next()
                                .unwrap_or("")
                                .trim()
                                .trim_end_matches(',');
                            
                            if !member.is_empty() {
                                members.push(member.to_string());
                            }
                        }
                    }
                }
            }
        }
    }
    
    members
}

/// Helper function to extract package name from Cargo.toml content
fn extract_package_name(cargo_content: &str) -> Option<String> {
    if let Some(package_section) = cargo_content.split("[package]").nth(1) {
        if let Some(name_line) = package_section
            .lines()
            .find(|line| line.trim().starts_with("name"))
        {
            if let Some(name) = name_line
                .split('=')
                .nth(1)
                .map(|s| s.trim())
                .map(|s| s.trim_matches('"'))
                .map(|s| s.trim_matches('\''))
            {
                return Some(name.to_string());
            }
        }
    }
    None
}

/// Helper function to extract the package section from Cargo.toml content
fn extract_package_section(cargo_content: &str) -> String {
    if let Some(package_section) = cargo_content.split("[package]").nth(1) {
        if let Some(end_index) = package_section.find('[') {
            let section = &package_section[..end_index];
            return format!("[package]{}", section);
        } else {
            return format!("[package]{}", package_section);
        }
    }
    String::new()
}