-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.rs
138 lines (114 loc) · 3.53 KB
/
build.rs
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
use std::{
collections::HashMap,
env,
fs::File,
io::{Error, Write},
path::{Path, PathBuf},
};
use clap::{CommandFactory, ValueEnum};
use clap_complete::generate_to;
use clap_mangen::Man;
use quote::quote;
use serde::{Deserialize, Serialize};
include!("src/cli.rs");
#[derive(Debug, Serialize, Deserialize)]
pub struct Detector {
detect_files: Vec<String>,
detect_extensions: Vec<String>,
detect_folders: Vec<String>,
}
fn collect_detectors(out_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
let dir = env::current_dir().unwrap().join("data");
let json = dir.join("parsed.json");
let content = std::fs::read_to_string(json)?;
let detector: HashMap<String, Detector> = serde_json::from_str(&content)?;
let json = dir.join("aliases.json");
let content = std::fs::read_to_string(json)?;
let aliases: HashMap<String, String> = serde_json::from_str(&content)?;
let res: HashMap<String, Detector> = detector
.into_iter()
.fold(HashMap::new(), |mut acc, (k, v)| {
if let Some(alias) = aliases.get(&k) {
acc.insert(alias.clone(), v);
} else {
acc.insert(k.clone(), v);
}
acc
})
.into_iter()
.filter(|(_, v)| {
!v.detect_files.is_empty()
|| !v.detect_extensions.is_empty()
|| !v.detect_folders.is_empty()
})
.collect();
let mut output = File::create(out_dir.join("detectors.rs"))?;
writeln!(
output,
r#"
fn detectors() -> Vec<Detector> {{
vec![
"#
)?;
for (lang, detection) in res {
let matchers = detection
.detect_files
.into_iter()
.map(|file| {
quote! { Matcher::FileName(OsString::from(#file)) }
})
.chain(detection.detect_extensions.into_iter().map(|ext| {
quote! { Matcher::FileExtension(OsString::from(#ext)) }
}))
.chain(detection.detect_folders.into_iter().map(|folder| {
quote! { Matcher::DirName(OsString::from(#folder)) }
}))
.collect::<Vec<_>>();
let detector_code = quote! {
Detector {
template: String::from(#lang),
matchers: vec![#(#matchers),*],
},
};
writeln!(output, "{}", detector_code)?;
}
writeln!(
output,
r#"
]
}}
"#
)?;
Ok(())
}
fn build_shell_completion(outdir: &Path) -> Result<(), Error> {
let mut app = Cli::command();
let shells = Shell::value_variants();
for shell in shells {
generate_to(*shell, &mut app, "git-ignore", outdir)?;
}
Ok(())
}
fn build_manpages(outdir: &Path) -> Result<(), Error> {
let app = Cli::command();
let file = outdir.join("git-ignore.1");
let mut file = File::create(file)?;
Man::new(app).render(&mut file)?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=src/cli.rs");
println!("cargo:rerun-if-changed=man");
let outdir = match env::var_os("OUT_DIR") {
None => return Ok(()),
Some(outdir) => outdir,
};
let out_path = PathBuf::from(outdir);
let mut path = out_path.ancestors().nth(4).unwrap().to_owned();
path.push("assets");
std::fs::create_dir_all(&path).unwrap();
build_shell_completion(&path)?;
build_manpages(&path)?;
collect_detectors(&out_path)?;
Ok(())
}