714aed9610
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
104 lines
3.2 KiB
Rust
104 lines
3.2 KiB
Rust
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
pub use crate::cookbook::generate_recipe;
|
|
pub use crate::rbpkgbuild::*;
|
|
|
|
use crate::converter;
|
|
use crate::error::CubError;
|
|
use crate::storage::CubStore;
|
|
|
|
pub fn recipe_from_aur_pkgbuild(raw_pkgbuild: &str) -> Result<(RbPkgBuild, String), CubError> {
|
|
let conversion = converter::convert_pkgbuild(raw_pkgbuild)?;
|
|
let recipe_toml = generate_recipe(&conversion.rbpkg)?;
|
|
|
|
Ok((conversion.rbpkg, recipe_toml))
|
|
}
|
|
|
|
pub fn recipe_toml_from_aur(raw_pkgbuild: &str) -> Result<String, CubError> {
|
|
let (_, recipe_toml) = recipe_from_aur_pkgbuild(raw_pkgbuild)?;
|
|
Ok(recipe_toml)
|
|
}
|
|
|
|
pub fn save_recipe_to_store(rbpkg: &RbPkgBuild, store: &CubStore) -> Result<PathBuf, CubError> {
|
|
let recipe_toml = generate_recipe(rbpkg)?;
|
|
let recipe_dir = store.recipes_dir().join(&rbpkg.package.name);
|
|
fs::create_dir_all(&recipe_dir)?;
|
|
|
|
let recipe_path = recipe_dir.join("recipe.toml");
|
|
fs::write(&recipe_path, recipe_toml)?;
|
|
|
|
Ok(recipe_path)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::fs;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
const SAMPLE_AUR_PKGBUILD: &str = r#"
|
|
pkgname=demo_pkg
|
|
pkgver=1.2.3
|
|
pkgrel=4
|
|
pkgdesc="Demo application"
|
|
url="https://example.com/demo"
|
|
license=('MIT')
|
|
depends=('glibc' 'openssl>=1.1')
|
|
makedepends=('cargo' 'pkg-config')
|
|
checkdepends=('python')
|
|
source=('https://example.com/demo-1.2.3.tar.xz')
|
|
sha256sums=('abc123deadbeef')
|
|
|
|
build() {
|
|
cargo build --release
|
|
}
|
|
"#;
|
|
|
|
#[test]
|
|
fn recipe_from_aur_pkgbuild_returns_rbpkg_and_recipe() {
|
|
let (rbpkg, recipe_toml) =
|
|
recipe_from_aur_pkgbuild(SAMPLE_AUR_PKGBUILD).expect("convert PKGBUILD to recipe");
|
|
|
|
assert_eq!(rbpkg.package.name, "demo-pkg");
|
|
|
|
let value: toml::Value = toml::from_str(&recipe_toml).expect("parse generated recipe");
|
|
assert_eq!(value["build"]["template"].as_str(), Some("cargo"));
|
|
assert_eq!(
|
|
value["source"]["tar"].as_str(),
|
|
Some("https://example.com/demo-1.2.3.tar.xz")
|
|
);
|
|
assert_eq!(value["source"]["blake3"].as_str(), Some("abc123deadbeef"));
|
|
}
|
|
|
|
#[test]
|
|
fn recipe_toml_from_aur_returns_recipe_only() {
|
|
let recipe_toml = recipe_toml_from_aur(SAMPLE_AUR_PKGBUILD)
|
|
.expect("convert PKGBUILD directly to recipe TOML");
|
|
|
|
let value: toml::Value = toml::from_str(&recipe_toml).expect("parse generated recipe");
|
|
assert_eq!(value["build"]["template"].as_str(), Some("cargo"));
|
|
assert_eq!(value["package"]["dependencies"][0].as_str(), Some("relibc"));
|
|
}
|
|
|
|
#[test]
|
|
fn save_recipe_to_store_writes_recipe_toml() {
|
|
let (rbpkg, expected_recipe_toml) =
|
|
recipe_from_aur_pkgbuild(SAMPLE_AUR_PKGBUILD).expect("convert PKGBUILD to recipe");
|
|
let temp = tempdir().expect("create tempdir");
|
|
let store = CubStore {
|
|
root_dir: temp.path().join(".cub"),
|
|
};
|
|
|
|
let saved_path = save_recipe_to_store(&rbpkg, &store).expect("save recipe to store");
|
|
|
|
assert_eq!(
|
|
saved_path,
|
|
store.recipes_dir().join("demo-pkg").join("recipe.toml")
|
|
);
|
|
let saved_recipe = fs::read_to_string(&saved_path).expect("read saved recipe");
|
|
assert_eq!(saved_recipe, expected_recipe_toml);
|
|
}
|
|
}
|