Skip to content

Commit

Permalink
Find .butane in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Jul 25, 2023
1 parent 8f8a81d commit fea0791
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions butane_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sqlite-bundled = ["butane/sqlite-bundled"]
[dependencies]
anyhow = "1.0"
butane = { features = ["default", "pg", "sqlite"], workspace = true }
cargo_metadata = "0.15"
chrono = { workspace = true }
clap = { version = "4.1" }
lazy_static = "1.4"
Expand Down
60 changes: 60 additions & 0 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use butane::migrations::{
};
use butane::query::BoolExpr;
use butane::{db, db::Connection, db::ConnectionMethods, migrations};
use cargo_metadata::MetadataCommand;
use chrono::Utc;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -273,6 +274,65 @@ pub fn working_dir_path() -> PathBuf {
}
}

/// Extract the directory of a cargo workspace member identified by PackageId
pub fn extract_package_directory(
packages: &[cargo_metadata::Package],
package_id: cargo_metadata::PackageId,
) -> Result<std::path::PathBuf> {
let pkg = packages
.iter()
.find(|p| p.id == package_id)
.ok_or(anyhow::anyhow!("No package found"))?;
// Strip 'Cargo.toml' from the manifest_path
let parent = pkg.manifest_path.parent().unwrap();
Ok(parent.to_owned().into())
}

/// Find all cargo workspace members that have a `.butane` subdirectory
pub fn find_butane_workspace_member_paths() -> Result<Vec<PathBuf>> {
let metadata = MetadataCommand::new().no_deps().exec()?;
let workspace_members = metadata.workspace_members;

let mut possible_directories: Vec<PathBuf> = vec![];
// Find all workspace member with a .butane
for member in workspace_members {
let package_dir = extract_package_directory(&metadata.packages, member)?;
let member_butane_dir = package_dir.join(".butane/");

if member_butane_dir.exists() {
possible_directories.push(package_dir);
}
}
Ok(possible_directories)
}

/// Get the project path if only one workspace member contains a `.butane` directory
pub fn get_butane_project_path() -> Result<PathBuf> {
let possible_directories = find_butane_workspace_member_paths()?;

match possible_directories.len() {
0 => Err(anyhow::anyhow!("No .butane exists")),
1 => Ok(possible_directories[0].to_owned()),
_ => Err(anyhow::anyhow!("Multiple .butane exists")),
}
}

/// Find a .butane directory to act as the base for butane.
pub fn base_dir() -> PathBuf {
let current_directory = working_dir_path();
let local_butane_dir = current_directory.join(".butane/");

if !local_butane_dir.exists() {
if let Ok(member_dir) = get_butane_project_path() {
println!("Using workspace member {:?}", member_dir);
return member_dir;
}
}

// Fallback to the current directory
current_directory
}

pub fn handle_error(r: Result<()>) {
if let Err(e) = r {
eprintln!("Encountered unexpected error: {e}");
Expand Down
6 changes: 3 additions & 3 deletions butane_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::path::PathBuf;
use clap::{value_parser, Arg, ArgMatches};

use butane_cli::{
clean, clear_data, collapse_migrations, delete_table, embed, handle_error, list_migrations,
migrate, working_dir_path, Result,
base_dir, clean, clear_data, collapse_migrations, delete_table, embed, handle_error,
list_migrations, migrate, Result,
};

fn main() {
lazy_static::lazy_static! {
static ref WORKING_DIR_PATH: PathBuf = working_dir_path();
static ref WORKING_DIR_PATH: PathBuf = base_dir();
}
let app = clap::Command::new("butane")
.version(env!("CARGO_PKG_VERSION"))
Expand Down
5 changes: 5 additions & 0 deletions butane_cli/tests/cargo_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn find_existing_butane_dirs() {
let possible_directories = butane_cli::find_butane_workspace_member_paths().unwrap();
assert_eq!(possible_directories.len(), 3);
}

0 comments on commit fea0791

Please sign in to comment.