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
pub mod build_tool_manager;
pub mod build_tools;
mod cli;
pub mod find_projects;
mod fs;
pub mod project;

use anyhow::Context;
use build_tool_manager::BuildToolManager;
use clap::{CommandFactory, ErrorKind};
use console::{colors_enabled, style};
use dialoguer::{
    theme::{ColorfulTheme, SimpleTheme, Theme},
    Confirm,
};
use project::Project;
use std::{
    collections::{hash_map::Entry, HashMap, HashSet},
    io,
    path::{Path, PathBuf},
};
use time::Duration;
use tracing::debug;

pub use crate::cli::Cli;
use crate::{
    find_projects::projects_below,
    fs::format_size,
    project::{dto::ProjectDto, mtime::HumanReadableElapsed, ProjectFilter, StatusFilter},
};

/// Prints projects to stdout.
pub fn list(cli: Cli, build_tool_manager: BuildToolManager) -> anyhow::Result<()> {
    let project_filter = {
        let min_stale = cli.min_stale.unwrap_or(Duration::ZERO);
        let status = StatusFilter::Any;
        ProjectFilter { min_stale, status }
    };
    debug!("listing projects with {project_filter:?}");

    // We use a Set as directories could overlap, and we don't want to print projects multiple times
    let mut printed_paths: HashSet<PathBuf> = HashSet::new();
    let mut freeable_bytes = 0;
    for directory in cli.directories {
        for project in projects_below(&directory, &project_filter, &build_tool_manager) {
            let is_new = printed_paths.insert(project.path().to_owned());
            if is_new {
                print_project(&project, cli.json)?;
                freeable_bytes += project
                    .build_tools()
                    .iter()
                    .map(|x| match x.status() {
                        Ok(build_tools::BuildStatus::Built { freeable_bytes }) => freeable_bytes,
                        _ => 0,
                    })
                    .sum::<u64>();
            }
        }
    }

    if !cli.json {
        println!();
        let message = format!(
            "Found {} of build artifacts and dependencies.",
            format_size(freeable_bytes)
        );
        if colors_enabled() {
            println!("{}", style(message).green());
        } else {
            println!("{}", message);
        }
    }

    Ok(())
}

/// Removes generated and downloaded files from code projects to free up space.
///
/// Runs in interactive mode unless either one of `cli.dry_run` and `cli.yes` is true.
pub fn clean(cli: Cli, build_tool_manager: BuildToolManager) -> anyhow::Result<()> {
    // I couldn't figure out how to do this with Clap..
    if cli.json && !cli.dry_run && !cli.yes {
        // Would be interactive mode, which doesn't make sense with JSON - the
        // prompt is not JSON formatted, after all.
        let mut cmd = Cli::command();
        cmd.error(
            ErrorKind::ArgumentConflict,
            "With `--json`, either `--dry-run` or `--yes` is required.",
        )
        .exit();
    }

    let project_filter = {
        let min_stale = cli.min_stale.unwrap_or_else(|| Duration::days(30));
        let status = if cli.archive {
            StatusFilter::Any
        } else {
            StatusFilter::ExceptClean
        };
        ProjectFilter { min_stale, status }
    };

    // We use a HashMap as directories could overlap, and archiving a directory twice doesn't work
    let mut projects: HashMap<PathBuf, Project> = HashMap::new();
    for directory in cli.directories {
        for project in projects_below(&directory, &project_filter, &build_tool_manager) {
            if let Entry::Vacant(entry) = projects.entry(project.path().to_owned()) {
                print_project(&project, cli.json)?;
                entry.insert(project);
            }
        }
    }

    if cli.json && cli.dry_run {
        // If we'd continue, we'd fck up the JSON output, as the dry-run output
        // is not formatted.
        return Ok(());
    }

    let freeable_bytes = projects
        .values()
        .flat_map(|p| p.build_tools().iter())
        .map(|bt| match bt.status() {
            Ok(build_tools::BuildStatus::Built { freeable_bytes }) => freeable_bytes,
            _ => 0,
        })
        .sum::<u64>();

    let has_cleaned = {
        if projects.is_empty() {
            false
        } else {
            let do_continue = if cli.dry_run {
                println!("\n{}", style("WOULD DO:").bold());
                true
            } else if cli.yes {
                true
            } else {
                println!();

                let theme = theme();
                let prompt = format!("Clean up those projects ({})?", format_size(freeable_bytes));
                Confirm::with_theme(&*theme)
                    .with_prompt(prompt)
                    .default(true)
                    .interact()?
            };

            if do_continue {
                // First clean all of them
                for project in projects.values_mut() {
                    project
                        .clean(cli.dry_run)
                        .with_context(|| format!("Failed to clean project {project}"))?;
                }

                if cli.archive {
                    // Then we check which one of them should be archived.
                    //
                    // Projects are only archived if they're not part of another
                    // project that is also archived. In other words: we don't
                    // want nested tar.xz files.
                    let projects_to_archive: Vec<PathBuf> = projects
                        .keys()
                        .filter(|path| {
                            // A project is part of another considered project, if
                            // its path is part of another path in projects.keys().
                            // So: keep it only if no other key starts_with the path.
                            projects
                                .keys()
                                .all(|k| *k == **path || !path.starts_with(k))
                        })
                        .cloned()
                        .collect();

                    for path in projects_to_archive {
                        let project = projects.get_mut(&path).expect("must be there");
                        project.archive(cli.dry_run).with_context(|| {
                            format!("Failed to archive cleaned project {project}")
                        })?;
                    }
                }

                !cli.dry_run
            } else {
                println!("No changes made.");
                false
            }
        }
    };

    if !cli.json {
        println!();
        println!("{}", style("SUMMARY:").bold());
        let projects_label = if projects.len() == 1 {
            "project"
        } else {
            "projects"
        };
        println!(
            "  {}",
            style(if has_cleaned {
                format!(
                    "{} {projects_label} cleaned, which freed approx. {} of build artifacts and dependencies.",
                    projects.len(),
                    format_size(freeable_bytes)
                )
            } else {
                format!(
                    "{} built {projects_label} found, with {} of build artifacts and dependencies.",
                    projects.len(),
                    format_size(freeable_bytes)
                )
            })
            .green()
        );
        let n_projects_without_vcs = projects.values().filter(|p| p.vcs().is_none()).count();
        if n_projects_without_vcs > 0 {
            println!(
                "  {}",
                style(format!(
                    "{} projects not under version control:",
                    n_projects_without_vcs
                ))
                .red()
            );
            projects
                .values()
                .filter(|p| p.vcs().is_none())
                .for_each(|p| println!("    {}", style(p.path().display()).dim()));
        }
    }

    Ok(())
}

fn theme() -> Box<dyn Theme> {
    if colors_enabled() {
        Box::<ColorfulTheme>::default()
    } else {
        Box::new(SimpleTheme {})
    }
}

fn print_project(project: &Project, json: bool) -> anyhow::Result<()> {
    if json {
        let dto = ProjectDto::from(project);
        serde_json::to_writer(io::stdout(), &dto)?;
        // Add the newline:
        println!();
    } else {
        pretty_print_project(project)?;
    }
    Ok(())
}

fn pretty_print_project(project: &Project) -> anyhow::Result<()> {
    let use_color = colors_enabled();

    let tools = project
        .build_tools()
        .iter()
        .map(|x| x.to_string())
        .collect::<Vec<_>>()
        .join(", ");
    let vcs = project
        .vcs()
        .as_ref()
        .map(|x| x.name())
        .unwrap_or_else(|| "no VCS");
    let freeable = match project.freeable_bytes() {
        0 => String::new(),
        bytes => format!("; {}", format_size(bytes)),
    };
    let mtime = project.mtime().human_readable_elapsed();

    let path = ProjectPath::from(project).render(use_color);

    let line = if use_color {
        let info = style(format!("({tools}; {vcs}; {mtime}{freeable})")).dim();
        format!("{} {}", path, info)
    } else {
        format!("{path} ({tools}; {vcs}; {mtime}{freeable})")
    };

    println!("{line}");

    Ok(())
}

struct ProjectPath {
    /// The path up to the project_part.
    parent: PathBuf,
    /// The path to the project directory within its Git repository, or the name of its
    /// directory otherwise.
    project: PathBuf,
}

impl ProjectPath {
    fn from(project: &Project) -> Self {
        // normalize, i.e., remove trailing slash
        let path: PathBuf = project.path().components().collect();

        if let Some(vcs_root) = project.vcs().as_ref().map(|vcs| vcs.root()) {
            match path.strip_prefix(&vcs_root) {
                Ok(postfix) if postfix == Path::new("") => {
                    // The project is at the root of its repository; we treat it as if there
                    // was no repository.
                }
                Ok(_) => {
                    // The project is within a parent repository/project. When displaying the
                    // project, this parent project should be considered part of the project.
                    let parent = vcs_root.parent().expect("is canonical path and not root");
                    // normalize, i.e., remove trailing slash
                    let project: PathBuf = path
                        .strip_prefix(parent)
                        .expect("is within parent")
                        .components()
                        .collect();
                    return ProjectPath {
                        parent: parent.to_owned(),
                        project,
                    };
                }
                Err(_) => panic!("expected the VCS root to be <= the project's own path"),
            }
        }

        let parent = path.parent().expect("is canonical path and not root");
        let project = path.strip_prefix(parent).expect("is within parent");
        ProjectPath {
            parent: parent.to_owned(),
            project: project.to_owned(),
        }
    }

    fn render(&self, use_color: bool) -> String {
        if use_color {
            format!(
                "{}/{}",
                self.parent.display(),
                style(self.project.display()).bold()
            )
        } else {
            format!("{}/{}", self.parent.display(), self.project.display())
        }
    }
}