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.
- checksums.yaml +5 -13
- data/CONTRIBUTORS.md +8 -0
- data/Gemfile.lock +7 -7
- data/README.md +57 -8
- data/Rakefile +4 -4
- data/bin/prmd +3 -117
- data/docs/schemata.md +11 -11
- data/lib/prmd/cli/base.rb +151 -0
- data/lib/prmd/cli/combine.rb +42 -0
- data/lib/prmd/cli/doc.rb +69 -0
- data/lib/prmd/cli/generate.rb +44 -0
- data/lib/prmd/cli/render.rb +48 -0
- data/lib/prmd/cli/verify.rb +49 -0
- data/lib/prmd/cli.rb +108 -0
- data/lib/prmd/commands/combine.rb +85 -58
- data/lib/prmd/commands/init.rb +30 -98
- data/lib/prmd/commands/render.rb +30 -17
- data/lib/prmd/commands/verify.rb +78 -35
- data/lib/prmd/commands.rb +4 -0
- data/lib/prmd/core/combiner.rb +91 -0
- data/lib/prmd/core/generator.rb +27 -0
- data/lib/prmd/core/renderer.rb +56 -0
- data/lib/prmd/core/schema_hash.rb +47 -0
- data/lib/prmd/core_ext/optparse.rb +6 -0
- data/lib/prmd/hash_helpers.rb +38 -0
- data/lib/prmd/load_schema_file.rb +25 -0
- data/lib/prmd/rake_tasks/base.rb +33 -0
- data/lib/prmd/rake_tasks/combine.rb +50 -0
- data/lib/prmd/rake_tasks/doc.rb +73 -0
- data/lib/prmd/rake_tasks/verify.rb +60 -0
- data/lib/prmd/schema.rb +86 -34
- data/lib/prmd/template.rb +65 -8
- data/lib/prmd/templates/combine_head.json +6 -0
- data/lib/prmd/templates/init_default.json +9 -0
- data/lib/prmd/templates/init_resource.json.erb +90 -0
- data/lib/prmd/templates/link_schema_properties.md.erb +5 -0
- data/lib/prmd/templates/schema.erb +2 -2
- data/lib/prmd/templates/schemata/helper.erb +29 -15
- data/lib/prmd/templates/schemata/link.md.erb +74 -0
- data/lib/prmd/templates/schemata/{link_curl_example.erb → link_curl_example.md.erb} +9 -2
- data/lib/prmd/templates/schemata.md.erb +37 -0
- data/lib/prmd/url_generator.rb +11 -69
- data/lib/prmd/url_generators/generators/default.rb +66 -0
- data/lib/prmd/url_generators/generators/json.rb +30 -0
- data/lib/prmd/version.rb +10 -1
- data/lib/prmd.rb +7 -18
- data/prmd.gemspec +15 -15
- data/test/cli/combine_test.rb +23 -0
- data/test/cli/doc_test.rb +25 -0
- data/test/cli/generate_test.rb +23 -0
- data/test/cli/render_test.rb +25 -0
- data/test/cli/verify_test.rb +21 -0
- data/test/commands/init_test.rb +7 -0
- data/test/commands/render_test.rb +93 -0
- data/test/commands/verify_test.rb +60 -60
- data/test/helpers.rb +61 -6
- data/test/schema_test.rb +17 -11
- data/test/schemata/input/doc-settings.json +4 -0
- metadata +73 -28
- data/lib/prmd/commands/expand.rb +0 -108
- data/lib/prmd/templates/link_schema_properties.erb +0 -16
- data/lib/prmd/templates/schemata/link.erb +0 -61
- data/lib/prmd/templates/schemata.erb +0 -47
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'prmd/cli/base'
|
|
2
|
+
require 'prmd/commands/init'
|
|
3
|
+
|
|
4
|
+
module Prmd
|
|
5
|
+
module CLI
|
|
6
|
+
# 'init' command module.
|
|
7
|
+
# Though this is called, Generate, it is used by the init method for
|
|
8
|
+
# creating new Schema files
|
|
9
|
+
module Generate
|
|
10
|
+
extend CLI::Base
|
|
11
|
+
|
|
12
|
+
# Returns a OptionParser for parsing 'init' command options.
|
|
13
|
+
#
|
|
14
|
+
# @param (see Prmd::CLI::Base#make_parser)
|
|
15
|
+
# @return (see Prmd::CLI::Base#make_parser)
|
|
16
|
+
def self.make_parser(options = {})
|
|
17
|
+
binname = options.fetch(:bin, 'prmd')
|
|
18
|
+
|
|
19
|
+
OptionParser.new do |opts|
|
|
20
|
+
opts.banner = "#{binname} init [options] <resource name>"
|
|
21
|
+
opts.on('-y', '--yaml', 'Generate YAML') do |y|
|
|
22
|
+
yield :yaml, y
|
|
23
|
+
end
|
|
24
|
+
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
|
|
25
|
+
yield :output_file, n
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Executes the 'init' command.
|
|
31
|
+
#
|
|
32
|
+
# @example Usage
|
|
33
|
+
# Prmd::CLI::Generate.execute(argv: ['bread'],
|
|
34
|
+
# output_file: 'schema/schemata/bread.json')
|
|
35
|
+
#
|
|
36
|
+
# @param (see Prmd::CLI::Base#execute)
|
|
37
|
+
# @return (see Prmd::CLI::Base#execute)
|
|
38
|
+
def self.execute(options = {})
|
|
39
|
+
name = options.fetch(:argv).first
|
|
40
|
+
write_result Prmd.init(name, options), options
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require 'prmd/cli/base'
|
|
2
|
+
require 'prmd/commands/render'
|
|
3
|
+
|
|
4
|
+
module Prmd
|
|
5
|
+
module CLI
|
|
6
|
+
# 'render' command module.
|
|
7
|
+
module Render
|
|
8
|
+
extend CLI::Base
|
|
9
|
+
|
|
10
|
+
# Returns a OptionParser for parsing 'render' 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} render [options] <combined schema>"
|
|
19
|
+
opts.on('-p', '--prepend header,overview', Array, 'Prepend files to output') do |p|
|
|
20
|
+
yield :prepend, p
|
|
21
|
+
end
|
|
22
|
+
opts.on('-t', '--template templates', String, 'Use alternate template') do |t|
|
|
23
|
+
yield :template, t
|
|
24
|
+
end
|
|
25
|
+
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
|
|
26
|
+
yield :output_file, n
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Executes the 'render' command.
|
|
32
|
+
#
|
|
33
|
+
# @example Usage
|
|
34
|
+
# Prmd::CLI::Render.execute(argv: ['schema/api.json'],
|
|
35
|
+
# template: 'my_template.md.erb',
|
|
36
|
+
# output_file: 'schema/api.md')
|
|
37
|
+
#
|
|
38
|
+
# @param (see Prmd::CLI::Base#execute)
|
|
39
|
+
# @return (see Prmd::CLI::Base#execute)
|
|
40
|
+
def self.execute(options = {})
|
|
41
|
+
filename = options.fetch(:argv).first
|
|
42
|
+
_, data = try_read(filename)
|
|
43
|
+
schema = Prmd::Schema.new(data)
|
|
44
|
+
write_result Prmd.render(schema, options), options
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'prmd/cli/base'
|
|
2
|
+
require 'prmd/commands/verify'
|
|
3
|
+
|
|
4
|
+
module Prmd
|
|
5
|
+
module CLI
|
|
6
|
+
# 'verify' command module.
|
|
7
|
+
module Verify
|
|
8
|
+
extend CLI::Base
|
|
9
|
+
|
|
10
|
+
# Returns a OptionParser for parsing 'verify' 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} verify [options] <combined schema>"
|
|
19
|
+
opts.on('-y', '--yaml', 'Generate YAML') do |y|
|
|
20
|
+
yield :yaml, y
|
|
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 'verify' command.
|
|
29
|
+
#
|
|
30
|
+
# @example Usage
|
|
31
|
+
# Prmd::CLI::Verify.execute(argv: ['schema/api.json'])
|
|
32
|
+
#
|
|
33
|
+
# @param (see Prmd::CLI::Base#execute)
|
|
34
|
+
# @return (see Prmd::CLI::Base#execute)
|
|
35
|
+
def self.execute(options = {})
|
|
36
|
+
filename = options.fetch(:argv).first
|
|
37
|
+
_, data = try_read(filename)
|
|
38
|
+
errors = Prmd.verify(data)
|
|
39
|
+
unless errors.empty?
|
|
40
|
+
errors.map! { |error| "#{filename}: #{error}" } if filename
|
|
41
|
+
errors.each { |error| $stderr.puts(error) }
|
|
42
|
+
exit(1)
|
|
43
|
+
end
|
|
44
|
+
result = options[:yaml] ? data.to_yaml : JSON.pretty_generate(data)
|
|
45
|
+
write_result result, options
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/prmd/cli.rb
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
require 'prmd/core_ext/optparse'
|
|
2
|
+
require 'prmd/cli/combine'
|
|
3
|
+
require 'prmd/cli/doc'
|
|
4
|
+
require 'prmd/cli/generate'
|
|
5
|
+
require 'prmd/cli/render'
|
|
6
|
+
require 'prmd/cli/verify'
|
|
7
|
+
|
|
8
|
+
# :nodoc:
|
|
9
|
+
module Prmd
|
|
10
|
+
# Main CLI module
|
|
11
|
+
module CLI
|
|
12
|
+
# @param [Hash<Symbol, Object>] props
|
|
13
|
+
# @return [Hash<Symbol, OptionParser>] all dem parsers
|
|
14
|
+
def self.make_command_parsers(props = {})
|
|
15
|
+
{
|
|
16
|
+
combine: CLI::Combine.make_parser(props),
|
|
17
|
+
doc: CLI::Doc.make_parser(props),
|
|
18
|
+
init: CLI::Generate.make_parser(props),
|
|
19
|
+
render: CLI::Render.make_parser(props),
|
|
20
|
+
verify: CLI::Verify.make_parser(props)
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# List of all available commands
|
|
25
|
+
#
|
|
26
|
+
# @return [Array] available commands
|
|
27
|
+
def self.commands
|
|
28
|
+
@commands ||= make_command_parsers.keys
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Creates the CLI main parser
|
|
32
|
+
#
|
|
33
|
+
# @param [Hash<Symbol, Object>] options
|
|
34
|
+
# @param [Hash<Symbol, Object>] props
|
|
35
|
+
def self.make_parser(options, props = {})
|
|
36
|
+
binname = props.fetch(:bin, 'prmd')
|
|
37
|
+
|
|
38
|
+
# This is only used to attain the help commands
|
|
39
|
+
commands = make_command_parsers(props)
|
|
40
|
+
help_text = commands.values.map do |command|
|
|
41
|
+
" #{command.banner}"
|
|
42
|
+
end.join("\n")
|
|
43
|
+
|
|
44
|
+
global = OptionParser.new do |opts|
|
|
45
|
+
opts.banner = "Usage: #{binname} [options] [command [options]]"
|
|
46
|
+
opts.separator "\nAvailable options:"
|
|
47
|
+
opts.on('--version', 'Return version') do
|
|
48
|
+
puts "prmd #{Prmd::VERSION}"
|
|
49
|
+
exit(0)
|
|
50
|
+
end
|
|
51
|
+
opts.on('--noop', 'Commands will not execute') do |v|
|
|
52
|
+
options[:noop] = v
|
|
53
|
+
end
|
|
54
|
+
opts.separator "\nAvailable commands:"
|
|
55
|
+
opts.separator help_text
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
global
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Parse top level CLI options from argv
|
|
62
|
+
#
|
|
63
|
+
# @param [Array<String>] argv
|
|
64
|
+
# @param [Hash<Symbol, Object>] opts
|
|
65
|
+
# @return [Hash<Symbol, Object>] parsed options
|
|
66
|
+
def self.parse_options(argv, opts = {})
|
|
67
|
+
options = {}
|
|
68
|
+
parser = make_parser(options, opts)
|
|
69
|
+
abort parser if argv.empty?
|
|
70
|
+
com_argv = parser.order(argv)
|
|
71
|
+
abort parser if com_argv.empty?
|
|
72
|
+
command = com_argv.shift.to_sym
|
|
73
|
+
abort parser unless commands.include?(command)
|
|
74
|
+
options[:argv] = com_argv
|
|
75
|
+
options[:command] = command
|
|
76
|
+
options
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Execute the Prmd CLI, or its subcommands
|
|
80
|
+
#
|
|
81
|
+
# @param [Array<String>] uargv
|
|
82
|
+
# @param [Hash<Symbol, Object>] opts
|
|
83
|
+
# @return [void]
|
|
84
|
+
def self.run(uargv, opts = {})
|
|
85
|
+
options = parse_options(uargv, opts)
|
|
86
|
+
argv = options.delete(:argv)
|
|
87
|
+
command = options.delete(:command)
|
|
88
|
+
|
|
89
|
+
case command
|
|
90
|
+
when :combine
|
|
91
|
+
CLI::Combine.run(argv, options)
|
|
92
|
+
when :doc
|
|
93
|
+
CLI::Doc.run(argv, options)
|
|
94
|
+
when :init
|
|
95
|
+
CLI::Generate.run(argv, options)
|
|
96
|
+
when :render
|
|
97
|
+
CLI::Render.run(argv, options)
|
|
98
|
+
when :verify
|
|
99
|
+
CLI::Verify.run(argv, options)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
class << self
|
|
104
|
+
private :make_command_parsers
|
|
105
|
+
private :commands
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -1,73 +1,100 @@
|
|
|
1
|
+
require 'prmd/load_schema_file'
|
|
2
|
+
require 'prmd/core/schema_hash'
|
|
3
|
+
require 'prmd/core/combiner'
|
|
4
|
+
|
|
5
|
+
# :nodoc:
|
|
1
6
|
module Prmd
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
begin
|
|
14
|
-
schemata << [file, YAML.load(File.read(file))]
|
|
15
|
-
rescue
|
|
16
|
-
$stderr.puts "unable to parse #{file}"
|
|
7
|
+
# Schema combine
|
|
8
|
+
module Combine
|
|
9
|
+
# @api private
|
|
10
|
+
# @param [#size] given
|
|
11
|
+
# @param [#size] expected
|
|
12
|
+
# @return [void]
|
|
13
|
+
def self.handle_faulty_load(given, expected)
|
|
14
|
+
unless given.size == expected.size
|
|
15
|
+
abort 'Somes files have failed to parse. ' \
|
|
16
|
+
'If you wish to continue without them,' \
|
|
17
|
+
'please enable faulty_load using --faulty-load'
|
|
17
18
|
end
|
|
18
19
|
end
|
|
19
|
-
unless schemata.length == files.length
|
|
20
|
-
exit(1) # one or more files failed to parse
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
data = {
|
|
24
|
-
'$schema' => 'http://json-schema.org/draft-04/hyper-schema',
|
|
25
|
-
'definitions' => {},
|
|
26
|
-
'properties' => {},
|
|
27
|
-
'type' => ['object']
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
# tracks which entities where defined in which file
|
|
31
|
-
schemata_map = {}
|
|
32
20
|
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
# @api private
|
|
22
|
+
# @param [Array<String>] paths
|
|
23
|
+
# @param [Hash<Symbol, Object>] options
|
|
24
|
+
# @return [Array<String>] list of filenames from paths
|
|
25
|
+
def self.crawl_map(paths, options = {})
|
|
26
|
+
files = [*paths].map do |path|
|
|
27
|
+
if File.directory?(path)
|
|
28
|
+
Dir.glob(File.join(path, '**', '*.{json,yml,yaml}'))
|
|
29
|
+
else
|
|
30
|
+
path
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
files.flatten!
|
|
34
|
+
files.delete(options[:meta])
|
|
35
|
+
files
|
|
35
36
|
end
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
# @api private
|
|
39
|
+
# @param [String] filename
|
|
40
|
+
# @return [SchemaHash]
|
|
41
|
+
def self.load_schema_hash(filename)
|
|
42
|
+
data = Prmd.load_schema_file(filename)
|
|
43
|
+
SchemaHash.new(data, filename: filename)
|
|
44
|
+
end
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
46
|
+
# @api private
|
|
47
|
+
# @param [Array<String>] files
|
|
48
|
+
# @param [Hash<Symbol, Object>] options
|
|
49
|
+
# @return [Array<SchemaHash>] schema hashes
|
|
50
|
+
def self.load_files(files, options = {})
|
|
51
|
+
files.each_with_object([]) do |filename, result|
|
|
52
|
+
begin
|
|
53
|
+
result << load_schema_hash(filename)
|
|
54
|
+
rescue JSON::ParserError, Psych::SyntaxError => ex
|
|
55
|
+
$stderr.puts "unable to parse #{filename} (#{ex.inspect})"
|
|
56
|
+
end
|
|
42
57
|
end
|
|
43
|
-
|
|
58
|
+
end
|
|
44
59
|
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
# @api private
|
|
61
|
+
# @param [Array<String>] paths
|
|
62
|
+
# @param [Hash<Symbol, Object>] options
|
|
63
|
+
# @return (see .load_files)
|
|
64
|
+
def self.load_schemas(paths, options = {})
|
|
65
|
+
files = crawl_map(paths, options)
|
|
66
|
+
# sort for stable loading on any platform
|
|
67
|
+
schemata = load_files(files.sort, options)
|
|
68
|
+
handle_faulty_load(schemata, files) unless options[:faulty_load]
|
|
69
|
+
schemata
|
|
70
|
+
end
|
|
47
71
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
else
|
|
63
|
-
datum
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
reference_localizer.call(data['definitions'][id])
|
|
72
|
+
# Merges all found schema files in the given paths into a single Schema
|
|
73
|
+
#
|
|
74
|
+
# @param [Array<String>] paths
|
|
75
|
+
# @param [Hash<Symbol, Object>] options
|
|
76
|
+
# @return (see Prmd::Combiner#combine)
|
|
77
|
+
def self.combine(paths, options = {})
|
|
78
|
+
schemata = load_schemas(paths)
|
|
79
|
+
base = Prmd::Template.load_json('combine_head.json')
|
|
80
|
+
schema = base['$schema']
|
|
81
|
+
meta = {}
|
|
82
|
+
meta = Prmd.load_schema_file(options[:meta]) if options[:meta]
|
|
83
|
+
combiner = Prmd::Combiner.new(meta: meta, base: base, schema: schema)
|
|
84
|
+
combiner.combine(*schemata)
|
|
85
|
+
end
|
|
67
86
|
|
|
68
|
-
|
|
87
|
+
class << self
|
|
88
|
+
private :handle_faulty_load
|
|
89
|
+
private :crawl_map
|
|
90
|
+
private :load_schema_hash
|
|
91
|
+
private :load_files
|
|
92
|
+
private :load_schemas
|
|
69
93
|
end
|
|
94
|
+
end
|
|
70
95
|
|
|
71
|
-
|
|
96
|
+
# (see Prmd::Combine.combine)
|
|
97
|
+
def self.combine(paths, options = {})
|
|
98
|
+
Combine.combine(paths, { faulty_load: false }.merge(options))
|
|
72
99
|
end
|
|
73
100
|
end
|
data/lib/prmd/commands/init.rb
CHANGED
|
@@ -1,107 +1,39 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'prmd/template'
|
|
3
|
+
require 'prmd/core/generator'
|
|
4
|
+
|
|
5
|
+
# :nodoc:
|
|
1
6
|
module Prmd
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
'
|
|
9
|
-
'
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
# Schema generation
|
|
8
|
+
module Generate
|
|
9
|
+
# Creates a default Prmd::Generator using default templates
|
|
10
|
+
#
|
|
11
|
+
# @return [Prmd::Generator]
|
|
12
|
+
def self.make_generator
|
|
13
|
+
base = Prmd::Template.load_json('init_default.json')
|
|
14
|
+
template = Prmd::Template.load_template('init_resource.json.erb', '')
|
|
15
|
+
Prmd::Generator.new(base: base, template: template)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
# Generate a schema template
|
|
20
|
+
#
|
|
21
|
+
# @param [String] resource
|
|
22
|
+
# @param [Hash<Symbol, Object>] options
|
|
23
|
+
# @return [String] schema template in YAML (yaml option was enabled) else JSON
|
|
24
|
+
def self.init(resource, options = {})
|
|
25
|
+
gen = Generate.make_generator
|
|
14
26
|
|
|
27
|
+
generator_options = { resource: nil, parent: nil }
|
|
15
28
|
if resource
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
schema['title'] = "FIXME - #{resource[0...1].upcase}#{resource[1..-1]}"
|
|
21
|
-
schema['definitions'] = {
|
|
22
|
-
"created_at" => {
|
|
23
|
-
"description" => "when #{resource} was created",
|
|
24
|
-
"example" => "2012-01-01T12:00:00Z",
|
|
25
|
-
"format" => "date-time",
|
|
26
|
-
"type" => ["string"]
|
|
27
|
-
},
|
|
28
|
-
"id" => {
|
|
29
|
-
"description" => "unique identifier of #{resource}",
|
|
30
|
-
"example" => "01234567-89ab-cdef-0123-456789abcdef",
|
|
31
|
-
"format" => "uuid",
|
|
32
|
-
"type" => ["string"]
|
|
33
|
-
},
|
|
34
|
-
"identity" => {
|
|
35
|
-
"$ref" => "/schemata/#{resource}#/definitions/id"
|
|
36
|
-
},
|
|
37
|
-
"updated_at" => {
|
|
38
|
-
"description" => "when #{resource} was updated",
|
|
39
|
-
"example" => "2012-01-01T12:00:00Z",
|
|
40
|
-
"format" => "date-time",
|
|
41
|
-
"type" => ["string"]
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
schema['links'] = [
|
|
45
|
-
{
|
|
46
|
-
"description" => "Create a new #{resource}.",
|
|
47
|
-
"href" => "/#{resource}s",
|
|
48
|
-
"method" => "POST",
|
|
49
|
-
"rel" => "create",
|
|
50
|
-
"schema" => {
|
|
51
|
-
"properties" => {},
|
|
52
|
-
"type" => ["object"]
|
|
53
|
-
},
|
|
54
|
-
"title" => "Create"
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
"description" => "Delete an existing #{resource}.",
|
|
58
|
-
"href" => "/#{resource}s/{(%2Fschemata%2F#{resource}%23%2Fdefinitions%2Fidentity)}",
|
|
59
|
-
"method" => "DELETE",
|
|
60
|
-
"rel" => "destroy",
|
|
61
|
-
"title" => "Delete"
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
"description" => "Info for existing #{resource}.",
|
|
65
|
-
"href" => "/#{resource}s/{(%2Fschemata%2F#{resource}%23%2Fdefinitions%2Fidentity)}",
|
|
66
|
-
"method" => "GET",
|
|
67
|
-
"rel" => "self",
|
|
68
|
-
"title" => "Info"
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
"description" => "List existing #{resource}s.",
|
|
72
|
-
"href" => "/#{resource}s",
|
|
73
|
-
"method" => "GET",
|
|
74
|
-
"rel" => "instances",
|
|
75
|
-
"title" => "List"
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
"description" => "Update an existing #{resource}.",
|
|
79
|
-
"href" => "/#{resource}s/{(%2Fschemata%2F#{resource}%23%2Fdefinitions%2Fidentity)}",
|
|
80
|
-
"method" => "PATCH",
|
|
81
|
-
"rel" => "update",
|
|
82
|
-
"schema" => {
|
|
83
|
-
"properties" => {},
|
|
84
|
-
"type" => ["object"]
|
|
85
|
-
},
|
|
86
|
-
"title" => "Update"
|
|
87
|
-
}
|
|
88
|
-
]
|
|
89
|
-
if parent
|
|
90
|
-
schema['links'] << {
|
|
91
|
-
"description" => "List existing #{resource}s for existing #{parent}.",
|
|
92
|
-
"href" => "/#{parent}s/{(%2Fschemata%2F#{parent}%23%2Fdefinitions%2Fidentity)}/#{resource}s",
|
|
93
|
-
"method" => "GET",
|
|
94
|
-
"rel" => "instances",
|
|
95
|
-
"title" => "List"
|
|
96
|
-
}
|
|
97
|
-
end
|
|
98
|
-
schema['properties'] = {
|
|
99
|
-
"created_at" => { "$ref" => "/schemata/#{resource}#/definitions/created_at" },
|
|
100
|
-
"id" => { "$ref" => "/schemata/#{resource}#/definitions/id" },
|
|
101
|
-
"updated_at" => { "$ref" => "/schemata/#{resource}#/definitions/updated_at" }
|
|
102
|
-
}
|
|
29
|
+
parent = nil
|
|
30
|
+
parent, resource = resource.split('/') if resource.include?('/')
|
|
31
|
+
generator_options[:parent] = parent
|
|
32
|
+
generator_options[:resource] = resource
|
|
103
33
|
end
|
|
104
34
|
|
|
35
|
+
schema = gen.generate(generator_options)
|
|
36
|
+
|
|
105
37
|
if options[:yaml]
|
|
106
38
|
schema.to_yaml
|
|
107
39
|
else
|
data/lib/prmd/commands/render.rb
CHANGED
|
@@ -1,24 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
def self.render(schema, options={})
|
|
3
|
-
doc = ''
|
|
4
|
-
|
|
5
|
-
options[:content_type] ||= 'application/json'
|
|
6
|
-
options[:style] ||= 'default'
|
|
1
|
+
require 'prmd/core/renderer'
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
# :nodoc:
|
|
4
|
+
module Prmd
|
|
5
|
+
# Render helper module
|
|
6
|
+
module Render
|
|
7
|
+
# Retrieve the schema template
|
|
8
|
+
#
|
|
9
|
+
# @param [Hash<Symbol, Object>] options
|
|
10
|
+
# @return (see Prmd::Template.load_template)
|
|
11
|
+
def self.get_template(options)
|
|
12
|
+
template = options.fetch(:template) do
|
|
13
|
+
abort 'render: Template was not provided'
|
|
14
|
+
end
|
|
15
|
+
template_dir = File.expand_path(template)
|
|
16
|
+
# to keep backward compatibility
|
|
17
|
+
template_dir = File.dirname(template) unless File.directory?(template_dir)
|
|
18
|
+
Prmd::Template.load_template('schema.erb', template_dir)
|
|
10
19
|
end
|
|
20
|
+
end
|
|
11
21
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
22
|
+
# Render provided schema to Markdown
|
|
23
|
+
#
|
|
24
|
+
# @param [Prmd::Schema] schema
|
|
25
|
+
# @return [String] rendered schema in Markdown
|
|
26
|
+
def self.render(schema, options = {})
|
|
27
|
+
renderer = Prmd::Renderer.new(template: Render.get_template(options))
|
|
28
|
+
doc = ''
|
|
29
|
+
if options[:prepend]
|
|
30
|
+
doc <<
|
|
31
|
+
options[:prepend].map { |path| File.read(path) }.join("\n") <<
|
|
32
|
+
"\n"
|
|
15
33
|
end
|
|
16
|
-
|
|
17
|
-
doc << Prmd::Template::render('schema.erb', template_dir, {
|
|
18
|
-
options: options,
|
|
19
|
-
schema: schema
|
|
20
|
-
})
|
|
21
|
-
|
|
34
|
+
doc << renderer.render(schema, options)
|
|
22
35
|
doc
|
|
23
36
|
end
|
|
24
37
|
end
|