jf 0.1.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +9 -5
- data/bin/jf +14 -4
- data/jf.gemspec +1 -1
- data/lib/ext.rb +74 -0
- data/lib/jf.rb +23 -3
- data/lib/jf/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 384e6c5133e5bb542ed6a71d9f1f9e6e368d9b44
|
4
|
+
data.tar.gz: b437306e0bf1241df7f5a955f1b4f25bfe72878e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5a7f2a6e9006a159c665b29b9e2b9fc4e33c752d2642afd424ed04dd2daad55d35f2a53a8ea8de9ceb24127f69b9d895e94891b85995b0053bc5cc75d1f773d
|
7
|
+
data.tar.gz: 65955f60ef83882d6634e082bf4e3b7e5e67ffa1dd72f28f32a8da0261cf8762d7720604162b3084c6fb8c8a0cca2ea71dd9abb07115756c613af17b97b621c5
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
JF
|
2
2
|
=====
|
3
3
|
|
4
4
|
Dead simple JSON Formatter
|
@@ -6,10 +6,15 @@ Dead simple JSON Formatter
|
|
6
6
|
## Example
|
7
7
|
|
8
8
|
```
|
9
|
-
$ jf [--
|
10
|
-
$ jf .
|
11
|
-
$ cat raw.json | jf
|
9
|
+
$ jf raw.json [--gitdiff|--minify|-i] # Attach format to a json file.
|
10
|
+
$ jf . # Attach format to all of json files under the current directory.
|
11
|
+
$ cat raw.json | jf # Output pretty json for the received STDIN.
|
12
12
|
> (print pretty json)
|
13
|
+
|
14
|
+
# Merge options
|
15
|
+
$ jf origin.json --merge target.json # Push the object of inside of target.json back to the origin.json.
|
16
|
+
$ jf origin.json --merge target.json --after key1 # Push the object of inside of target.json to "key1" object of origin.json.
|
17
|
+
$ jf origin.json --merge target.json --after key1.innerkey1.0 # Push the object of inside of target.json to "origin[:key1][:innerkey1][:deep][0]".
|
13
18
|
```
|
14
19
|
|
15
20
|
## Options
|
@@ -35,4 +40,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/timaki
|
|
35
40
|
## License
|
36
41
|
|
37
42
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
38
|
-
|
data/bin/jf
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require "jf"
|
4
4
|
require 'optparse'
|
5
5
|
|
6
|
-
ARG_OPTIONS = ["i:", "git-diff", "minify"]
|
6
|
+
# ARG_OPTIONS = ["i:", "git-diff", "minify", "merge:"]
|
7
7
|
|
8
8
|
module InputProxy
|
9
9
|
def stdin?
|
@@ -14,7 +14,17 @@ module InputProxy
|
|
14
14
|
end
|
15
15
|
|
16
16
|
Target = Struct.new("Target", :is_stdin, :body)
|
17
|
-
target = InputProxy.stdin? ? Target.new(true, $stdin.readlines.join("\n")) : Target.new(false, ARGV
|
18
|
-
opts = ARGV.getopts(*ARG_OPTIONS)
|
17
|
+
target = InputProxy.stdin? ? Target.new(true, $stdin.readlines.join("\n")) : Target.new(false, ARGV[0])
|
19
18
|
|
20
|
-
|
19
|
+
option={}
|
20
|
+
OptionParser.new do |opt|
|
21
|
+
opt.on('-i', '--indent=VALUE') {|v| option[:indent] = v }
|
22
|
+
opt.on('', '--gitdiff') {|v| option["git-diff"] = v }
|
23
|
+
opt.on('', '--minify') {|v| option[:minify] = v }
|
24
|
+
opt.on('', '--merge=VALUE') {|v| option[:merge] = v }
|
25
|
+
opt.on('', '--key=VALUE') {|v| option[:key] = v }
|
26
|
+
|
27
|
+
opt.parse!(ARGV)
|
28
|
+
end
|
29
|
+
|
30
|
+
Jf.execute(target, option)
|
data/jf.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["timaki.st@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = "JSON formatter on CLI with dead simple command."
|
13
|
-
spec.description = "Format JSON
|
13
|
+
spec.description = "Format JSON with dead simple command"
|
14
14
|
spec.homepage = "https://github.com/timakin/jf"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
data/lib/ext.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
class String
|
2
|
+
def numstr_to_i
|
3
|
+
is_int?(self) ? self.to_i : self
|
4
|
+
end
|
5
|
+
|
6
|
+
def is_int?(str)
|
7
|
+
Integer(str)
|
8
|
+
true
|
9
|
+
rescue ArgumentError
|
10
|
+
false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Array
|
15
|
+
def one?
|
16
|
+
self.size == 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Hash
|
21
|
+
def slice(*keys)
|
22
|
+
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
|
23
|
+
keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def slice!(*keys)
|
27
|
+
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
|
28
|
+
omit = slice(*self.keys - keys)
|
29
|
+
hash = slice(*keys)
|
30
|
+
hash.default = default
|
31
|
+
hash.default_proc = default_proc if default_proc
|
32
|
+
replace(hash)
|
33
|
+
omit
|
34
|
+
end
|
35
|
+
|
36
|
+
def extract!(*keys)
|
37
|
+
keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def insert_after(key, obj)
|
41
|
+
splitter = self.keys.index(key)
|
42
|
+
before_keys = self.keys[0..splitter]
|
43
|
+
before = self.extract!(*before_keys)
|
44
|
+
after = self.slice!(*before_keys)
|
45
|
+
merged = before.merge(obj).merge(after)
|
46
|
+
merged
|
47
|
+
end
|
48
|
+
|
49
|
+
def deep_merge(*keys, val)
|
50
|
+
merge_target = keys.inject(self) { |h, k| h[k.numstr_to_i] }
|
51
|
+
if keys.one?
|
52
|
+
self[keys.last] = merge_target.merge(val)
|
53
|
+
else
|
54
|
+
if merge_target.kind_of?(Array)
|
55
|
+
merge_to_array(*keys, merge_target, val)
|
56
|
+
else
|
57
|
+
merge_to_hash(*keys, merge_target, val)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def merge_to_array(*keys, target, val)
|
66
|
+
last_key = keys.pop
|
67
|
+
keys.inject(self) { |h, k| h[k.numstr_to_i] }[last_key.numstr_to_i] = target.push(val)
|
68
|
+
end
|
69
|
+
|
70
|
+
def merge_to_hash(*keys, target, val)
|
71
|
+
last_key = keys.pop
|
72
|
+
keys.inject(self) { |h, k| h[k.numstr_to_i] }[last_key.numstr_to_i] = target.merge(val)
|
73
|
+
end
|
74
|
+
end
|
data/lib/jf.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "jf/version"
|
2
|
+
require "ext"
|
2
3
|
require 'json'
|
3
4
|
|
4
5
|
class FileParseError < StandardError; end
|
@@ -11,7 +12,7 @@ module Jf
|
|
11
12
|
end
|
12
13
|
|
13
14
|
if File.exists?(target.body)
|
14
|
-
paths = opts[
|
15
|
+
paths = opts[:gitdiff] ? get_diff_list.split(/$/).map(&:strip) : get_path_list(target.body)
|
15
16
|
else
|
16
17
|
assert_file_not_found
|
17
18
|
exit
|
@@ -20,7 +21,13 @@ module Jf
|
|
20
21
|
for path in paths
|
21
22
|
next if is_not_json(path)
|
22
23
|
begin
|
23
|
-
opts[
|
24
|
+
if opts[:minify]
|
25
|
+
minify(path)
|
26
|
+
elsif opts[:merge]
|
27
|
+
merge(path, opts)
|
28
|
+
else
|
29
|
+
rewrite(path, opts)
|
30
|
+
end
|
24
31
|
rescue FileParseError => e
|
25
32
|
assert_parse_failed
|
26
33
|
next
|
@@ -38,11 +45,24 @@ module Jf
|
|
38
45
|
|
39
46
|
def self.apply_format(target, opts)
|
40
47
|
data_hash = JSON.parse(target)
|
41
|
-
indent = opts[
|
48
|
+
indent = opts[:indent] != nil ? " " * opts[:indent].to_i : " "
|
42
49
|
formatted = JSON.pretty_generate(data_hash, {:indent => indent}).gsub /^$\n/, ''
|
43
50
|
return formatted
|
44
51
|
end
|
45
52
|
|
53
|
+
def self.merge(origin, opts)
|
54
|
+
origin_json = JSON.parse(File.read(origin))
|
55
|
+
target_json = JSON.parse(File.read(opts[:merge]))
|
56
|
+
if opts[:key]
|
57
|
+
target_keys = opts[:key].split(".")
|
58
|
+
origin_json = origin_json.deep_merge(*target_keys, target_json)
|
59
|
+
else
|
60
|
+
origin_json = origin_json.merge(target_json)
|
61
|
+
end
|
62
|
+
File.write(origin, origin_json.to_json)
|
63
|
+
rewrite(origin, opts)
|
64
|
+
end
|
65
|
+
|
46
66
|
def self.rewrite(path, opts)
|
47
67
|
File.write(path, apply_format(File.read(path).force_encoding("UTF-8"), opts))
|
48
68
|
end
|
data/lib/jf/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- timakin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
-
description: Format JSON
|
41
|
+
description: Format JSON with dead simple command
|
42
42
|
email:
|
43
43
|
- timaki.st@gmail.com
|
44
44
|
executables:
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- bin/jf
|
57
57
|
- bin/setup
|
58
58
|
- jf.gemspec
|
59
|
+
- lib/ext.rb
|
59
60
|
- lib/jf.rb
|
60
61
|
- lib/jf/version.rb
|
61
62
|
homepage: https://github.com/timakin/jf
|