class Gemika::GithubActionsGenerator

Constants

TYPES

Public Class Methods

new(bundler_version:) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 46
def initialize(bundler_version:)
  @bundler_version = bundler_version
end

Public Instance Methods

generate(rows) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 50
def generate(rows)
  rows_by_type = split_rows_by_gemfile(rows)
  jobs = {}
  rows_by_type.each do |type, type_rows|
    jobs[type.to_s] = job_by_type(type, type_rows)
  end
  full_config(jobs)
end

Private Instance Methods

full_config(jobs) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 135
def full_config(jobs)
  {
    'name' => 'Tests',
    'on' => {
      'push' => {
        'branches' => ['master'],
      },
      'pull_request' => {
        'branches' => ['master'],
      },
    },
    'jobs' => jobs,
  }
end
full_matrix(rows) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 113
def full_matrix(rows)
  rubies = rows.map(&:ruby)
  gemfiles = rows.map(&:gemfile)
  if rubies.size * gemfiles.size == rows.size
    {
      'ruby' => rubies,
      'gemfile' => gemfiles,
    }
  end
end
include_matrix(rows) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 124
def include_matrix(rows)
  {
    'include' => rows.map do |row|
      {
        'ruby' => row.ruby,
        'gemfile' => row.gemfile,
      }
    end,
  }
end
job_by_type(type, rows) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 69
def job_by_type(type, rows)
  matrix = full_matrix(rows) || include_matrix(rows)
  type_definition = TYPES[type]

  steps = [{
    'uses' => 'actions/checkout@v2',
  }, {
    'name' => 'Install ruby',
    'uses' => 'ruby/setup-ruby@v1',
    'with' => {'ruby-version' => '${{ matrix.ruby }}'},
  }]

  if (database_setup = type_definition[:database_setup])
    steps << {
      'name' => 'Setup database',
      'run' => database_setup.join("\n") + "\n",
    }
  end

  steps += [{
    'name' => 'Bundle',
    'run' => "gem install bundler:#{@bundler_version}\nbundle install --no-deployment\n",
  }, {
    'name' => 'Run tests',
    'run' => 'bundle exec rspec',
  }]

  job = {}
  job['runs-on'] = 'ubuntu-20.04'
  if (services = type_definition[:services])
    job['services'] = services
  end
  job['strategy'] = {
    'fail-fast' => false,
    'matrix' => matrix,
  }
  job['env'] = {
    'BUNDLE_GEMFILE' => '${{ matrix.gemfile }}',
  }
  job['steps'] = steps

  job
end
split_rows_by_gemfile(rows) click to toggle source
# File lib/gemika/github_actions_generator.rb, line 61
def split_rows_by_gemfile(rows)
  rows.group_by do |row|
    TYPES.detect do |type, type_definition|
      row.gemfile =~ type_definition[:gemfile_filter]
    end.first
  end
end