/
init.rs
339 lines (297 loc) · 10.6 KB
/
init.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
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/
use std::io::ErrorKind;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use anyhow::Context;
use buck2_client_ctx::client_ctx::ClientCommandContext;
use buck2_client_ctx::common::CommonConsoleOptions;
use buck2_client_ctx::exit_result::ExitResult;
use buck2_client_ctx::final_console::FinalConsole;
use buck2_client_ctx::path_arg::PathArg;
use buck2_core::fs::fs_util;
/// Buck2 Init
///
/// This command is intended to be part-tutorial part-convenience
/// for generating buck2 projects. Given a path and optional name
/// (in the case that the folder name is not desirable).
#[derive(Debug, clap::Parser)]
#[clap(name = "install", about = "Initialize a buck2 project")]
pub struct InitCommand {
#[clap(flatten)]
console_opts: CommonConsoleOptions,
/// The path to initialize the project in. The folder does not need to exist.
#[clap(default_value = ".")]
path: PathArg,
/// The name for the project. If not provided will default to the last segment
/// of the path.
#[clap(short, long)]
name: Option<String>,
/// Don't generate a prelude or a toolchain.
#[clap(long)]
no_prelude: bool,
/// Initialize the project even if the git repo at \[PATH\] has uncommitted changes.
#[clap(long)]
allow_dirty: bool,
// Use git to initialize the project and pull in buck2-prelude as a submodule
#[clap(long)]
git: bool,
}
impl InitCommand {
pub fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext) -> ExitResult {
let console = self.console_opts.final_console();
match exec_impl(self, ctx, &console) {
Ok(_) => ExitResult::Status(0),
Err(e) => {
// include the backtrace with the error output
// (same behaviour as returning the Error from main)
console.print_error(&format!("{:?}", e))?;
ExitResult::Status(1)
}
}
}
}
fn exec_impl(
cmd: InitCommand,
ctx: ClientCommandContext,
console: &FinalConsole,
) -> anyhow::Result<()> {
let path = cmd.path.resolve(&ctx.working_dir);
fs_util::create_dir_all(&path)?;
let absolute = fs_util::canonicalize(&path)?;
let git = cmd.git;
if absolute.is_file() {
return Err(anyhow::anyhow!(
"Target path {} cannot be an existing file",
absolute.display()
));
}
if git {
let status = match Command::new("git")
.args(["status", "--porcelain"])
.current_dir(&absolute)
.output()
{
Err(e) if e.kind().eq(&ErrorKind::NotFound) => {
console.print_error(
"Warning: no git found on path, can't check for dirty repo. Proceeding anyway.",
)?;
None
}
r => Some(r.context("Couldn't detect dirty status of folder.")?),
};
let changes = status.filter(|o| o.status.success()).map(|o| {
String::from_utf8_lossy(&o.stdout)
.trim()
.lines()
.any(|l| !l.starts_with("??"))
});
if let (Some(true), false) = (changes, cmd.allow_dirty) {
return Err(anyhow::anyhow!(
"Refusing to initialize in a dirty repo. Stash your changes or use `--allow-dirty` to override."
));
}
}
set_up_project(&absolute, git, !cmd.no_prelude)
}
fn initialize_buckconfig(path: &Path, prelude: bool) -> anyhow::Result<()> {
let mut buckconfig = std::fs::File::create(path.join(".buckconfig"))?;
writeln!(buckconfig, "[repositories]")?;
writeln!(buckconfig, "root = .")?;
writeln!(buckconfig, "prelude = prelude")?;
// Add additional configs that depend on prelude / no-prelude mode
if prelude {
writeln!(buckconfig, "toolchains = toolchains")?;
writeln!(buckconfig, "none = none")?;
writeln!(buckconfig)?;
writeln!(buckconfig, "[repository_aliases]")?;
writeln!(buckconfig, "config = prelude")?;
writeln!(buckconfig, "fbcode = none")?;
writeln!(buckconfig, "fbsource = none")?;
writeln!(buckconfig, "buck = none")?;
writeln!(buckconfig)?;
writeln!(buckconfig, "[parser]")?;
writeln!(
buckconfig,
"target_platform_detector_spec = target:root//...->prelude//platforms:default"
)?;
} else {
// For the no-prelude mode, create an empty prelude/prelude.bzl as Buck2 expects one.
let prelude_dir = path.join("prelude");
fs_util::create_dir(prelude_dir.as_path())?;
fs_util::create_file(prelude_dir.join("prelude.bzl"))?;
}
Ok(())
}
fn initialize_toolchains_buck(path: &Path) -> anyhow::Result<()> {
let mut buck = std::fs::File::create(path.join("BUCK"))?;
writeln!(
buck,
"load(\"@prelude//toolchains:genrule.bzl\", \"system_genrule_toolchain\")",
)?;
writeln!(buck)?;
writeln!(buck, "system_genrule_toolchain(")?;
writeln!(buck, " name = \"genrule\",")?;
writeln!(buck, " visibility = [\"PUBLIC\"],")?;
writeln!(buck, ")")?;
Ok(())
}
fn initialize_root_buck(path: &Path, prelude: bool) -> anyhow::Result<()> {
let mut buck = std::fs::File::create(path.join("BUCK"))?;
if prelude {
writeln!(
buck,
"# A list of available rules and their signatures can be found here: https://buck2.build/docs/api/rules/"
)?;
writeln!(buck)?;
writeln!(buck, "genrule(")?;
writeln!(buck, " name = \"hello_world\",")?;
writeln!(buck, " out = \"out.txt\",")?;
writeln!(buck, " cmd = \"echo BUILT BY BUCK2> $OUT\",")?;
writeln!(buck, ")")?;
}
// TODO: Add a doc pointers for rules
Ok(())
}
fn set_up_prelude(path: &Path, git: bool) -> anyhow::Result<()> {
if git {
if !Command::new("git")
.args([
"submodule",
"add",
"https://github.com/facebook/buck2-prelude.git",
"prelude",
])
.current_dir(path)
.status()?
.success()
{
return Err(anyhow::anyhow!(
"Unable to clone the prelude. Is the folder in use?"
));
}
} else {
println!(
"* Download https://github.com/facebookincubator/buck2-prelude.git into `prelude/` with a VCS of your choice."
);
println!("* If you wish to use git submodule, run the command again with --git",);
}
Ok(())
}
fn set_up_project(path: &Path, git: bool, prelude: bool) -> anyhow::Result<()> {
if git {
if !Command::new("git")
.arg("init")
.current_dir(path)
.status()?
.success()
{
return Err(anyhow::anyhow!("Failure when running `git init`."));
};
}
if prelude {
set_up_prelude(path, git)?;
}
// If the project already contains a .buckconfig, leave it alone
if path.join(".buckconfig").exists() {
return Ok(());
}
initialize_buckconfig(path, prelude)?;
if prelude {
let toolchains = path.join("toolchains");
if !toolchains.exists() {
fs_util::create_dir(&toolchains)?;
initialize_toolchains_buck(toolchains.as_path())?;
}
}
if !path.join("BUCK").exists() {
initialize_root_buck(path, prelude)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use buck2_core::fs::fs_util;
use crate::commands::init::initialize_buckconfig;
use crate::commands::init::initialize_root_buck;
use crate::commands::init::set_up_project;
#[test]
fn test_set_up_project_with_prelude_no_git() -> anyhow::Result<()> {
let tempdir = tempfile::tempdir()?;
let tempdir_path = tempdir.path();
fs_util::create_dir_all(tempdir_path)?;
// no git, with prelude
set_up_project(tempdir_path, false, true)?;
assert!(tempdir_path.join(".buckconfig").exists());
assert!(tempdir_path.join("toolchains").exists());
assert!(tempdir_path.join("toolchains/BUCK").exists());
assert!(tempdir_path.join("BUCK").exists());
Ok(())
}
#[test]
fn test_buckconfig_generation_with_prelude() -> anyhow::Result<()> {
let tempdir = tempfile::tempdir()?;
let tempdir_path = tempdir.path();
fs_util::create_dir_all(tempdir_path)?;
let buckconfig_path = tempdir_path.join(".buckconfig");
initialize_buckconfig(tempdir_path, true)?;
let actual_buckconfig = fs_util::read_to_string(buckconfig_path)?;
let expected_buckconfig = "[repositories]
root = .
prelude = prelude
toolchains = toolchains
none = none
[repository_aliases]
config = prelude
fbcode = none
fbsource = none
buck = none
[parser]
target_platform_detector_spec = target:root//...->prelude//platforms:default
";
assert_eq!(actual_buckconfig, expected_buckconfig);
Ok(())
}
#[test]
fn test_buckconfig_generation_without_prelude() -> anyhow::Result<()> {
let tempdir = tempfile::tempdir()?;
let tempdir_path = tempdir.path();
fs_util::create_dir_all(tempdir_path)?;
let buckconfig_path = tempdir_path.join(".buckconfig");
initialize_buckconfig(tempdir_path, false)?;
let actual_buckconfig = fs_util::read_to_string(buckconfig_path)?;
let expected_buckconfig = "[repositories]
root = .
prelude = prelude
";
assert_eq!(actual_buckconfig, expected_buckconfig);
// Test we have an empty prelude directory and prelude.bzl file
assert!(tempdir_path.join("prelude/prelude.bzl").exists());
Ok(())
}
#[test]
fn test_buckfile_generation_with_prelude() -> anyhow::Result<()> {
let tempdir = tempfile::tempdir()?;
let tempdir_path = tempdir.path();
fs_util::create_dir_all(tempdir_path)?;
let buck_path = tempdir_path.join("BUCK");
initialize_root_buck(tempdir_path, true)?;
let actual_buck = fs_util::read_to_string(buck_path)?;
let expected_buck = "# A list of available rules and their signatures can be found here: https://buck2.build/docs/api/rules/
genrule(
name = \"hello_world\",
out = \"out.txt\",
cmd = \"echo BUILT BY BUCK2> $OUT\",
)
";
assert_eq!(actual_buck, expected_buck);
Ok(())
}
}