class Fastlane::Actions::MergeJsonsAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 91
def self.authors
  ["Martin Gonzalez"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 58
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :jsons_paths,
                                 description: "Array of json files paths",
                                 type: Array,
                                 optional: false,
                                 verify_block: proc do |value|
                                   UI.user_error!("You must set jsons_paths") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :output_path,
                                 description: "Output path where result will be saved",
                                 type: String,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :verbose,
                                 description: "verbose",
                                 optional: true,
                                 default_value: false,
                                 type: Boolean)

  ]
end
description() click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 50
def self.description
  "Merge a group of jsons files and expose a hash with symbolized names as result. Last json predominate over the rest"
end
details() click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 54
def self.details
  "Use this action to merge jsons files and access to it as Hash with symbolized names. Also you can set the output_path to save the result"
end
is_supported?(_platform) click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 95
def self.is_supported?(_platform)
  true
end
print_params(options) click to toggle source
put_error!(message) click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 45
def self.put_error!(message)
  UI.user_error!(message)
  raise StandardError, message
end
return_value() click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 87
def self.return_value
  "Hash"
end
run(params) click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 8
def self.run(params)
  jsons_paths = params[:jsons_paths]
  output_path = params[:output_path]
  @is_verbose = params[:verbose]

  print_params(params) if @is_verbose
  put_error!("jsons_path cannot be empty ❌") if jsons_paths.empty?

  hashes = jsons_paths.map do |json_path|
    put_error!("json_path: #{json_path} is not valid ❌") unless File.exist?(json_path)
    json_content = File.read(File.expand_path(json_path))
    begin
      JSON.parse(json_content, symbolize_names: true)
    rescue
      put_error!("File at path #{json_path} has invalid content. ❌")
    end
  end

  merged_hash = hashes.reduce({}, :merge)

  write_hash_at(output_path, merged_hash) unless output_path.to_s.empty?

  merged_hash
end
write_hash_at(output_path, hash) click to toggle source
# File lib/fastlane/plugin/json/actions/merge_jsons_action.rb, line 33
def self.write_hash_at(output_path, hash)
  file_path_expanded = File.expand_path(output_path)
  file_dir = File.dirname(file_path_expanded)
  Dir.mkdir(file_dir) unless File.directory?(file_dir)

  begin
    File.open(output_path, "w") { |f| f.write(JSON.pretty_generate(hash)) }
  rescue
    put_error!("Failed to write json at #{output_path}. ❌")
  end
end