# -*- mode: ruby; coding: utf-8 -*-
# frozen_string_literal: true

# Rakefile for DataPlx package.

task default: %w[test]

# Testing with Minitest

desc 'Execute the tests in spec/ (with coverage)'
task :test do
  require_relative 'spec/spec_helper'
  DATAPLX_ITK_GEM.files.grep(%r{^spec/.*_spec.rb$}).each do |t|
    require_relative t
  end
end


# Gem packaging

require 'rubygems/package_task'

load 'dataplx-itk.gemspec'
Gem::PackageTask.new(DATAPLX_ITK_GEM) do |pkg|
  pkg.need_zip = false
  pkg.need_tar = false
end


# Documentation with YARD

require 'yard'
require 'yard/rake/yardoc_task'

YARD::Rake::YardocTask.new(:doc) do |task| # uses .yardopts
  task.stats_options = %w[--list-undoc --compact]
end


# StyleGuide/Ruby checks with Rubocop

begin
  require 'rubocop/rake_task'
  RuboCop::RakeTask.new(:lint) do |task|
    task.patterns =
      DATAPLX_ITK_GEM.files - %w[.yardopts .rubocop.yml README.md]
    task.verbose = true
    task.options = %w[--debug]
  end
rescue LoadError
  warn '(no rubocop support)'
end

desc 'Quick syntax check (ruby -c)'
task :syntax do
  DATAPLX_ITK_GEM.files.grep(/\.rb$/) { |rb| ruby '-c', rb }
end

# MkDocs documentation

namespace :mkdocs do
  desc 'Generate MkDocs reference pages from transform metadata'
  task :generate do
    ruby '-Ilib', 'tools/mkdocs_reference.rb'
    ruby '-e', <<~RUBY
      require 'dataplx/mkdocs_generator'
      DataPlx::MkDocsGenerator
        .write_llms_postprocess('tmp/llms_postprocess.py')
    RUBY
  end

  desc 'Build MkDocs site and post-process llms/'
  task build: :generate do
    sh 'uv', 'run', '--group', 'doc', 'mkdocs', 'build'
    sh 'python3', 'tmp/llms_postprocess.py'
  end

  desc 'Serve MkDocs locally'
  task serve: :generate do
    sh 'uv', 'run', '--group', 'doc', 'mkdocs', 'serve'
  end

  desc 'Build PDF guide'
  task pdf: :generate do
    sh 'ENABLE_PDF_EXPORT=1 uv run --group doc mkdocs build' \
       ' -f mkdocs_pdf.yml'
    mkdir_p 'pdf'
    mv 'html/DataPlx-ITK-Guide.pdf', 'pdf/'
  end

  desc 'Build all: PDF, then HTML+llms'
  task all: :generate do
    Rake::Task['mkdocs:pdf'].invoke
    Rake::Task['mkdocs:build'].invoke
  end
end


# This Gem was originally generated by 'mkstuff gem', here's how to re-run it:

desc 'Regenerate the Gem boiler-plate files'
task :mkstuff do
  # rubocop:disable Style/StringLiterals, Layout/LineLength
  system 'mkstuff', "gem", "dataplx-itk", "-m", "DataPlx", "--author", "Christian Boos <cboos@bct-technology.com>"
  # rubocop:enable Style/StringLiterals, Layout/LineLength
end
