-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,52 +10,60 @@ class XCScheme | |
# LaunchAction or TestAction scheme group. | ||
# | ||
class EnvironmentVariables < XMLElementWrapper | ||
# @param [REXML::Element,Array{EnvironmentVariable},Array{Hash{String => String}}] node_or_target | ||
# The 'EnvironmentVariables' XML node that this object represents. | ||
# If nil, it will create a default XML node to use. | ||
# @param [REXML::Element,Array<EnvironmentVariable>,Array<Hash{Symbol => String,Bool}>] node_or_variables | ||
# The 'EnvironmentVariables' XML node that this object represents. If nil, it will create a default XML | ||
# node to use. | ||
# @note If initializing with an array of hashes, the hash should be of the form | ||
# {:key => <String>, :value => <String>(, :enabled => <Bool>)}. | ||
# @see EnvironmentVariable#initialize | ||
# | ||
def initialize(node_or_target = nil) | ||
create_xml_element_with_fallback(node_or_target, VARIABLES_NODE) do | ||
def initialize(node_or_variables = nil) | ||
create_xml_element_with_fallback(node_or_variables, VARIABLES_NODE) do | ||
@all_variables = [] | ||
node_or_target.each { |var| add_variable(var) } unless node_or_target.nil? | ||
node_or_variables.each { |var| upsert_variable(var) } unless node_or_variables.nil? | ||
end | ||
end | ||
|
||
# @return [Array{EnvironmentVariable}] | ||
# @return [Array<EnvironmentVariable>] | ||
# The key value pairs currently set in @xml_element | ||
# | ||
def all_variables | ||
@all_variables ||= @xml_element.get_elements(VARIABLE_NODE).map { |variable| EnvironmentVariable.new(variable) } | ||
end | ||
|
||
# @param [EnvironmentVariable,Hash{String => String}] variable | ||
# The key value pairs currently set in @xml_element | ||
# Adds a given variable to the set of environment variables, or replaces it if that key already exists | ||
# | ||
# @return [Array{EnvironmentVariable}] | ||
# @param [EnvironmentVariable,Hash{Symbol => String,Bool}] variable | ||
# The variable to set | ||
# @note If initializing with an array of hashes, the hash should be of the form | ||
# {:key => <String>, :value => <String>(, :enabled => <Bool>)}. | ||
# @see EnvironmentVariable#initialize | ||
# @return [Array<EnvironmentVariable>] | ||
# The new set of environment variables after addition | ||
# | ||
def add_variable(variable) | ||
def upsert_variable(variable) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
justinseanmartin
|
||
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 | ||
|
||
# @param [EnvironmentVariable,Hash{String => String}] variable | ||
# The key value pairs currently set in @xml_element | ||
# Removes a specified variable (by string or object) from the set of environment variables | ||
# | ||
# @return [Array{EnvironmentVariable}] | ||
# @param [EnvironmentVariable,String] variable | ||
# The variable to remove | ||
# @return [Array<EnvironmentVariable>] | ||
# The new set of environment variables after removal | ||
# | ||
def remove_variable(variable) | ||
env_var = variable.is_a?(EnvironmentVariable) ? variable : all_variables.select { |var| var.key == variable[:key] }[0] | ||
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 | ||
|
||
# @param [EnvironmentVariable,Hash{String => String}] variable | ||
# The key value pairs currently set in @xml_element | ||
# @return Array<Hash{Symbol => String,Bool}> | ||
# The current environment variables represented as an array | ||
# | ||
def to_a | ||
all_variables.map(&:to_h) | ||
|
@@ -67,48 +75,68 @@ def to_a | |
# [[NSProcessInfo processInfo] environment] in your app code. | ||
# | ||
class EnvironmentVariable < XMLElementWrapper | ||
# @param [REXML::Element,EnvironmentVariable,Hash{String => String}] value | ||
# The 'EnvironmentVariable' XML node that this object represents. | ||
# If nil, it will create a default XML node to use. If a hash, it | ||
# must contain keys 'key' and 'value', and optionally have 'enabled'. | ||
# If not supplied, it will default to being enabled. | ||
# @param [REXML::Element,Hash{Symbol => String,Bool}] value | ||
# The 'EnvironmentVariable' XML node that this object represents. If nil, | ||
# it will create a default XML node to use. If a hash, it must contain | ||
# keys 'key' and 'value' with string values, and optionally have 'enabled' | ||
This comment has been minimized.
Sorry, something went wrong.
AliSoftware
Contributor
|
||
# as a boolean. If 'enabled' is not supplied, it will default to true. | ||
# @note If initializing with a hash, it should be of the form | ||
# {:key => <String>, :value => <String>(, :enabled => <Bool>)}. | ||
# | ||
def initialize(value) | ||
create_xml_element_with_fallback(value, VARIABLE_NODE) do | ||
raise "Must pass a Hash with 'key' and 'value'!" unless value.is_a?(Hash) && value.key?(:key) && value.key?(:value) | ||
def initialize(node_or_variable) | ||
create_xml_element_with_fallback(node_or_variable, VARIABLE_NODE) do | ||
raise "Must pass a Hash with 'key' and 'value'!" unless node_or_variable.is_a?(Hash) && | ||
node_or_variable.key?(:key) && node_or_variable.key?(:value) | ||
|
||
@xml_element.attributes['key'] = node_or_variable[:key] | ||
@xml_element.attributes['value'] = node_or_variable[:value] | ||
|
||
@xml_element.attributes['key'] = value[:key] | ||
@xml_element.attributes['value'] = value[:value] | ||
@xml_element.attributes['isEnabled'] = value.key?(:enabled) ? bool_to_string(value[:enabled]) : bool_to_string(true) | ||
if node_or_variable.key?(:enabled) | ||
@xml_element.attributes['isEnabled'] = bool_to_string(node_or_variable[:enabled]) | ||
else | ||
@xml_element.attributes['isEnabled'] = bool_to_string(true) | ||
end | ||
end | ||
end | ||
|
||
# Returns the EnvironmentValue's key | ||
# @return [String] | ||
def key | ||
@xml_element.attributes['key'] | ||
end | ||
|
||
# Sets the EnvironmentValue's key | ||
# @param [String] key | ||
def key=(key) | ||
@xml_element.attributes['key'] = key | ||
end | ||
|
||
# Returns the EnvironmentValue's value | ||
# @return [String] | ||
def value | ||
@xml_element.attributes['value'] | ||
end | ||
|
||
# Sets the EnvironmentValue's value | ||
# @param [String] value | ||
def value=(value) | ||
@xml_element.attributes['value'] = value | ||
end | ||
|
||
# Returns the EnvironmentValue's enabled state | ||
# @return [Bool] | ||
def enabled | ||
string_to_bool(@xml_element.attributes['isEnabled']) | ||
end | ||
|
||
# Sets the EnvironmentValue's enabled state | ||
# @param [Bool] enabled | ||
def enabled=(enabled) | ||
@xml_element.attributes['isEnabled'] = bool_to_string(enabled) | ||
end | ||
|
||
# @return [Hash{:key => String, :value => String, :enabled => Boolean}] | ||
# The environment variable XML node with attributes converted to Hash keys | ||
# @return [Hash{:key => String, :value => String, :enabled => Bool}] | ||
# The environment variable XML node with attributes converted to a representative Hash | ||
def to_h | ||
{ :key => key, :value => value, :enabled => enabled } | ||
end | ||
|
We definitely need to find a better name for that method.