-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
191 lines (176 loc) · 6.18 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
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
/*
* Copyright © 2020-2022 The fjp Authors
*
* This file is part of fjp
*
* fjp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* fjp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use clap::IntoApp;
use clap_complete::{generate, generate_to, Shell};
use std::env::var_os;
use std::fs::{create_dir_all, File};
use std::io::BufWriter;
use std::io::Write;
use std::path::Path;
include!("src/cli.rs");
const BIN_NAME: &str = "fjp";
const ZCOMP_HEADER: &str = r#"
#compdef fjp
autoload -U is-at-least
_profiles() {
echo $1/*.{inc,local,profile} | sed -E "s;$1\/;;g"
}
_disabled_profiles() {
local disabled_profiles=$(_profiles $HOME/.config/firejail/disabled)
if [[ ${#disabled_profiles[@]} -ne 0 ]]; then
_values 'disabled-profiles' $disabled_profiles
fi
}
_system_profiles() {
_values 'system-profiles' $(_profiles /etc/firejail)
}
_user_profiles() {
_values 'user-profiles' $(_profiles $HOME/.config/firejail)
}
_all_profiles() {
_system_profiles
_user_profiles
}
"#;
fn main() {
let out_dir = match var_os("FJP_SHELLCOMP_DIR").or_else(|| var_os("OUT_DIR")) {
Some(out_dir) => out_dir,
None => {
println!("cargo:warning=Failed to generate shell completions. err:out_dir");
return;
}
};
create_dir_all(&out_dir).unwrap();
let mut app = Cli::into_app();
generate_to(Shell::Bash, &mut app, BIN_NAME, &out_dir).expect("generate_to bash");
generate_to(Shell::Fish, &mut app, BIN_NAME, &out_dir).expect("generate_to fish");
let mut buf = Vec::new();
generate(Shell::Zsh, &mut app, BIN_NAME, &mut buf);
let rzcomp = match String::from_utf8(buf) {
Ok(comp) => comp,
Err(err) => {
println!("cargo:warning=Failed to generate zsh completions: {}", err);
return;
}
};
let mut zcomp = BufWriter::new(File::create(Path::new(&out_dir).join("fjp.zsh")).unwrap());
write!(zcomp, "{}", &ZCOMP_HEADER[1..ZCOMP_HEADER.len()]).unwrap();
let mut sub_c_arm = None;
for line in rzcomp.lines().skip(3) {
match line.trim() {
"(cat)" => {
sub_c_arm = Some("cat");
writeln!(zcomp, "{}", line)
}
"(diff)" => {
sub_c_arm = Some("diff");
writeln!(zcomp, "{}", line)
}
"(disable)" => {
sub_c_arm = Some("disable");
writeln!(zcomp, "{}", line)
}
"(edit)" => {
sub_c_arm = Some("edit");
writeln!(zcomp, "{}", line)
}
"(enable)" => {
sub_c_arm = Some("enable");
writeln!(zcomp, "{}", line)
}
"(generate-standalone)" => {
sub_c_arm = Some("generate-standalone");
writeln!(zcomp, "{}", line)
}
"(has)" => {
sub_c_arm = Some("has");
writeln!(zcomp, "{}", line)
}
"(rm)" => {
sub_c_arm = Some("rm");
writeln!(zcomp, "{}", line)
}
";;" => {
sub_c_arm = None;
writeln!(zcomp, "{}", line)
}
_ => match sub_c_arm {
None => writeln!(zcomp, "{}", line),
Some("cat") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_all_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("diff") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_all_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("disable") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_user_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("edit") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_all_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("enable") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_disabled_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("generate-standalone") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_all_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("has") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_all_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
Some("rm") => {
if line.contains("_files") {
writeln!(zcomp, "{}", line.replace("_files", "_user_profiles"))
} else {
writeln!(zcomp, "{}", line)
}
}
_ => unreachable!(),
},
}
.unwrap();
}
}