Skip to content

Commit 408f6b2

Browse files
committed
Change how namespaced options are populated into the env
Instead of `$SPK_OPT_pkgname.optname`, which is an invalid environment variable name, use `$SPK_PKG_pkgname_OPT_optname`. This format then resembles how other environment variables are named, such as `$SPK_PKG_pkgname_VERSION`. The old format is also still populated for backward compatibility and to allow projects to update to the new format. Signed-off-by: J Robert Ray <jrray@jrray.org>
1 parent 8b75563 commit 408f6b2

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

crates/spk-schema/crates/foundation/src/name/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use error::{Error, Result};
1212
use miette::Diagnostic;
1313
use thiserror::Error;
1414

15-
use crate::spec_ops::EnvName;
15+
use crate::spec_ops::{EnvName, Named};
1616

1717
#[cfg(test)]
1818
#[path = "./name_test.rs"]
@@ -72,6 +72,12 @@ parsedbuf::parsed!(OptName, Error, "option");
7272
parsedbuf::parsed!(PkgName, Error, "package");
7373
parsedbuf::parsed!(RepositoryName, Error, "repository");
7474

75+
impl Named for PkgName {
76+
fn name(&self) -> &PkgName {
77+
self
78+
}
79+
}
80+
7581
impl Borrow<OptName> for PkgName {
7682
fn borrow(&self) -> &OptName {
7783
self.as_ref()
@@ -267,6 +273,13 @@ impl OptNameBuf {
267273
}
268274

269275
impl EnvName for OptNameBuf {
276+
#[inline]
277+
fn env_name(&self) -> String {
278+
self.0.replace('-', "_")
279+
}
280+
}
281+
282+
impl EnvName for OptName {
270283
fn env_name(&self) -> String {
271284
self.0.replace('-', "_")
272285
}

crates/spk-schema/crates/foundation/src/option_map/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,22 @@ impl OptionMap {
191191
pub fn to_environment(&self) -> HashMap<String, String> {
192192
let mut out = HashMap::default();
193193
for (name, value) in self.iter() {
194-
let var_name = format!("SPK_OPT_{}", name.env_name());
195-
out.insert(var_name, value.into());
194+
if let Some(namespace) = name.namespace() {
195+
let var_name = format!(
196+
"SPK_PKG_{}_OPT_{}",
197+
namespace.env_name(),
198+
name.without_namespace().env_name()
199+
);
200+
out.insert(var_name, value.into());
201+
202+
// Also insert in the original format for compatibility,
203+
// to be removed in a future release.
204+
let var_name = format!("SPK_OPT_{}", name.env_name());
205+
out.insert(var_name, value.into());
206+
} else {
207+
let var_name = format!("SPK_OPT_{}", name.env_name());
208+
out.insert(var_name, value.into());
209+
}
196210
}
197211
out
198212
}
@@ -228,7 +242,10 @@ impl OptionMap {
228242
pub fn clean_environment(env: &mut HashMap<String, String>) {
229243
let to_remove = env
230244
.keys()
231-
.filter(|name| name.starts_with("SPK_OPT_"))
245+
.filter(|name| {
246+
name.starts_with("SPK_OPT_")
247+
|| (name.starts_with("SPK_PKG_") && name.contains("_OPT_"))
248+
})
232249
.map(|k| k.to_owned())
233250
.collect_vec();
234251
for name in to_remove.into_iter() {

crates/spk-schema/crates/foundation/src/spec_ops/env_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub trait EnvName {
1212

1313
impl<T> EnvName for T
1414
where
15-
T: Named,
15+
T: Named + ?Sized,
1616
{
1717
fn env_name(&self) -> String {
1818
self.name().replace('-', "_")

docs/use/create/build.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ build:
2020
2121
All options that are declared in your package should be used in the build script, otherwise they are not relevant build options and your package may need rebuilding unnecessarily.
2222
23-
When writing your build script, the value of each option is made available in an environment variable with the name `SPK_OPT_{name}`. Package options are also resolved into the build environment and can be accessed more concretely with the variables `SPK_PKG_{name}`, `SPK_PKG_{name}_VERSION`, `SPK_PKG_{name}_BUILD`, `SPK_PKG_{name}_VERSION_MAJOR`, `SPK_PKG_{name}_VERSION_MINOR`, `SPK_PKG_{name}_VERSION_PATCH`
23+
When writing your build script, the value of each option is made available in an
24+
environment variable with the name `SPK_OPT_{name}`. Options specific to a
25+
package are in the form `SPK_PKG_{pkg_name}_OPT_{opt_name}`.
26+
Package properties are also resolved into the build environment and can be
27+
accessed more concretely with the variables `SPK_PKG_{pkg_name}`,
28+
`SPK_PKG_{pkg_name}_VERSION`, `SPK_PKG_{pkg_name}_BUILD`,
29+
`SPK_PKG_{pkg_name}_VERSION_MAJOR`, `SPK_PKG_{pkg_name}_VERSION_MINOR`,
30+
`SPK_PKG_{pkg_name}_VERSION_PATCH`
2431

2532
> [!TIP]
2633
> Best practice for defining boolean options is to follow the cmake convention of having two choices: `on` and `off`

0 commit comments

Comments
 (0)