cargo-trim 0.10.2

Binary application to cleanup $CARGO_HOME cache
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
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use anyhow::{Context, Result};
use semver::Version;
use serde::Deserialize;
use url::Url;

use crate::config_file::ConfigFile;
use crate::crate_detail::{CrateDetail, CrateMetaData};
use crate::dir_path::DirPath;

/// struct store Cargo.toml file location
pub(crate) struct CargoTomlLocation {
    path: Vec<PathBuf>,
}

impl CargoTomlLocation {
    pub(crate) fn new() -> Self {
        Self { path: Vec::new() }
    }

    pub(crate) fn add_path(&mut self, path: PathBuf) {
        self.path.push(path);
    }

    pub(crate) fn append(&mut self, mut lock_location: Self) {
        self.path.append(&mut lock_location.path);
    }

    pub(crate) fn location_path(&self) -> &Vec<PathBuf> {
        &self.path
    }
}

#[derive(Clone, Deserialize)]
struct LockData {
    package: Option<Vec<Package>>,
}

impl LockData {
    fn package(&self) -> &Option<Vec<Package>> {
        &self.package
    }
}

#[derive(Clone, Deserialize)]
struct Package {
    name: String,
    version: String,
    source: Option<String>,
}

impl Package {
    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &str {
        &self.version
    }

    fn source(&self) -> &Option<String> {
        &self.source
    }
}

/// struct to store all crate list detail with its type
pub(crate) struct CrateList {
    installed_bin: Vec<CrateMetaData>,
    installed_crate_registry: Vec<CrateMetaData>,
    installed_crate_git: Vec<CrateMetaData>,
    old_crate_registry: Vec<CrateMetaData>,
    old_crate_git: Vec<CrateMetaData>,
    used_crate_registry: Vec<CrateMetaData>,
    used_crate_git: Vec<CrateMetaData>,
    orphan_crate_registry: Vec<CrateMetaData>,
    orphan_crate_git: Vec<CrateMetaData>,
    cargo_toml_location: CargoTomlLocation,
}

impl CrateList {
    /// create list of all types of crate present in directory
    pub(crate) fn create_list(
        dir_path: &DirPath,
        config_file: &ConfigFile,
        crate_detail: &mut CrateDetail,
    ) -> Result<Self> {
        let bin_dir = dir_path.bin_dir();
        let cache_dir = dir_path.cache_dir();
        let src_dir = dir_path.src_dir();
        let checkout_dir = dir_path.checkout_dir();
        let db_dir = dir_path.db_dir();

        // list installed crates
        let installed_bin = crate_detail.list_installed_bin(bin_dir)?;
        let installed_crate_registry =
            crate_detail.list_installed_crate_registry(src_dir, cache_dir)?;
        let installed_crate_git = crate_detail.list_installed_crate_git(checkout_dir, db_dir)?;

        // list old registry crate
        let (old_crate_registry, old_crate_git) = list_old_crates(
            db_dir,
            installed_crate_registry.clone(),
            &installed_crate_git,
        )?;

        // list all used crates in rust program
        let (cargo_toml_location, used_crate_registry, used_crate_git) =
            list_used_crates(config_file)?;

        // list orphan crates. If crate is not used then it is orphan
        let (orphan_crate_registry, orphan_crate_git) = list_orphan_crates(
            &installed_crate_registry,
            &installed_crate_git,
            &used_crate_registry,
            &used_crate_git,
        );

        Ok(Self {
            installed_bin,
            installed_crate_registry,
            installed_crate_git,
            old_crate_registry,
            old_crate_git,
            used_crate_registry,
            used_crate_git,
            orphan_crate_registry,
            orphan_crate_git,
            cargo_toml_location,
        })
    }

    /// provide list of installed bin
    pub(crate) fn installed_bin(&self) -> &Vec<CrateMetaData> {
        &self.installed_bin
    }

    /// provide list of installed registry
    pub(crate) fn installed_registry(&self) -> &Vec<CrateMetaData> {
        &self.installed_crate_registry
    }

    /// provide list of old registry
    pub(crate) fn old_registry(&self) -> &Vec<CrateMetaData> {
        &self.old_crate_registry
    }

    /// provide list of used registry
    pub(crate) fn used_registry(&self) -> &Vec<CrateMetaData> {
        &self.used_crate_registry
    }

    /// provide list o orphan registry
    pub(crate) fn orphan_registry(&self) -> &Vec<CrateMetaData> {
        &self.orphan_crate_registry
    }

    /// provide list of installed git
    pub(crate) fn installed_git(&self) -> &Vec<CrateMetaData> {
        &self.installed_crate_git
    }

    /// provide list of old git
    pub(crate) fn old_git(&self) -> &Vec<CrateMetaData> {
        &self.old_crate_git
    }

    /// provide list of used git
    pub(crate) fn used_git(&self) -> &Vec<CrateMetaData> {
        &self.used_crate_git
    }

    /// provide list of orphan git
    pub(crate) fn orphan_git(&self) -> &Vec<CrateMetaData> {
        &self.orphan_crate_git
    }

    /// list out path of directory which contains cargo lock file
    pub(crate) fn cargo_toml_location(&self) -> &CargoTomlLocation {
        &self.cargo_toml_location
    }

    /// list crates which is both old and orphan
    pub(crate) fn list_old_orphan_registry(&self) -> Vec<CrateMetaData> {
        let mut old_orphan_registry = Vec::new();
        let orphan_list = self.orphan_registry();
        for crates in self.old_registry() {
            if orphan_list.contains(crates) {
                old_orphan_registry.push(crates.clone());
            }
        }
        old_orphan_registry
    }

    /// list out git crates which is both old and orphan
    pub(crate) fn list_old_orphan_git(&self) -> Vec<CrateMetaData> {
        let mut old_orphan_git = Vec::new();
        let orphan_list = self.orphan_git();
        for crates in self.old_git() {
            if orphan_list.contains(crates) {
                old_orphan_git.push(crates.clone());
            }
        }
        old_orphan_git
    }
}

/// Read out content of cargo.lock file to list out crates present so can be
/// used for orphan clean
fn read_content(list: &[PathBuf]) -> Result<(Vec<CrateMetaData>, Vec<CrateMetaData>)> {
    let mut present_crate_registry = Vec::new();
    let mut present_crate_git = Vec::new();
    for lock in list.iter() {
        let mut lock_folder = lock.clone();
        lock_folder.push("Cargo.lock");
        if lock_folder.exists() {
            let file_content = std::fs::read_to_string(lock_folder)
                .context("failed to read cargo lock content to string")?;
            let cargo_lock_data: LockData =
                toml::from_str(&file_content).context("Failed to convert to Toml format")?;
            if let Some(packages) = cargo_lock_data.package() {
                for package in packages {
                    if let Some(source) = package.source() {
                        let name = package.name();
                        let version = package.version();
                        if source.contains("registry+") {
                            let url = Url::from_str(&source.replace("registry+", ""))
                                .context("Failed registry source url kind conversion")?;
                            present_crate_registry.push(CrateMetaData::new(
                                name.to_string(),
                                Some(
                                    Version::parse(version)
                                        .context("failed Cargo.lock semver version parse")?,
                                ),
                                0,
                                Some(url),
                            ));
                        }
                        if source.contains("git+") {
                            let url_with_kind;
                            let rev_sha_vec: Vec<&str>;
                            // determine url with kind according to git source have query param or
                            // not
                            if source.contains("?rev=")
                                || source.contains("?branch=")
                                || source.contains("?tag=")
                            {
                                let split_url: Vec<&str> = if source.contains("?rev=") {
                                    source.split("?rev=").collect()
                                } else if source.contains("?branch=") {
                                    source.split("?branch=").collect()
                                } else {
                                    source.split("?tag=").collect()
                                };
                                rev_sha_vec = split_url[1].split('#').collect();
                                url_with_kind = split_url[0];
                            } else {
                                rev_sha_vec = source.split('#').collect();
                                url_with_kind = rev_sha_vec[0];
                            }
                            let rev_short_form = &rev_sha_vec[1][..=6];
                            let full_name = format!("{name}-{rev_short_form}");
                            let url = Url::from_str(&url_with_kind.replace("git+", "")).context(
                                "Failed git source url kind with query params conversion",
                            )?;
                            present_crate_git.push(CrateMetaData::new(
                                full_name,
                                None,
                                0,
                                Some(url),
                            ));
                        }
                        if source.contains("sparse+") {
                            let url = Url::from_str(&source.replace("sparse+", ""))
                                .context("Failed sparse source url kind conversion")?;
                            present_crate_registry.push(CrateMetaData::new(
                                name.to_string(),
                                Some(
                                    Version::parse(version)
                                        .context("failed Cargo.lock semver version parse")?,
                                ),
                                0,
                                Some(url),
                            ));
                        }
                    }
                }
            }
        }
    }
    Ok((present_crate_registry, present_crate_git))
}

/// List old crates
fn list_old_crates(
    db_dir: &Path,
    installed_crate_registry: Vec<CrateMetaData>,
    installed_crate_git: &[CrateMetaData],
) -> Result<(Vec<CrateMetaData>, Vec<CrateMetaData>)> {
    let mut old_crate_registry = Vec::new();
    let mut registry_crates = installed_crate_registry;
    registry_crates.sort();
    if registry_crates.len() > 1 {
        for i in 0..(registry_crates.len() - 1) {
            let crate_metadata = &registry_crates[i];
            let next_crate_metadata = &registry_crates[i + 1];
            if crate_metadata.name() == next_crate_metadata.name()
                && crate_metadata.source() == next_crate_metadata.source()
            {
                old_crate_registry.push(crate_metadata.clone());
            }
        }
    }
    old_crate_registry.sort();
    old_crate_registry.dedup();

    // list old git crate
    let mut old_crate_git = Vec::new();
    // analyze each crates of db dir and create list of head rev value
    if db_dir.exists() {
        let mut full_name_list = Vec::new();
        for crates in fs::read_dir(db_dir).context("failed to read db dir")? {
            let entry = crates?.path();
            let file_name = entry
                .file_name()
                .context("failed to get sold crate db dir file name")?
                .to_str()
                .context("Failed to convert db dir entry file name to str")?;
            let rev_value = latest_rev_value(&entry)?;
            let full_name = format!("{file_name}-{rev_value}");
            full_name_list.push(full_name);
        }
        for crate_metadata in installed_crate_git {
            let crate_name = crate_metadata.name();
            if !crate_name.contains("-HEAD") && !full_name_list.contains(crate_name) {
                old_crate_git.push(crate_metadata.clone());
            }
        }
    }
    old_crate_git.sort();
    old_crate_git.dedup();

    Ok((old_crate_registry, old_crate_git))
}

/// list used crates
fn list_used_crates(
    config_file: &ConfigFile,
) -> Result<(CargoTomlLocation, Vec<CrateMetaData>, Vec<CrateMetaData>)> {
    let mut used_crate_registry = Vec::new();
    let mut used_crate_git = Vec::new();
    let mut cargo_toml_location = CargoTomlLocation::new();
    let config_directory = config_file.directory().clone();
    // read a Cargo.lock file and determine out a used registry and git crate
    for path in &config_directory {
        let list_cargo_toml = config_file.list_cargo_toml(Path::new(path))?;
        let (mut registry_crate, mut git_crate) = read_content(list_cargo_toml.location_path())?;
        cargo_toml_location.append(list_cargo_toml);
        used_crate_registry.append(&mut registry_crate);
        used_crate_git.append(&mut git_crate);
    }
    used_crate_registry.sort();
    used_crate_registry.dedup();
    used_crate_git.sort();
    used_crate_registry.dedup();
    Ok((cargo_toml_location, used_crate_registry, used_crate_git))
}

/// list orphan crates
fn list_orphan_crates(
    installed_crate_registry: &[CrateMetaData],
    installed_crate_git: &[CrateMetaData],
    used_crate_registry: &[CrateMetaData],
    used_crate_git: &[CrateMetaData],
) -> (Vec<CrateMetaData>, Vec<CrateMetaData>) {
    let mut orphan_crate_registry = Vec::new();
    let mut orphan_crate_git = Vec::new();
    for crates in installed_crate_registry {
        if !used_crate_registry.contains(crates) {
            orphan_crate_registry.push(crates.clone());
        }
    }
    for crates in installed_crate_git {
        let crate_name = crates.name();
        if crate_name.contains("-HEAD") {
            let split_installed = crate_name.rsplitn(2, '-').collect::<Vec<&str>>();
            if used_crate_git.is_empty() {
                orphan_crate_git.push(crates.clone());
            }
            let mut used_in_project = false;
            for used in used_crate_git {
                if used.name().contains(split_installed[1]) {
                    used_in_project = true;
                    // Break if found to be used one time no need to check for other
                    break;
                }
            }
            if !used_in_project {
                orphan_crate_git.push(crates.clone());
            }
        } else if !used_crate_git.contains(crates) {
            orphan_crate_git.push(crates.clone());
        }
    }
    orphan_crate_registry.sort();
    orphan_crate_registry.dedup();
    orphan_crate_git.sort();
    orphan_crate_git.dedup();
    (orphan_crate_registry, orphan_crate_git)
}

/// get latest commit rev value from git repository
fn latest_rev_value(path: &Path) -> Result<String> {
    let mut fetch_head_file = PathBuf::new();
    fetch_head_file.push(path);
    fetch_head_file.push("FETCH_HEAD");
    let content = fs::read_to_string(fetch_head_file).context("Failed to read FETCH_HEAD file")?;
    // read first 7 value which is same as hash for git based checkout folder
    Ok(content[..7].to_string())
}