use std::fmt::Write as _;
use zenops_safe_relative_path::srpath;
use super::common::{
StoredShellConfig, write_aliases, write_brew_llvm_flags, write_environment,
write_path_variable, write_pkg_inits,
};
use crate::{
config::{Config, pkg::Shell},
config_files::{ConfigFilePath, ConfigFileSource, ConfigFiles},
error::Error,
};
pub(in super::super) type StoredBashConfig = StoredShellConfig;
pub(super) fn make_config_files(
shell_config: &StoredShellConfig,
config: &Config,
config_files: &mut ConfigFiles,
) -> Result<(), Error> {
let StoredShellConfig { environment, alias } = shell_config;
let mut bash_config = String::new();
_ = writeln!(
bash_config,
"#\n# Bash config generated by zenops, should be sourced from .bash_profile\n#\n"
);
#[cfg(target_os = "macos")]
{
_ = writeln!(
bash_config,
"# Homebrew environment (PATH, MANPATH, INFOPATH)"
);
_ = writeln!(
bash_config,
"eval \"$(/opt/homebrew/bin/brew shellenv bash)\""
);
bash_config.push('\n');
_ = writeln!(bash_config, "# Bash completions (brew-managed)");
_ = writeln!(
bash_config,
"if [ -f $(brew --prefix)/etc/bash_completion ]; then"
);
_ = writeln!(bash_config, " . $(brew --prefix)/etc/bash_completion");
_ = writeln!(bash_config, "fi");
bash_config.push('\n');
}
write_pkg_inits(&mut bash_config, &config.env_pkg_inits(Shell::Bash));
write_aliases(&mut bash_config, alias);
write_environment(&mut bash_config, environment);
_ = writeln!(bash_config, "[ -f ~/.bashrc ] && source ~/.bashrc");
bash_config.push('\n');
write_pkg_inits(&mut bash_config, &config.interactive_pkg_inits(Shell::Bash));
if let Some(path) = config.path_variable() {
write_path_variable(&mut bash_config, &path);
}
#[cfg(target_os = "macos")]
if config.has_brew_llvm() {
write_brew_llvm_flags(&mut bash_config);
}
config_files.add(
ConfigFilePath::in_home(srpath!(".zenops_bash_profile")),
ConfigFileSource::Generated(bash_config),
);
Ok(())
}