Class: Fastlane::Actions::MergeJsonsAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/json/actions/merge_jsons_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



91
92
93
# File 'lib/fastlane/plugin/json/actions/merge_jsons_action.rb', line 91

def self.authors
  ["Martin Gonzalez"]
end

.available_optionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

.descriptionObject



50
51
52
# 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

.detailsObject



54
55
56
# 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) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/fastlane/plugin/json/actions/merge_jsons_action.rb', line 95

def self.is_supported?(_platform)
  true
end


80
81
82
83
84
85
# File 'lib/fastlane/plugin/json/actions/merge_jsons_action.rb', line 80

def self.print_params(options)
  table_title = "Params for merge_json #{Fastlane::Json::VERSION}"
  FastlaneCore::PrintTable.print_values(config: options,
                                        hide_keys: [],
                                        title: table_title)
end

.put_error!(message) ⇒ Object

Raises:

  • (StandardError)


45
46
47
48
# 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_valueObject



87
88
89
# File 'lib/fastlane/plugin/json/actions/merge_jsons_action.rb', line 87

def self.return_value
  "Hash"
end

.run(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# 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