clone_lib/projectmanager/vscode/
mod.rs

1use serde::{Deserialize, Serialize};
2use std::fs;
3use std::path::PathBuf;
4
5#[derive(Serialize, Deserialize, Debug, Clone)]
6#[serde(rename_all = "camelCase")]
7struct Entry {
8    name: String,
9    root_path: PathBuf,
10    paths: Vec<PathBuf>,
11    tags: Vec<String>,
12    enabled: Option<bool>,
13}
14
15impl Default for Entry {
16    fn default() -> Self {
17        Entry {
18            name: "".to_string(),
19            root_path: "".into(),
20            paths: [].to_vec(),
21            tags: [].to_vec(),
22            enabled: Some(true),
23        }
24    }
25}
26
27pub fn add_project(
28    target_path: PathBuf,
29    workspace: PathBuf,
30    host: String,
31    group: String,
32    name: String,
33    debug: bool,
34) {
35    // Read the JSON file
36    let json_data = fs::read_to_string(&target_path).unwrap_or("[]".to_string());
37    let mut entries: Vec<Entry> =
38        serde_json::from_str(&json_data).expect("JSON was not well-formed");
39    let must_add = !entries.iter().any(|e| e.root_path == workspace);
40    if debug && !must_add {
41        println!("Entry already exist {:?}.", workspace);
42    }
43    // Create a new entry from the provided parameters
44    if must_add {
45        let new_entry = Entry {
46            name,
47            root_path: workspace,
48            tags: [group].to_vec(),
49            ..Entry::default()
50        };
51
52        // Append the new entry to the array in the JSON data
53        entries.push(new_entry.clone());
54        // Write the updated JSON data back to the file
55        let updated_data =
56            serde_json::to_string_pretty(&entries).expect("Failed to serialize JSON");
57        let _ = fs::create_dir_all(&target_path.parent().unwrap());
58        fs::write(&target_path, updated_data).expect("Unable to write file");
59        if debug {
60            println!("Entry added {:#?}", new_entry);
61        }
62    }
63}