Skip to content

Commit c4fbd08

Browse files
authored
Merge pull request #1492 from cardoso/feat/more_build_info
feat(plugins): provide additional build info to plugins
2 parents bb84aa4 + 22a83fa commit c4fbd08

File tree

9 files changed

+68
-94
lines changed

9 files changed

+68
-94
lines changed

Cargo.lock

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

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ e2e-testing = { path = "crates/e2e-testing" }
8686

8787
[build-dependencies]
8888
cargo-target-dep = { git = "https://github.com/fermyon/cargo-target-dep", rev = "b7b1989fe0984c0f7c4966398304c6538e52fe49" }
89-
vergen = { version = "7", default-features = false, features = [
89+
vergen = { version = "8", default-features = false, features = [
9090
"build",
9191
"git",
92+
"gitcl",
93+
"cargo",
9294
] }
9395

9496
[features]

build.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ const RUST_OUTBOUND_REDIS_INTEGRATION_TEST: &str = "tests/outbound-redis/http-ru
1414
const TIMER_TRIGGER_INTEGRATION_TEST: &str = "examples/spin-timer/app-example";
1515

1616
fn main() {
17-
let mut config = vergen::Config::default();
18-
*config.git_mut().sha_kind_mut() = vergen::ShaKind::Short;
19-
*config.git_mut().commit_timestamp_kind_mut() = vergen::TimestampKind::DateOnly;
20-
vergen::vergen(config).expect("failed to extract build information");
17+
vergen::EmitBuilder::builder()
18+
.build_date()
19+
.build_timestamp()
20+
.cargo_target_triple()
21+
.cargo_debug()
22+
.git_branch()
23+
.git_commit_date()
24+
.git_commit_timestamp()
25+
.git_sha(true)
26+
.fail_on_error()
27+
.emit()
28+
.expect("failed to extract build information");
2129

2230
let build_spin_tests = env::var("BUILD_SPIN_EXAMPLES")
2331
.map(|v| v == "1")

src/bin/spin.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::Error;
22
use clap::{CommandFactory, Parser, Subcommand};
33
use is_terminal::IsTerminal;
44
use lazy_static::lazy_static;
5+
use spin_cli::build_info::*;
56
use spin_cli::commands::{
67
build::BuildCommand,
78
cloud::CloudCommands,
@@ -47,7 +48,7 @@ fn version() -> &'static str {
4748
#[derive(Parser)]
4849
#[clap(
4950
name = "spin",
50-
version = version(),
51+
version = version()
5152
)]
5253
enum SpinApp {
5354
#[clap(subcommand, alias = "template")]
@@ -108,10 +109,5 @@ impl SpinApp {
108109

109110
/// Returns build information, similar to: 0.1.0 (2be4034 2022-03-31).
110111
fn build_info() -> String {
111-
format!(
112-
"{} ({} {})",
113-
env!("VERGEN_BUILD_SEMVER"),
114-
env!("VERGEN_GIT_SHA_SHORT"),
115-
env!("VERGEN_GIT_COMMIT_DATE")
116-
)
112+
format!("{SPIN_VERSION} ({SPIN_COMMIT_SHA} {SPIN_COMMIT_DATE})")
117113
}

src/build_info.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Build information for the Spin CLI.
2+
3+
/// The version of the Spin CLI.
4+
pub const SPIN_VERSION: &str = env!("CARGO_PKG_VERSION");
5+
/// The major version of the Spin CLI.
6+
pub const SPIN_VERSION_MAJOR: &str = env!("CARGO_PKG_VERSION_MAJOR");
7+
/// The minor version of the Spin CLI.
8+
pub const SPIN_VERSION_MINOR: &str = env!("CARGO_PKG_VERSION_MINOR");
9+
/// The patch version of the Spin CLI.
10+
pub const SPIN_VERSION_PATCH: &str = env!("CARGO_PKG_VERSION_PATCH");
11+
/// The pre-release version of the Spin CLI.
12+
pub const SPIN_VERSION_PRE: &str = env!("CARGO_PKG_VERSION_PRE");
13+
/// The build date of the Spin CLI.
14+
pub const SPIN_BUILD_DATE: &str = env!("VERGEN_BUILD_DATE");
15+
/// The commit hash of the Spin CLI.
16+
pub const SPIN_COMMIT_SHA: &str = env!("VERGEN_GIT_SHA");
17+
/// The commit date of the Spin CLI.
18+
pub const SPIN_COMMIT_DATE: &str = env!("VERGEN_GIT_COMMIT_DATE");
19+
/// The branch of the Spin CLI.
20+
pub const SPIN_BRANCH: &str = env!("VERGEN_GIT_BRANCH");
21+
/// The target triple of the Spin CLI.
22+
pub const SPIN_TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE");
23+
/// The profile of the Spin CLI.
24+
pub const SPIN_DEBUG: &str = env!("VERGEN_CARGO_DEBUG");

src/commands/external.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::build_info::*;
12
use crate::opts::PLUGIN_OVERRIDE_COMPATIBILITY_CHECK_FLAG;
23
use anyhow::{anyhow, Result};
34
use spin_plugins::{error::Error, manifest::warn_unsupported_version, PluginStore};
@@ -40,9 +41,8 @@ pub async fn execute_external_subcommand(
4041
let plugin_store = PluginStore::try_default()?;
4142
match plugin_store.read_plugin_manifest(&plugin_name) {
4243
Ok(manifest) => {
43-
let spin_version = env!("VERGEN_BUILD_SEMVER");
4444
if let Err(e) =
45-
warn_unsupported_version(&manifest, spin_version, override_compatibility_check)
45+
warn_unsupported_version(&manifest, SPIN_VERSION, override_compatibility_check)
4646
{
4747
eprintln!("{e}");
4848
process::exit(1);
@@ -102,19 +102,26 @@ fn similar_commands(app: clap::App, target: &str) -> Vec<String> {
102102

103103
fn get_env_vars_map() -> Result<HashMap<String, String>> {
104104
let map: HashMap<String, String> = vec![
105+
("SPIN_VERSION", SPIN_VERSION),
106+
("SPIN_VERSION_MAJOR", SPIN_VERSION_MAJOR),
107+
("SPIN_VERSION_MINOR", SPIN_VERSION_MINOR),
108+
("SPIN_VERSION_PATCH", SPIN_VERSION_PATCH),
109+
("SPIN_VERSION_PRE", SPIN_VERSION_PRE),
110+
("SPIN_COMMIT_SHA", SPIN_COMMIT_SHA),
111+
("SPIN_COMMIT_DATE", SPIN_COMMIT_DATE),
112+
("SPIN_BRANCH", SPIN_BRANCH),
113+
("SPIN_BUILD_DATE", SPIN_BUILD_DATE),
114+
("SPIN_TARGET_TRIPLE", SPIN_TARGET_TRIPLE),
115+
("SPIN_DEBUG", SPIN_DEBUG),
105116
(
106-
"SPIN_VERSION".to_string(),
107-
env!("VERGEN_BUILD_SEMVER").to_owned(),
108-
),
109-
(
110-
"SPIN_BIN_PATH".to_string(),
117+
"SPIN_BIN_PATH",
111118
env::current_exe()?
112119
.to_str()
113-
.ok_or_else(|| anyhow!("Could not convert binary path to string"))?
114-
.to_string(),
120+
.ok_or_else(|| anyhow!("Could not convert binary path to string"))?,
115121
),
116122
]
117123
.into_iter()
124+
.map(|(k, v)| (k.to_string(), v.to_string()))
118125
.collect();
119126
Ok(map)
120127
}

src/commands/plugins.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
1414
use tracing::log;
1515
use url::Url;
1616

17+
use crate::build_info::*;
1718
use crate::opts::*;
1819

1920
/// Install/uninstall Spin plugins.
@@ -373,7 +374,7 @@ pub(crate) enum PluginCompatibility {
373374
impl PluginCompatibility {
374375
pub(crate) fn for_current(manifest: &PluginManifest) -> Self {
375376
if manifest.has_compatible_package() {
376-
let spin_version = env!("VERGEN_BUILD_SEMVER");
377+
let spin_version = SPIN_VERSION;
377378
if manifest.is_compatible_spin_version(spin_version) {
378379
Self::Compatible
379380
} else {
@@ -450,10 +451,9 @@ async fn try_install(
450451
override_compatibility_check: bool,
451452
downgrade: bool,
452453
) -> Result<bool> {
453-
let spin_version = env!("VERGEN_BUILD_SEMVER");
454454
let install_action = manager.check_manifest(
455455
manifest,
456-
spin_version,
456+
SPIN_VERSION,
457457
override_compatibility_check,
458458
downgrade,
459459
)?;

src/commands/templates.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use spin_templates::{
1111
SkippedReason, Template, TemplateManager, TemplateSource,
1212
};
1313

14+
use crate::build_info::*;
15+
1416
const INSTALL_FROM_DIR_OPT: &str = "FROM_DIR";
1517
const INSTALL_FROM_GIT_OPT: &str = "FROM_GIT";
1618
const UPGRADE_ONLY: &str = "GIT_URL";
@@ -117,9 +119,7 @@ impl Install {
117119
let template_manager = TemplateManager::try_default()
118120
.context("Failed to construct template directory path")?;
119121
let source = match (&self.git, &self.dir) {
120-
(Some(git), None) => {
121-
TemplateSource::try_from_git(git, &self.branch, env!("VERGEN_BUILD_SEMVER"))?
122-
}
122+
(Some(git), None) => TemplateSource::try_from_git(git, &self.branch, SPIN_VERSION)?,
123123
(None, Some(dir)) => {
124124
let abs_dir = dir.absolutize().map(|d| d.to_path_buf());
125125
TemplateSource::File(abs_dir.unwrap_or_else(|_| dir.clone()))
@@ -364,8 +364,7 @@ struct RepoSelection {
364364

365365
impl RepoSelection {
366366
async fn from_repo(repo: &str) -> Option<Self> {
367-
let template_source =
368-
TemplateSource::try_from_git(repo, &None, env!("VERGEN_BUILD_SEMVER")).ok()?;
367+
let template_source = TemplateSource::try_from_git(repo, &None, SPIN_VERSION).ok()?;
369368
let resolved_tag = template_source.resolved_tag().await;
370369
Some(Self {
371370
repo: repo.to_owned(),

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod build_info;
12
pub mod commands;
23
pub mod manifest;
34
pub(crate) mod opts;

0 commit comments

Comments
 (0)