Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

companions #11

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
pkgs.push(pkg);
}

let companions = pantry_db::companions_for_projects(
&pkgs
.iter()
.map(|project| project.project.clone())
.collect::<Vec<_>>(),
&conn,
)?;

pkgs.extend(companions);

let graph = hydrate(&pkgs, |project| {
pantry_db::deps_for_project(&project, &conn)
})
Expand All @@ -89,6 +99,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
Ok(())
} else {
let pkgx_lvl = std::env::var("PKGX_LVL")
.unwrap_or("0".to_string())
.parse()
.unwrap_or(0)
Comment on lines +103 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i love this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SO SAFE. SUCH SECURITY

+ 1;
if pkgx_lvl >= 10 {
return Err("PKGX_LVL exceeded: https://github.com/orgs/pkgxdev/discussions/11".into());
}

let cmd = if find_program {
utils::find_program(&args.remove(0), &env["PATH"], &config).await?
} else if args[0].contains('/') {
Expand All @@ -113,7 +132,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
utils::find_program(&args.remove(0), &paths, &config).await?
};
let env = env::mix(env);
let env = env::mix_runtime(&env, &installations, &conn)?;
let mut env = env::mix_runtime(&env, &installations, &conn)?;
env.insert("PKGX_LVL".to_string(), pkgx_lvl.to_string());
execve(cmd, args, env)
}
}
30 changes: 30 additions & 0 deletions src/pantry_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,33 @@ pub fn resolve(mut pkg: PackageReq, conn: &Connection) -> Result<PackageReq, Box
}
Ok(pkg)
}

pub fn companions_for_projects(
projects: &[String],
conn: &Connection,
) -> Result<Vec<PackageReq>, Box<dyn Error>> {
if projects.is_empty() {
return Ok(Vec::new());
}

// Generate placeholders for the IN clause (?, ?, ?, ...)
let placeholders = projects.iter().map(|_| "?").collect::<Vec<_>>().join(", ");
let query = format!(
"SELECT pkgspec FROM companions WHERE project IN ({})",
placeholders
);

let mut stmt = conn.prepare(&query)?;

let companions = stmt.query_map(
rusqlite::params_from_iter(projects.iter()), // Efficiently bind the projects
|row| {
let pkgspec: String = row.get(0)?;
let pkgrq = PackageReq::parse(&pkgspec).unwrap(); //TODO handle error!
Ok(pkgrq)
},
)?;

// Collect results into a Vec<PackageReq>, propagating errors
Ok(companions.collect::<Result<Vec<_>, _>>()?)
}
Loading