1# frozen_string_literal: true
2
3module ExpandVariables
4  VARIABLES_REGEXP = /\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/.freeze
5
6  class << self
7    def expand(value, variables)
8      replace_with(value, variables) do |vars_hash, last_match|
9        match_or_blank_value(vars_hash, last_match)
10      end
11    end
12
13    def expand_existing(value, variables)
14      replace_with(value, variables) do |vars_hash, last_match|
15        match_or_original_value(vars_hash, last_match)
16      end
17    end
18
19    def possible_var_reference?(value)
20      return unless value
21
22      %w[$ %].any? { |symbol| value.include?(symbol) }
23    end
24
25    private
26
27    def replace_with(value, variables)
28      variables_hash = nil
29
30      value.gsub(VARIABLES_REGEXP) do
31        variables_hash ||= transform_variables(variables)
32        yield(variables_hash, Regexp.last_match)
33      end
34    end
35
36    def match_or_blank_value(variables, last_match)
37      variables[last_match[1] || last_match[2]]
38    end
39
40    def match_or_original_value(variables, last_match)
41      match_or_blank_value(variables, last_match) || last_match[0]
42    end
43
44    def transform_variables(variables)
45      # Lazily initialise variables
46      variables = variables.call if variables.is_a?(Proc)
47
48      # Convert Collection to variables
49      variables = variables.to_hash if variables.is_a?(Gitlab::Ci::Variables::Collection)
50
51      # Convert hash array to variables
52      if variables.is_a?(Array)
53        variables = variables.reduce({}) do |hash, variable|
54          hash[variable[:key]] = variable[:value]
55          hash
56        end
57      end
58
59      variables
60    end
61  end
62end
63