use crate::{
command::{get::get_property, install::install_extension},
CommandExecute,
};
use eyre::eyre;
use pgx_utils::{get_target_dir, pg_config::PgConfig};
use std::path::PathBuf;
#[derive(clap::Args, Debug)]
#[clap(author)]
pub(crate) struct Package {
#[clap(env = "PROFILE", long, short)]
debug: bool,
#[clap(long, short = 'c', parse(from_os_str))]
pg_config: Option<PathBuf>,
#[clap(flatten)]
features: clap_cargo::Features,
#[clap(from_global, parse(from_occurrences))]
verbose: usize,
}
impl CommandExecute for Package {
#[tracing::instrument(level = "error", skip(self))]
fn execute(self) -> eyre::Result<()> {
let metadata = crate::metadata::metadata(&self.features)?;
crate::metadata::validate(&metadata)?;
let manifest = crate::manifest::manifest(&metadata)?;
let pg_config = match self.pg_config {
None => PgConfig::from_path(),
Some(config) => PgConfig::new(PathBuf::from(config)),
};
let pg_version = format!("pg{}", pg_config.major_version()?);
let features = crate::manifest::features_for_version(self.features, &manifest, &pg_version);
package_extension(&pg_config, self.debug, &features)
}
}
#[tracing::instrument(level = "error", skip_all, fields(
pg_version = %pg_config.version()?,
release = !is_debug,
))]
pub(crate) fn package_extension(
pg_config: &PgConfig,
is_debug: bool,
features: &clap_cargo::Features,
) -> eyre::Result<()> {
let base_path = build_base_path(pg_config, is_debug)?;
if base_path.exists() {
std::fs::remove_dir_all(&base_path)?;
}
if !base_path.exists() {
std::fs::create_dir_all(&base_path)?;
}
install_extension(pg_config, !is_debug, false, Some(base_path), features)
}
fn build_base_path(pg_config: &PgConfig, is_debug: bool) -> eyre::Result<PathBuf> {
let mut target_dir = get_target_dir()?;
let pgver = pg_config.major_version()?;
let extname = get_property("extname")?.ok_or(eyre!("could not determine extension name"))?;
target_dir.push(if is_debug { "debug" } else { "release" });
target_dir.push(format!("{}-pg{}", extname, pgver));
Ok(target_dir)
}