prmd 0.6.1 → 0.7.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.
Files changed (63) hide show
  1. checksums.yaml +5 -13
  2. data/CONTRIBUTORS.md +8 -0
  3. data/Gemfile.lock +7 -7
  4. data/README.md +57 -8
  5. data/Rakefile +4 -4
  6. data/bin/prmd +3 -117
  7. data/docs/schemata.md +11 -11
  8. data/lib/prmd/cli/base.rb +151 -0
  9. data/lib/prmd/cli/combine.rb +42 -0
  10. data/lib/prmd/cli/doc.rb +69 -0
  11. data/lib/prmd/cli/generate.rb +44 -0
  12. data/lib/prmd/cli/render.rb +48 -0
  13. data/lib/prmd/cli/verify.rb +49 -0
  14. data/lib/prmd/cli.rb +108 -0
  15. data/lib/prmd/commands/combine.rb +85 -58
  16. data/lib/prmd/commands/init.rb +30 -98
  17. data/lib/prmd/commands/render.rb +30 -17
  18. data/lib/prmd/commands/verify.rb +78 -35
  19. data/lib/prmd/commands.rb +4 -0
  20. data/lib/prmd/core/combiner.rb +91 -0
  21. data/lib/prmd/core/generator.rb +27 -0
  22. data/lib/prmd/core/renderer.rb +56 -0
  23. data/lib/prmd/core/schema_hash.rb +47 -0
  24. data/lib/prmd/core_ext/optparse.rb +6 -0
  25. data/lib/prmd/hash_helpers.rb +38 -0
  26. data/lib/prmd/load_schema_file.rb +25 -0
  27. data/lib/prmd/rake_tasks/base.rb +33 -0
  28. data/lib/prmd/rake_tasks/combine.rb +50 -0
  29. data/lib/prmd/rake_tasks/doc.rb +73 -0
  30. data/lib/prmd/rake_tasks/verify.rb +60 -0
  31. data/lib/prmd/schema.rb +86 -34
  32. data/lib/prmd/template.rb +65 -8
  33. data/lib/prmd/templates/combine_head.json +6 -0
  34. data/lib/prmd/templates/init_default.json +9 -0
  35. data/lib/prmd/templates/init_resource.json.erb +90 -0
  36. data/lib/prmd/templates/link_schema_properties.md.erb +5 -0
  37. data/lib/prmd/templates/schema.erb +2 -2
  38. data/lib/prmd/templates/schemata/helper.erb +29 -15
  39. data/lib/prmd/templates/schemata/link.md.erb +74 -0
  40. data/lib/prmd/templates/schemata/{link_curl_example.erb → link_curl_example.md.erb} +9 -2
  41. data/lib/prmd/templates/schemata.md.erb +37 -0
  42. data/lib/prmd/url_generator.rb +11 -69
  43. data/lib/prmd/url_generators/generators/default.rb +66 -0
  44. data/lib/prmd/url_generators/generators/json.rb +30 -0
  45. data/lib/prmd/version.rb +10 -1
  46. data/lib/prmd.rb +7 -18
  47. data/prmd.gemspec +15 -15
  48. data/test/cli/combine_test.rb +23 -0
  49. data/test/cli/doc_test.rb +25 -0
  50. data/test/cli/generate_test.rb +23 -0
  51. data/test/cli/render_test.rb +25 -0
  52. data/test/cli/verify_test.rb +21 -0
  53. data/test/commands/init_test.rb +7 -0
  54. data/test/commands/render_test.rb +93 -0
  55. data/test/commands/verify_test.rb +60 -60
  56. data/test/helpers.rb +61 -6
  57. data/test/schema_test.rb +17 -11
  58. data/test/schemata/input/doc-settings.json +4 -0
  59. metadata +73 -28
  60. data/lib/prmd/commands/expand.rb +0 -108
  61. data/lib/prmd/templates/link_schema_properties.erb +0 -16
  62. data/lib/prmd/templates/schemata/link.erb +0 -61
  63. data/lib/prmd/templates/schemata.erb +0 -47
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MGRjMWI5YWQ0MWVmZDNiZWM5YTE5MGIwYmIzMTZiYjI1MzMzNGYzNQ==
5
- data.tar.gz: !binary |-
6
- YWM0MzYxYWYwNmZiOTdjZDI5MWU5OGZjNjcxNTY5N2JjNTU5MTU2Mg==
2
+ SHA1:
3
+ metadata.gz: 92c67299c0bc9e4bec5141b516ec7fa78d841f6d
4
+ data.tar.gz: dbcdf2ec6712b37f5980904e998d7085d3ca6dc9
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NDQ2NzlhN2RkOWNlODRhNzRiYTM5MGJhN2U3MDlmNTJmZjFmNjFjNDU3MDFk
10
- YmM4MThkNjQ4ZmY3NjhmZmZkMzNlOTNmYTVhOGFmOWRkMjM1ZGUzZjBmNGNm
11
- NjMyZTRjODQ5ZmFhZjZhMDkwZjAxNmViNTM5NjA2YTY5ZjBkZTc=
12
- data.tar.gz: !binary |-
13
- NjJhY2I3ZTNjZWU3ODdjNDdkM2UxODhlODg3NDk1ZDdhNzdkOGQzNmVjM2M2
14
- MzY1OWY0MmI0ODBmZDI5NDBkYmQ2ZjY4ZmZiZDA0OWM0M2YxY2ZmMDIwYjg4
15
- NDI1NjFlZDBjYWMwOGNkMGNiZmQ0OWY4NTI5ZDBhMzM1NTg5NzQ=
6
+ metadata.gz: bc6d901ec87dc0df4882265c7d5f95210f5bcf2777dda10870366547aabe9b42a90adc8007fa872b925f35ac1b192237a1c2eaebe8464f3b4083cad8405e90a5
7
+ data.tar.gz: 2b117de96b7c80c06db4e7173d3954265531ab3f70df26512118854ea6025068e3d0852cc615f1c79a5da84bd5609a4c9ba95824f5292fef9ca41cfd393db06d
data/CONTRIBUTORS.md CHANGED
@@ -1,14 +1,20 @@
1
1
  * Adel Qalieh <aqalieh95@gmail.com>
2
+ * Alex Sergeyev <alex.sergeyev@gmail.com>
3
+ * Arnold <arnoldcano@yahoo.com>
2
4
  * Brandur <brandur@heroku.com>
3
5
  * Brandur <brandur@mutelight.org>
4
6
  * Chris Continanza <christopher.continanza@gmail.com>
7
+ * Corey Powell <mistdragon100@gmail.com>
5
8
  * Dan Peterson <dpiddy@gmail.com>
6
9
  * Dane Harrigan <dane.harrigan@gmail.com>
7
10
  * Eric J. Holmes <eric@ejholmes.net>
8
11
  * Ernesto Jiménez <me@ernesto-jimenez.com>
12
+ * Jason Rudolph <github@jasonrudolph.com>
13
+ * Juan Pablo Buritica <juanpablo@buritica.org>
9
14
  * Justin Halsall <justin@juice10.com>
10
15
  * Kousuke Ebihara <Kosuke_Ebihara@voyagegroup.com>
11
16
  * Kousuke Ebihara <kousuke@co3k.org>
17
+ * Lucas Carvalho <lucas.carvalho@corp.globo.com>
12
18
  * Mark McGranaghan <mmcgrana@gmail.com>
13
19
  * Martin Seeler <developer@chasmo.de>
14
20
  * Matthew Conway <himself@mattonrails.com>
@@ -16,4 +22,6 @@
16
22
  * Scott Clasen <scott@heroku.com>
17
23
  * Timothée Peignier <timothee.peignier@tryphon.org>
18
24
  * Wesley Beary <geemus+github@gmail.com>
25
+ * Wesley Beary <geemus@gmail.com>
26
+ * Zack Shapiro <zack.shapiro@kontagent.com>
19
27
  * geemus <geemus@gmail.com>
data/Gemfile.lock CHANGED
@@ -1,23 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prmd (0.6.1)
4
+ prmd (0.6.2)
5
5
  erubis (~> 2.7)
6
- json_schema (~> 0.1)
6
+ json_schema (~> 0.3, >= 0.3.1)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  erubis (2.7.0)
12
- json_schema (0.1.4)
13
- minitest (5.3.2)
14
- rake (10.2.2)
12
+ json_schema (0.3.1)
13
+ minitest (5.4.2)
14
+ rake (10.3.2)
15
15
 
16
16
  PLATFORMS
17
17
  ruby
18
18
 
19
19
  DEPENDENCIES
20
20
  bundler (~> 1.3)
21
- minitest (~> 5.3)
21
+ minitest (~> 5.4)
22
22
  prmd!
23
- rake (~> 10.2)
23
+ rake (~> 10.3)
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Prmd
1
+ # Prmd [![Travis Status](https://travis-ci.org/interagent/prmd.svg)](https://travis-ci.org/interagent/prmd)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/prmd.svg)](http://badge.fury.io/rb/prmd)
2
4
 
3
5
  JSON Schema tooling: scaffold, verify, and generate documentation
4
6
  from JSON Schema documents.
@@ -21,7 +23,7 @@ described in [/docs/schemata.md](/docs/schemata.md).
21
23
 
22
24
  Install the command-line tool with:
23
25
 
24
- ```console
26
+ ```bash
25
27
  $ gem install prmd
26
28
  ```
27
29
 
@@ -32,7 +34,7 @@ to the application's Gemfile:
32
34
  gem 'prmd'
33
35
  ```
34
36
 
35
- ```console
37
+ ```bash
36
38
  $ bundle install
37
39
  ```
38
40
 
@@ -48,7 +50,7 @@ Prmd provides four main commands:
48
50
 
49
51
  Here's an example of using these commands in a typical workflow:
50
52
 
51
- ```console
53
+ ```bash
52
54
  # Fill out the resource schemata
53
55
  $ mkdir -p schemata
54
56
  $ prmd init app > schemata/app.json
@@ -80,25 +82,72 @@ $ prmd doc schema.json > schema.md
80
82
 
81
83
  # Render from schema
82
84
 
83
- ```console
85
+ ```bash
84
86
  $ prmd render --template schemata.erb schema.json > schema.md
85
87
  ```
86
88
 
87
89
  Typically you'll want to prepend header and overview information to
88
90
  your API documentation. You can do this with the `--prepend` flag:
89
91
 
90
- ```console
91
- $ prmd doc schema.json --prepend overview.md > schema.md
92
+ ```bash
93
+ $ prmd doc --prepend overview.md schema.json > schema.md
92
94
  ```
93
95
 
94
96
  You can also chain commands together as needed, e.g.:
95
97
 
96
- ```console
98
+ ```bash
97
99
  $ prmd combine --meta meta.json schemata/ | prmd verify | prmd doc --prepend overview.md > schema.md
98
100
  ```
99
101
 
100
102
  See `prmd <command> --help` for additional usage details.
101
103
 
104
+ ## Documentation rendering settings
105
+
106
+ Out of the box you can supply a settings file (in either `JSON` or `YAML`) that will tweak the layout of your documentation.
107
+
108
+ ```bash
109
+ $ prmd doc --settings config.json schema.json > schema.md
110
+ ```
111
+
112
+ Available options (and their defaults)
113
+ ```json
114
+ {
115
+ "doc": {
116
+ "url_style": "default", // can also be "json"
117
+ "disable_title_and_description": false // remove the title and the description, useful when using your own custom header
118
+ }
119
+ }
120
+ ```
121
+
122
+ ## Use as rake task
123
+
124
+ In addition, prmd can be used via rake tasks
125
+
126
+ ```ruby
127
+ # Rakefile
128
+ require 'prmd/rake_tasks/combine'
129
+ require 'prmd/rake_tasks/verify'
130
+ require 'prmd/rake_tasks/doc'
131
+
132
+ namespace :schema do
133
+ Prmd::RakeTasks::Combine.new do |t|
134
+ t.options[:meta] = 'schema/meta.json'
135
+ t.paths << 'schema/schemata/api'
136
+ t.output_file = 'schema/api.json'
137
+ end
138
+
139
+ Prmd::RakeTasks::Verify.new do |t|
140
+ t.files << 'schema/api.json'
141
+ end
142
+
143
+ Prmd::RakeTasks::Doc.new do |t|
144
+ t.files = { 'schema/api.json' => 'schema/api.md' }
145
+ end
146
+ end
147
+
148
+ task default: ['schema:combine', 'schema:verify', 'schema:doc']
149
+ ```
150
+
102
151
  ## File Layout
103
152
 
104
153
  We suggest the following file layout for JSON schema related files:
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.pattern = "test/**/*_test.rb"
5
+ t.pattern = 'test/**/*_test.rb'
6
6
  end
7
7
 
8
- task :default => :test
8
+ task default: :test
data/bin/prmd CHANGED
@@ -1,120 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'optparse'
3
- require(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'prmd')))
3
+ require 'prmd'
4
+ require 'prmd/cli'
4
5
 
5
- options = {}
6
-
7
- commands = {
8
- combine: OptionParser.new do |opts|
9
- opts.banner = "prmd combine [options] <file or directory>"
10
- opts.on("-m", "--meta meta.json", "Set defaults for schemata") do |m|
11
- options[:meta] = m
12
- end
13
- end,
14
- doc: OptionParser.new do |opts|
15
- opts.banner = "prmd doc [options] <combined schema>"
16
- opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p|
17
- options[:prepend] = p
18
- end
19
- opts.on("-u", "--url-style json", String, "URL style to output (default/json)") do |s|
20
- options[:style] = s
21
- end
22
- opts.on("-c", "--content-type application/json", String, "Content-Type header") do |c|
23
- options[:content_type] = c
24
- end
25
- end,
26
- init: OptionParser.new do |opts|
27
- opts.banner = "prmd init [options] <resource name>"
28
- opts.on("-y", "--yaml", "Generate YAML") do |y|
29
- options[:yaml] = y
30
- end
31
- end,
32
- render: OptionParser.new do |opts|
33
- opts.banner = "prmd render [options] <combined schema>"
34
- opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p|
35
- options[:prepend] = p
36
- end
37
- opts.on("-t", "--template templates", String, "Use alternate template") do |t|
38
- options[:template] = t
39
- end
40
- end,
41
- verify: OptionParser.new do |opts|
42
- opts.banner = "prmd verify [options] <combined schema>"
43
- end
44
- }
45
-
46
- help_text = commands.values.map do |command|
47
- " #{command.banner}"
48
- end.join("\n")
49
-
50
- global = OptionParser.new do |opts|
51
- opts.banner = "Usage: prmd [options] [command [options]]"
52
- opts.separator "\nAvailable options:"
53
- opts.on("--version", "Return version") do |opts|
54
- puts "prmd #{Prmd::VERSION}"
55
- exit(0)
56
- end
57
- opts.separator "\nAvailable commands:"
58
- opts.separator help_text
59
- end
60
-
61
- if ARGV.empty?
62
- puts global
63
- exit(1)
64
- end
65
- global.order!
66
-
67
- command = ARGV.shift.to_sym
68
- option = commands[command]
69
- if option.nil?
70
- puts global
71
- exit(1)
72
- end
73
-
74
- if ARGV.empty? && $stdin.tty?
75
- puts option
76
- exit(1)
77
- end
78
- option.order!
79
-
80
- case command
81
- when :combine
82
- puts Prmd.combine(ARGV[0], options).to_s
83
- when :doc
84
- data = unless $stdin.tty?
85
- $stdin.read
86
- else
87
- File.read(ARGV[0])
88
- end
89
- schema = Prmd::Schema.new(JSON.parse(data))
90
-
91
- options[:template] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'prmd', 'templates'))
92
-
93
- puts Prmd.render(schema, options)
94
- when :init
95
- puts Prmd.init(ARGV[0], options)
96
- when :render
97
- data = unless $stdin.tty?
98
- $stdin.read
99
- else
100
- File.read(ARGV[0])
101
- end
102
- schema = Prmd::Schema.new(JSON.parse(data))
103
- puts Prmd.render(schema, options)
104
- when :verify
105
- data, errors = '', []
106
- unless $stdin.tty?
107
- data = $stdin.read
108
- errors.concat(Prmd.verify(JSON.parse(data)))
109
- else
110
- data = JSON.parse(File.read(ARGV[0]))
111
- Prmd.verify(data).each do |error|
112
- errors << "#{ARGV[0]}: #{error}"
113
- end
114
- end
115
- errors.each do |error|
116
- $stderr.puts(error)
117
- end
118
- exit(1) unless errors.empty?
119
- puts(data) unless $stdout.tty?
120
- end
6
+ Prmd::CLI.run(ARGV.dup, bin: File.basename(__FILE__))
data/docs/schemata.md CHANGED
@@ -18,7 +18,7 @@ We have opted to split apart the schema into individual resource schema and a ro
18
18
  Each schema MUST include some meta-data, which we cluster at the top of the file, including:
19
19
 
20
20
  * `description` - a description of the resource described by the schema
21
- * `id` - an id for this schema, it MUST be in the form `"schema/#{lower_case_singular_resource}"`
21
+ * `id` - an id for this schema, it MUST be in the form `"schemata/#{lower_case_singular_resource}"`
22
22
  * `$schema` - defines what meta-schema is in use, it MUST be `http://json-schema.org/draft-04/hyper-schema`
23
23
  * `title` - title for this resource, it MUST be in the form `"#{title_case_API_name} - #{title_case_plural_resource}"`
24
24
  * `type` - the type(s) of this schema, it MUST be `["object"]`
@@ -74,7 +74,7 @@ The links array MUST include an object defining each action available. Each acti
74
74
 
75
75
  * `description` - a description of the action to perform
76
76
  * `href` - the path associated with this action, use [URI templates](http://tools.ietf.org/html/rfc6570) as needed, CGI escaping any JSON pointer values used for identity
77
- * `method` - the http method to be used with this actio
77
+ * `method` - the http method to be used with this action
78
78
  * `rel` - describes relation of link to resource, SHOULD be one of `["create", "destroy", "self", "instances", "update"]`
79
79
  * `title` - title for the link
80
80
 
@@ -94,8 +94,8 @@ If this field is not present, all attributes in this link are considered as opti
94
94
  "rel": "create",
95
95
  "schema": {
96
96
  "properties": {
97
- "owner": { "$ref": "/schema/user#/definitions/identity" },
98
- "url": { "$ref": "/schema/resource/definitions/url" }
97
+ "owner": { "$ref": "/schemata/user#/definitions/identity" },
98
+ "url": { "$ref": "/schemata/resource/definitions/url" }
99
99
  },
100
100
  "required": [ "owner", "url" ]
101
101
  },
@@ -103,14 +103,14 @@ If this field is not present, all attributes in this link are considered as opti
103
103
  },
104
104
  {
105
105
  "description": "Delete an existing resource.",
106
- "href": "/resources/{(%2Fschema%2Fresources%23%2Fdefinitions%2Fidentity)}",
106
+ "href": "/resources/{(%2Fschemata%2Fresources%23%2Fdefinitions%2Fidentity)}",
107
107
  "method": "DELETE",
108
108
  "rel": "destroy",
109
109
  "title": "Delete"
110
110
  },
111
111
  {
112
112
  "description": "Info for existing resource.",
113
- "href": "/resources/{(%2Fschema%2Fresources%23%2Fdefinitions%2Fidentity)}",
113
+ "href": "/resources/{(%2Fschemata%2Fresources%23%2Fdefinitions%2Fidentity)}",
114
114
  "method": "GET",
115
115
  "rel": "self",
116
116
  "title": "Info"
@@ -124,12 +124,12 @@ If this field is not present, all attributes in this link are considered as opti
124
124
  },
125
125
  {
126
126
  "description": "Update an existing resource.",
127
- "href": "/resources/{(%2Fschema%2Fresource%23%2Fdefinitions%2Fidentity)}",
127
+ "href": "/resources/{(%2Fschemata%2Fresource%23%2Fdefinitions%2Fidentity)}",
128
128
  "method": "PATCH",
129
129
  "rel": "update",
130
130
  "schema": {
131
131
  "properties": {
132
- "url": { "$ref": "/schema/resource/definitions/url" }
132
+ "url": { "$ref": "/schemata/resource/definitions/url" }
133
133
  }
134
134
  },
135
135
  "title": "Update"
@@ -146,15 +146,15 @@ The properties object MUST contain all the serialized attributes for the object.
146
146
  ```javascript
147
147
  {
148
148
  "properties": {
149
- "id": { "$ref": "/schema/resource#/definitions/id" },
149
+ "id": { "$ref": "/schemata/resource#/definitions/id" },
150
150
  "owner": {
151
151
  "description": "unique identifier of the user who owns this resource",
152
152
  "properties": {
153
- "id": { "$ref": "/schema/user#/definitions/id" }
153
+ "id": { "$ref": "/schemata/user#/definitions/id" }
154
154
  },
155
155
  "type": ["object"]
156
156
  },
157
- "url": { "$ref": "/schema/resource#/definitions/url" }
157
+ "url": { "$ref": "/schemata/resource#/definitions/url" }
158
158
  }
159
159
  }
160
160
  ```
@@ -0,0 +1,151 @@
1
+ require 'json'
2
+ require 'prmd/core_ext/optparse'
3
+ require 'prmd/load_schema_file'
4
+
5
+ module Prmd
6
+ module CLI
7
+ # Base module for CLI commands.
8
+ # @api
9
+ module Base
10
+ # Create a parser specific for this command.
11
+ # The parsers produced by this method should yield their options.
12
+ #
13
+ # @example Overwriting
14
+ # def make_parser(options = {})
15
+ # OptionParser.new do |opts|
16
+ # opts.on("-v", "--verbose", "set verbose debugging") do |v|
17
+ # yield :verbose, v
18
+ # end
19
+ # end
20
+ # end
21
+ #
22
+ # @param [Hash<Symbol, Object>] options
23
+ # @return [OptionParser] newly created parser
24
+ # @abstract
25
+ def make_parser(options = {})
26
+ #
27
+ end
28
+
29
+ # Runs the provided parser with the provided argv.
30
+ # This method can be overwritten to use a different parser method.
31
+ #
32
+ # @param [OptionParser] parser
33
+ # @param [Array<String>] argv
34
+ # @return [Array<String>] remaining arguments
35
+ # @private
36
+ def execute_parser(parser, argv)
37
+ parser.parse(argv)
38
+ end
39
+
40
+ # Set the given key and value in the given options Hash.
41
+ # This method can ben overwritten to support special keys or values
42
+ #
43
+ # @example Handling special keys
44
+ # def self.set_option(options, key, value)
45
+ # if key == :settings
46
+ # options.replace(value.merge(options))
47
+ # else
48
+ # super
49
+ # end
50
+ # end
51
+ #
52
+ # @param [Hash<Symbol, Object>] options
53
+ # @param [Symbol] key
54
+ # @param [Object] value
55
+ # @return [void]
56
+ def set_option(options, key, value)
57
+ options[key] = value
58
+ end
59
+
60
+ # Parse the given argv and produce a options Hash specific to the command.
61
+ # The returned options Hash will include an :argv key which contains
62
+ # the remaining args from the parse operation.
63
+ #
64
+ # @param [Array<String>] argv
65
+ # @param [Hash<Symbol, Object>] options
66
+ # @return [Hash<Symbol, Object>] parsed options
67
+ def parse_options(argv, options = {})
68
+ opts = {}
69
+ parser = make_parser(options) do |key, value|
70
+ set_option(opts, key, value)
71
+ end
72
+ argv = execute_parser(parser, argv)
73
+ opts[:argv] = argv
74
+ opts
75
+ end
76
+
77
+ # Helper method for writing command results to a file or STD* IO.
78
+ #
79
+ # @param [String] data to be written
80
+ # @param [Hash<Symbol, Object>] options
81
+ # @return [void]
82
+ def write_result(data, options = {})
83
+ output_file = options[:output_file]
84
+ if output_file
85
+ File.open(output_file, 'w') do |f|
86
+ f.write(data)
87
+ end
88
+ else
89
+ $stdout.puts data
90
+ end
91
+ end
92
+
93
+ # Helper method for reading schema data from a file or STD* IO.
94
+ #
95
+ # @param [String] filename file to read
96
+ # @return [Array[Symbol, String]] source, data
97
+ def try_read(filename = nil)
98
+ if filename && !filename.empty?
99
+ return :file, Prmd.load_schema_file(filename)
100
+ elsif !$stdin.tty?
101
+ return :io, JSON.load($stdin.read)
102
+ else
103
+ abort 'Nothing to read'
104
+ end
105
+ end
106
+
107
+ # Method called to actually execute the command provided with an
108
+ # options Hash from the commands #parse_options.
109
+ #
110
+ # @param [Hash<Symbol, Object>] options
111
+ # @return [void]
112
+ # @abstract
113
+ def execute(options = {})
114
+ #
115
+ end
116
+
117
+ # Method called when the command is ran with the :noop option enabled.
118
+ # As the option implies, this should do absolutely nothing.
119
+ #
120
+ # @param [Hash<Symbol, Object>] options
121
+ # @return [void]
122
+ def noop_execute(options = {})
123
+ $stderr.puts options
124
+ end
125
+
126
+ # Run this command given a argv and optional options Hash.
127
+ # If all you have is the options from the #parse_options method, use
128
+ # #execute instead.
129
+ #
130
+ # @see #execute
131
+ # @see #parse_options
132
+ #
133
+ # @param [Array<String>] argv
134
+ # @param [Hash<Symbol, Object>] options
135
+ # @return [void]
136
+ def run(argv, options = {})
137
+ options = options.merge(parse_options(argv, options))
138
+ if options[:noop]
139
+ noop_execute(options)
140
+ else
141
+ execute(options)
142
+ end
143
+ end
144
+
145
+ private :execute_parser
146
+ private :set_option
147
+ private :write_result
148
+ private :try_read
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,42 @@
1
+ require 'prmd/cli/base'
2
+ require 'prmd/commands/combine'
3
+
4
+ module Prmd
5
+ module CLI
6
+ # 'combine' command module.
7
+ module Combine
8
+ extend CLI::Base
9
+
10
+ # Returns a OptionParser for parsing 'combine' command options.
11
+ #
12
+ # @param (see Prmd::CLI::Base#make_parser)
13
+ # @return (see Prmd::CLI::Base#make_parser)
14
+ def self.make_parser(options = {})
15
+ binname = options.fetch(:bin, 'prmd')
16
+
17
+ OptionParser.new do |opts|
18
+ opts.banner = "#{binname} combine [options] <file or directory>"
19
+ opts.on('-m', '--meta FILENAME', String, 'Set defaults for schemata') do |m|
20
+ yield :meta, m
21
+ end
22
+ opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
23
+ yield :output_file, n
24
+ end
25
+ end
26
+ end
27
+
28
+ # Executes the 'combine' command.
29
+ #
30
+ # @example Usage
31
+ # Prmd::CLI::Combine.execute(argv: ['schema/schemata/api'],
32
+ # meta: 'schema/meta.json',
33
+ # output_file: 'schema/api.json')
34
+ #
35
+ # @param (see Prmd::CLI::Base#execute)
36
+ # @return (see Prmd::CLI::Base#execute)
37
+ def self.execute(options = {})
38
+ write_result Prmd.combine(options[:argv], options).to_s, options
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,69 @@
1
+ require 'prmd/cli/base'
2
+ require 'prmd/commands/render'
3
+ require 'prmd/hash_helpers'
4
+
5
+ module Prmd
6
+ module CLI
7
+ # 'doc' command module.
8
+ module Doc
9
+ extend CLI::Base
10
+
11
+ # Returns a OptionParser for parsing 'doc' command options.
12
+ #
13
+ # @param (see Prmd::CLI::Base#make_parser)
14
+ # @return (see Prmd::CLI::Base#make_parser)
15
+ def self.make_parser(options = {})
16
+ binname = options.fetch(:bin, 'prmd')
17
+
18
+ OptionParser.new do |opts|
19
+ opts.banner = "#{binname} doc [options] <combined schema>"
20
+ opts.on('-s', '--settings FILENAME', String, 'Config file to use') do |s|
21
+ settings = Prmd.load_schema_file(s) || {}
22
+ options = HashHelpers.deep_symbolize_keys(settings)
23
+ yield :settings, options
24
+ end
25
+ opts.on('-p', '--prepend header,overview', Array, 'Prepend files to output') do |p|
26
+ yield :prepend, p
27
+ end
28
+ opts.on('-c', '--content-type application/json', String, 'Content-Type header') do |c|
29
+ yield :content_type, c
30
+ end
31
+ opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
32
+ yield :output_file, n
33
+ end
34
+ end
35
+ end
36
+
37
+ # Overwritten to support :settings merging.
38
+ #
39
+ # @see Prmd::CLI::Base#set_option
40
+ #
41
+ # @param (see Prmd::CLI::Base#set_option)
42
+ # @return (see Prmd::CLI::Base#set_option)
43
+ def self.set_option(options, key, value)
44
+ if key == :settings
45
+ options.replace(value.merge(options))
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ # Executes the 'doc' command.
52
+ #
53
+ # @example Usage
54
+ # Prmd::CLI::Doc.execute(argv: ['schema/api.json'],
55
+ # output_file: 'schema/api.md')
56
+ #
57
+ # @param (see Prmd::CLI::Base#execute)
58
+ # @return (see Prmd::CLI::Base#execute)
59
+ def self.execute(options = {})
60
+ filename = options.fetch(:argv).first
61
+ template = File.expand_path('templates', File.dirname(__FILE__))
62
+ _, data = try_read(filename)
63
+ schema = Prmd::Schema.new(data)
64
+ opts = options.merge(template: template)
65
+ write_result Prmd.render(schema, opts), options
66
+ end
67
+ end
68
+ end
69
+ end