Skip to content

Commit fa7569e

Browse files
author
Nichol Yip
committed
Added feature to generate a summary of lints during "spk build"
Signed-off-by: Nichol Yip <[email protected]>
1 parent e8f7f43 commit fa7569e

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spk-cli/cmd-build/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ clap = { workspace = true }
2121
spk-cli-common = { path = '../common' }
2222
spk-cmd-make-binary = { path = '../cmd-make-binary' }
2323
spk-cmd-make-source = { path = '../cmd-make-source' }
24+
spk-schema = { path = '../../spk-schema' }
25+
tracing = { workspace = true }
2426

2527
[dev-dependencies]
2628
rstest = { workspace = true }
27-
spk-schema = { path = '../../spk-schema' }
2829
spk-storage = { path = '../../spk-storage' }
2930
tempfile = { workspace = true }
3031
tokio = { workspace = true }

crates/spk-cli/cmd-build/src/cmd_build.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// https://github.com/imageworks/spk
44

5+
use std::collections::BTreeMap;
6+
57
use clap::Args;
68
use miette::Result;
79
use spk_cli_common::{flags, CommandArgs, Run};
810
use spk_cmd_make_binary::cmd_make_binary::PackageSpecifier;
11+
use spk_schema::Lint::Key;
912

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

74+
let mut generated_lints = BTreeMap::new();
7175
let mut builds_for_summary = Vec::new();
7276
for packages in runs {
7377
let mut make_source = spk_cmd_make_source::cmd_make_source::MakeSource {
@@ -76,10 +80,13 @@ impl Run for Build {
7680
packages: packages.clone(),
7781
runtime: self.runtime.clone(),
7882
created_src: std::mem::take(&mut builds_for_summary),
83+
lints: BTreeMap::new(),
7984
};
8085
let idents = make_source.make_source().await?;
8186
builds_for_summary = std::mem::take(&mut make_source.created_src);
8287

88+
generated_lints.extend(std::mem::take(&mut make_source.lints));
89+
8390
let mut make_binary = spk_cmd_make_binary::cmd_make_binary::MakeBinary {
8491
verbose: self.verbose,
8592
runtime: self.runtime.clone(),
@@ -113,6 +120,17 @@ impl Run for Build {
113120
println!("{msg}");
114121
}
115122

123+
if !generated_lints.is_empty() {
124+
println!("Lints:");
125+
for (pkg, lints) in generated_lints.iter() {
126+
for lint in lints.iter() {
127+
match lint {
128+
Key(k) => tracing::warn!("{} {}", pkg, k.generate_message()),
129+
}
130+
}
131+
}
132+
}
133+
116134
Ok(0)
117135
}
118136
}

crates/spk-cli/cmd-make-source/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ migration-to-components = [
1919
miette = { workspace = true, features = ["fancy"] }
2020
async-trait = { workspace = true }
2121
clap = { workspace = true }
22+
serde_yaml = { workspace = true }
2223
spfs = { path = '../../spfs' }
2324
spk-build = { path = '../../spk-build' }
2425
spk-cli-common = { path = '../common' }

crates/spk-cli/cmd-make-source/src/cmd_make_source.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// https://github.com/imageworks/spk
44

5+
use std::collections::BTreeMap;
56
use std::path::PathBuf;
67
use std::sync::Arc;
78

@@ -12,7 +13,17 @@ use spk_cli_common::{flags, CommandArgs, Run};
1213
use spk_schema::foundation::format::FormatIdent;
1314
use spk_schema::foundation::spec_ops::Named;
1415
use spk_schema::ident::LocatedBuildIdent;
15-
use spk_schema::{Package, Recipe, SpecTemplate, Template, TemplateExt};
16+
use spk_schema::v0::Spec;
17+
use spk_schema::{
18+
AnyIdent,
19+
Lint,
20+
LintedItem,
21+
Package,
22+
Recipe,
23+
SpecTemplate,
24+
Template,
25+
TemplateExt,
26+
};
1627
use spk_storage as storage;
1728

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

3546
/// Populated with the created src to generate a summary from the caller.
47+
#[clap(skip)]
3648
pub created_src: Vec<String>,
49+
50+
/// Used to gather lints to output at the end of a build.
51+
#[clap(skip)]
52+
pub lints: BTreeMap<String, Vec<Lint>>,
3753
}
3854

3955
#[async_trait::async_trait]
@@ -79,6 +95,7 @@ impl MakeSource {
7995
template
8096
}
8197
};
98+
8299
let root = template
83100
.file_path()
84101
.parent()
@@ -89,6 +106,20 @@ impl MakeSource {
89106
let recipe = template.render(&options)?;
90107
let ident = recipe.ident();
91108

109+
let lints: std::result::Result<LintedItem<Spec<AnyIdent>>, serde_yaml::Error> =
110+
serde_yaml::from_str(&template.render_to_string(&options)?);
111+
112+
match lints {
113+
Ok(linted_item) => match linted_item.lints.is_empty() {
114+
true => (),
115+
false => {
116+
self.lints
117+
.insert(ident.format_ident(), linted_item.lints.clone());
118+
}
119+
},
120+
Err(e) => tracing::error!("Failed to retrieve lints: {e}"),
121+
}
122+
92123
tracing::info!("saving package recipe for {}", ident.format_ident());
93124
local.force_publish_recipe(&recipe).await?;
94125

0 commit comments

Comments
 (0)