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

51 lines
1.2 KiB
Ruby
Raw 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
2023-06-22 18:19:35 +00:00
arguments = @args.to_a || []
2022-11-22 10:24:42 -05:00
arguments.concat(["--custom-transformers", *@custom_transformers]) if @custom_transformers.length.positive?
2023-06-22 18:19:35 +00: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",
arguments.map do |a|
2023-05-15 10:58:01 -07:00
value = a.include?(" ") ? a.inspect : a
2023-05-15 12:29:15 -07:00
2023-05-15 10:58:01 -07:00
unless value.start_with?("--")
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-05-15 10:58:01 -07:00
set_environment(name, value)
2023-05-15 12:29:15 -07:00
2023-05-15 10:58:01 -07:00
value = "$#{name}"
end
2022-01-07 19:45:53 +00:00
2023-05-15 10:58:01 -07:00
value
2022-01-07 19:45:53 +00:00
end.join(" ")
)
end
end