Class: Xcodeproj::XCScheme::EnvironmentVariables

Inherits:
XMLElementWrapper show all
Defined in:
lib/xcodeproj/scheme/environment_variables.rb

Overview

This class wraps the EnvironmentVariables node of a .xcscheme XML file. This is just a container of EnvironmentVariable objects. It can either appear on a LaunchAction or TestAction scheme group.

Instance Attribute Summary

Attributes inherited from XMLElementWrapper

#xml_element

Instance Method Summary collapse

Methods inherited from XMLElementWrapper

#to_s

Constructor Details

#initialize(node_or_variables = nil) ⇒ EnvironmentVariables

Returns a new instance of EnvironmentVariables.

Parameters:

  • node_or_variables (nil, REXML::Element, Array<EnvironmentVariable>, Array<Hash{Symbol => String,Bool}>) (defaults to: nil)

    The ‘EnvironmentVariables’ XML node, or list of environment variables, that this object represents.

    - If nil, an empty 'EnvironmentVariables' XML node will be created
    - If an REXML::Element, it must be named 'EnvironmentVariables'
    - If an Array of objects or Hashes, they'll each be passed to {#assign_variable}
    


19
20
21
22
23
24
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 19

def initialize(node_or_variables = nil)
  create_xml_element_with_fallback(node_or_variables, VARIABLES_NODE) do
    @all_variables = []
    node_or_variables.each { |var| assign_variable(var) } unless node_or_variables.nil?
  end
end

Instance Method Details

#[](key) ⇒ EnvironmentVariable

Variable Returns the matching environment variable for a specified key

Parameters:

  • key (String)

    The key to lookup

Returns:

  • (EnvironmentVariable)

    variable Returns the matching environment variable for a specified key



68
69
70
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 68

def [](key)
  all_variables.find { |var| var.key == key }
end

#[]=(key, value) ⇒ EnvironmentVariable

Assigns a value for a specified key

Parameters:

  • key (String)

    The key to update in the environment variables

  • value (String)

    The value to lookup

Returns:



81
82
83
84
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 81

def []=(key, value)
  assign_variable(:key => key, :value => value)
  self[key]
end

#all_variablesArray<EnvironmentVariable>

Returns The key value pairs currently set in @xml_element.

Returns:



29
30
31
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 29

def all_variables
  @all_variables ||= @xml_element.get_elements(VARIABLE_NODE).map { |variable| EnvironmentVariable.new(variable) }
end

#assign_variable(variable) ⇒ Array<EnvironmentVariable>

Adds a given variable to the set of environment variables, or replaces it if that key already exists

Parameters:

  • variable (EnvironmentVariable, Hash{Symbol => String,Bool})

    The variable to add or update, backed by an EnvironmentVariable node.

    - If an EnvironmentVariable, the previous reference will still be valid
    - If a Hash, must conform to {EnvironmentVariable#initialize} requirements
    

Returns:



42
43
44
45
46
47
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 42

def assign_variable(variable)
  env_var = variable.is_a?(EnvironmentVariable) ? variable : EnvironmentVariable.new(variable)
  all_variables.each { |existing_var| remove_variable(existing_var) if existing_var.key == env_var.key }
  @xml_element.add_element(env_var.xml_element)
  @all_variables << env_var
end

#remove_variable(variable) ⇒ Array<EnvironmentVariable>

Removes a specified variable (by string or object) from the set of environment variables

Parameters:

Returns:



56
57
58
59
60
61
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 56

def remove_variable(variable)
  env_var = variable.is_a?(EnvironmentVariable) ? variable : all_variables.find { |var| var.key == variable }
  raise "Unexpected parameter type: #{env_var.class}" unless env_var.is_a?(EnvironmentVariable)
  @xml_element.delete_element(env_var.xml_element)
  @all_variables -= [env_var]
end

#to_aArray<Hash{Symbol => String,Bool}>

Returns The current environment variables represented as an array.

Returns:

  • (Array<Hash{Symbol => String,Bool}>)

    The current environment variables represented as an array



89
90
91
# File 'lib/xcodeproj/scheme/environment_variables.rb', line 89

def to_a
  all_variables.map(&:to_h)
end