Skip to content

Commit

Permalink
chore(infra): move get_absolute_path function to infra_utils (#2074)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Nov 21, 2024
1 parent fde23db commit b204349
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
// TODO(Arni): Move the function get_absolute_path to this file.
use std::env;
use std::path::PathBuf;

// TODO(Tsabary/ Arni): consolidate with other get_absolute_path functions.
/// Resolves a relative path from the project root directory and returns its absolute path.
///
/// # Arguments
/// * `relative_path` - A string slice representing the relative path from the project root.
///
/// # Returns
/// * An absolute `PathBuf` representing the resolved path starting from the project root.
pub fn resolve_project_relative_path(relative_path: &str) -> PathBuf {
path_of_project_root().join(relative_path)
}

fn path_of_project_root() -> PathBuf {
env::var("CARGO_MANIFEST_DIR")
// Attempt to get the `CARGO_MANIFEST_DIR` environment variable and convert it to `PathBuf`.
// Ascend two directories ("../..") to get to the project root.
.map(|dir| PathBuf::from(dir).join("../.."))
// If `CARGO_MANIFEST_DIR` isn't set, fall back to the current working directory
.unwrap_or_else(|_| env::current_dir().expect("Failed to get current directory"))
}
1 change: 1 addition & 0 deletions crates/starknet_sequencer_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ anyhow.workspace = true
clap.workspace = true
const_format.workspace = true
futures.workspace = true
infra_utils.workspace = true
papyrus_config.workspace = true
papyrus_proc_macros = { workspace = true, optional = true }
rstest.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_sequencer_node/src/config/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::LazyLock;
use std::vec::Vec;

use clap::Command;
use infra_utils::path::resolve_project_relative_path;
use papyrus_config::dumping::{
append_sub_config_name,
generate_struct_pointer,
Expand All @@ -28,7 +29,6 @@ use starknet_sierra_compile::config::SierraToCasmCompilationConfig;
use validator::Validate;

use crate::config::component_config::ComponentConfig;
use crate::utils::get_absolute_path;
use crate::version::VERSION_FULL;

// The path of the default configuration file, provided as part of the crate.
Expand Down Expand Up @@ -157,7 +157,7 @@ impl SequencerNodeConfig {
) -> Result<Self, ConfigError> {
let config_file_name = match config_file_name {
Some(file_name) => Path::new(file_name),
None => &get_absolute_path(DEFAULT_CONFIG_PATH),
None => &resolve_project_relative_path(DEFAULT_CONFIG_PATH),
};

let default_config_file = File::open(config_file_name)?;
Expand Down
15 changes: 0 additions & 15 deletions crates/starknet_sequencer_node/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::env;
use std::path::PathBuf;

use crate::clients::{create_node_clients, SequencerNodeClients};
use crate::communication::create_node_channels;
use crate::components::create_node_components;
Expand All @@ -17,15 +14,3 @@ pub fn create_node_modules(

(clients, servers)
}

// TODO(Tsabary): consolidate with other get_absolute_path functions.
/// Returns the absolute path from the project root.
pub fn get_absolute_path(relative_path: &str) -> PathBuf {
let base_dir = env::var("CARGO_MANIFEST_DIR")
// Attempt to get the `CARGO_MANIFEST_DIR` environment variable and convert it to `PathBuf`.
// Ascend two directories ("../..") to get to the project root.
.map(|dir| PathBuf::from(dir).join("../.."))
// If `CARGO_MANIFEST_DIR` isn't set, fall back to the current working directory
.unwrap_or_else(|_| env::current_dir().expect("Failed to get current directory"));
base_dir.join(relative_path)
}

0 comments on commit b204349

Please sign in to comment.