~neon/activity-graph

ref: 244b37d839bc92e47a1faff52fcb2c0d79fc53fb activity-graph/src/main.rs -rw-r--r-- 6.4 KiB
244b37d8Jens Pitkanen Add -q 2 years ago
                                                                                
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
// First, enable all the warnings.
#![warn(clippy::all, clippy::pedantic)]
// Then, disable the pedantic warnings I don't like.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::must_use_candidate
)]

use structopt::StructOpt;

use std::fs::File;
use std::io::{BufWriter, Write};
#[cfg(feature = "server")]
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::time;

mod commits;
mod find_repositories;
mod log;
mod render;
#[cfg(feature = "server")]
mod server;

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ProjectMetadata {
    name: String,
    path: PathBuf,
}

#[derive(Clone, Default)]
pub struct Day {
    filler: bool,
    commits: Vec<ProjectMetadata>,
}

#[derive(Clone)]
pub struct Year {
    year: usize,
    days: Vec<Day>,
}

#[derive(StructOpt)]
#[structopt(author)]
#[structopt(about)]
/// Generates a nice activity graph from a bunch of Git repositories
pub struct Args {
    #[structopt(subcommand)]
    command: Option<CommandArgs>,
}

#[derive(StructOpt, Default, Clone)]
pub struct GenerationData {
    /// Regex that matches the author(s) whose commits are being
    /// counted (if not set, all commits will be counted)
    #[structopt(short, long)]
    author: Option<String>,
    /// How many subdirectories deep the program should search (if not
    /// set, there is no limit)
    #[structopt(short, long)]
    depth: Option<i32>,
    /// Path(s) to the directory (or directories) containing the
    /// repositories you want to include
    #[structopt(short, long)]
    input: Vec<PathBuf>,
}

#[derive(StructOpt, Clone, Default)]
pub struct ExternalResources {
    /// A html file that will be pasted in the <head> element
    #[structopt(long)]
    external_head: Option<PathBuf>,
    /// A html file that will be pasted at the beginning of the <body>
    /// element
    #[structopt(long)]
    external_header: Option<PathBuf>,
    /// A html file that will be pasted at the end of the <body>
    /// element
    #[structopt(long)]
    external_footer: Option<PathBuf>,
    /// A css file that will be pasted at the end of the css
    #[structopt(long)]
    external_css: Option<PathBuf>,
}

#[derive(StructOpt)]
pub struct Verbosity {
    /// Prints verbose information
    #[structopt(short, long)]
    verbose: bool,
    /// Disables all prints
    #[structopt(short, long)]
    quiet: bool,
}

#[derive(StructOpt)]
enum CommandArgs {
    /// Output the generated html into a file
    Generate {
        #[structopt(flatten)]
        verbosity: Verbosity,
        #[structopt(flatten)]
        gen: GenerationData,
        #[structopt(flatten)]
        ext: ExternalResources,
        /// The file that the resulting html will be printed out to
        #[structopt(short = "o", long, default_value = "activity-graph.html")]
        html: PathBuf,
        /// The file that the stylesheet will be printed out to (if not
        /// set, it will be included in the html inside a style-element)
        #[structopt(short, long)]
        css: Option<PathBuf>,
    },

    /// Prints a visualization into stdout
    Stdout {
        #[structopt(flatten)]
        verbosity: Verbosity,
        #[structopt(flatten)]
        gen: GenerationData,
    },

    #[cfg(feature = "server")]
    /// Run a server that serves the generated activity graph html
    Server {
        #[structopt(flatten)]
        verbosity: Verbosity,
        #[structopt(flatten)]
        gen: GenerationData,
        #[structopt(flatten)]
        ext: ExternalResources,
        /// The address that the server is hosted on
        #[structopt(long, default_value = "127.0.0.1:80")]
        host: SocketAddr,
        /// The minimum amount of seconds between regenerating the
        /// html and css
        #[structopt(long, default_value = "1")]
        cache_lifetime: u64,
    },
}

fn main() {
    let start_time = time::Instant::now();
    let args = Args::from_args();

    if let Some(command) = &args.command {
        match command {
            CommandArgs::Generate {
                verbosity,
                gen,
                ext,
                html,
                css,
            } => {
                log::set_verbosity(verbosity);

                let write_to_file = |path: &Path, s: String, name: &str| {
                    let mut writer = File::create(path).map(BufWriter::new);
                    match &mut writer {
                        Ok(writer) => {
                            if let Err(err) = writer.write(&s.as_bytes()) {
                                log::println(&format!(
                                    "error: encountered while writing out the {}: {}",
                                    name, err
                                ));
                            }
                        }
                        Err(err) => {
                            log::println(&format!(
                                "error: encountered while creating the {} file: {}",
                                name, err
                            ));
                        }
                    }
                };

                let years = generate_years(&gen);

                let output_html = render::html(&ext, &html, css.as_ref(), &years);
                write_to_file(&html, output_html, "html");

                if let Some(css) = css {
                    let output_css = render::css(&ext);
                    write_to_file(&css, output_css, "css");
                }
            }

            CommandArgs::Stdout { verbosity, gen } => {
                log::set_verbosity(verbosity);
                println!("{}", render::ascii(&generate_years(gen)));
            }

            #[cfg(feature = "server")]
            CommandArgs::Server {
                verbosity,
                gen,
                ext,
                host,
                cache_lifetime,
            } => {
                log::set_verbosity(verbosity);
                server::run(&gen, &ext, *host, *cache_lifetime);
            }
        }
    }

    log::verbose_println(
        &format!(
            "finished all tasks, this run of the program took {:?}",
            time::Instant::now() - start_time
        ),
        false,
    );
}

pub fn generate_years(gen: &GenerationData) -> Vec<Year> {
    let repos = find_repositories::from_paths(&gen.input, gen.depth);
    let commit_dates = commits::find_dates(gen.author.as_ref(), &repos);
    render::gather_years(commit_dates)
}