Files
importer-issue-ops/lib/models/arguments.rb

57 lines
1.4 KiB
Ruby
Raw Permalink Normal View History

2022-01-07 19:45:53 +00:00
# frozen_string_literal: true
2023-05-15 10:58:01 -07:00
require_relative "../concerns/environment_writer"
2022-01-07 19:45:53 +00:00
class Arguments
2023-05-15 10:58:01 -07:00
include EnvironmentWriter
2022-01-07 19:45:53 +00:00
def initialize(provider, command, issue_content)
@args = argument_class(provider, command, issue_content)
2022-11-22 10:24:42 -05:00
@custom_transformers = custom_transformers(command)
2022-01-07 19:45:53 +00:00
end
def argument_class(provider, command, issue_content)
provider.module.const_get(command.classify).new(issue_content, command)
end
2022-11-22 10:24:42 -05:00
def custom_transformers(command)
command.options&.dig("custom-transformers")&.split(" ") || Dir.glob("transformers/**/*.rb")
end
2022-01-07 19:45:53 +00:00
def to_output
arguments = @args.to_a || []
2023-07-31 16:45:40 -07:00
arguments.push(["--custom-transformers", *@custom_transformers]) if @custom_transformers.length.positive?
2022-11-22 10:24:42 -05:00
return if arguments.blank?
2023-05-15 12:29:15 -07:00
rng = Random.new
2023-05-17 12:57:32 -07:00
variable_names = Set.new
2023-05-15 10:58:01 -07:00
2022-01-07 19:45:53 +00:00
set_output(
"args",
2023-07-31 16:45:40 -07:00
arguments.map do |value|
unless value.is_a?(Array)
value = value.inspect if value.include?(" ")
2023-07-31 16:52:50 -07:00
2023-07-31 16:45:40 -07:00
next value
end
value.map.with_index do |v, index|
v = v.inspect if v.include?(" ")
next v if index.zero?
2023-05-15 12:29:15 -07:00
name = "variable_#{rng.rand(1000..9999)}"
2023-05-17 12:57:32 -07:00
name = "variable_#{rng.rand(1000..9999)}" while variable_names.include?(name)
variable_names.add(name)
2023-07-31 16:45:40 -07:00
set_environment(name, v)
2023-05-15 12:29:15 -07:00
2023-07-31 16:45:40 -07:00
"$#{name}"
2023-05-15 10:58:01 -07:00
end
2022-01-07 19:45:53 +00:00
end.join(" ")
)
end
end