Skip to content

Commit

Permalink
init: add buck-out to .gitignore generated by buck2 init --git
Browse files Browse the repository at this point in the history
Summary:
buck-out should be in .gitignore (if it's empty)

see #140

Reviewed By: stepancheg

Differential Revision: D44882336

fbshipit-source-id: e9d46fedf14f57fcc21513c51c075ba00c20d1b2
  • Loading branch information
themarwhal authored and facebook-github-bot committed Apr 11, 2023
1 parent 5fc648f commit 18bcd6f
Showing 1 changed file with 59 additions and 17 deletions.
76 changes: 59 additions & 17 deletions app/buck2_client/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ fn exec_impl(
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"))?;
fn initialize_buckconfig(repo_root: &Path, prelude: bool) -> anyhow::Result<()> {
let mut buckconfig = std::fs::File::create(repo_root.join(".buckconfig"))?;
writeln!(buckconfig, "[repositories]")?;
writeln!(buckconfig, "root = .")?;
writeln!(buckconfig, "prelude = prelude")?;
Expand All @@ -142,15 +142,15 @@ fn initialize_buckconfig(path: &Path, prelude: bool) -> anyhow::Result<()> {
)?;
} else {
// For the no-prelude mode, create an empty prelude/prelude.bzl as Buck2 expects one.
let prelude_dir = path.join("prelude");
let prelude_dir = repo_root.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"))?;
fn initialize_toolchains_buck(repo_root: &Path) -> anyhow::Result<()> {
let mut buck = std::fs::File::create(repo_root.join("BUCK"))?;

writeln!(
buck,
Expand All @@ -165,8 +165,8 @@ fn initialize_toolchains_buck(path: &Path) -> anyhow::Result<()> {
Ok(())
}

fn initialize_root_buck(path: &Path, prelude: bool) -> anyhow::Result<()> {
let mut buck = std::fs::File::create(path.join("BUCK"))?;
fn initialize_root_buck(repo_root: &Path, prelude: bool) -> anyhow::Result<()> {
let mut buck = std::fs::File::create(repo_root.join("BUCK"))?;

if prelude {
writeln!(
Expand All @@ -184,7 +184,7 @@ fn initialize_root_buck(path: &Path, prelude: bool) -> anyhow::Result<()> {
Ok(())
}

fn set_up_prelude(path: &Path, git: bool) -> anyhow::Result<()> {
fn set_up_prelude(repo_root: &Path, git: bool) -> anyhow::Result<()> {
if git {
if !Command::new("git")
.args([
Expand All @@ -193,7 +193,7 @@ fn set_up_prelude(path: &Path, git: bool) -> anyhow::Result<()> {
"https://github.com/facebook/buck2-prelude.git",
"prelude",
])
.current_dir(path)
.current_dir(repo_root)
.status()?
.success()
{
Expand All @@ -210,37 +210,47 @@ fn set_up_prelude(path: &Path, git: bool) -> anyhow::Result<()> {
Ok(())
}

fn set_up_project(path: &Path, git: bool, prelude: bool) -> anyhow::Result<()> {
fn set_up_gitignore(repo_root: &Path) -> anyhow::Result<()> {
let gitignore = repo_root.join(".gitignore");
// If .gitignore is empty or doesn't exist, add in buck-out
if !gitignore.exists() || fs_util::metadata(&gitignore)?.len() == 0 {
fs_util::write(gitignore, "/buck-out\n")?;
}
Ok(())
}

fn set_up_project(repo_root: &Path, git: bool, prelude: bool) -> anyhow::Result<()> {
if git {
if !Command::new("git")
.arg("init")
.current_dir(path)
.current_dir(repo_root)
.status()?
.success()
{
return Err(anyhow::anyhow!("Failure when running `git init`."));
};
set_up_gitignore(repo_root)?;
}

if prelude {
set_up_prelude(path, git)?;
set_up_prelude(repo_root, git)?;
}

// If the project already contains a .buckconfig, leave it alone
if path.join(".buckconfig").exists() {
if repo_root.join(".buckconfig").exists() {
return Ok(());
}

initialize_buckconfig(path, prelude)?;
initialize_buckconfig(repo_root, prelude)?;
if prelude {
let toolchains = path.join("toolchains");
let toolchains = repo_root.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)?;
if !repo_root.join("BUCK").exists() {
initialize_root_buck(repo_root, prelude)?;
}
Ok(())
}
Expand All @@ -251,6 +261,7 @@ mod tests {

use crate::commands::init::initialize_buckconfig;
use crate::commands::init::initialize_root_buck;
use crate::commands::init::set_up_gitignore;
use crate::commands::init::set_up_project;

#[test]
Expand All @@ -268,6 +279,37 @@ mod tests {
Ok(())
}

#[test]
fn test_default_gitignore() -> anyhow::Result<()> {
let tempdir = tempfile::tempdir()?;
let tempdir_path = tempdir.path();
fs_util::create_dir_all(tempdir_path)?;

// .gitignore does not exist yet
set_up_gitignore(tempdir_path)?;
let gitignore_path = tempdir_path.join(".gitignore");
assert!(gitignore_path.exists());
let actual = fs_util::read_to_string(&gitignore_path)?;
let expected = "/buck-out\n";
assert_eq!(actual, expected);

// If an empty .buckconfig exists (this is the case we would hit after running `git init`), add `buck-out`
fs_util::write(&gitignore_path, "")?;
set_up_gitignore(tempdir_path)?;
assert!(gitignore_path.exists());
let actual = fs_util::read_to_string(&gitignore_path)?;
assert_eq!(actual, expected);

// If a non-empty.buckconfig exists, don't touch it
fs_util::write(&gitignore_path, "foo\nbar\n")?;
set_up_gitignore(tempdir_path)?;
assert!(gitignore_path.exists());
let actual = fs_util::read_to_string(&gitignore_path)?;
let expected = "foo\nbar\n";
assert_eq!(actual, expected);
Ok(())
}

#[test]
fn test_buckconfig_generation_with_prelude() -> anyhow::Result<()> {
let tempdir = tempfile::tempdir()?;
Expand Down

0 comments on commit 18bcd6f

Please sign in to comment.