Skip to content

Commit

Permalink
Merge pull request #26 from SchemaPlus/feat-cleanup
Browse files Browse the repository at this point in the history
Remove no longer needed gems and update code to be ruby 2.5+ compliant
  • Loading branch information
urkle committed Dec 9, 2021
2 parents 55dc43b + f116297 commit 5642276
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 58 deletions.
47 changes: 20 additions & 27 deletions lib/schema_dev/config.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
require 'active_support/core_ext/hash'
require 'enumerator'
require 'fastandand'
require 'its-it'
require 'key_struct'
require 'pathname'
require 'yaml'
require 'hash_keyword_args'

module SchemaDev
CONFIG_FILE = "schema_dev.yml"
Expand All @@ -17,22 +13,21 @@ class Config
def self._reset ; @@config = nil end # for use by rspec

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

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

def initialize(opts={}) # once we no longer support ruby 1.9.3, can switch to native keyword args
opts = opts.keyword_args(ruby: :required, activerecord: :required, db: :required, dbversions: nil, exclude: nil, notify: nil, quick: nil)
@ruby = Array.wrap(opts.ruby)
@activerecord = Array.wrap(opts.activerecord)
@db = Array.wrap(opts.db)
@dbversions = (opts.dbversions || {}).symbolize_keys
@exclude = Array.wrap(opts.exclude).map(&:symbolize_keys).map {|tuple| Tuple.new(tuple)}
@notify = Array.wrap(opts.notify)
@quick = Array.wrap(opts.quick || {ruby: @ruby.last, activerecord: @activerecord.last, db: @db.last})
def initialize(ruby:, activerecord:, db:, dbversions: nil, exclude: nil, notify: nil, quick: nil)
@ruby = Array.wrap(ruby)
@activerecord = Array.wrap(activerecord)
@db = Array.wrap(db)
@dbversions = (dbversions || {}).symbolize_keys
@exclude = Array.wrap(exclude).map(&:symbolize_keys).map {|tuple| Tuple.new(**tuple)}
@notify = Array.wrap(notify)
@quick = Array.wrap(quick || {ruby: @ruby.last, activerecord: @activerecord.last, db: @db.last})
end

def dbms
Expand All @@ -43,37 +38,36 @@ def dbms_versions_for(db, default = [])
@dbversions.fetch(db, default)
end

def matrix(opts={}) # once we no longer support ruby 1.9.3, can switch to native keyword args
opts = opts.keyword_args(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil)
def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil)
use_ruby = @ruby
use_activerecord = @activerecord
use_db = @db
if opts.quick
if quick
use_ruby = @quick.map{|q| q[:ruby]}
use_activerecord = @quick.map{|q| q[:activerecord]}
use_db = @quick.map{|q| q[:db]}
end
use_ruby = Array.wrap(opts.ruby) if opts.ruby
use_activerecord = Array.wrap(opts.activerecord) if opts.activerecord
use_db = Array.wrap(opts.db) if opts.db
use_ruby = Array.wrap(ruby) if ruby
use_activerecord = Array.wrap(activerecord) if activerecord
use_db = Array.wrap(db) if db

use_ruby = [nil] unless use_ruby.any?
use_activerecord = [nil] unless use_activerecord.any?
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.reject(&it.match_any?(@exclude)) unless opts.excluded == :none
m = m.reject { |r| r.match_any?(@exclude) } unless excluded == :none
m = m.map(&:to_hash)

if opts.excluded == :only
return matrix(opts.merge(excluded: :none)) - m
if excluded == :only
matrix(quick: quick, ruby: ruby, activerecord: activerecord, db: db, excluded: :none) - m
else
return m
m
end
end

class Tuple < KeyStruct[:ruby, :activerecord, :db]
Tuple = Struct.new(:ruby, :activerecord, :db, 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
Expand All @@ -86,9 +80,8 @@ def match_any?(others)
end

def to_hash
super.reject{ |k, val| val.nil? }
to_h.compact
end
end

end
end
10 changes: 8 additions & 2 deletions lib/schema_dev/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def get_binding
def self.schema_plus_core_version
@core_version ||= begin
gems = JSON.parse Faraday.get('https://rubygems.org/api/v1/versions/schema_plus_core.json').body
gems.reject(&it["prerelease"]).sort_by(&it["number"].split('.')).last["number"]
gems.reject { |e| e["prerelease"] }
.sort_by { |e| e["number"].split('.') }
.last["number"]
end
end

Expand Down Expand Up @@ -163,7 +165,11 @@ def freshen
def git_init
Dir.chdir gem_name do
system "git init"
system "git add #{gem_root.find.select(&:exist?).reject(&it.basename.to_s == 'Gemfile.local').join(' ')}"
add_param = gem_root.find
.select(&:exist?)
.reject { |e| e.basename.to_s == 'Gemfile.local' }
.join(' ')
system "git add #{add_param}"
system "git commit -m 'Initial skeleton generated by `schema_dev gem #{gem_name}`'"
end
end
Expand Down
10 changes: 4 additions & 6 deletions lib/schema_dev/gemfile_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ module SchemaDev
module GemfileSelector
extend self

def gemfile(opts = {})
opts = opts.keyword_args(activerecord: :required, db: :required)
Pathname.new(GEMFILES_DIR).join("activerecord-#{opts.activerecord}", "Gemfile.#{opts.db}")
def gemfile(activerecord:, db:)
Pathname.new(GEMFILES_DIR).join("activerecord-#{activerecord}", "Gemfile.#{db}")
end

def command(opts={})
opts = opts.keyword_args(activerecord: :required, db: :required)
"BUNDLE_GEMFILE=#{gemfile(activerecord: opts.activerecord, db: opts.db)}"
def command(activerecord:, db:)
"BUNDLE_GEMFILE=#{gemfile(activerecord: activerecord, db: db)}"
end

def infer_db
Expand Down
6 changes: 3 additions & 3 deletions lib/schema_dev/readme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def sub_matrix(lines)
replace_block(lines, %r{^\s*<!-- SCHEMA_DEV: MATRIX}) do |contents|
contents << "<!-- SCHEMA_DEV: MATRIX - begin -->\n"
contents << "<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->\n"
self.matrix.group_by(&it.slice(:ruby, :activerecord)).each do |pair, items|
self.matrix.group_by {|e| e.slice(:ruby, :activerecord) }.each do |pair, items|
contents << "* ruby **#{pair[:ruby]}** with activerecord **#{pair[:activerecord]}**, using #{items.map{|item| "**#{item[:db]}**"}.to_sentence(last_word_connector: ' or ')}\n"
end
contents << "\n"
Expand Down Expand Up @@ -57,9 +57,9 @@ def sub_template(template, lines)
end

def replace_block(lines, pattern)
before = lines.take_while(&it !~ pattern)
before = lines.take_while { |e| e !~ pattern }
return lines if before == lines
after = lines.reverse.take_while(&it !~ pattern).reverse
after = lines.reverse.take_while { |e| e !~ pattern }.reverse
contents = []
yield contents
before + contents + after
Expand Down
5 changes: 0 additions & 5 deletions lib/schema_dev/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ def self.setup
Db.setup
end

def self.setup_db
ActiveSupport::Deprecation.warn "SchemaDev::Rspec.setup_db is deprecated. Use SchemaDev::Rspec.setup"
self.setup
end

def self.db_configuration
Db.configuration
end
Expand Down
10 changes: 7 additions & 3 deletions lib/schema_dev/ruby_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module SchemaDev
module RubySelector
def self.command(ruby)
@@selector ||= [Chruby, Rvm, Rbenv].find(&:installed?).andand.new || abort("No ruby version manager found")
@@selector ||= [Chruby, Rvm, Rbenv].find(&:installed?)&.new || abort("No ruby version manager found")
@@selector.command ruby
end
def self._reset # for rspec, to avoid stickiness
Expand All @@ -21,11 +21,15 @@ class Chruby < ManagerBase
CORE_COMMAND = "chruby-exec"

def initialize
@rubies = Pathname.new(ENV['HOME']).join(".rubies").entries().map(&its.basename.to_s)
@rubies = Pathname.new(ENV['HOME'])
.join(".rubies")
.entries()
.map { |e| e.basename.to_s }
end
def command(ruby)
bash = Which.which 'bash' || abort("no bash shell found")
ruby = @rubies.select(&it =~ /^(ruby-)?#{ruby}(-p.*)?$/).last || ruby
ruby = @rubies.select { |e| e =~ /^(ruby-)?#{ruby}(-p.*)?$/ }
.last || ruby
"SHELL=#{bash} #{CORE_COMMAND} #{ruby} --"
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/schema_dev/travis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def build(config)
else
# we need to include against the various gemfiles so we only use PG for PG tests (and not other DBs)
config.matrix(db: 'postgresql').map { |entry|
gemfile = GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s
gemfile = GemfileSelector.gemfile(**entry.slice(:activerecord, :db)).to_s
skip_gemfiles << gemfile
include.concat versions.map {|version|
{
Expand All @@ -86,7 +86,7 @@ def build(config)
services << 'mysql'
else
config.matrix(db: 'mysql2').map do |entry|
gemfile = GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s
gemfile = GemfileSelector.gemfile(**entry.slice(:activerecord, :db)).to_s
skip_gemfiles << gemfile
include << {
"gemfile" => gemfile,
Expand All @@ -99,12 +99,12 @@ def build(config)
end
env = env.join(' ')

gemfiles = config.matrix.map{|entry| GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s}.uniq
gemfiles = config.matrix.map{|entry| GemfileSelector.gemfile(**entry.slice(:activerecord, :db)).to_s}.uniq
gemfiles.reject! { |gemfile| skip_gemfiles.include?(gemfile) }

exclude = config.matrix(excluded: :only).map { |entry| {}.tap {|ex|
ex["rvm"] = entry[:ruby]
ex["gemfile"] = GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s
ex["gemfile"] = GemfileSelector.gemfile(**entry.slice(:activerecord, :db)).to_s
}}.reject{|ex| not gemfiles.include? ex["gemfile"]}

{}.tap { |travis|
Expand Down
12 changes: 4 additions & 8 deletions schema_dev.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ Gem::Specification.new do |gem|

gem.add_dependency "activesupport", ">= 5.2", "< 6.2"
gem.add_dependency "coveralls_reborn", "~> 0.23"
gem.add_dependency "faraday", "~> 0.9"
gem.add_dependency "fastandand", "~> 1.0"
gem.add_dependency "hash_keyword_args", "~> 0.1"
gem.add_dependency "its-it", "~> 1.3"
gem.add_dependency "key_struct", "~> 0.4"
gem.add_dependency "thor", "~> 0.19"
gem.add_dependency "faraday", "~> 1.0"
gem.add_dependency "thor", '>= 0.19', '< 2.0'
gem.add_dependency "which_works", "~> 1.0"

gem.add_development_dependency "bundler"
gem.add_development_dependency "rake", "~> 10.0"
gem.add_development_dependency "rake", "~> 13.0"
gem.add_development_dependency "rspec", "~> 3.0"
gem.add_development_dependency "rspec-given", "~> 3.8"
gem.add_development_dependency "simplecov"
gem.add_development_dependency "webmock", "~> 2.1"
gem.add_development_dependency "webmock", "~> 3.0"
end

0 comments on commit 5642276

Please sign in to comment.