require 'faraday' require 'json' require 'fileutils' require 'pathname' require 'active_support/core_ext/string' require_relative 'runner' require_relative 'templates' module SchemaDev class Gem def self.build(name) new(name).build end attr_accessor :gem_name, :gem_module, :gem_root, :gem_parent_name, :gem_base_name, :gem_lib_path, :fullname, :email def initialize(name) self.gem_name = name.underscore self.gem_root = Pathname.new(gem_name) if gem_name =~ /^(schema_plus)_(.*)/ parent, base = [$1, $2] self.gem_module = [parent, base].map(&:camelize).join('::') self.gem_lib_path = [parent, base].join('/') self.gem_parent_name = parent self.gem_base_name = base @subdir = true else self.gem_module = gem_name.camelize self.gem_lib_path = gem_name self.gem_base_name = gem_name @subdir = false end get_fullname_and_email end def build ensure_not_in_git ensure_doesnt_exist copy_template self.gem_root = self.gem_root.realpath rename_files fixup_subdir if @subdir substitute_keys freshen git_init puts <<-END.strip_heredoc Created #{gem_name}. Your recommended next steps are: $ cd #{gem_name} $ bundle install $ schema_dev bundle install $ schema_dev rspec END end def die(msg) abort "schema_dev: #{msg}" end def ensure_not_in_git if system("git rev-parse >& /dev/null") die "Cannot create new gem inside existing git worktree; please cd elsewhere" end end def ensure_doesnt_exist if gem_root.exist? die "Cannot create new gem: '#{gem_root}' already exists" end end def get_fullname_and_email {'fullname' => 'name', 'email' => 'email' }.each do |myattr, gitattr| if (self.send myattr+"=", `git config user.#{gitattr}`.strip).blank? die "Who are you? Please run 'git config --global user.#{gitattr} '" end end end def copy_template FileUtils.cp_r Templates.root + "gem", gem_root (gem_root + "gitignore").rename gem_root + ".gitignore" end def rename_files Dir.glob(gem_root + "**/*GEM_NAME*").each do |path| FileUtils.mv path, path.gsub(/GEM_NAME/, gem_name) end Dir.glob(gem_root + "**/*GEM_BASE_NAME*").each do |path| FileUtils.mv path, path.gsub(/GEM_BASE_NAME/, gem_base_name) end end def fixup_subdir libdir = gem_root + "lib" aside = libdir.to_s + "x" subdir = libdir + gem_parent_name FileUtils.mv libdir, aside libdir.mkpath FileUtils.mv aside, subdir (gem_root + "lib" + "#{gem_name}.rb").write <<-END.lstrip require_relative '#{gem_parent_name}/#{gem_base_name}' END end def substitute_keys gem_root.find.each do |path| next unless path.file? path.write subs(path.read) end end def subs(s) s = s.gsub('%GEM_NAME%', gem_name) s = s.gsub('%GEM_BASE_NAME%', gem_base_name) s = s.gsub('%GEM_LIB_PATH%', gem_lib_path) s = s.gsub('%GEM_MODULE%', gem_module) s = s.gsub('%FULLNAME%', fullname) s = s.gsub('%EMAIL%', email) s = s.gsub('%SCHEMA_PLUS_CORE_DEPENDENCY%') { dependency(schema_plus_core_version) } s = s.gsub('%SCHEMA_DEV_DEPENDENCY%', dependency(SchemaDev::VERSION)) s = s.gsub('%YEAR%', Time.now.strftime("%Y")) end def dependency(v) major, minor, patch = v.split('.') dep = %Q{"~> #{major}.#{minor}"} dep += %Q{, ">= #{v}"} if patch != "0" dep end def 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"] end end def freshen Dir.chdir gem_root do Runner.new(Config.read).freshen(quiet:true) end end 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(' ')}" system "git commit -m 'Initial skeleton generated by `schema_dev gem #{gem_name}`'" end end end end