appmap_swagger 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fab77dbb3fd27bda9de4005dec030569a6c6c2e357e3aa395d0db0b756e2a17a
4
+ data.tar.gz: b21378cc57387b4a692eef8d6559656a94328d34c4b7e4f81faf2f1de7671e31
5
+ SHA512:
6
+ metadata.gz: ccea92116d5bcceecdad969785e62b2e229abf288a810a52e144382a156a68a8357be1099fcf58c9f399d2fac557cf19731da716db4734b512e78f5e159e5671
7
+ data.tar.gz: bfaf3e21575f94349442bfad0c9906941328303e9749df18ba35fed2a82babd4959118f0dea9259ceb8d75944271f0c8694364201d8e0a72b69b8e0ad455694f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in appmap_swagger.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ appmap_swagger (0.1.0)
5
+ activesupport
6
+ rake
7
+ rdoc
8
+ reverse_markdown
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activesupport (6.1.3.1)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ zeitwerk (~> 2.3)
19
+ concurrent-ruby (1.1.8)
20
+ i18n (1.8.10)
21
+ concurrent-ruby (~> 1.0)
22
+ mini_portile2 (2.5.0)
23
+ minitest (5.14.4)
24
+ nokogiri (1.11.2)
25
+ mini_portile2 (~> 2.5.0)
26
+ racc (~> 1.4)
27
+ racc (1.5.2)
28
+ rake (12.3.3)
29
+ rdoc (6.3.0)
30
+ reverse_markdown (2.0.0)
31
+ nokogiri
32
+ tzinfo (2.0.4)
33
+ concurrent-ruby (~> 1.0)
34
+ zeitwerk (2.4.2)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ appmap_swagger!
41
+ minitest (~> 5.0)
42
+ rake (~> 12.0)
43
+
44
+ BUNDLED WITH
45
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Kevin Gilpin
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # `appmap_swagger`
2
+
3
+ This gem provides a Rake task called `appmap:swagger` that generates [Swagger 3](https://swagger.io/specification/) (aka OpenAPI) YAML from [AppMap](https://github.com/applandinc/appmap-ruby) data.
4
+
5
+ It depends on an NPM package called [@appland/appmap-swagger](https://www.npmjs.com/package/@appland/appmap-swagger), which does most of the heavy lifting of converting AppMaps to Swagger. This gem adds the Rake task and some niceties such as Rails integration.
6
+
7
+ # How it works
8
+
9
+ The Rake task `appmap:swagger`:
10
+
11
+ 1. Requires Node.js, and it requires the `@appland/appmap-swagger` package to be installed from NPM.
12
+ 2. Runs the Node.js program `appmap-swagger` to generate Swagger YAML.
13
+ 3. Merges the generated Swagger with a template file.
14
+ 4. Applies some sensible defaults for Ruby, and Ruby on Rails.
15
+ 5. Outputs two files to the specified directory (default: `swagger`):
16
+ 1. `openapi.yaml` Full Swagger, including documentation and examples.
17
+ 2. `openapi_stable.yaml` Swagger without documentation and examples, so that it's more stable across versions.
18
+
19
+ `openapi_stable.yaml` is ideal for use in code reviews, to see if (and how) web services have been changed.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ group :development do
27
+ gem 'appmap_swagger'
28
+ end
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle install
34
+
35
+ # Usage
36
+
37
+ ## Defining the `appmap:swagger` Rake task
38
+
39
+ You need to define the `appmap:swagger` Rake task. In Rails, this is done by creating a file like `lib/tasks/appmap.rake`.
40
+
41
+ In the file, check if `AppMap` is loaded, and then configure the Rake task. You'll probably want to provide
42
+ a project name and version. (The default project name is determined from your Rails Application class name and might be fine, actually).
43
+
44
+ ```ruby
45
+ if defined?(AppMap::Swagger)
46
+ AppMap::Swagger::RakeTask.new.tap do |task|
47
+ task.project_name = 'My Server API'
48
+ # You may not have a VERSION file. Do what works best for you.
49
+ task.project_version = "v#{File.read(File.join(Rails.root, 'VERSION')).strip}"
50
+ end
51
+ end
52
+ ```
53
+
54
+ ## Incorporating the Swagger API and UI
55
+
56
+ Two other gems work great with `appmap:swagger`: `rswag-api` and `rswag-ui` from [rswag](https://github.com/rswag/rswag).
57
+
58
+ Install in your Gemfile:
59
+
60
+ ```ruby
61
+ # By default, let's not run this in production until we've thought about the implications.
62
+ group :test, :development do
63
+ gem 'rswag-api'
64
+ gem 'rswag-ui'
65
+ end
66
+ ```
67
+
68
+ Then run the install commands:
69
+
70
+ ```sh-session
71
+ $ rails g rswag:api:install
72
+ $ rails g rswag:ui:install
73
+ ```
74
+
75
+ Update `routes.rb`:
76
+
77
+ ```ruby
78
+ if defined?(Rswag)
79
+ mount Rswag::Ui::Engine => '/api-docs'
80
+ mount Rswag::Api::Engine => '/api-docs'
81
+ end
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake gem: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).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/applandinc/appmap_swagger-ruby.
93
+
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'appmap/swagger/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'appmap_swagger'
9
+ spec.version = AppMap::Swagger::VERSION
10
+ spec.authors = ['Kevin Gilpin']
11
+ spec.email = ['kgilpin@gmail.com']
12
+
13
+ spec.summary = %q{Provides a Rake task to generate Swagger from AppMap data}
14
+ spec.homepage = 'https://github.com/applandinc/appmap_swagger-ruby'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'rake'
28
+ spec.add_dependency 'rdoc'
29
+ spec.add_dependency 'reverse_markdown'
30
+ spec.add_dependency 'activesupport'
31
+
32
+ spec.add_development_dependency 'minitest'
33
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "appmap_swagger"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require 'rdoc'
4
+ require 'reverse_markdown'
5
+
6
+ module AppMap
7
+ module Swagger
8
+ # Transform description fields into Markdown.
9
+ class MarkdownDescriptions
10
+ def initialize(swagger_yaml)
11
+ @swagger_yaml = swagger_yaml
12
+ end
13
+
14
+ def converter
15
+ method(:rdoc_to_markdown)
16
+ end
17
+
18
+ def perform
19
+ to_markdown = lambda do |obj|
20
+ return obj.each(&to_markdown) if obj.is_a?(Array)
21
+ return unless obj.is_a?(Hash)
22
+
23
+ description = obj['description']
24
+ obj['description'] = converter.(description) if description
25
+
26
+ obj.reject { |k,v| k == 'properties' }.each_value(&to_markdown)
27
+
28
+ obj
29
+ end
30
+
31
+ to_markdown.(@swagger_yaml.deep_dup)
32
+ end
33
+
34
+ protected
35
+
36
+ def rdoc_to_markdown(comment)
37
+ # Strip tags
38
+ comment = comment.split("\n").reject { |line| line =~ /^\s*@/ }.join("\n")
39
+ converter = ::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
40
+ html = converter.convert(comment).strip
41
+ ::ReverseMarkdown.convert(html).strip
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'rake/tasklib'
5
+ require 'appmap/swagger/stable'
6
+ require 'appmap/swagger/markdown_descriptions'
7
+
8
+ module AppMap
9
+ module Swagger
10
+ class RakeTask < ::Rake::TaskLib
11
+ DEFAULT_APPMAP_DIR = 'tmp/appmap'
12
+ DEFAULT_OUTPUT_DIR = 'swagger'
13
+ DEFAULT_SWAGGERGEN = './node_modules/@appland/appmap-swagger/cli.js'
14
+
15
+ attr_accessor :name, :verbose, :swaggergen, :appmap_dir, :output_dir, :project_name, :project_version
16
+
17
+ def initialize(*args, &task_block)
18
+ @name = args.shift || :swagger
19
+ @verbose = true
20
+ @swaggergen = DEFAULT_SWAGGERGEN
21
+ @appmap_dir = DEFAULT_APPMAP_DIR
22
+ @output_dir = DEFAULT_OUTPUT_DIR
23
+
24
+ # https://www.rubydoc.info/docs/rails/Module#module_parent_name-instance_method
25
+ module_parent_name = ->(cls) { cls.name =~ /::[^:]+\Z/ ? $`.freeze : nil }
26
+
27
+ @project_name = \
28
+ if defined?(Rails)
29
+ [ module_parent_name.(Rails.application.class).humanize.titleize, 'API' ].join(' ')
30
+ else
31
+ 'MyProject API'
32
+ end
33
+ @project_version = 'v1.0'
34
+
35
+
36
+ define(args, &task_block)
37
+ end
38
+
39
+ def run_task(verbose)
40
+ FileUtils.mkdir_p output_dir
41
+
42
+ do_fail = lambda do |msg|
43
+ warn msg if verbose
44
+ exit $?.exitstatus || 1
45
+ end
46
+
47
+ return do_fail.(%Q('node' not found; please install NodeJS)) unless system('node --version 2>&1 > /dev/null')
48
+ return do_fail.(%Q('#{swaggergen}' not found; please install appmap-swagger from NPM)) unless File.exists?(swaggergen)
49
+
50
+ warn swagger_command.join(' ') if verbose
51
+
52
+ swagger_raw = `#{swagger_command.join(' ')}`.strip
53
+ return do_fail.(%Q(Swagger generation failed: #{swagger_raw})) if $?.exitstatus != 0
54
+
55
+ gen_swagger = YAML.load(swagger_raw)
56
+ gen_swagger_full = AppMap::Swagger::MarkdownDescriptions.new(gen_swagger).perform
57
+ gen_swagger_stable = AppMap::Swagger::Stable.new(gen_swagger).perform
58
+
59
+ swagger = swagger_template.merge(gen_swagger_full)
60
+ File.write File.join(output_dir, 'openapi.yaml'), YAML.dump(swagger)
61
+
62
+ swagger = swagger_template.merge(gen_swagger_stable)
63
+ File.write File.join(output_dir, 'openapi_stable.yaml'), YAML.dump(swagger)
64
+ end
65
+
66
+ def swagger_template
67
+ YAML.load <<~TEMPLATE
68
+ openapi: 3.0.1
69
+ info:
70
+ title: #{project_name}
71
+ version: #{project_version}
72
+ paths:
73
+ components:
74
+ servers:
75
+ - url: http://{defaultHost}
76
+ variables:
77
+ defaultHost:
78
+ default: localhost:3000
79
+ TEMPLATE
80
+ end
81
+
82
+ def swagger_command
83
+ [ 'node', swaggergen, 'generate', '--directory', appmap_dir ]
84
+ end
85
+
86
+ private
87
+
88
+ # This bit of black magic - https://github.com/rspec/rspec-core/blob/main/lib/rspec/core/rake_task.rb#L110
89
+ def define(args, &task_block)
90
+ desc "Generate Swagger from AppMaps" unless ::Rake.application.last_description
91
+
92
+ task(name, *args) do |_, task_args|
93
+ RakeFileUtils.__send__(:verbose, verbose) do
94
+ task_block.call(*[self, task_args].slice(0, task_block.arity)) if task_block
95
+ run_task verbose
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+
4
+ module AppMap
5
+ module Swagger
6
+ # Transform raw Swagger into a "stable" variant. For example, remove descriptions
7
+ # and parameter examples, whose variance does not substantially affect the API.
8
+ class Stable
9
+ def initialize(swagger_yaml)
10
+ @swagger_yaml = swagger_yaml
11
+ end
12
+
13
+ def perform
14
+ clean_only = nil
15
+ clean = lambda do |obj, properties = %w[description example]|
16
+ return obj.each(&clean_only.(properties)) if obj.is_a?(Array)
17
+ return unless obj.is_a?(Hash)
18
+
19
+ properties.each { |property| obj.delete property }
20
+
21
+ obj.each do |key, value|
22
+ # Don't clean 'description' from within 'properties'
23
+ props = key == 'properties' ? %w[example] : properties
24
+ clean_only.(props).(value)
25
+ end
26
+
27
+ obj
28
+ end
29
+
30
+ clean_only = lambda do |properties|
31
+ lambda do |example|
32
+ clean.(example, properties)
33
+ end
34
+ end
35
+
36
+ clean.(@swagger_yaml.deep_dup)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ module AppMap
2
+ module Swagger
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'appmap/swagger/version'
2
+ require 'appmap/swagger/rake_task'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: appmap_swagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Gilpin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: reverse_markdown
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - kgilpin@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - appmap_swagger.gemspec
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/appmap/swagger/markdown_descriptions.rb
101
+ - lib/appmap/swagger/rake_task.rb
102
+ - lib/appmap/swagger/stable.rb
103
+ - lib/appmap/swagger/version.rb
104
+ - lib/appmap_swagger.rb
105
+ homepage: https://github.com/applandinc/appmap_swagger-ruby
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.3.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Provides a Rake task to generate Swagger from AppMap data
128
+ test_files: []