Module: BoringGenerators::GeneratorHelper

Instance Method Summary collapse

Instance Method Details

#app_ruby_versionObject



5
6
7
8
9
10
# File 'lib/boring_generators/generator_helper.rb', line 5

def app_ruby_version
  with_ruby_string = File.read("Gemfile")[/^ruby\s+["']?([\d.]+)["']?/] || File.read(".ruby-version").strip

  # only keep 3.3.0 from ruby-3.3.0
  with_ruby_string.gsub(/[^\d\.]/, "").squish
end

#bundle_installObject



18
19
20
# File 'lib/boring_generators/generator_helper.rb', line 18

def bundle_install
  Bundler.with_unbundled_env { run "bundle install" }
end

#check_and_install_gem(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/boring_generators/generator_helper.rb', line 22

def check_and_install_gem(*args)
  gem_name, = args

  if gem_installed?(gem_name)
    say "#{gem_name} is already in the Gemfile, skipping it...", :yellow
  else
    gem *args unless gem_installed?(gem_name)
  end

  bundle_install
end

#gem_installed?(gem_name) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/boring_generators/generator_helper.rb', line 12

def gem_installed?(gem_name)
  gem_regex = /^\s*gem\s*['"]#{gem_name}['"]/

  File.read("Gemfile").match?(gem_regex)
end

#inject_into_file_if_new(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/boring_generators/generator_helper.rb', line 34

def inject_into_file_if_new(*args)
  file_name, content_to_add, = args

  file_content = File.read(file_name)

  content_exists = file_content.include?(content_to_add)

  return if content_exists

  inject_into_file *args
end