Skip to content

Commit

Permalink
Merge branch 'main' into aarch64-runners
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Jan 20, 2025
2 parents 0bb9db3 + 2e2a354 commit a9f5042
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions brush-core/src/builtins/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ impl DeclareCommand {
var.assign(initial_value, false)?;
}

if context.shell.options.export_variables_on_modification && !var.value().is_array() {
var.export();
}

self.apply_attributes_after_update(&mut var, verb)?;

let scope = if create_var_local {
Expand Down
16 changes: 15 additions & 1 deletion brush-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum EnvironmentScope {
pub struct ShellEnvironment {
/// Stack of scopes, with the top of the stack being the current scope.
pub(crate) scopes: Vec<(EnvironmentScope, ShellVariableMap)>,
/// Whether or not to auto-export variables on creation or modification.
export_variables_on_modification: bool,
}

impl Default for ShellEnvironment {
Expand All @@ -46,6 +48,7 @@ impl ShellEnvironment {
pub fn new() -> Self {
Self {
scopes: vec![(EnvironmentScope::Global, ShellVariableMap::new())],
export_variables_on_modification: false,
}
}

Expand Down Expand Up @@ -377,12 +380,19 @@ impl ShellEnvironment {
) -> Result<(), error::Error> {
let name = name.into();

let auto_export = self.export_variables_on_modification;
if let Some(var) = self.get_mut_using_policy(&name, lookup_policy) {
var.assign(value, false)?;
if auto_export {
var.export();
}
updater(var)
} else {
let mut var = ShellVariable::new(ShellValue::Unset(ShellValueUnsetType::Untyped));
var.assign(value, false)?;
if auto_export {
var.export();
}
updater(&mut var)?;

self.add(name, var, scope_if_creating)
Expand Down Expand Up @@ -438,9 +448,13 @@ impl ShellEnvironment {
pub fn add<N: Into<String>>(
&mut self,
name: N,
var: ShellVariable,
mut var: ShellVariable,
target_scope: EnvironmentScope,
) -> Result<(), error::Error> {
if self.export_variables_on_modification {
var.export();
}

for (scope_type, map) in self.scopes.iter_mut().rev() {
if *scope_type == target_scope {
map.set(name, var);
Expand Down
14 changes: 12 additions & 2 deletions brush-core/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ async fn expand_assignment_value(
async fn apply_assignment(
assignment: &ast::Assignment,
shell: &mut Shell,
export: bool,
mut export: bool,
required_scope: Option<EnvironmentScope>,
creation_scope: EnvironmentScope,
) -> Result<(), error::Error> {
Expand Down Expand Up @@ -1243,6 +1243,13 @@ async fn apply_assignment(
}
}
} else {
if !export
&& shell.options.export_variables_on_modification
&& !matches!(new_value, ShellValueLiteral::Array(_))
{
export = true;
}

existing_value.assign(new_value, assignment.append)?;
}

Expand All @@ -1267,7 +1274,10 @@ async fn apply_assignment(
}
} else {
match new_value {
ShellValueLiteral::Scalar(s) => ShellValue::String(s),
ShellValueLiteral::Scalar(s) => {
export = export || shell.options.export_variables_on_modification;
ShellValue::String(s)
}
ShellValueLiteral::Array(values) => ShellValue::indexed_array_from_literals(values)?,
}
};
Expand Down
21 changes: 15 additions & 6 deletions brush-shell/tests/cases/options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ cases:
echo "Default options: $-"
- name: "set -a"
known_failure: true # TODO: set -a not implemented
stdin: |
unexported=original
v1=1 v2=2 v3=3 v4=4 v5=(a b c)
declare -p v1 v2 v3 v4
set -a
newvar=bar
unexported=updated
env | grep newvar
env | grep unexported
v1=reassigned
v2+=appended
declare -i v3
v4+=(appended)
v5[2]=updated
v6=new
v7[0]=new
v8=(new)
declare v9=new
declare -a v10=(new)
declare -p v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
- name: "set -n"
stdin: |
Expand Down

0 comments on commit a9f5042

Please sign in to comment.