prj-cli 1.0.0

A CLI tool for managing projects on your local machine
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
use clap::{Parser, Subcommand};
use colored::*;
use dialoguer::theme::ColorfulTheme;
use dialoguer::{Confirm, Select, Sort};
use prettytable::{format, Cell, Row, Table};
use std::env::current_dir;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;

mod input;
mod project;

use project::{project_state_dir, Project, ProjectStorage, ProjectType};

#[derive(Parser, Debug)]
#[command(name = "prj", version, about, long_about = None)]
struct Args {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Add a new project
    Add {
        /// Project name (optional; will prompt if missing)
        name: Option<String>,
        /// Project type (optional; will prompt if missing)
        #[arg(value_enum)]
        project_type: Option<ProjectType>,
        /// Path to the project (optional; defaults to current directory if not provided)
        path: Option<PathBuf>,
    },
    /// Print project path
    PrintPath {
        /// Project name to get path
        name: Option<String>,
    },
    /// Remove a project by name or path
    Remove {
        /// Project name to remove
        #[arg(short, long)]
        name: Option<String>,
        /// Path to the project to remove
        #[arg(short, long)]
        path: Option<PathBuf>,
    },
    /// List all projects
    List {
        /// Enable interactive mode
        #[arg(short, long)]
        interactive: bool,
        /// Allow selecting multiple projects
        /// (only applicable in interactive mode)
        /// Keyboard shortcuts:
        /// - `TAB`: Select multiple items
        /// - `CTRL-A`: Select all items
        /// - `CTRL-U`: Clear query
        #[arg(short, long)]
        multi: bool,
    },
    /// Reorder projects
    Reorder,
    /// Setup the project manager
    Setup,
    /// Clone a new GitHub repository and add as a project
    Clone {
        /// GitHub repository URL to clone
        repo_url: String,
        /// Optional project type (e.g., Cargo, CMake)
        #[arg(value_enum, long = "type")]
        project_type: Option<ProjectType>,
        #[arg(long = "name")]
        /// Optional project name (defaults to repository name)
        project_name: Option<String>,
        #[arg(long = "path")]
        project_path: Option<PathBuf>,
        /// Additional Git options (like branch)
        #[arg(last = true)]
        git_options: Vec<String>,
    },
}

impl FromStr for ProjectType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "CMake" => Ok(ProjectType::CMake),
            "Cargo" => Ok(ProjectType::Cargo),
            "Other" => Ok(ProjectType::Other),
            _ => Err(format!("Invalid project type: {}", s)),
        }
    }
}

fn main() {
    let args = Args::parse();
    let mut storage = ProjectStorage::load_or_initialize();

    match args.command {
        Commands::Add {
            name,
            project_type,
            path,
        } => {
            // Current directory name as default project name
            let name = name.unwrap_or_else(|| {
                let current_dir_name = current_dir()
                    .expect("Unable to get current directory")
                    .file_name()
                    .expect("Unable to get current directory name")
                    .to_string_lossy()
                    .to_string();

                input::prompt(
                    "Enter project name: ".bold().green().to_string(),
                    Some(current_dir_name),
                )
            });
            let project_type = project_type.unwrap_or_else(|| {
                match input::prompt_enum(
                    "Enter project type (CMake, Cargo, Other): "
                        .bold()
                        .green()
                        .to_string(),
                    &["CMake", "Cargo", "Other"],
                    Some("Other".to_string()),
                ) {
                    Some(project_type) => project_type,
                    None => {
                        println!("{}", "Project creation canceled.".bold().yellow());
                        std::process::exit(0);
                    }
                }
            });
            let path = path.unwrap_or_else(|| {
                let input = input::prompt_empty(
                    "Enter project path (empty for current directory): "
                        .bold()
                        .green()
                        .to_string()
                );

                if input.is_empty() {
                    current_dir().expect("Unable to get current directory")
                } else {
                    match fs::canonicalize(PathBuf::from(input)) {
                        Ok(path) => path,
                        Err(e) => {
                            eprintln!("Failed to resolve path: {}", e);
                            std::process::exit(1);
                        }
                    }
                }
            });

            if !path.exists() {
                eprintln!("{}", "Path does not exist.".bold().red());
                return;
            }

            if !path.is_dir() {
                eprintln!("{}", "Path is not a directory.".bold().red());
                return;
            }

            let project = Project {
                name,
                project_type,
                path,
            };
            storage.add_project(project);
            println!("{}", "Project added successfully.".bold().blue());
        }
        Commands::PrintPath { name } => {
            let name = name.unwrap_or_else(|| {
                input::choose_project_name(&storage, false)
                    .map(|names| names.first().unwrap().clone())
                    .unwrap_or_else(|| {
                        input::prompt("Enter project name: ".bold().green().to_string(), None)
                    })
            });

            if let Some(path) = storage.get_project_path(&name) {
                println!("{}", "Project path:".bold().green());
                println!("{}", path.display());
            } else {
                println!("{}", "Project not found.".bold().red());
            }
        }
        Commands::Remove { name, path } => {
            if name.is_some() || path.is_some() {
                storage.remove_project(name, path);
                println!("{}", "Project removed successfully.".bold().blue());
            } else {
                println!("{}", "Please specify a project name or path.".bold().red());
            }
        }
        Commands::List { interactive, multi } => {
            if interactive {
                let names = match input::choose_project_name(&storage, multi) {
                    Some(names) => names,
                    None => return,
                };

                let projects = storage
                    .projects
                    .iter()
                    .filter(|project| names.contains(&project.name))
                    .cloned()
                    .collect::<Vec<_>>();

                let actions = vec!["Print Path", "Remove", "Exit"];
                let action_index = Select::with_theme(&ColorfulTheme::default())
                    .with_prompt(format!(
                        "Select an action for '{}'",
                        if projects.len() == 1 {
                            projects.first().unwrap().name.clone()
                        } else {
                            format!("{} projects", projects.len())
                        }
                    ))
                    .items(&actions)
                    .default(0)
                    .interact_opt()
                    .expect("Failed to open action menu")
                    .unwrap_or(2);

                match action_index {
                    0 => {
                        for project in projects {
                            println!("{}", "Project path:".bold().green());
                            println!("{}", project.path.display());
                        }
                    }
                    1 => {
                        // "Remove"
                        let confirm = Confirm::with_theme(&ColorfulTheme::default())
                            .with_prompt(format!(
                                "Are you sure you want to remove '{}'",
                                if projects.len() == 1 {
                                    projects.first().unwrap().name.clone()
                                } else {
                                    format!("{} projects", projects.len())
                                }
                            ))
                            .default(false)
                            .interact_opt()
                            .expect("Failed to capture confirmation")
                            .unwrap_or(false);

                        if confirm {
                            for project in projects {
                                storage.remove_project(Some(project.name.clone()), None);
                                println!("{} '{}'", "Removed project".bold().red(), project.name);
                            }
                        } else {
                            println!("Removal canceled.");
                        }
                    }
                    2 => {}
                    _ => unreachable!(),
                }
            } else {
                println!("{}", "Listing all projects:".bold().blue());
                display_projects(&storage);
            }
        }
        Commands::Reorder => {
            let theme = ColorfulTheme::default();
            let reorder_indices = Sort::with_theme(&theme)
                .with_prompt("Reorder projects (space to select)")
                .items(&storage.projects.iter().map(|p| &p.name).collect::<Vec<_>>())
                .interact_opt()
                .expect("Failed to reorder projects");

            if reorder_indices.is_none() {
                println!("{}", "Reordering canceled.".bold().yellow());
                return;
            }

            let mut reordered_projects = Vec::with_capacity(storage.projects.len());
            for index in reorder_indices.unwrap() {
                reordered_projects.push(storage.projects[index].clone());
            }

            assert_eq!(reordered_projects.len(), storage.projects.len());
            storage.projects = reordered_projects;

            println!("{}", "Projects reordered successfully.".bold().blue());

        }
        Commands::Setup => {
            setup_script();
        }
        Commands::Clone {
            repo_url,
            project_type,
            project_name,
            project_path,
            git_options,
        } => {
            clone_and_add_project(
                &repo_url,
                project_type,
                project_name,
                project_path,
                git_options,
                &mut storage,
            );
        }
    }
}

fn clone_and_add_project(
    repo_url: &str,
    project_type: Option<ProjectType>,
    project_name: Option<String>,
    project_path: Option<PathBuf>,
    git_options: Vec<String>,
    storage: &mut ProjectStorage,
) {
    // Parse the repository name from the URL
    let git_name = repo_url
        .split('/')
        .last()
        .expect("Invalid repo URL")
        .replace(".git", "");
    let repo_name = project_name.unwrap_or(git_name.clone());

    // Define the clone path
    let clone_path = current_dir()
        .expect("Unable to get current directory")
        .join(project_path.unwrap_or_else(|| PathBuf::from(&git_name)));

    println!(
        "{} {}",
        "Cloning repository to:".bold().green(),
        clone_path.display()
    );

    // Prepare the `git clone` command with additional options
    let mut command = Command::new("git");
    command.arg("clone").arg(repo_url).arg(&clone_path);

    // Add additional git options
    for arg in git_options {
        command.arg(arg);
    }

    // Execute the `git clone` command
    let status = command.status();

    match status {
        Ok(status) if status.success() => {
            let project = Project {
                name: repo_name.clone(),
                project_type: project_type.unwrap_or(ProjectType::Other),
                path: clone_path,
            };
            storage.add_project(project);
            println!("{}", "Project cloned and added successfully.".bold().blue());
        }
        Ok(_) => println!(
            "{} Git command exited with an error.",
            "Failed to clone repository:".bold().red()
        ),
        Err(e) => println!("{} {}", "Failed to clone repository:".bold().red(), e),
    }
}

fn setup_script() {
    let script_content = include_str!("pj.sh");
    let mut script_path = project_state_dir();
    script_path.push("setup.sh");

    fs::write(&script_path, script_content).expect("Failed to write setup script");

    let shell_rc = if cfg!(target_os = "macos") {
        ".zshrc"
    } else {
        ".bashrc"
    };
    let home_dir = dirs::home_dir().expect("Could not determine home directory");
    let rc_file = home_dir.join(shell_rc);

    let source_line = format!("source {}", script_path.display());
    if let Ok(contents) = fs::read_to_string(&rc_file) {
        if contents.contains(&source_line) {
            println!(
                "{}",
                "Setup already completed. No changes made.".bold().yellow()
            );
            return;
        }
    }

    let mut file = fs::OpenOptions::new()
        .append(true)
        .open(&rc_file)
        .expect("Failed to open shell rc file");
    writeln!(file, "\n# Generated by prj\n{}", source_line)
        .expect("Failed to write to shell rc file");

    println!(
        "{} {}",
        "Setup completed.".bold().green(),
        "Please restart your terminal or run 'source ~/{shell_rc}' to activate 'pj' command."
            .italic()
            .yellow()
    );
}

fn display_projects(storage: &ProjectStorage) {
    let mut table = Table::new();
    table.set_format(*format::consts::FORMAT_BOX_CHARS);

    table.set_titles(Row::new(vec![
        Cell::new("Name").style_spec("Fb"),
        Cell::new("Type").style_spec("Fb"),
        Cell::new("Path").style_spec("Fb"),
    ]));

    for project in &storage.projects {
        table.add_row(Row::new(vec![
            Cell::new(&project.name).style_spec("Fb"), // Name in Bold
            Cell::new(&format!("{:?}", project.project_type)).style_spec("Fc"),
            Cell::new(&project.path.display().to_string()).style_spec("id"),
        ]));
    }

    table.printstd();
}