diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2019-08-13 17:03:52 +0200 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2019-08-13 19:14:20 +0200 |
commit | c4295286a491fd02b8474192d5680dbe63af09fe (patch) | |
tree | 6018a2050db45c1488fbc5c2661137a8d850bf2d /lib/expand_variables.rb | |
parent | a55869483d023978655658d389aad36d63c9d2b2 (diff) | |
download | gitlab-ce-expand-variables-only-when-needed.tar.gz |
Expand variables only when neededexpand-variables-only-when-needed
This makes us to expand variables only when needed,
instead of requesting all variables each time.
This specifically helps in situation when explicit name
of `environment: production` is used.
Diffstat (limited to 'lib/expand_variables.rb')
-rw-r--r-- | lib/expand_variables.rb | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/lib/expand_variables.rb b/lib/expand_variables.rb index c83cec9dc4a..6d220f273f9 100644 --- a/lib/expand_variables.rb +++ b/lib/expand_variables.rb @@ -3,6 +3,20 @@ module ExpandVariables class << self def expand(value, variables) + variables_hash = nil + + value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do + variables_hash ||= transform_variables(variables) + variables_hash[$1 || $2] + end + end + + private + + def transform_variables(variables) + # Lazilly initialise variables + variables = variables.call if variables.is_a?(Proc) + # Convert hash array to variables if variables.is_a?(Array) variables = variables.reduce({}) do |hash, variable| @@ -11,9 +25,7 @@ module ExpandVariables end end - value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do - variables[$1 || $2] - end + variables end end end |