-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
189 lines (166 loc) · 6.46 KB
/
build.rs
File metadata and controls
189 lines (166 loc) · 6.46 KB
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
#[cfg(feature = "assets")]
mod asset_build {
use clap::CommandFactory;
use clap::ValueEnum;
use clap_complete::{Shell, generate_to};
use clap_complete_nushell::Nushell;
use clap_mangen::Man;
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::{env, fs::create_dir_all, path::Path};
include!("src/cli.rs");
pub fn run() {
println!("cargo:rerun-if-env-changed=ASSET_DIR");
const NAME: &str = "flake-edit";
const COMPLETIONS_DIR: &str = "assets/completions";
const FISH_COMPLETIONS: &str = "fish/completions.fish";
let manifest_dir =
env::var_os("CARGO_MANIFEST_DIR").expect("Could not find env CARGO_MANIFEST_DIR");
if let Some(dir) = env::var_os("ASSET_DIR") {
let out = &Path::new(&dir);
create_dir_all(out).unwrap();
let cmd = &mut CliArgs::command();
gen_man(NAME, out.to_path_buf());
Shell::value_variants().iter().for_each(|shell| {
generate_to(*shell, cmd, NAME.to_string(), out).unwrap();
// claps completions generation mechanisms are very immature,
// include self adjusted ones
if *shell == Shell::Fish {
let mut source = PathBuf::from(manifest_dir.clone());
source.push(COMPLETIONS_DIR);
source.push(FISH_COMPLETIONS);
let source = fs::read_to_string(source).expect("Could not read source file");
let path = out.join(format!("{NAME}.fish"));
let mut file = OpenOptions::new()
.append(true)
.open(path)
.expect("Could not create path.");
let _ = file.write_all(source.as_bytes());
}
});
generate_to(Nushell, cmd, NAME.to_string(), out).unwrap();
} else {
eprintln!("ASSET_DIR environment variable not set");
eprintln!("Not able to generate completion files");
eprintln!("Not able to generate manpage files");
}
}
fn gen_man(name: &str, dir: PathBuf) {
use roff::Roff;
use std::fs::write;
let path = dir.join(format!("{name}.1"));
let mut buf: Vec<u8> = Vec::new();
let man = Man::new(CliArgs::command());
man.render_title(&mut buf)
.expect("Not able to render title.");
man.render_name_section(&mut buf)
.expect("Not able to render name section.");
man.render_synopsis_section(&mut buf)
.expect("Not able to render synopsis section.");
let mut roff = Roff::new();
roff.control("SH", ["DESCRIPTION"]);
roff.text(vec!["Edit your flake inputs with ease.".into()]);
roff.to_writer(&mut buf)
.expect("Not able to write description.");
// man.render_description_section(&mut buf)
// .expect("Not able to render description section.");
man.render_options_section(&mut buf)
.expect("Not able to render options section.");
man.render_subcommands_section(&mut buf)
.expect("Not able to render subcommands section.");
// Examples
roff.control("SH", ["EXAMPLES"]);
// Add a new flake input
roff.text(vec!["Add a new flake input:".to_string().into()]);
roff.control("RS", []);
roff.text(vec![
format!("{} add nixpkgs github:NixOS/nixpkgs", name).into(),
]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// Add with auto-inference
roff.text(vec![
"Add an input with automatic ID inference:"
.to_string()
.into(),
]);
roff.control("RS", []);
roff.text(vec![
format!("{} add github:nix-community/home-manager", name).into(),
]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// Remove an input
roff.text(vec!["Remove a flake input:".to_string().into()]);
roff.control("RS", []);
roff.text(vec![format!("{} remove nixpkgs", name).into()]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// List inputs
roff.text(vec!["List all current inputs:".to_string().into()]);
roff.control("RS", []);
roff.text(vec![format!("{} list", name).into()]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// Update inputs
roff.text(vec![
"Update all inputs to latest versions:".to_string().into(),
]);
roff.control("RS", []);
roff.text(vec![format!("{} update", name).into()]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// Pin an input
roff.text(vec![
"Pin an input to its current revision:".to_string().into(),
]);
roff.control("RS", []);
roff.text(vec![format!("{} pin nixpkgs", name).into()]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// Show diff without applying changes
roff.text(vec![
"Preview changes without applying them:".to_string().into(),
]);
roff.control("RS", []);
roff.text(vec![
format!(
"{} --diff add home-manager github:nix-community/home-manager",
name
)
.into(),
]);
roff.control("RE", []);
roff.text(vec!["".into()]);
// Skip lockfile update
roff.text(vec![
"Add input without updating lockfile:".to_string().into(),
]);
roff.control("RS", []);
roff.text(vec![
format!(
"{} --no-lock add nixos-hardware github:NixOS/nixos-hardware",
name
)
.into(),
]);
roff.control("RE", []);
roff.to_writer(&mut buf).expect("Not able to write roff.");
// Footer
man.render_version_section(&mut buf)
.expect("Not able to render subcommands section.");
man.render_authors_section(&mut buf)
.expect("Not able to render subcommands section.");
write(path, buf).expect("Not able to write manpage");
}
}
#[cfg(feature = "assets")]
fn main() {
asset_build::run();
}
#[cfg(not(feature = "assets"))]
fn main() {
// Keep build.rs compiling when the assets feature (and build deps) are disabled.
}