summaryrefslogtreecommitdiff
path: root/lib/expand_variables.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-11-19 08:27:35 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-11-19 08:27:35 +0000
commit7e9c479f7de77702622631cff2628a9c8dcbc627 (patch)
treec8f718a08e110ad7e1894510980d2155a6549197 /lib/expand_variables.rb
parente852b0ae16db4052c1c567d9efa4facc81146e88 (diff)
downloadgitlab-ce-7e9c479f7de77702622631cff2628a9c8dcbc627.tar.gz
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'lib/expand_variables.rb')
-rw-r--r--lib/expand_variables.rb28
1 files changed, 25 insertions, 3 deletions
diff --git a/lib/expand_variables.rb b/lib/expand_variables.rb
index 3a50925d628..dc8f9d0c970 100644
--- a/lib/expand_variables.rb
+++ b/lib/expand_variables.rb
@@ -1,17 +1,39 @@
# frozen_string_literal: true
module ExpandVariables
+ VARIABLES_REGEXP = /\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/.freeze
+
class << self
def expand(value, variables)
+ replace_with(value, variables) do |vars_hash, last_match|
+ match_or_blank_value(vars_hash, last_match)
+ end
+ end
+
+ def expand_existing(value, variables)
+ replace_with(value, variables) do |vars_hash, last_match|
+ match_or_original_value(vars_hash, last_match)
+ end
+ end
+
+ private
+
+ def replace_with(value, variables)
variables_hash = nil
- value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
+ value.gsub(VARIABLES_REGEXP) do
variables_hash ||= transform_variables(variables)
- variables_hash[Regexp.last_match(1) || Regexp.last_match(2)]
+ yield(variables_hash, Regexp.last_match)
end
end
- private
+ def match_or_blank_value(variables, last_match)
+ variables[last_match[1] || last_match[2]]
+ end
+
+ def match_or_original_value(variables, last_match)
+ match_or_blank_value(variables, last_match) || last_match[0]
+ end
def transform_variables(variables)
# Lazily initialise variables