Skip to content

Commit

Permalink
Added feature to generate a summary of lints during "spk build"
Browse files Browse the repository at this point in the history
Signed-off-by: Nichol Yip <nyip@imageworks.com>
  • Loading branch information
Nichol Yip committed Jan 20, 2024
1 parent e8f7f43 commit fa7569e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/spk-cli/cmd-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ clap = { workspace = true }
spk-cli-common = { path = '../common' }
spk-cmd-make-binary = { path = '../cmd-make-binary' }
spk-cmd-make-source = { path = '../cmd-make-source' }
spk-schema = { path = '../../spk-schema' }
tracing = { workspace = true }

[dev-dependencies]
rstest = { workspace = true }
spk-schema = { path = '../../spk-schema' }
spk-storage = { path = '../../spk-storage' }
tempfile = { workspace = true }
tokio = { workspace = true }
18 changes: 18 additions & 0 deletions crates/spk-cli/cmd-build/src/cmd_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
// https://github.com/imageworks/spk

use std::collections::BTreeMap;

use clap::Args;
use miette::Result;
use spk_cli_common::{flags, CommandArgs, Run};
use spk_cmd_make_binary::cmd_make_binary::PackageSpecifier;
use spk_schema::Lint::Key;

#[cfg(test)]
#[path = "./cmd_build_test.rs"]
Expand Down Expand Up @@ -68,6 +71,7 @@ impl Run for Build {
runs.push(Vec::new());
}

let mut generated_lints = BTreeMap::new();
let mut builds_for_summary = Vec::new();
for packages in runs {
let mut make_source = spk_cmd_make_source::cmd_make_source::MakeSource {
Expand All @@ -76,10 +80,13 @@ impl Run for Build {
packages: packages.clone(),
runtime: self.runtime.clone(),
created_src: std::mem::take(&mut builds_for_summary),
lints: BTreeMap::new(),
};
let idents = make_source.make_source().await?;
builds_for_summary = std::mem::take(&mut make_source.created_src);

generated_lints.extend(std::mem::take(&mut make_source.lints));

let mut make_binary = spk_cmd_make_binary::cmd_make_binary::MakeBinary {
verbose: self.verbose,
runtime: self.runtime.clone(),
Expand Down Expand Up @@ -113,6 +120,17 @@ impl Run for Build {
println!("{msg}");
}

if !generated_lints.is_empty() {
println!("Lints:");
for (pkg, lints) in generated_lints.iter() {
for lint in lints.iter() {
match lint {
Key(k) => tracing::warn!("{} {}", pkg, k.generate_message()),
}
}
}
}

Ok(0)
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/spk-cli/cmd-make-source/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ migration-to-components = [
miette = { workspace = true, features = ["fancy"] }
async-trait = { workspace = true }
clap = { workspace = true }
serde_yaml = { workspace = true }
spfs = { path = '../../spfs' }
spk-build = { path = '../../spk-build' }
spk-cli-common = { path = '../common' }
Expand Down
33 changes: 32 additions & 1 deletion crates/spk-cli/cmd-make-source/src/cmd_make_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// https://github.com/imageworks/spk

use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::Arc;

Expand All @@ -12,7 +13,17 @@ use spk_cli_common::{flags, CommandArgs, Run};
use spk_schema::foundation::format::FormatIdent;
use spk_schema::foundation::spec_ops::Named;
use spk_schema::ident::LocatedBuildIdent;
use spk_schema::{Package, Recipe, SpecTemplate, Template, TemplateExt};
use spk_schema::v0::Spec;
use spk_schema::{
AnyIdent,
Lint,
LintedItem,
Package,
Recipe,
SpecTemplate,
Template,
TemplateExt,
};
use spk_storage as storage;

/// Build a source package from a spec file.
Expand All @@ -33,7 +44,12 @@ pub struct MakeSource {
pub packages: Vec<String>,

/// Populated with the created src to generate a summary from the caller.
#[clap(skip)]
pub created_src: Vec<String>,

/// Used to gather lints to output at the end of a build.
#[clap(skip)]
pub lints: BTreeMap<String, Vec<Lint>>,
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -79,6 +95,7 @@ impl MakeSource {
template
}
};

let root = template
.file_path()
.parent()
Expand All @@ -89,6 +106,20 @@ impl MakeSource {
let recipe = template.render(&options)?;
let ident = recipe.ident();

let lints: std::result::Result<LintedItem<Spec<AnyIdent>>, serde_yaml::Error> =
serde_yaml::from_str(&template.render_to_string(&options)?);

match lints {
Ok(linted_item) => match linted_item.lints.is_empty() {
true => (),
false => {
self.lints
.insert(ident.format_ident(), linted_item.lints.clone());
}
},
Err(e) => tracing::error!("Failed to retrieve lints: {e}"),
}

tracing::info!("saving package recipe for {}", ident.format_ident());
local.force_publish_recipe(&recipe).await?;

Expand Down

0 comments on commit fa7569e

Please sign in to comment.