graphql-formatter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/graphql-formatter.gemspec +24 -0
- data/lib/graphql-formatter/character_string.rb +20 -0
- data/lib/graphql-formatter/line_collection.rb +48 -0
- data/lib/graphql-formatter/line_of_character_strings.rb +51 -0
- data/lib/graphql-formatter/version.rb +3 -0
- data/lib/graphql-formatter.rb +113 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3dea5941af756c54d84315c6de2a455ad736cda0
|
4
|
+
data.tar.gz: f2fe14c150be133d2a64a99ca2b172fadf5adab1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5c715fd330e96989bbd765ff2d2267ed80f0725b7298feb21df97746e6a33750089ea5598334612fd7e22f49f0a284170ae635574bd5007d009208fa93d648b2
|
7
|
+
data.tar.gz: 76611a203b5772c38b8f8c15a3152de46e271446289720c08d20ccbcf2fc788e083d1e6fec997e958e0b11738aa26a5ac593772ba9adaf2c8b15dd73e51c3580
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Brandfolder
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Graphql::Formatter
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/graphql/formatter`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'graphql-formatter'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install graphql-formatter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql-formatter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "graphql/formatter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'graphql-formatter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "graphql-formatter"
|
8
|
+
spec.version = GraphQLFormatter::VERSION
|
9
|
+
spec.authors = ["Brandfolder, Inc."]
|
10
|
+
spec.email = ["developers@brandfolder.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Pretty prints GraphQL queries}
|
13
|
+
spec.homepage = "https://github.com/brandfolder/graphql-formatter"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class GraphQLFormatter::CharacterString
|
2
|
+
extend Forwardable
|
3
|
+
attr_reader :string
|
4
|
+
attr_accessor :color
|
5
|
+
|
6
|
+
def_delegators :string, :empty?, :end_with?, :chars, :<<
|
7
|
+
|
8
|
+
def initialize(color: nil)
|
9
|
+
self.color = color
|
10
|
+
@string = ''
|
11
|
+
end
|
12
|
+
|
13
|
+
def last_char
|
14
|
+
chars.last
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s(colorize: true)
|
18
|
+
colorize ? string.colorize(color) : string
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class GraphQLFormatter::LineCollection
|
2
|
+
include Enumerable
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
attr_reader :lines
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@lines = [GraphQLFormatter::LineOfCharacterStrings.new]
|
9
|
+
end
|
10
|
+
|
11
|
+
def_delegators :current_line, :current_color, :current_string
|
12
|
+
def_delegators :lines, :each
|
13
|
+
|
14
|
+
def current_line
|
15
|
+
lines.last
|
16
|
+
end
|
17
|
+
|
18
|
+
def new_line(indent_level: current_indent_level, **opts)
|
19
|
+
GraphQLFormatter::LineOfCharacterStrings.new(indent_level: indent_level, **opts).tap do |line|
|
20
|
+
lines << line
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def new_string(**opts)
|
25
|
+
current_line.new_string(**opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def previous_line
|
29
|
+
lines[-2]
|
30
|
+
end
|
31
|
+
|
32
|
+
def previous_string
|
33
|
+
current_line.previous_string || previous_line.current_string
|
34
|
+
end
|
35
|
+
|
36
|
+
def current_indent_level
|
37
|
+
current_line.indent_level
|
38
|
+
end
|
39
|
+
|
40
|
+
def last_character
|
41
|
+
(current_line && current_line.last_char) ||
|
42
|
+
(previous_line && previous_line.last_char)
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s(**opts)
|
46
|
+
reject(&:empty?).map { |line| line.to_s(**opts) }.join("\n")
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class GraphQLFormatter::LineOfCharacterStrings
|
2
|
+
include Enumerable
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
attr_reader :strings
|
6
|
+
attr_accessor :indent_level
|
7
|
+
|
8
|
+
def_delegators :strings, :each
|
9
|
+
|
10
|
+
def initialize(indent_level: 0)
|
11
|
+
self.indent_level = indent_level
|
12
|
+
@strings = [GraphQLFormatter::CharacterString.new]
|
13
|
+
end
|
14
|
+
|
15
|
+
def new_string(color: nil, **opts)
|
16
|
+
return current_string if color == current_color
|
17
|
+
if current_string.empty?
|
18
|
+
current_string.color = color
|
19
|
+
return current_string
|
20
|
+
end
|
21
|
+
GraphQLFormatter::CharacterString.new(color: color, **opts).tap do |string|
|
22
|
+
@strings << string
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def last_char
|
27
|
+
(current_string && current_string.last_char) ||
|
28
|
+
(previous_string && previous_string.last_char)
|
29
|
+
end
|
30
|
+
|
31
|
+
def previous_string
|
32
|
+
@strings[-2]
|
33
|
+
end
|
34
|
+
|
35
|
+
def current_string
|
36
|
+
@strings.last
|
37
|
+
end
|
38
|
+
|
39
|
+
def current_color
|
40
|
+
current_string.color
|
41
|
+
end
|
42
|
+
|
43
|
+
def empty?
|
44
|
+
strings.all? &:empty?
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_s(indenter: (' ' * 4), **opts)
|
48
|
+
indent = indenter * indent_level
|
49
|
+
indent + reject(&:empty?).map { |str| str.to_s(**opts) }.join
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
class GraphQLFormatter
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
autoload :CharacterString, 'graphql-formatter/character_string'
|
7
|
+
autoload :LineCollection, 'graphql-formatter/line_collection'
|
8
|
+
autoload :LineOfCharacterStrings, 'graphql-formatter/line_of_character_strings'
|
9
|
+
autoload :VERSION, 'graphql-formatter/version'
|
10
|
+
|
11
|
+
attr_reader :result, :in_parens
|
12
|
+
def_delegators :result, :new_line, :new_string, :previous_string, :current_string,
|
13
|
+
:current_line, :last_character, :current_indent_level, :to_s,
|
14
|
+
:current_color
|
15
|
+
|
16
|
+
def initialize(input)
|
17
|
+
@input = input
|
18
|
+
@result = LineCollection.new
|
19
|
+
@in_parens = false
|
20
|
+
parse!
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def parse!
|
26
|
+
@input.chars do |char|
|
27
|
+
case char
|
28
|
+
when '('
|
29
|
+
parse_open_paren
|
30
|
+
when ')'
|
31
|
+
parse_open_close_paren
|
32
|
+
when '{'
|
33
|
+
parse_open_bracket
|
34
|
+
when '}'
|
35
|
+
parse_close_bracket
|
36
|
+
when ':'
|
37
|
+
parse_colon
|
38
|
+
when ','
|
39
|
+
parse_comma
|
40
|
+
when ' '
|
41
|
+
parse_space
|
42
|
+
else
|
43
|
+
parse_character char
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_open_paren
|
49
|
+
@in_parens = true
|
50
|
+
new_string << '('
|
51
|
+
new_string color: :light_yellow
|
52
|
+
end
|
53
|
+
|
54
|
+
def parse_open_close_paren
|
55
|
+
@in_parens = false
|
56
|
+
new_string << ')'
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse_open_bracket
|
60
|
+
current_string << ' ' unless last_character == ' '
|
61
|
+
new_string << '{'
|
62
|
+
new_line indent_level: current_indent_level + 1
|
63
|
+
new_string color: :light_blue
|
64
|
+
end
|
65
|
+
|
66
|
+
def parse_close_bracket
|
67
|
+
current_string << ' ' unless last_character == ' ' || current_line.empty?
|
68
|
+
new_line indent_level: current_indent_level - 1
|
69
|
+
new_string << '}'
|
70
|
+
new_line
|
71
|
+
end
|
72
|
+
|
73
|
+
def parse_colon
|
74
|
+
new_string << ':'
|
75
|
+
new_string color: in_parens ? :yellow : :blue
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse_comma
|
79
|
+
(last_character == '}' ? previous_string : new_string) << ','
|
80
|
+
if in_parens
|
81
|
+
new_string(color: :light_yellow)
|
82
|
+
else
|
83
|
+
new_line unless current_line.empty?
|
84
|
+
new_string(color: :light_blue)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def parse_space
|
89
|
+
if current_indent_level == 0
|
90
|
+
new_color =
|
91
|
+
case current_color
|
92
|
+
when :light_green
|
93
|
+
:light_magenta
|
94
|
+
when :light_magenta
|
95
|
+
nil
|
96
|
+
when nil
|
97
|
+
:light_cyan
|
98
|
+
else
|
99
|
+
current_color
|
100
|
+
end
|
101
|
+
new_string color: new_color
|
102
|
+
end
|
103
|
+
current_string << ' ' unless current_line.empty?
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse_character(char)
|
107
|
+
current_string << ' ' if in_parens && %w{: ,}.include?(last_character)
|
108
|
+
new_string color: :light_green if current_indent_level == 0 && current_line.empty?
|
109
|
+
current_string.color = :magenta if current_string.end_with? '...'
|
110
|
+
current_string << char
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graphql-formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandfolder, Inc.
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- developers@brandfolder.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- graphql-formatter.gemspec
|
73
|
+
- lib/graphql-formatter.rb
|
74
|
+
- lib/graphql-formatter/character_string.rb
|
75
|
+
- lib/graphql-formatter/line_collection.rb
|
76
|
+
- lib/graphql-formatter/line_of_character_strings.rb
|
77
|
+
- lib/graphql-formatter/version.rb
|
78
|
+
homepage: https://github.com/brandfolder/graphql-formatter
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.8
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Pretty prints GraphQL queries
|
102
|
+
test_files: []
|