Skip to content

Commit

Permalink
Added merge_jsons action
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Gonzalez committed Jul 21, 2020
1 parent f89d89d commit 0fe92cf
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 45 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [0.1.4] (2020-07-20)

### Added
- [Action] merge_jsons

### Fixed
- Error handling in all actions

## [0.1.3] (2020-07-20)

### Added
Expand Down
66 changes: 61 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Fastlane Json plugin
# Fastlane Json plugin <!-- omit in toc -->

[![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-json)


- [Getting Started](#getting-started)
- [Actions](#actions)
- [read_json](#read_json)
- [download_json](#download_json)
- [write_json](#write_json)
- [merge_jsons](#merge_jsons)
- [Example](#example)
- [Run tests for this plugin](#run-tests-for-this-plugin)
- [Issues and Feedback](#issues-and-feedback)
- [Troubleshooting](#troubleshooting)
- [Using _fastlane_ Plugins](#using-fastlane-plugins)
- [About _fastlane_](#about-fastlane)

## Getting Started

This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-json`, add it to your project by running:
Expand All @@ -10,7 +24,7 @@ This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To
fastlane add_plugin json
```

## About json
## Actions

This plugin provide several actions that will allow you to manipulate and create json files.

Expand Down Expand Up @@ -104,11 +118,53 @@ Will create a my_json.json file with the following content:
}
```

## Example
### merge_jsons

| Key | Description | Env Var | Default |
|-------------|-------------------------------------------|---------|---------|
| jsons_paths | Array of json files paths | | |
| output_path | Output path where result will be saved | | |
| verbose | verbose | | false |

Merges several json files into one hash as output. Also you can set the `output_path` to save the merged hash into a json file.

Having this files:

`example.json`
```json
{
"name": "Martin",
"age": 30
}
```

Check out the [example `Fastfile`](fastlane/Fastfile) to see how to use this plugin. Try it by cloning the repo, running `fastlane install_plugins` and `bundle exec fastlane test`.
`example2.json`
```json
{
"lastName": "Gonzalez",
"age": 40,
"isDev": true
}
```

```ruby
output_path = "#{__dir__}/tmp/merged.json"

merged_hash = merge_jsons(
jsons_paths: [
"path/to/example.json",
"path/to/example2.json"
],
output_path: output_path
)

# {:name=>"Martin", :age=>40, :lastName=>"Gonzalez", :isDev=>true}
```


## Example

**Note to author:** Please set up a sample project to make it easy for users to explore what your plugin does. Provide everything that is necessary to try out the plugin in this project (including a sample Xcode/Android project if necessary)
Check out the [example `Fastfile`](fastlane/Fastfile) to see how to use this plugin. Try it by cloning the repo, running `fastlane install_plugins` and `bundle exec fastlane all`.

## Run tests for this plugin

Expand Down
27 changes: 27 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
lane :all do
read_json_test
download_json_test
write_json_test
merge_jsons_test
end

lane :read_json_test do
my_json = read_json(
json_path: "#{__dir__}/spec/resources/example.json",
Expand Down Expand Up @@ -35,3 +42,23 @@ lane :write_json_test do

FileUtils.rm_rf(file_dir)
end

lane :merge_jsons_test do
output_path = "#{__dir__}/tmp/merged.json"

merged_hash = merge_jsons(
jsons_paths: [
"#{__dir__}/spec/resources/example.json",
"#{__dir__}/spec/resources/example2.json"
],
output_path: output_path,
verbose: true
)

puts("Hash Value")
puts(merged_hash)

puts("Json file Value")
puts(File.read("../#{output_path}"))
FileUtils.rm_rf(File.dirname("../#{output_path}"))
end
3 changes: 2 additions & 1 deletion lib/fastlane/plugin/json/actions/download_json_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def self.run(params)

def self.puts_error!(message)
UI.user_error!(message)
raise StandardError, message
end

def self.description
Expand Down Expand Up @@ -54,7 +55,7 @@ def self.available_options
end

def self.print_params(options)
table_title = "Params for read_json #{Fastlane::Json::VERSION}"
table_title = "Params for download_json #{Fastlane::Json::VERSION}"
FastlaneCore::PrintTable.print_values(config: options,
hide_keys: [],
title: table_title)
Expand Down
100 changes: 100 additions & 0 deletions lib/fastlane/plugin/json/actions/merge_jsons_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# frozen_string_literal: true

require "json"

module Fastlane
module Actions
class MergeJsonsAction < Action
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

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

def self.put_error!(message)
UI.user_error!(message)
raise StandardError, message
end

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

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

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

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

def self.return_value
"Hash"
end

def self.authors
["Martin Gonzalez"]
end

def self.is_supported?(_platform)
true
end
end
end
end
1 change: 1 addition & 0 deletions lib/fastlane/plugin/json/actions/read_json_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def self.run(params)

def self.put_error!(message)
UI.user_error!(message)
raise StandardError, message
end

def self.description
Expand Down
13 changes: 3 additions & 10 deletions lib/fastlane/plugin/json/actions/write_json_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@ def self.run(params)
@is_verbose = params[:verbose]
print_params(params) if @is_verbose

if file_path.nil? || file_path.empty?
put_error!("file_path: value cannot be nil or empty")
return
end

if hash.nil?
put_error!("hash: value cannot be nil")
return
end
put_error!("file_path: value cannot be nil or empty") if file_path.nil? || file_path.empty?
put_error!("hash: value cannot be nil") if hash.nil?

file_path_expanded = File.expand_path(file_path)
file_dir = File.dirname(file_path_expanded)
Dir.mkdir(file_dir) unless File.directory?(file_dir)
print_params(params) if @is_verbose

begin
File.open(file_path, "w") do |f|
Expand All @@ -37,6 +29,7 @@ def self.run(params)

def self.put_error!(message)
UI.user_error!(message)
raise StandardError, message
end

def self.description
Expand Down
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/json/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module Json
VERSION = "0.1.3"
VERSION = "0.1.4"
end
end
16 changes: 10 additions & 6 deletions spec/download_json_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
it "raise an error with a messsage if download fails" do
expect(FastlaneCore::UI).to receive(:user_error!)

Fastlane::Actions::DownloadJsonAction.run(
json_url: "https://gist.someunixistingplace.com/dsdssd/dssds/raw/sddssd/someExample.json"
)
expect do
Fastlane::Actions::DownloadJsonAction.run(
json_url: "https://gist.someunixistingplace.com/dsdssd/dssds/raw/sddssd/someExample.json"
)
end.to raise_error(StandardError)
end

it "raise an error with a messsage if download succeed but json content is invalid" do
expect(FastlaneCore::UI).to receive(:user_error!)

Fastlane::Actions::DownloadJsonAction.run(
json_url: "https://gist.githubusercontent.com/MartinGonzalez/c14ee66436eb9f9c77004f43b4e47ed8/raw/d77fd0648258cf15a6fa30da1b3a887a8332b24a/invalid.json"
)
expect do
Fastlane::Actions::DownloadJsonAction.run(
json_url: "https://gist.githubusercontent.com/MartinGonzalez/c14ee66436eb9f9c77004f43b4e47ed8/raw/d77fd0648258cf15a6fa30da1b3a887a8332b24a/invalid.json"
)
end.to raise_error(StandardError)
end
end
Loading

0 comments on commit 0fe92cf

Please sign in to comment.