Skip to content

Commit

Permalink
Pass build directory to pipelines
Browse files Browse the repository at this point in the history
Instead of the release metadata. The build pipelines don't care about
the metadata, but need to know the directory in which to execute. Also
add a placeholder for using `sudo`.

Use a `PathBuf` in the pipeline implementations, rather than
`AsRef<Path>`, mostly because I couldn't figure out the proper
incantation to hold onto an `AsRef` through a trait definition. Not
worth the time, so just convert to a `PathBuf` and worry about it later
(if ever).

Hold onto the metadata in the parent method, however, as it may well
need to use it at some point. This requires a bit of futzing while
looking for the pipeline type in the metadata. Will implement the
pipeline-detection `todo!()`s next to address it.

While at it, remove the `download` and `unpack` methods from `Builder`,
as they're implemented by `api::Api`.
  • Loading branch information
theory committed Nov 19, 2024
1 parent afc52d0 commit 57d7b8e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
47 changes: 23 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod pipeline;

use crate::{error::BuildError, pgrx::Pgrx, pgxs::Pgxs, pipeline::Pipeline};
use pgxn_meta::{dist, release::Release};
use std::path::Path;

/// Defines the types of builders.
#[derive(Debug, PartialEq)]
Expand All @@ -28,54 +29,52 @@ enum Build {

/// Builder builds PGXN releases.
#[derive(Debug, PartialEq)]
pub struct Builder(Build);
pub struct Builder {
pipeline: Build,
meta: Release,
}

impl Builder {
/// Creates and returns a new builder using the appropriate pipeline.
pub fn new(meta: Release) -> Result<Self, BuildError> {
if let Some(deps) = meta.dependencies() {
if let Some(pipeline) = deps.pipeline() {
return match pipeline {
dist::Pipeline::Pgxs => Ok(Builder(Build::Pgxs(Pgxs::new(meta)))),
dist::Pipeline::Pgrx => Ok(Builder(Build::Pgrx(Pgrx::new(meta)))),
_ => Err(BuildError::UnknownPipeline(pipeline.to_string())),
};
pub fn new<P: AsRef<Path>>(dir: P, meta: Release) -> Result<Self, BuildError> {
let pipeline = if let Some(deps) = meta.dependencies() {
if let Some(pipe) = deps.pipeline() {
let dir = dir.as_ref().to_path_buf();
match pipe {
dist::Pipeline::Pgxs => Build::Pgxs(Pgxs::new(dir, true)),
dist::Pipeline::Pgrx => Build::Pgrx(Pgrx::new(dir, true)),
_ => return Err(BuildError::UnknownPipeline(pipe.to_string())),
}
} else {
todo!("Detect pipeline");
}
}
println!("HERE");
todo!("Detect pipeline");
}

/// Downloads a release.
pub fn download(&self) -> Result<(), BuildError> {
Ok(())
}
} else {
todo!("Detect pipeline");
};

/// Unpacks a release.
pub fn unpack(&self) -> Result<(), BuildError> {
Ok(())
Ok(Builder { pipeline, meta })
}

/// Configures a distribution to build on a particular platform and
/// Postgres version.
pub fn configure(&self) -> Result<(), BuildError> {
match &self.0 {
match &self.pipeline {
Build::Pgxs(pgxs) => pgxs.configure(),
Build::Pgrx(pgrx) => pgrx.configure(),
}
}

/// Compiles a distribution on a particular platform and Postgres version.
pub fn compile(&self) -> Result<(), BuildError> {
match &self.0 {
match &self.pipeline {
Build::Pgxs(pgxs) => pgxs.compile(),
Build::Pgrx(pgrx) => pgrx.compile(),
}
}

/// Tests a distribution a particular platform and Postgres version.
pub fn test(&self) -> Result<(), BuildError> {
match &self.0 {
match &self.pipeline {
Build::Pgxs(pgxs) => pgxs.test(),
Build::Pgrx(pgrx) => pgrx.test(),
}
Expand Down
10 changes: 6 additions & 4 deletions src/pgrx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
use crate::error::BuildError;
use crate::pipeline::Pipeline;
use pgxn_meta::release::Release;
use std::path::PathBuf;

/// Builder implementation for [pgrx] Pipelines.
///
/// [pgrx]: https://github.com/pgcentralfoundation/pgrx
#[derive(Debug, PartialEq)]
pub(crate) struct Pgrx {
meta: Release,
dir: PathBuf,
sudo: bool,
}

impl Pipeline for Pgrx {
fn new(meta: Release) -> Self {
Pgrx { meta }
fn new(dir: PathBuf, sudo: bool) -> Self {
Pgrx { dir, sudo }
}

fn configure(&self) -> Result<(), BuildError> {
Ok(())
}
Expand Down
9 changes: 5 additions & 4 deletions src/pgxs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
use crate::error::BuildError;
use crate::pipeline::Pipeline;
use pgxn_meta::release::Release;
use std::path::PathBuf;

/// Builder implementation for [PGXS] Pipelines.
///
/// [PGXS]: https://www.postgresql.org/docs/current/extend-pgxs.html
#[derive(Debug, PartialEq)]
pub(crate) struct Pgxs {
meta: Release,
dir: PathBuf,
sudo: bool,
}

impl Pipeline for Pgxs {
fn new(meta: Release) -> Self {
Pgxs { meta }
fn new(dir: PathBuf, sudo: bool) -> Self {
Pgxs { dir, sudo }
}

fn configure(&self) -> Result<(), BuildError> {
Expand Down
4 changes: 2 additions & 2 deletions src/pipeline/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Build Pipeline interface definition.
use crate::error::BuildError;
use pgxn_meta::release::Release;
use std::path::PathBuf;

/// Defines the interface for build pipelines to configure, compile, and test
/// PGXN distributions.
pub(crate) trait Pipeline {
/// Creates an instance of a Builder.
fn new(meta: Release) -> Self;
fn new(dir: PathBuf, sudo: bool) -> Self;

/// Configures a distribution to build on a particular platform and
/// Postgres version.
Expand Down
26 changes: 15 additions & 11 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ fn release_meta(pipeline: &str) -> Value {
fn pgxs() {
// Test pgxs pipeline.
let meta = release_meta("pgxs");
let dir = Path::new("dir");
let rel = Release::try_from(meta.clone()).unwrap();
let builder = Builder::new(rel).unwrap();
let builder = Builder::new(dir, rel).unwrap();
let rel = Release::try_from(meta).unwrap();
let exp = Builder(Build::Pgxs(Pgxs::new(rel)));
let exp = Builder {
pipeline: Build::Pgxs(Pgxs::new(dir.to_path_buf(), true)),
meta: rel,
};
assert_eq!(exp, builder, "pgxs");
assert!(builder.download().is_ok());
assert!(builder.unpack().is_ok());
assert!(builder.configure().is_ok());
assert!(builder.compile().is_ok());
assert!(builder.test().is_ok());
Expand All @@ -49,13 +51,15 @@ fn pgxs() {
fn pgrx() {
// Test pgrx pipeline.
let meta = release_meta("pgrx");
let dir = Path::new("dir");
let rel = Release::try_from(meta.clone()).unwrap();
let builder = Builder::new(rel).unwrap();
let builder = Builder::new(dir, rel).unwrap();
let rel = Release::try_from(meta).unwrap();
let exp = Builder(Build::Pgrx(Pgrx::new(rel)));
let exp = Builder {
pipeline: Build::Pgrx(Pgrx::new(dir.to_path_buf(), true)),
meta: rel,
};
assert_eq!(exp, builder, "pgrx");
assert!(builder.download().is_ok());
assert!(builder.unpack().is_ok());
assert!(builder.configure().is_ok());
assert!(builder.compile().is_ok());
assert!(builder.test().is_ok());
Expand All @@ -68,7 +72,7 @@ fn unsupported_pipeline() {
let rel = Release::try_from(meta).unwrap();
assert_eq!(
BuildError::UnknownPipeline("meson".to_string()).to_string(),
Builder::new(rel).unwrap_err().to_string(),
Builder::new("dir", rel).unwrap_err().to_string(),
);
}

Expand All @@ -79,7 +83,7 @@ fn detect_pipeline() {
let mut meta = release_meta("");
meta.as_object_mut().unwrap().remove("dependencies");
let rel = Release::try_from(meta).unwrap();
_ = Builder::new(rel);
_ = Builder::new("dir", rel);
}

#[test]
Expand All @@ -98,5 +102,5 @@ fn no_pipeline() {
deps.remove("pipeline");
deps.insert("postgres".to_string(), json!({"version": "14"}));
let rel = Release::try_from(meta).unwrap();
_ = Builder::new(rel);
_ = Builder::new("dir", rel);
}

0 comments on commit 57d7b8e

Please sign in to comment.