Class: AppMap::Swagger::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/appmap/swagger/rake_task.rb

Constant Summary collapse

DEFAULT_APPMAP_DIR =
'tmp/appmap'
DEFAULT_OUTPUT_DIR =
'swagger'
DEFAULT_SWAGGERGEN =
'./node_modules/@appland/appmap-swagger/cli.js'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &task_block) ⇒ RakeTask

Returns a new instance of RakeTask.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/appmap/swagger/rake_task.rb', line 17

def initialize(*args, &task_block)
  @name            = args.shift || :swagger
  @swaggergen      = DEFAULT_SWAGGERGEN
  @appmap_dir      = DEFAULT_APPMAP_DIR
  @output_dir      = DEFAULT_OUTPUT_DIR

  # https://www.rubydoc.info/docs/rails/Module#module_parent_name-instance_method
  module_parent_name = ->(cls) { cls.name =~ /::[^:]+\Z/ ? $`.freeze : nil }
  
  @project_name    = \
    if defined?(::Rails)
      [ module_parent_name.(::Rails.application.class).humanize.titleize, 'API' ].join(' ')
    else
      'MyProject API'
    end
  @project_version = 'v1.0'


  define(args, &task_block)
end

Instance Attribute Details

#appmap_dirObject

Returns the value of attribute appmap_dir.



15
16
17
# File 'lib/appmap/swagger/rake_task.rb', line 15

def appmap_dir
  @appmap_dir
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/appmap/swagger/rake_task.rb', line 15

def name
  @name
end

#output_dirObject

Returns the value of attribute output_dir.



15
16
17
# File 'lib/appmap/swagger/rake_task.rb', line 15

def output_dir
  @output_dir
end

#project_nameObject

Returns the value of attribute project_name.



15
16
17
# File 'lib/appmap/swagger/rake_task.rb', line 15

def project_name
  @project_name
end

#project_versionObject

Returns the value of attribute project_version.



15
16
17
# File 'lib/appmap/swagger/rake_task.rb', line 15

def project_version
  @project_version
end

#swaggergenObject

Returns the value of attribute swaggergen.



15
16
17
# File 'lib/appmap/swagger/rake_task.rb', line 15

def swaggergen
  @swaggergen
end

Instance Method Details

#run_taskObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/appmap/swagger/rake_task.rb', line 42

def run_task
  FileUtils.mkdir_p output_dir

  do_fail = lambda do |msg|
    warn msg
    exit $?.exitstatus || 1
  end

  return do_fail.(%Q('node' not found; please install NodeJS)) unless system('node --version 2>&1 > /dev/null')
  return do_fail.(%Q('#{swaggergen}' not found; please install @appland/appmap-swagger from NPM)) unless File.exists?(swaggergen)

  warn swagger_command.join(' ') if verbose

  swagger_raw = `#{swagger_command.join(' ')}`.strip
  return do_fail.(%Q(Swagger generation failed: #{swagger_raw})) if $?.exitstatus != 0

  gen_swagger = YAML.load(swagger_raw)
  gen_swagger_full = AppMap::Swagger::MarkdownDescriptions.new(gen_swagger).perform
  gen_swagger_stable = AppMap::Swagger::Stable.new(gen_swagger).perform

  swagger = swagger_template.merge(gen_swagger_full)
  File.write File.join(output_dir, 'openapi.yaml'), YAML.dump(swagger)

  swagger = swagger_template.merge(gen_swagger_stable)
  File.write File.join(output_dir, 'openapi_stable.yaml'), YAML.dump(swagger)
end

#swagger_commandObject



85
86
87
# File 'lib/appmap/swagger/rake_task.rb', line 85

def swagger_command
  [ 'node', swaggergen, 'generate', '--directory', appmap_dir ]
end

#swagger_templateObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/appmap/swagger/rake_task.rb', line 69

def swagger_template
  YAML.load <<~TEMPLATE
  openapi: 3.0.1
  info:
    title: #{project_name}
    version: #{project_version}
  paths:
  components:
  servers:
  - url: http://{defaultHost}
    variables:
      defaultHost:
        default: localhost:3000
  TEMPLATE
end

#verboseObject



38
39
40
# File 'lib/appmap/swagger/rake_task.rb', line 38

def verbose
  Rake.verbose == true
end