openapi-sourcetools 0.4.2 → 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.
data/lib/common.rb CHANGED
@@ -1,25 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- def aargh(message, exit_code = nil)
3
+ # Copyright © 2021-2024 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ require 'pathname'
7
+
8
+
9
+ def aargh(message, return_value = nil)
10
+ message = message.map(&:to_s).join("\n") if message.is_a? Array
4
11
  $stderr.puts message
5
- exit exit_code unless exit_code.nil?
12
+ return_value
6
13
  end
7
14
 
8
- def env(var, value = nil)
9
- k = var.to_s.upcase
10
- ENV[k] = { false => '0', true => '1' }.fetch(value, value) unless value.nil?
11
- v = ENV.fetch(k, nil)
12
- case v
13
- when '0' then false
14
- when '1' then true
15
- else
16
- v
15
+ def bury(doc, path, value)
16
+ (path.size - 1).times do |k|
17
+ p = path[k]
18
+ doc[p] = {} unless doc.key?(p)
19
+ doc = doc[p]
17
20
  end
21
+ doc[path.last] = value
18
22
  end
19
23
 
20
- def default_env(var, value)
21
- v = env(var)
22
- env(var, value) if v.nil?
24
+ module Output
25
+ attr_reader :count
26
+
27
+ def put(message)
28
+ aargh(message)
29
+ count += 1
30
+ end
23
31
  end
24
32
 
25
33
  def split_path(p, spec = false)
@@ -37,32 +45,50 @@ def split_path(p, spec = false)
37
45
  parts
38
46
  end
39
47
 
48
+ def load_source(input)
49
+ YAML.safe_load(input.nil? ? $stdin : File.read(input))
50
+ rescue Errno::ENOENT
51
+ aargh "Could not load #{input || 'stdin'}"
52
+ rescue StandardError => e
53
+ aargh "#{e}\nFailed to read #{input || 'stdin'}"
54
+ end
55
+
56
+ def dump_result(output, doc, error_return)
57
+ if output.nil?
58
+ $stdout.puts doc
59
+ else
60
+ fp = Pathname.new output
61
+ fp.open('w') do |f|
62
+ f.puts doc
63
+ end
64
+ end
65
+ 0
66
+ rescue StandardError => e
67
+ aargh([ e, "Failed to write output: #{output || 'stdout'}" ], error_return)
68
+ end
69
+
40
70
  ServerPath = Struct.new(:parts) do
41
- def <=>(p) # Variables are after fixed strings.
42
- pp = p.is_a?(Array) ? p : p.parts
71
+ # Variables are after fixed strings.
72
+ def <=>(other)
73
+ pp = other.is_a?(Array) ? other : other.parts
43
74
  parts.each_index do |k|
44
75
  return 1 if pp.size <= k # Longer comes after shorter.
45
76
  pk = parts[k]
46
77
  ppk = pp[k]
47
78
  if pk.is_a? String
48
- if ppk.is_a? String
49
- c = pk <=> ppk
50
- else
51
- return -1
52
- end
79
+ return -1 unless ppk.is_a? String
80
+ c = pk <=> ppk
53
81
  else
54
- if ppk.is_a? String
55
- return 1
56
- else
57
- c = pk.fetch('var', '') <=> ppk.fetch('var', '')
58
- end
82
+ return 1 if ppk.is_a? String
83
+ c = pk.fetch('var', '') <=> ppk.fetch('var', '')
59
84
  end
60
85
  return c unless c.zero?
61
86
  end
62
87
  (parts.size < pp.size) ? -1 : 0
63
88
  end
64
89
 
65
- def compare(p, range = nil) # Not fit for sorting. Variable equals anything.
90
+ # Not fit for sorting. Variable equals anything.
91
+ def compare(p, range = nil)
66
92
  pp = p.is_a?(Array) ? p : p.parts
67
93
  if range.nil?
68
94
  range = 0...parts.size
metadata CHANGED
@@ -1,21 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi-sourcetools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismo Kärkkäinen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-15 00:00:00.000000000 Z
11
+ date: 2024-05-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
 
15
- Tools for generating source code from API specification in OpenAPI format.
15
+ Tools for handling API specification in OpenAPI format. Replacement of
16
+ duplicate definitions with references. Other checks. Does not validate
17
+ the document against specification.
16
18
  email: ismokarkkainen@icloud.com
17
19
  executables:
18
- - openapi-generatecode
20
+ - openapi-addheaders
21
+ - openapi-addresponses
22
+ - openapi-addschemas
23
+ - openapi-checkschemas
19
24
  - openapi-frequencies
20
25
  - openapi-merge
21
26
  - openapi-processpaths
@@ -23,15 +28,20 @@ extensions: []
23
28
  extra_rdoc_files: []
24
29
  files:
25
30
  - LICENSE.txt
31
+ - bin/openapi-addheaders
32
+ - bin/openapi-addresponses
33
+ - bin/openapi-addschemas
34
+ - bin/openapi-checkschemas
26
35
  - bin/openapi-frequencies
27
- - bin/openapi-generatecode
28
36
  - bin/openapi-merge
29
37
  - bin/openapi-processpaths
38
+ - lib/apiobjects.rb
30
39
  - lib/common.rb
31
40
  homepage: https://xn--ismo-krkkinen-gfbd.fi/openapi-sourcetools/index.html
32
41
  licenses:
33
42
  - UPL-1.0
34
- metadata: {}
43
+ metadata:
44
+ rubygems_mfa_required: 'true'
35
45
  post_install_message:
36
46
  rdoc_options: []
37
47
  require_paths:
@@ -40,14 +50,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
40
50
  requirements:
41
51
  - - ">="
42
52
  - !ruby/object:Gem::Version
43
- version: '0'
53
+ version: 3.0.0
44
54
  required_rubygems_version: !ruby/object:Gem::Requirement
45
55
  requirements:
46
56
  - - ">="
47
57
  - !ruby/object:Gem::Version
48
58
  version: '0'
49
59
  requirements: []
50
- rubygems_version: 3.1.2
60
+ rubygems_version: 3.2.33
51
61
  signing_key:
52
62
  specification_version: 4
53
63
  summary: Tools for creating source code from API specification.
@@ -1,128 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- # Copyright © 2021 Ismo Kärkkäinen
5
- # Licensed under Universal Permissive License. See LICENSE.txt.
6
-
7
- require_relative '../lib/common.rb'
8
- require 'optparse'
9
- require 'yaml'
10
- require 'erb'
11
-
12
-
13
- default_env(:out, '')
14
- default_env(:in, '')
15
- default_env(:template, '')
16
-
17
- ENV['POSIXLY_CORRECT'] = '1'
18
- parser = OptionParser.new do |opts|
19
- opts.summary_indent = ' '
20
- opts.summary_width = 26
21
- opts.banner = 'Usage: openapi-generatecode [options] [additions...]'
22
- opts.separator ''
23
- opts.separator 'Options (equivalent environment variable and value in parentheses):'
24
- opts.on('-i', '--input FILE', 'Read processed API from FILE, not stdin (IN=FILE).') do |f|
25
- env(:in, f)
26
- end
27
- opts.on('-o', '--output FILE', 'Output result to FILE, not stdout (OUT=FILE).') do |f|
28
- env(:out, f)
29
- end
30
- opts.on('-t', '--template FILE', 'Read template from FILE (TEMPLATE=FILE).') do |f|
31
- env(:template, f)
32
- end
33
- opts.on('-h', '--help', 'Print this help and exit.') do
34
- $stdout.puts %(#{opts}
35
-
36
- Loads ERB template and optional additions to a context along with the processed
37
- API document and produces the template result.
38
- )
39
- exit 0
40
- end
41
- end
42
- parser.parse!
43
-
44
- aargh('Template file name must be given.', 1) if env(:template).empty?
45
-
46
- def load_content(name)
47
- name.empty? ? $stdin.read : File.read(name)
48
- rescue Errno::ENOENT
49
- aargh("Could not load #{name || 'stdin'}", 2)
50
- rescue StandardError => e
51
- aargh("#{e}\nFailed to read #{name || 'stdin'}", 2)
52
- end
53
-
54
- class Generator
55
- attr_accessor :addition, :order, :full_name_order, :template, :document
56
-
57
- def initialize(document_content, template_content)
58
- @addition = Hash.new
59
- @order = []
60
- @full_name_order = []
61
- @document = document_content
62
- @template = template_content
63
- end
64
-
65
- def get_binding
66
- binding
67
- end
68
-
69
- def add(name, content, strip_suffix)
70
- @full_name_order.push({ filename: name, contents: content })
71
- name = File.basename(name)
72
- @order.push name
73
- if strip_suffix
74
- idx = name.rindex('.')
75
- name = name.slice(0, idx) unless idx.nil?
76
- end
77
- @addition[name] = content
78
- end
79
- end
80
- t = load_content(env(:template))
81
- d = load_content(env(:in))
82
- begin
83
- $generator = Generator.new(YAML.safe_load(d), t)
84
- rescue StandardError => e
85
- aargh('Failed to parse API document.', 3)
86
- end
87
-
88
- ARGV.each do |name|
89
- aargh('Addition file name is empty.', 1) if name.empty?
90
- if name.end_with? '.rb'
91
- begin
92
- require File.absolute_path(name)
93
- rescue SyntaxError => e
94
- aargh("Syntax error in addition #{name}: #{e.to_s}", 4)
95
- rescue StandardError => e
96
- aargh("Failed to require addition #{name}: #{e.to_s}", 5)
97
- end
98
- else
99
- c = load_content(name)
100
- begin
101
- d = YAML.safe_load(c)
102
- rescue StandardError
103
- d = c
104
- end
105
- $generator.add(name, d, name.upcase.end_with?('.YAML') || name.upcase.end_with?('.JSON'))
106
- end
107
- end
108
-
109
- begin
110
- out = ERB.new($generator.template).result($generator.get_binding)
111
- rescue SyntaxError => e
112
- aargh("Template syntax error: #{e.to_s}", 6)
113
- rescue StandardError => e
114
- aargh("Template error: #{e.to_s}", 7)
115
- end
116
-
117
- output = env(:out)
118
- if output.empty?
119
- output = $stdout
120
- else
121
- begin
122
- output = File.open(output, 'w')
123
- rescue StandardError
124
- aargh("Failed to open for writing: #{output}", 1)
125
- end
126
- end
127
- output.write(out)
128
- output.close