Skip to content

Commit b3d96b9

Browse files
authored
Merge pull request #1287 from spkenv/ns-opt-env-names
Change how namespaced options are populated into the env
2 parents e35f540 + 97a805c commit b3d96b9

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

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

Lines changed: 13 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()
@@ -272,6 +278,12 @@ impl EnvName for OptNameBuf {
272278
}
273279
}
274280

281+
impl EnvName for OptName {
282+
fn env_name(&self) -> String {
283+
self.0.replace('-', "_")
284+
}
285+
}
286+
275287
/// Ensure that the provided string is a valid option name.
276288
///
277289
/// This is for checking option names with or without any leading

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)