Class: SchemaDev::Gem
- Inherits:
-
Object
- Object
- SchemaDev::Gem
- Defined in:
- lib/schema_dev/gem.rb
Defined Under Namespace
Classes: TemplateEnv
Instance Attribute Summary collapse
-
#email ⇒ Object
Returns the value of attribute email.
-
#fullname ⇒ Object
Returns the value of attribute fullname.
-
#gem_base_name ⇒ Object
Returns the value of attribute gem_base_name.
-
#gem_lib_path ⇒ Object
Returns the value of attribute gem_lib_path.
-
#gem_module ⇒ Object
Returns the value of attribute gem_module.
-
#gem_name ⇒ Object
Returns the value of attribute gem_name.
-
#gem_parent_name ⇒ Object
Returns the value of attribute gem_parent_name.
-
#gem_root ⇒ Object
Returns the value of attribute gem_root.
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #copy_template ⇒ Object
- #die(msg) ⇒ Object
- #ensure_doesnt_exist ⇒ Object
- #ensure_not_in_git ⇒ Object
- #erb(string) ⇒ Object
- #fixup_subdir ⇒ Object
- #freshen ⇒ Object
- #get_fullname_and_email ⇒ Object
- #git_init ⇒ Object
-
#initialize(name) ⇒ Gem
constructor
A new instance of Gem.
- #rename_files ⇒ Object
- #template_binding ⇒ Object
Constructor Details
#initialize(name) ⇒ Gem
Returns a new instance of Gem.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/schema_dev/gem.rb', line 62 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 |
Instance Attribute Details
#email ⇒ Object
Returns the value of attribute email.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def email @email end |
#fullname ⇒ Object
Returns the value of attribute fullname.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def fullname @fullname end |
#gem_base_name ⇒ Object
Returns the value of attribute gem_base_name.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def gem_base_name @gem_base_name end |
#gem_lib_path ⇒ Object
Returns the value of attribute gem_lib_path.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def gem_lib_path @gem_lib_path end |
#gem_module ⇒ Object
Returns the value of attribute gem_module.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def gem_module @gem_module end |
#gem_name ⇒ Object
Returns the value of attribute gem_name.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def gem_name @gem_name end |
#gem_parent_name ⇒ Object
Returns the value of attribute gem_parent_name.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def gem_parent_name @gem_parent_name end |
#gem_root ⇒ Object
Returns the value of attribute gem_root.
18 19 20 |
# File 'lib/schema_dev/gem.rb', line 18 def gem_root @gem_root end |
Class Method Details
.build(name) ⇒ Object
14 15 16 |
# File 'lib/schema_dev/gem.rb', line 14 def self.build(name) new(name).build end |
Instance Method Details
#build ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/schema_dev/gem.rb', line 81 def build ensure_not_in_git ensure_doesnt_exist copy_template self.gem_root = gem_root.realpath rename_files fixup_subdir if @subdir freshen git_init puts <<~TEXT Created #{gem_name}. Your recommended next steps are: $ cd #{gem_name} $ bundle install $ schema_dev bundle install $ schema_dev rspec TEXT end |
#copy_template ⇒ Object
125 126 127 |
# File 'lib/schema_dev/gem.rb', line 125 def copy_template Templates.install_subtree src: 'gem', dst: gem_root, bound: template_binding end |
#die(msg) ⇒ Object
101 102 103 |
# File 'lib/schema_dev/gem.rb', line 101 def die(msg) abort "schema_dev: #{msg}" end |
#ensure_doesnt_exist ⇒ Object
111 112 113 114 115 |
# File 'lib/schema_dev/gem.rb', line 111 def ensure_doesnt_exist if gem_root.exist? die "Cannot create new gem: '#{gem_root}' already exists" end end |
#ensure_not_in_git ⇒ Object
105 106 107 108 109 |
# File 'lib/schema_dev/gem.rb', line 105 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 |
#erb(string) ⇒ Object
153 154 155 |
# File 'lib/schema_dev/gem.rb', line 153 def erb(string) ERB.new(string).result template_binding end |
#fixup_subdir ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/schema_dev/gem.rb', line 140 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 <<~RUBY require_relative '#{gem_parent_name}/#{gem_base_name}' RUBY end |
#freshen ⇒ Object
161 162 163 164 165 |
# File 'lib/schema_dev/gem.rb', line 161 def freshen Dir.chdir gem_root do Runner.new(Config.read).freshen(quiet: true) end end |
#get_fullname_and_email ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/schema_dev/gem.rb', line 117 def get_fullname_and_email { 'fullname' => 'name', 'email' => 'email' }.each do |myattr, gitattr| if (send myattr + '=', `git config user.#{gitattr}`.strip).blank? die "Who are you? Please run 'git config --global user.#{gitattr} <your-#{gitattr}>'" end end end |
#git_init ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/schema_dev/gem.rb', line 167 def git_init Dir.chdir gem_name do system 'git init' 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 |
#rename_files ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/schema_dev/gem.rb', line 129 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 Dir.glob(gem_root + '**/*GEM_BASE_NAME*').each do |path| FileUtils.mv path, path.gsub(/GEM_BASE_NAME/, gem_base_name) end end |
#template_binding ⇒ Object
157 158 159 |
# File 'lib/schema_dev/gem.rb', line 157 def template_binding @template_binding ||= TemplateEnv.new(self).get_binding end |