Expand description
Platform-specific utilities and cross-platform compatibility helpers
This module provides abstractions over platform differences to ensure AGPM works consistently across Windows, macOS, and Linux. It handles differences in:
- Path separators and conventions
- Home directory resolution
- Command execution and shell interfaces
- File system behavior and limitations
- Environment variable handling
§Cross-Platform Design
AGPM is designed to provide identical functionality across all supported platforms while respecting platform conventions and limitations. This module encapsulates the platform-specific logic to achieve this goal.
§Examples
use agpm_cli::utils::platform::{get_home_dir, resolve_path, is_windows};
// Get platform-appropriate home directory
let home = get_home_dir()?;
println!("Home directory: {}", home.display());
// Resolve paths with tilde expansion and env vars
let config_path = resolve_path("~/.agpm/config.toml")?;
// Handle platform differences
if is_windows() {
println!("Running on Windows");
} else {
println!("Running on Unix-like system");
}§Platform Support Matrix
| Feature | Windows | macOS | Linux |
|---|---|---|---|
| Long paths (>260 chars) | ✅ | ✅ | ✅ |
| Case sensitivity | No | Configurable | Yes |
| Tilde expansion | ✅ | ✅ | ✅ |
| Environment variables | %VAR% | $VAR | $VAR |
| Shell commands | cmd.exe | sh | sh |
| Git command | git.exe | git | git |
§Security Considerations
- Path traversal prevention in
safe_join - Input validation in
validate_path_chars - Safe environment variable expansion
- Proper handling of special Windows filenames
Functions§
- command_
exists - Checks if a command is available in the system PATH.
- compute_
relative_ install_ path - Computes the relative install path by removing redundant directory prefixes.
- get_
cache_ dir - Returns the platform-specific cache directory for AGPM.
- get_
data_ dir - Returns the platform-specific data directory for AGPM.
- get_
git_ command - Returns the appropriate Git command name for the current platform.
- get_
home_ dir - Gets the home directory path for the current user.
- get_
shell_ command - Returns the appropriate shell command and flag for the current platform.
- is_
windows - Checks if the current platform is Windows.
- normalize_
path_ for_ storage - Normalizes a path for cross-platform storage by converting all separators to forward slashes.
- normalize_
path_ separator - Converts a path to use the correct separator for the current platform.
- path_
to_ os_ str - Returns a path as an
OsStrfor use in command arguments. - path_
to_ string - Safely converts a path to a string, handling non-UTF-8 paths gracefully.
- paths_
equal - Compares two paths for equality, respecting platform case sensitivity rules.
- resolve_
path - Resolves a path with tilde expansion and environment variable substitution.
- safe_
canonicalize - Canonicalizes a path with proper cross-platform handling.
- safe_
join - Safely joins a base path with a relative path, preventing directory traversal.
- validate_
path_ chars - Validates that a path contains only characters valid for the current platform.
- windows_
long_ path - No-op implementation of
windows_long_pathfor non-Windows platforms.