-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a FromStr implementation to debian::MultiarchName
Implment functionality to load the environment variables associate with packages from the project.toml file
- Loading branch information
1 parent
8949e5d
commit 2db46d1
Showing
10 changed files
with
234 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[_] | ||
schema-version = "0.2" | ||
|
||
[com.heroku.buildpacks.deb-packages] | ||
install = [ | ||
{ name = "git", | ||
env = { "GIT_EXEC_PATH" = "{install_dir}/usr/lib/git-core", | ||
"GIT_TEMPLATE_DIR" = "{install_dir}/usr/share/git-core/templates" } | ||
}, | ||
{ name = "babeld" }, | ||
{ name = "ghostscript", | ||
skip_dependencies = true, | ||
force = true, | ||
env = { "GS_LIB" = "{install_dir}/var/lib/ghostscript" } | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::path::Path; | ||
use toml_edit::{DocumentMut}; | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct Environment { | ||
variables: HashMap<String, String>, | ||
} | ||
|
||
impl Environment { | ||
/// Load environment variables from the project.toml file based on package names. | ||
pub(crate) fn load_from_toml(file_path: &Path, install_dir: &str) -> Self { | ||
let mut env = Environment::default(); | ||
if let Ok(contents) = fs::read_to_string(file_path) { | ||
let doc = contents.parse::<DocumentMut>().unwrap(); | ||
if let Some(array_of_tables) = doc | ||
.as_table() | ||
.get("com") | ||
.and_then(|item| item.as_table()?.get("heroku")) | ||
.and_then(|item| item.as_table()?.get("buildpacks")) | ||
.and_then(|item| item.as_table()?.get("deb-packages")) | ||
.and_then(|item| item.as_table()?.get("install")) | ||
.and_then(|item| item.as_array()) | ||
{ | ||
for table in array_of_tables.iter() { | ||
if let Some(env_table) = table | ||
.as_inline_table() | ||
.and_then(|t| t.get("env")) | ||
.and_then(|e| e.as_inline_table()) | ||
{ | ||
for (key, value) in env_table.iter() { | ||
if let Some(value_str) = value.as_str() { | ||
let value_with_install_dir = value_str.replace("{install_dir}", install_dir); | ||
env.variables.insert(key.to_string(), value_with_install_dir); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
env | ||
} | ||
|
||
/// Apply environment variables to the current process. | ||
// pub(crate) fn apply(&self) { | ||
// for (key, value) in &self.variables { | ||
// std::env::set_var(key, value); | ||
// } | ||
// } | ||
|
||
/// Get environment variables as a `HashMap`. | ||
pub(crate) fn get_variables(&self) -> &HashMap<String, String> { | ||
&self.variables | ||
} | ||
|
||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_load_from_toml() { | ||
let toml_content = r#" | ||
schema-version = "0.2" | ||
[com.heroku.buildpacks.deb-packages] | ||
install = [ | ||
{ name = "git", env = { "GIT_EXEC_PATH" = "{install_dir}/usr/lib/git-core", "GIT_TEMPLATE_DIR" = "{install_dir}/usr/lib/git-core/templates" } }, | ||
{ name = "babeld" }, | ||
{ name = "ghostscript", skip_dependencies = true, force = true, env = { "GS_LIB" = "{install_dir}/var/lib/ghostscript", "GS_FONTPATH" = "{install_dir}/var/lib/ghostscript/fonts" } }, | ||
] | ||
"#; | ||
|
||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("project.toml"); | ||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(toml_content.as_bytes()).unwrap(); | ||
|
||
let env = Environment::load_from_toml(&file_path, "/build"); | ||
let variables = env.get_variables(); | ||
|
||
// Print the values of the variables | ||
// println!("GIT_EXEC_PATH: {:?}", variables.get("GIT_EXEC_PATH")); | ||
// println!("GIT_TEMPLATE_DIR: {:?}", variables.get("GIT_TEMPLATE_DIR")); | ||
// println!("GS_LIB: {:?}", variables.get("GS_LIB")); | ||
// println!("GS_FONTPATH: {:?}", variables.get("GS_FONTPATH")); | ||
|
||
assert_eq!(variables.get("GIT_EXEC_PATH"), Some(&"/build/usr/lib/git-core".to_string())); | ||
assert_eq!(variables.get("GIT_TEMPLATE_DIR"), Some(&"/build/usr/lib/git-core/templates".to_string())); | ||
assert_eq!(variables.get("GS_LIB"), Some(&"/build/var/lib/ghostscript".to_string())); | ||
assert_eq!(variables.get("GS_FONTPATH"), Some(&"/build/var/lib/ghostscript/fonts".to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub(crate) use requested_package::*; | |
|
||
mod buildpack_config; | ||
mod requested_package; | ||
pub(crate) mod environment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[_] | ||
schema-version = "0.2" | ||
|
||
[com.heroku.buildpacks.deb-packages] | ||
install = [ | ||
{ name = "git", | ||
env = { "GIT_EXEC_PATH" = "{install_dir}/usr/lib/git-core", | ||
"GIT_TEMPLATE_DIR" = "{install_dir}/usr/share/git-core/templates" } | ||
}, | ||
{ name = "babeld" }, | ||
{ name = "ghostscript", | ||
skip_dependencies = true, | ||
force = true, | ||
env = { "GS_LIB" = "{install_dir}/var/lib/ghostscript" } | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters