discourse/lib/tasks/api_docs.rake
Kelv 5d92499edd
FIX: only load the api-docs rake task definition when required gems are available (#35432)
This task was failing to load rspec-core because `lib/tasks` are loaded
in full in prod environments and this was meant to be run only in
dev/test environments (the task it is overwriting is defined in a gem
that's only loaded in those environments).

If we add more tasks like this, we can create a new subfolder in `tasks`
to segregate such tasks in a clearer manner.
2025-10-16 12:36:53 +08:00

44 lines
1.5 KiB
Ruby

# frozen_string_literal: true
# Only define this task when in development/test environments
# This prevents LoadError when running other rake tasks in environments without test gems
if Rails.env.local?
require "rspec/core/rake_task"
# Override the default rswag:specs:swaggerize task to include plugin API specs
namespace :rswag do
namespace :specs do
# Clear the existing task so we can redefine it
if Rake::Task.task_defined?("rswag:specs:swaggerize")
Rake::Task["rswag:specs:swaggerize"].clear
end
desc "Generate Swagger JSON files from integration specs (including plugins)"
RSpec::Core::RakeTask.new("swaggerize") do |t|
# Automatically load plugins for API documentation
ENV["LOAD_PLUGINS"] = "1" unless ENV["LOAD_PLUGINS"]
# Include plugin API specs in the pattern
t.pattern =
ENV.fetch(
"PATTERN",
"spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb, plugins/*/spec/requests/api/*_spec.rb",
)
additional_rspec_opts = ENV.fetch("ADDITIONAL_RSPEC_OPTS", "")
t.rspec_opts = [additional_rspec_opts]
if Rswag::Specs.config.rswag_dry_run
t.rspec_opts += [
"--format Rswag::Specs::SwaggerFormatter",
"--dry-run",
"--order defined",
]
else
t.rspec_opts += ["--format Rswag::Specs::SwaggerFormatter", "--order defined"]
end
end
end
end
end