Skip to content

Commit

Permalink
rework freshen to generate github actions scripts
Browse files Browse the repository at this point in the history
- cleanup config to handle DB versions better
- rewrite the "create_databases/drop_databases" to utilize AR's database tasks
- update initial gem template
  • Loading branch information
urkle committed Dec 10, 2021
1 parent f5ce221 commit 75a9c5c
Show file tree
Hide file tree
Showing 19 changed files with 1,205 additions and 618 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# SchemaDev

[![Gem Version](https://badge.fury.io/rb/schema_dev.svg)](http://badge.fury.io/rb/schema_dev)
[![Build Status](https://secure.travis-ci.org/SchemaPlus/schema_dev.svg)](http://travis-ci.org/SchemaPlus/schema_dev)
[![Build Status](https://github.com/SchemaPlus/schema_dev/actions/workflows/prs.yml/badge.svg)](https://github.com/SchemaPlus/schema_dev/actions)
[![Coverage Status](https://img.shields.io/coveralls/SchemaPlus/schema_dev.svg)](https://coveralls.io/r/SchemaPlus/schema_dev)
[![Dependency Status](https://gemnasium.com/SchemaPlus/schema_dev.svg)](https://gemnasium.com/SchemaPlus/schema_dev)

Development tools for the SchemaPlus family of gems.

Provides support for working with multiple ruby versions, ActiveRecord, and db versions. In particular provides a command `schema_dev` for running rspec (or whatever) on the matrix or a slice or element of it. It also auto-generates the `.travis.yml` file for [travis-ci](https://travis-ci.org) testing.
Provides support for working with multiple ruby versions, ActiveRecord, and db versions. In particular provides a command `schema_dev` for running rspec (or whatever) on the matrix or a slice or element of it. It also auto-generates the `.github/workflows/prs.yml` file for [github actions](https://docs.github.com/en/actions) testing.

## Creating a new gem for the SchemaPlus family

Expand Down Expand Up @@ -99,9 +98,9 @@ Note that freshening the gemfiles happens automatically whenever you run a schem

If you need to include extra specifications in the Gemfile (e.g. to specify a path for a gem), you can create a file `Gemfile.local` in the project root, and its contents will be included in the Gemfile.

### .travis.yml
### .github/workflows/prs.yml

The `.travis.yml` file gets created automatically. Don't edit it by hand.
The `.github/workflows/prs.yml` file gets created automatically. Don't edit it by hand.

### README.md

Expand Down Expand Up @@ -131,9 +130,9 @@ The client gem's `Rakefile` includes:

require 'schema_dev/tasks'

Which defines the rake task `create_databases` and also a task for travis-ci
Which defines the rake task `create_databases` and also a task for github actions

## Relase Notes
## Release Notes

Release notes for schema_dev versions:

Expand Down
2 changes: 1 addition & 1 deletion bin/schema_dev
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CLI < Thor
method_option :db, type: :string, desc: "Only execute for the specified database"
end

desc "freshen", "update files that depend on schema_dev.yml: .travis, gemfiles/, README.md"
desc "freshen", "update files that depend on schema_dev.yml: .github/worksflows/prs.yml, gemfiles/, README.md"
def freshen
runner.freshen
end
Expand Down
49 changes: 35 additions & 14 deletions lib/schema_dev/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,57 @@
require 'enumerator'
require 'pathname'
require 'yaml'
require 'rubygems/version'

module SchemaDev
CONFIG_FILE = "schema_dev.yml"

class Config

attr_accessor :quick, :db, :dbversions, :ruby, :activerecord, :notify, :exclude
attr_accessor :quick, :db, :dbversions, :ruby, :activerecord, :exclude

def self._reset ; @@config = nil end # for use by rspec
def self._reset ; @config = nil end # for use by rspec

def self.read
new(**(YAML.load Pathname.new(CONFIG_FILE).read).symbolize_keys)
end

def self.load
@@config ||= read
@config ||= read
end

def initialize(ruby:, activerecord:, db:, dbversions: nil, exclude: nil, notify: nil, quick: nil)
@ruby = Array.wrap(ruby)
@activerecord = Array.wrap(activerecord)
@ruby = Array.wrap(ruby).map(&:to_s)
@activerecord = Array.wrap(activerecord).map(&:to_s)
@db = Array.wrap(db)
@dbversions = (dbversions || {}).symbolize_keys
@exclude = Array.wrap(exclude).map(&:symbolize_keys).map {|tuple| Tuple.new(**tuple)}
@notify = Array.wrap(notify)
@exclude = Array.wrap(exclude).map(&:symbolize_keys).map {|tuple| Tuple.new(**tuple.transform_values(&:to_s))}
if @activerecord.include?('5.2')
ruby3 = ::Gem::Version.new('3.0')

@ruby.select { |e| ::Gem::Version.new(e) >= ruby3 }.each do |v|
@exclude << Tuple.new(ruby: v, activerecord: '5.2')
end
end
unless notify.nil?
warn "Notify is no longer supported"
end
@quick = Array.wrap(quick || {ruby: @ruby.last, activerecord: @activerecord.last, db: @db.last})
end

def dbms
@dbms ||= [:postgresql, :mysql].select{|dbm| @db.grep(/^#{dbm}/).any?}
end

def dbms_versions_for(db, default = [])
@dbversions.fetch(db, default)
DB_VERSION_DEFAULTS = {
postgresql: ['9.6']
}

def db_versions_for(db)
@dbversions.fetch(db.to_sym, DB_VERSION_DEFAULTS.fetch(db.to_sym, [])).map(&:to_s)
end

def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil)
def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil, with_dbversion: false)
use_ruby = @ruby
use_activerecord = @activerecord
use_db = @db
Expand All @@ -56,22 +70,29 @@ def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil)
use_db = [nil] unless use_db.any?

m = use_ruby.product(use_activerecord, use_db)
m = m.map { |_ruby, _activerecord, _db| Tuple.new(ruby: _ruby, activerecord: _activerecord, db: _db) }.compact
m = m.flat_map { |_ruby, _activerecord, _db|
if with_dbversion && !(dbversions = db_versions_for(_db)).empty?
dbversions.map { |v| Tuple.new(ruby: _ruby, activerecord: _activerecord, db: _db, dbversion: v) }
else
[Tuple.new(ruby: _ruby, activerecord: _activerecord, db: _db)]
end
}.compact
m = m.reject { |r| r.match_any?(@exclude) } unless excluded == :none
m = m.map(&:to_hash)

if excluded == :only
matrix(quick: quick, ruby: ruby, activerecord: activerecord, db: db, excluded: :none) - m
matrix(quick: quick, ruby: ruby, activerecord: activerecord, db: db, with_dbversion: with_dbversion, excluded: :none) - m
else
m
end
end

Tuple = Struct.new(:ruby, :activerecord, :db, keyword_init: true) do
Tuple = Struct.new(:ruby, :activerecord, :db, :dbversion, keyword_init: true) do
def match?(other)
return false if self.ruby and other.ruby and self.ruby != other.ruby
return false if self.activerecord and other.activerecord and self.activerecord != other.activerecord
return false if self.db and other.db and self.db != other.db
return false if self.dbversion and other.dbversion and self.dbversion != other.dbversion
true
end

Expand All @@ -80,7 +101,7 @@ def match_any?(others)
end

def to_hash
to_h.compact
to_h.compact.transform_values(&:to_s)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/schema_dev/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def copy_template

def rename_files
(gem_root + "gitignore").rename gem_root + ".gitignore"
(gem_root + "simplecov").rename gem_root + ".simplecov"
Dir.glob(gem_root + "**/*GEM_NAME*").each do |path|
FileUtils.mv path, path.gsub(/GEM_NAME/, gem_name)
end
Expand Down

0 comments on commit 75a9c5c

Please sign in to comment.