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 May 29, 2023
1 parent 2828363 commit 7ec6a87
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
73 changes: 73 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion butane/tests/common/blog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use butane::{db::Connection, ForeignKey, Many, ObjectState};
use chrono::{naive::NaiveDateTime, offset::Utc};

#[cfg(feature = "fake")]
use fake::{Dummy};
use fake::Dummy;

#[model]
#[derive(Debug, Eq, PartialEq)]
Expand Down
2 changes: 2 additions & 0 deletions butane_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ 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" }
quote = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
url = "2.3"
41 changes: 38 additions & 3 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ 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};
use url::Url;

pub type Result<T> = std::result::Result<T, anyhow::Error>;

Expand Down Expand Up @@ -259,10 +261,43 @@ pub fn get_migrations() -> Result<FsMigrations> {
Ok(migrations::from_root(root))
}

pub fn extract_package_directory(
package_id: cargo_metadata::PackageId,
) -> Result<std::path::PathBuf> {
let mut file_url = package_id
.repr
.split_once(&"path+".to_string())
.unwrap()
.1
.to_string();
file_url.truncate(file_url.len() - 1);
let url = Url::parse(file_url.as_str()).expect("Failed to parse URL");
let file_path = url.to_file_path().unwrap();
Ok(file_path)
}

pub fn base_dir() -> Result<PathBuf> {
std::env::current_dir()
.map(|d| d.join(".butane"))
.map_err(|e| e.into())
let current_directory = std::env::current_dir()?;
let local_butane_dir = current_directory.join(".butane/");

if !local_butane_dir.exists() {
let metadata = MetadataCommand::new().no_deps().exec()?;
let workspace_members = metadata.workspace_members;

// Find the first workspace member with a .butane
for member in workspace_members {
let package_dir = extract_package_directory(member)?;
let member_butane_dir = package_dir.join(".butane/");

if member_butane_dir.exists() {
eprintln!("Using workspace member {:?}", member_butane_dir);
return Ok(member_butane_dir);
}
}
}

// Fallback to the current directory
Ok(local_butane_dir)
}

pub fn handle_error(r: Result<()>) {
Expand Down

0 comments on commit 7ec6a87

Please sign in to comment.