forked from eclipse-iceoryx/iceoryx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-iceoryx#98] Move methods to find commands into a separate mo…
…dule
- Loading branch information
Showing
3 changed files
with
95 additions
and
69 deletions.
There are no files selected for viewing
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,75 @@ | ||
use std::env; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
pub fn find() -> Vec<String> { | ||
let mut commands = find_command_binaries_in_development_dirs(); | ||
if commands.is_empty() { | ||
commands = find_command_binaries_in_system_path(); | ||
} | ||
commands | ||
} | ||
|
||
fn find_command_binaries_in_development_dirs() -> Vec<String> { | ||
let mut commands = Vec::new(); | ||
let current_exe = match env::current_exe() { | ||
Ok(exe) => exe, | ||
Err(_) => return commands, | ||
}; | ||
let target_dir_name = if cfg!(debug_assertions) { | ||
"debug" | ||
} else { | ||
"release" | ||
}; | ||
let target_dir = current_exe | ||
.parent() | ||
.unwrap() | ||
.parent() | ||
.unwrap() | ||
.join(target_dir_name); | ||
|
||
if let Ok(entries) = fs::read_dir(&target_dir) { | ||
for entry in entries.filter_map(Result::ok) { | ||
let path = entry.path(); | ||
if is_valid_command_binary(&path) { | ||
if let Some(command_name) = path.file_name().and_then(|n| n.to_str()) { | ||
let stripped = command_name.strip_prefix("iox2-").unwrap_or(command_name); | ||
commands.push(stripped.to_string()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
commands | ||
} | ||
|
||
fn find_command_binaries_in_system_path() -> Vec<String> { | ||
let mut commands = Vec::new(); | ||
if let Ok(path_var) = env::var("PATH") { | ||
for path in env::split_paths(&path_var) { | ||
if let Ok(entries) = fs::read_dir(path) { | ||
for entry in entries.filter_map(Result::ok) { | ||
let path = entry.path(); | ||
if is_valid_command_binary(&path) { | ||
if let Some(command_name) = path.file_name().and_then(|n| n.to_str()) { | ||
commands.push(command_name.to_string()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
commands | ||
} | ||
|
||
fn is_valid_command_binary(path: &PathBuf) -> bool { | ||
path.is_file() | ||
&& path | ||
.file_stem() | ||
.unwrap() | ||
.to_str() | ||
.unwrap() | ||
.starts_with("iox2-") | ||
&& path.extension().is_none() // Exclude files with extensions (e.g. '.d') | ||
} |
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 |
---|---|---|
@@ -1,80 +1,29 @@ | ||
use std::env; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
#[cfg(not(debug_assertions))] | ||
use human_panic::setup_panic; | ||
|
||
fn find_command_binaries_in_development_dirs() -> Vec<String> { | ||
let mut commands = Vec::new(); | ||
let current_exe = match env::current_exe() { | ||
Ok(exe) => exe, | ||
Err(_) => return commands, | ||
}; | ||
let target_dir_name = if cfg!(debug_assertions) { | ||
"debug" | ||
} else { | ||
"release" | ||
}; | ||
let target_dir = current_exe | ||
.parent() | ||
.unwrap() | ||
.parent() | ||
.unwrap() | ||
.join(target_dir_name); | ||
#[cfg(debug_assertions)] | ||
extern crate better_panic; | ||
|
||
if let Ok(entries) = fs::read_dir(&target_dir) { | ||
for entry in entries.filter_map(Result::ok) { | ||
let path = entry.path(); | ||
if is_valid_command_binary(&path) { | ||
if let Some(command_name) = path.file_name().and_then(|n| n.to_str()) { | ||
let stripped = command_name.strip_prefix("iox2-").unwrap_or(command_name); | ||
commands.push(stripped.to_string()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
commands | ||
} | ||
|
||
fn find_command_binaries_in_system_path() -> Vec<String> { | ||
let mut commands = Vec::new(); | ||
if let Ok(path_var) = env::var("PATH") { | ||
for path in env::split_paths(&path_var) { | ||
if let Ok(entries) = fs::read_dir(path) { | ||
for entry in entries.filter_map(Result::ok) { | ||
let path = entry.path(); | ||
if is_valid_command_binary(&path) { | ||
if let Some(command_name) = path.file_name().and_then(|n| n.to_str()) { | ||
commands.push(command_name.to_string()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
commands | ||
} | ||
|
||
fn is_valid_command_binary(path: &PathBuf) -> bool { | ||
path.is_file() | ||
&& path | ||
.file_stem() | ||
.unwrap() | ||
.to_str() | ||
.unwrap() | ||
.starts_with("iox2-") | ||
&& path.extension().is_none() // Exclude files with extensions (e.g. '.d') | ||
} | ||
mod commands; | ||
|
||
fn main() { | ||
let mut commands = find_command_binaries_in_development_dirs(); | ||
if commands.is_empty() { | ||
commands = find_command_binaries_in_system_path(); | ||
#[cfg(not(debug_assertions))] | ||
{ | ||
setup_panic!(); | ||
} | ||
#[cfg(debug_assertions)] | ||
{ | ||
better_panic::Settings::debug() | ||
.most_recent_first(false) | ||
.lineno_suffix(true) | ||
.verbosity(better_panic::Verbosity::Full) | ||
.install(); | ||
} | ||
|
||
let commands = commands::find(); | ||
|
||
println!("Available commands:"); | ||
for command in commands { | ||
println!("- {}", command); | ||
} | ||
} | ||
|