Skip to content

Commit

Permalink
feat(cdk): prebuild artifacts for publish and deploy (#3252)
Browse files Browse the repository at this point in the history
Abstracts the build command used for `build`, `test`, `deploy` and `publish` and then
makes sure commands relying on binaries from build process output (Artifacts) are present
by spawning the `build` process before proceeding.
  • Loading branch information
EstebanBorai committed May 15, 2023
1 parent 0728e43 commit 7957551
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 30 deletions.
20 changes: 10 additions & 10 deletions crates/cdk/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::fmt::Debug;
use anyhow::Result;
use clap::Parser;

use cargo_builder::{package::PackageInfo, cargo::Cargo};
use cargo_builder::package::PackageInfo;

use crate::cmd::PackageCmd;
use crate::utils::build::{BuildOpts, build_connector};

/// Build the Connector in the current working directory
#[derive(Debug, Parser)]
Expand All @@ -21,15 +22,14 @@ pub struct BuildCmd {
impl BuildCmd {
pub(crate) fn process(self) -> Result<()> {
let opt = self.package.as_opt();
let p = PackageInfo::from_options(&opt)?;
let cargo = Cargo::build()
.profile(opt.release)
.lib(false)
.package(p.package_name())
.target(p.arch_target())
.extra_arguments(self.extra_arguments)
.build()?;
let package_info = PackageInfo::from_options(&opt)?;

cargo.run()
build_connector(
&package_info,
BuildOpts {
release: opt.release,
extra_arguments: self.extra_arguments,
},
)
}
}
21 changes: 14 additions & 7 deletions crates/cdk/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use fluvio_connector_package::metadata::ConnectorMetadata;
use tracing::{debug, trace};

use crate::cmd::PackageCmd;
use crate::utils::build::{BuildOpts, build_connector};

const CONNECTOR_METADATA_FILE_NAME: &str = "Connector.toml";

Expand Down Expand Up @@ -158,9 +159,14 @@ fn deploy_local(
secrets: Option<PathBuf>,
ipkg_file: Option<PathBuf>,
) -> Result<()> {
let opt = package_cmd.as_opt();
let package_info = PackageInfo::from_options(&opt)?;

build_connector(&package_info, BuildOpts::with_release(opt.release.as_str()))?;

let (executable, connector_metadata) = match ipkg_file {
Some(ipkg_file) => from_ipkg_file(ipkg_file).context("Failed to deploy from ipkg file")?,
None => from_cargo_package(package_cmd)
None => from_cargo_package(&package_info)
.context("Failed to deploy from within cargo package directory")?,
};

Expand All @@ -181,14 +187,15 @@ fn deploy_local(
local_index::store(result)
}

pub(crate) fn from_cargo_package(package_cmd: PackageCmd) -> Result<(PathBuf, ConnectorMetadata)> {
pub(crate) fn from_cargo_package(
package_info: &PackageInfo,
) -> Result<(PathBuf, ConnectorMetadata)> {
debug!("reading connector metadata from cargo package");

let opt = package_cmd.as_opt();
let p = PackageInfo::from_options(&opt)?;
let connector_metadata =
ConnectorMetadata::from_toml_file(p.package_relative_path(CONNECTOR_METADATA_FILE_NAME))?;
let executable_path = p.target_bin_path()?;
let connector_metadata = ConnectorMetadata::from_toml_file(
package_info.package_relative_path(CONNECTOR_METADATA_FILE_NAME),
)?;
let executable_path = package_info.target_bin_path()?;
Ok((executable_path, connector_metadata))
}

Expand Down
2 changes: 2 additions & 0 deletions crates/cdk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod deploy;
mod publish;
mod set_public;

pub(crate) mod utils;

fn main() -> anyhow::Result<()> {
use clap::Parser;

Expand Down
2 changes: 2 additions & 0 deletions crates/cdk/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use hubutil::packagename_validate;
use tracing::{debug, info};

use crate::cmd::PackageCmd;
use crate::utils::build::{BuildOpts, build_connector};

pub const CONNECTOR_TOML: &str = "Connector.toml";

Expand Down Expand Up @@ -70,6 +71,7 @@ impl PublishCmd {
remove_dir_all(&hubdir)?;
}

build_connector(&package_info, BuildOpts::with_release(opt.release.as_str()))?;
init_package_template(&package_info, &self.readme)?;
check_package_meta_visiblity(&package_info)?;

Expand Down
24 changes: 11 additions & 13 deletions crates/cdk/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::{fmt::Debug, path::PathBuf};
use anyhow::{Result, Context};
use clap::Parser;

use cargo_builder::{package::PackageInfo, cargo::Cargo};
use cargo_builder::package::PackageInfo;
use fluvio_connector_deployer::{Deployment, DeploymentType};

use crate::{cmd::PackageCmd, deploy::from_cargo_package};
use crate::cmd::PackageCmd;
use crate::deploy::from_cargo_package;
use crate::utils::build::{build_connector, BuildOpts};

/// Build and run the Connector in the current working directory
#[derive(Debug, Parser)]
Expand All @@ -30,19 +32,15 @@ pub struct TestCmd {
impl TestCmd {
pub(crate) fn process(self) -> Result<()> {
let opt = self.package.as_opt();
let p = PackageInfo::from_options(&opt)?;
let package_info = PackageInfo::from_options(&opt)?;
let build_options = BuildOpts {
release: opt.release,
extra_arguments: self.extra_arguments,
};

let cargo = Cargo::build()
.profile(opt.release)
.target(opt.target)
.lib(false)
.package(p.package_name())
.extra_arguments(self.extra_arguments)
.build()?;
build_connector(&package_info, build_options)?;

cargo.run()?;

let (executable, connector_metadata) = from_cargo_package(self.package)
let (executable, connector_metadata) = from_cargo_package(&package_info)
.context("Failed to deploy from within cargo package directory")?;

let mut builder = Deployment::builder();
Expand Down
33 changes: 33 additions & 0 deletions crates/cdk/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub mod build {
use anyhow::Result;

use cargo_builder::package::PackageInfo;
use cargo_builder::cargo::Cargo;

pub struct BuildOpts {
pub(crate) release: String,
pub(crate) extra_arguments: Vec<String>,
}

impl BuildOpts {
pub fn with_release(release: &str) -> Self {
Self {
release: release.to_string(),
extra_arguments: Vec::default(),
}
}
}

/// Builds a Connector given it's package info and Cargo Build options
pub fn build_connector(package_info: &PackageInfo, opts: BuildOpts) -> Result<()> {
let cargo = Cargo::build()
.profile(opts.release)
.lib(false)
.package(package_info.package_name())
.target(package_info.arch_target())
.extra_arguments(opts.extra_arguments)
.build()?;

cargo.run()
}
}

0 comments on commit 7957551

Please sign in to comment.