Module: OpenapiRuby::Generator::RakeTaskSupport

Defined in:
lib/openapi_ruby/generator/rake_task_support.rb

Overview

Helpers backing the ‘openapi_ruby:generate` rake task. Extracted so the framework detection / script generation logic is testable without booting Rake.

Class Method Summary collapse

Class Method Details

.default_pattern_for(framework) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 27

def default_pattern_for(framework)
  case framework
  when "rspec" then "spec/**/*_spec.rb"
  when "minitest" then "test/**/*_test.rb"
  when "hybrid" then "spec/**/*_spec.rb,test/**/*_test.rb"
  end
end

.detect_test_frameworkObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 11

def detect_test_framework
  rspec = File.exist?("spec/spec_helper.rb") || File.exist?("spec/rails_helper.rb")
  minitest = File.exist?("test/test_helper.rb")

  if rspec && minitest
    "hybrid"
  elsif rspec
    "rspec"
  elsif minitest
    "minitest"
  else
    raise ArgumentError,
      "Could not detect test framework. Set FRAMEWORK=rspec, FRAMEWORK=minitest, or FRAMEWORK=hybrid."
  end
end

.generate_script(framework, pattern) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 35

def generate_script(framework, pattern)
  case framework
  when "rspec" then rspec_script(pattern)
  when "minitest" then minitest_script(pattern)
  when "hybrid" then hybrid_script(pattern)
  else
    raise ArgumentError, "Unknown test framework '#{framework}'."
  end
end

.glob_loads(pattern) ⇒ Object



82
83
84
85
86
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 82

def glob_loads(pattern)
  pattern.split(",").map do |p|
    %[Dir.glob(#{p.strip.inspect}).sort.each { |f| require File.expand_path(f) }]
  end.join("\n")
end

.hybrid_script(pattern) ⇒ Object

Loads both adapters and both file globs in one process. Useful during a phased RSpec → Minitest migration where the suite holds both DSL styles. Consumers should guard ‘require “rails/test_help”` and `require “rspec/rails”` in their test helpers with `unless OpenapiRuby.schema_generating?` so the two test frameworks don’t both register Rails lazy hooks in the same process — only the DSL needs to be live for schema generation.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 70

def hybrid_script(pattern)
  <<~RUBY
    require "rspec/core"
    require "openapi_ruby/rspec"
    require "openapi_ruby/minitest"
    $LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
    $LOAD_PATH.unshift(File.expand_path("test")) unless $LOAD_PATH.include?(File.expand_path("test"))
    #{glob_loads(pattern)}
    OpenapiRuby::Generator::SchemaWriter.generate_all!
  RUBY
end

.minitest_script(pattern) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 54

def minitest_script(pattern)
  <<~RUBY
    require "openapi_ruby/minitest"
    #{glob_loads(pattern)}
    OpenapiRuby::Generator::SchemaWriter.generate_all!
  RUBY
end

.rspec_script(pattern) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 45

def rspec_script(pattern)
  <<~RUBY
    require "rspec/core"
    $LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
    #{glob_loads(pattern)}
    OpenapiRuby::Generator::SchemaWriter.generate_all!
  RUBY
end