Module: Heartwood::GeneratorsHelper
- Defined in:
- lib/generators/helpers/generators_helper.rb
Instance Method Summary collapse
-
#add_gems_to_gemfile(gems) ⇒ Object
Add several gems to the gemfile.
-
#add_model_concern(name) ⇒ Object
Copies model concern templates to the project.
-
#add_model_concerns(concerns) ⇒ Object
Add multiple concerns.
-
#add_to_gemfile(name, options = {}) ⇒ Object
Add gem to Gemfile.
-
#annotate ⇒ Object
Annotate our models and tests.
-
#be ⇒ Object
Shorthand for ‘bundle exec`.
-
#bundle ⇒ Object
Run ‘bundle install`.
-
#class_exists?(class_name) ⇒ Boolean
Check to see if a class exists.
-
#confirm_ask(question) ⇒ Object
Make sure we get a matching answer before moving on.
- #copy_files(files, dir) ⇒ Object
-
#copy_unless_exists(path, new_path = path) ⇒ Object
Copy template file to project unless it’s already there.
-
#directories(dir_arr, container) ⇒ Object
Copy multiple directories.
-
#file_contents(template) ⇒ Object
Read a template and return its contents.
-
#gem_dir ⇒ Object
Get the local path where the gems are installed.
-
#gem_exists?(name) ⇒ Boolean
Find if a gem exists.
-
#gem_installation_notification(gems) ⇒ Object
Notifies the user of the gems that were installed and how desperately important they are.
- #gem_root ⇒ Object
-
#gemfile_update_notification(gems) ⇒ Object
Similar to gem_installation_notification, except here we just tell the user we added gems to the gemfile and they will have to bundle.
- #help_message(file) ⇒ Object
-
#install_gem(name, options = {}) ⇒ Object
First, install the gem in the local gem directory, then add it to the Gemfile (if it doesn’t already exist).
-
#install_gems(gem_arr) ⇒ Object
Install an array of gems – useful if needing to install more than one gem.
-
#migrate ⇒ Object
Run rake db:migrate.
-
#migrate_and_annotate ⇒ Object
Run migrate and annotate.
-
#model_concern(name) ⇒ Object
Alias for previous method.
-
#run_cmd(cmd, options = {}) ⇒ Object
Runs a system command, but with pretty output.
-
#template_file(name) ⇒ Object
Get the path to a particular template.
-
#template_partial_path(name) ⇒ Object
Partial template path are for templates that aren’t the entire file to be copied.
- #template_snippet(template) ⇒ Object
-
#timestamp ⇒ Object
Return current time in migration timestamp format.
Instance Method Details
#add_gems_to_gemfile(gems) ⇒ Object
Add several gems to the gemfile
156 157 158 |
# File 'lib/generators/helpers/generators_helper.rb', line 156 def add_gems_to_gemfile(gems) gems.each { |g| add_to_gemfile(g) } end |
#add_model_concern(name) ⇒ Object
Copies model concern templates to the project
101 102 103 |
# File 'lib/generators/helpers/generators_helper.rb', line 101 def add_model_concern(name) copy_unless_exists "app/models/concerns/#{name}.rb" end |
#add_model_concerns(concerns) ⇒ Object
Add multiple concerns
113 114 115 |
# File 'lib/generators/helpers/generators_helper.rb', line 113 def add_model_concerns(concerns) concerns.each { |c| add_model_concern(c) } end |
#add_to_gemfile(name, options = {}) ⇒ Object
Add gem to Gemfile
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/generators/helpers/generators_helper.rb', line 144 def add_to_gemfile(name, = {}) unless gem_exists?(name) if [:require].present? text = "\n\ngem '#{name}', :require => '#{[:require]}'" else text = "\n\ngem '#{name}'" end insert_into_file "Gemfile", text, :after => "rubygems.org'" end end |
#annotate ⇒ Object
Annotate our models and tests
50 51 52 53 54 |
# File 'lib/generators/helpers/generators_helper.rb', line 50 def annotate # need to make sure we have the annotate gem to avoid errors install_gem('annotate') run_cmd "#{be} annotate" end |
#be ⇒ Object
Shorthand for ‘bundle exec`
8 9 10 |
# File 'lib/generators/helpers/generators_helper.rb', line 8 def be "bundle exec" end |
#bundle ⇒ Object
Run ‘bundle install`
129 130 131 132 |
# File 'lib/generators/helpers/generators_helper.rb', line 129 def bundle run_cmd "bundle install" run_cmd "bundle clean" end |
#class_exists?(class_name) ⇒ Boolean
Check to see if a class exists. This can be helpful in determining if other generators have been run.
221 222 223 224 |
# File 'lib/generators/helpers/generators_helper.rb', line 221 def class_exists?(class_name) klass = Module.const_get(class_name) klass.is_a?(Class) end |
#confirm_ask(question) ⇒ Object
Make sure we get a matching answer before moving on. Useful for usernames, passwords, and other items where user error is harmful (or painful)
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/generators/helpers/generators_helper.rb', line 31 def confirm_ask(question) answer = ask("\n#{question}") match = ask("CONFIRM #{question}") if answer == match answer else say set_color("Did not match.", :red) confirm_ask(question) end end |
#copy_files(files, dir) ⇒ Object
71 72 73 |
# File 'lib/generators/helpers/generators_helper.rb', line 71 def copy_files(files, dir) files.each { |f| copy_unless_exists("#{dir}/#{f}") } end |
#copy_unless_exists(path, new_path = path) ⇒ Object
Copy template file to project unless it’s already there
67 68 69 |
# File 'lib/generators/helpers/generators_helper.rb', line 67 def copy_unless_exists(path, new_path = path) copy_file(path, new_path) unless File.exists?("#{Rails.root}/#{new_path}") end |
#directories(dir_arr, container) ⇒ Object
Copy multiple directories
119 120 121 122 123 |
# File 'lib/generators/helpers/generators_helper.rb', line 119 def directories(dir_arr, container) dir_arr.each do |dir| directory "#{container}/#{dir}", "#{container}/#{dir}" end end |
#file_contents(template) ⇒ Object
Read a template and return its contents
90 91 92 |
# File 'lib/generators/helpers/generators_helper.rb', line 90 def file_contents(template) File.read(template_file(template)) end |
#gem_dir ⇒ Object
Get the local path where the gems are installed
136 137 138 139 140 |
# File 'lib/generators/helpers/generators_helper.rb', line 136 def gem_dir gem_dir = `bundle show rails` gem_dir = gem_dir.split('/') gem_dir[0..-3].join('/') end |
#gem_exists?(name) ⇒ Boolean
Find if a gem exists
162 163 164 |
# File 'lib/generators/helpers/generators_helper.rb', line 162 def gem_exists?(name) Gem::Specification::find_all_by_name(name).any? end |
#gem_installation_notification(gems) ⇒ Object
Notifies the user of the gems that were installed and how desperately important they are.
193 194 195 196 197 198 |
# File 'lib/generators/helpers/generators_helper.rb', line 193 def gem_installation_notification(gems) say "\nThis generator installed the following gems (and added them to " say "your Gemfile):\n\n" gems.each { |g| say " #{g}" } say "\nThese gems *may* be necessary for Heartwood to work properly. " end |
#gem_root ⇒ Object
226 227 228 |
# File 'lib/generators/helpers/generators_helper.rb', line 226 def gem_root Gem::Specification.find_by_name("heartwood").gem_dir end |
#gemfile_update_notification(gems) ⇒ Object
Similar to gem_installation_notification, except here we just tell the user we added gems to the gemfile and they will have to bundle
203 204 205 206 207 208 |
# File 'lib/generators/helpers/generators_helper.rb', line 203 def gemfile_update_notification(gems) say "\nThis generator added some gems to your Gemfile. You will want to " say "run:\n\n $ bundle install" say "\nto complete this generator. Please note, " say "these gems *may* be necessary for Heartwood to work properly." end |
#help_message(file) ⇒ Object
230 231 232 |
# File 'lib/generators/helpers/generators_helper.rb', line 230 def (file) puts File.read("#{gem_root}/lib/help/#{file}.txt") end |
#install_gem(name, options = {}) ⇒ Object
First, install the gem in the local gem directory, then add it to the Gemfile (if it doesn’t already exist).
Note: This will run bundle install after adding the gem, unless specified otherwise.
172 173 174 175 176 177 178 179 180 |
# File 'lib/generators/helpers/generators_helper.rb', line 172 def install_gem(name, = {}) if gem_exists?(name) say "Found existing gem: #{name}" else run "gem install #{name} -i #{gem_dir}" add_to_gemfile(name, ) bundle if [:bundle].nil? || [:bundle] == true end end |
#install_gems(gem_arr) ⇒ Object
Install an array of gems – useful if needing to install more than one gem.
185 186 187 188 |
# File 'lib/generators/helpers/generators_helper.rb', line 185 def install_gems(gem_arr) gem_arr.each { |gem_name| install_gem(gem_name, :bundle => false) } bundle end |
#migrate ⇒ Object
Run rake db:migrate
44 45 46 |
# File 'lib/generators/helpers/generators_helper.rb', line 44 def migrate rake "db:migrate" end |
#migrate_and_annotate ⇒ Object
Run migrate and annotate
58 59 60 61 |
# File 'lib/generators/helpers/generators_helper.rb', line 58 def migrate_and_annotate migrate annotate end |
#model_concern(name) ⇒ Object
Alias for previous method
107 108 109 |
# File 'lib/generators/helpers/generators_helper.rb', line 107 def model_concern(name) add_model_concern(name) end |
#run_cmd(cmd, options = {}) ⇒ Object
Runs a system command, but with pretty output
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/generators/helpers/generators_helper.rb', line 14 def run_cmd(cmd, = {}) print_table( [ [set_color("run", :green, :bold), cmd] ], :indent => 9 ) if [:quiet] == true `#{cmd}` else system(cmd) end end |
#template_file(name) ⇒ Object
Get the path to a particular template
77 78 79 |
# File 'lib/generators/helpers/generators_helper.rb', line 77 def template_file(name) File.("../../templates/#{name}", __FILE__) end |
#template_partial_path(name) ⇒ Object
Partial template path are for templates that aren’t the entire file to be copied.
84 85 86 |
# File 'lib/generators/helpers/generators_helper.rb', line 84 def template_partial_path(name) File.("../../templates/_partials/#{name}", __FILE__) end |
#template_snippet(template) ⇒ Object
94 95 96 97 |
# File 'lib/generators/helpers/generators_helper.rb', line 94 def template_snippet(template) require 'erb' ERB.new(File.read(template_file(template))).result(binding) end |
#timestamp ⇒ Object
Return current time in migration timestamp format
214 215 216 |
# File 'lib/generators/helpers/generators_helper.rb', line 214 def Time.now.gmtime.strftime('%Y%m%d%H%M%S') end |