Skip to content

Commit

Permalink
chore(vdev): Rewrite check-component-features script into vdev (vec…
Browse files Browse the repository at this point in the history
…tordotdev#16080)

* chore(vdev): Rewrite check-component-features into vdev

* Merge all parsing of `Cargo.toml` into util
  • Loading branch information
bruceg authored Jan 23, 2023
1 parent b72caa9 commit 40e7313
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 70 deletions.
29 changes: 0 additions & 29 deletions scripts/check-component-features

This file was deleted.

75 changes: 75 additions & 0 deletions vdev/src/commands/check/component_features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::env;

use anyhow::Result;

use crate::{app, util::CargoToml};

const CARGO: &str = "cargo";
const BASE_ARGS: [&str; 5] = [
"check",
"--tests",
"--bin",
"vector",
"--no-default-features",
];
const PARALLEL_ARGS: [&str; 7] = [
"--group",
"--verbose",
"--retries",
"2",
"scripts/check-one-feature",
"{}",
":::",
];

/// Check that all component features are set up properly
#[derive(clap::Args, Debug)]
#[command()]
pub struct Cli {}

impl Cli {
#[allow(clippy::dbg_macro)]
pub fn exec(self) -> Result<()> {
app::set_repo_dir()?;

let features = extract_features()?;

// Prime the pump to build most of the artifacts
app::exec(CARGO, BASE_ARGS, true)?;
app::exec(
CARGO,
BASE_ARGS.into_iter().chain(["--features", "default"]),
true,
)?;
app::exec(
CARGO,
BASE_ARGS
.into_iter()
.chain(["--features", "all-integration-tests"]),
true,
)?;

// The feature builds already run in parallel below, so don't overload the parallelism
env::set_var("CARGO_BUILD_JOBS", "1");

app::exec(
"parallel",
PARALLEL_ARGS
.into_iter()
.chain(features.iter().map(String::as_str)),
true,
)
}
}

fn extract_features() -> Result<Vec<String>> {
Ok(CargoToml::load()?
.features
.into_keys()
.filter(|feature| {
!feature.contains("-utils")
&& feature != "default"
&& feature != "all-integration-tests"
})
.collect())
}
7 changes: 1 addition & 6 deletions vdev/src/commands/check/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
crate::cli_subcommands! {
"Check parts of the Vector code base"
component_docs,
component_features,
mod component_features,
mod deny,
docs,
events,
Expand All @@ -19,11 +19,6 @@ crate::script_wrapper! {
=> "check-component-docs.sh"
}

crate::script_wrapper! {
component_features = "Check that all component features are set up properly"
=> "check-component-features"
}

crate::script_wrapper! {
docs = "Check that all /docs files are valid"
=> "check-docs.sh"
Expand Down
25 changes: 4 additions & 21 deletions vdev/src/commands/meta/starship.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use anyhow::Result;
use clap::Args;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::PathBuf;

use crate::app;

const VERSION_START: &str = "version = ";
use crate::util::CargoToml;

/// Custom Starship prompt plugin
#[derive(Args, Debug)]
Expand All @@ -17,21 +12,9 @@ impl Cli {
pub fn exec(self) -> Result<()> {
let mut contexts = vec![];

let path: PathBuf = [app::path(), "Cargo.toml"].iter().collect();
if let Ok(file) = File::open(path) {
let reader = BufReader::new(file);

for line in reader.lines() {
let line = line?;
if line.starts_with(VERSION_START) {
contexts.push(format!(
"version: {}",
&line[VERSION_START.len() + 1..line.len() - 1]
));
break;
}
}
};
if let Ok(cargo_toml) = CargoToml::load() {
contexts.push(format!("version: {}", cargo_toml.package.version));
}

println!("vector{{ {} }}", contexts.join(", "));

Expand Down
36 changes: 22 additions & 14 deletions vdev/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use std::fs;
use std::process::{Command, Output};
use std::{collections::BTreeMap, fs};

use anyhow::{Context as _, Result};
use serde::Deserialize;
use serde_json::Value;

/// Read the version string from `Cargo.toml`
pub fn read_version() -> Result<String> {
#[derive(Deserialize)]
struct Package {
version: String,
}
#[derive(Deserialize)]
struct CargoToml {
package: Package,
#[derive(Deserialize)]
pub struct CargoTomlPackage {
pub version: String,
}

/// The bits of the top-level `Cargo.toml` configuration that `vdev` uses to drive its features.
#[derive(Deserialize)]
pub struct CargoToml {
pub package: CargoTomlPackage,
pub features: BTreeMap<String, Value>,
}

impl CargoToml {
pub fn load() -> Result<CargoToml> {
let text = fs::read_to_string("Cargo.toml").context("Could not read `Cargo.toml`")?;
toml::from_str::<CargoToml>(&text).context("Invalid contents in `Cargo.toml`")
}
}

let text = fs::read_to_string("Cargo.toml").context("Could not read `Cargo.toml`")?;
toml::from_str::<CargoToml>(&text)
.context("Invalid contents in `Cargo.toml`")
.map(|cargo| cargo.package.version)
/// Read the version string from `Cargo.toml`
pub fn read_version() -> Result<String> {
CargoToml::load().map(|cargo| cargo.package.version)
}

pub fn git_head() -> Result<Output> {
Expand Down

0 comments on commit 40e7313

Please sign in to comment.