Module: Onotole::Helpers

Included in:
AppBuilder
Defined in:
lib/onotole/helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_gems_from_argsObject



60
61
62
63
64
65
# File 'lib/onotole/helpers.rb', line 60

def add_gems_from_args
  ARGV.each do |gem_name|
    next unless gem_name.slice(0, 2) == '--'
    add_to_user_choise gem_name[2..-1].to_sym
  end
end

#add_to_user_choise(gem) ⇒ Object



113
114
115
# File 'lib/onotole/helpers.rb', line 113

def add_to_user_choise(gem)
  AppBuilder.user_choice.push gem
end

#br(str = nil) ⇒ Object



150
151
152
# File 'lib/onotole/helpers.rb', line 150

def br(str = nil)
  str ? "\n" + str : "\n"
end

#choice(selector, variants) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/onotole/helpers.rb', line 10

def choice(selector, variants)
  return if variant_in_options? variants
  values = variants.keys
  say "\n  #{BOLDGREEN}#{selector}#{COLOR_OFF}"
  variants.each_with_index do |variant, i|
    say "#{i.to_s.rjust(5)}. #{BOLDBLUE}#{variant[1]}#{COLOR_OFF}"
  end
  numeric_answers = (0...variants.length).map(&:to_s)
  answer = ask_stylish('Enter choice:') until numeric_answers.include? answer
  symbol_answer = values[answer.to_i]
  symbol_answer == :none ? nil : symbol_answer
end

#clean_by_rubocopObject



125
126
127
128
# File 'lib/onotole/helpers.rb', line 125

def clean_by_rubocop
  return unless user_choose?(:rubocop)
  bundle_command "exec rubocop --auto-correct #{quiet_suffix}"
end

#cleanup_comments(file_name) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/onotole/helpers.rb', line 67

def cleanup_comments(file_name)
  accepted_content = File.readlines(file_name).reject do |line|
    line =~ /^\s*#.*$/ || line =~ /^$\n/
  end

  File.open(file_name, 'w') do |file|
    accepted_content.each { |line| file.puts line }
  end
end

#cover_def_by(file_name, lookup_str, external_def) ⇒ Object

does not recognize variable nesting, but now it does not matter



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/onotole/helpers.rb', line 78

def cover_def_by(file_name, lookup_str, external_def)
  expect_end = 0
  found = false
  accepted_content = ''
  File.readlines(file_name).each do |line|
    expect_end += 1 if found && line =~ /\sdo\s/
    expect_end -= 1 if found && line =~ /(\s+end|^end)/
    if line =~ Regexp.new(lookup_str)
      accepted_content += "#{external_def}\n#{line}"
      expect_end += 1
      found = true
    else
      accepted_content += line
    end
    if found && expect_end == 0
      accepted_content += "\nend"
      found = false
    end
  end
  File.open(file_name, 'w') { |file| file.puts accepted_content }
end

#heroku_adapterObject



52
53
54
# File 'lib/onotole/helpers.rb', line 52

def heroku_adapter
  @heroku_adapter ||= Adapters::Heroku.new(self)
end

#install_from_github(_gem_name) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/onotole/helpers.rb', line 100

def install_from_github(_gem_name)
  # TODO: in case of bundler update `bundle show` do now work any more
  true

  # return nil unless gem_name
  # path = `cd #{Dir.pwd} && bundle show #{gem_name}`.chomp
  # run "cd #{path} && bundle exec gem build #{gem_name}.gemspec && bundle exec gem install *.gem"
end

#mkdir_and_touch(dir) ⇒ Object



154
155
156
157
# File 'lib/onotole/helpers.rb', line 154

def mkdir_and_touch(dir)
  run "mkdir #{dir}"
  touch "#{dir}/.keep"
end

#multiple_choice(selector, variants) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/onotole/helpers.rb', line 27

def multiple_choice(selector, variants)
  values = []
  result = []
  answers = ''
  say "\n  #{BOLDGREEN}#{selector} Use space as separator#{COLOR_OFF}"
  variants.each_with_index do |variant, i|
    values.push variant[0]
    say "#{i.to_s.rjust(5)}. #{BOLDBLUE}#{variant[0]
          .to_s.ljust(20)}-#{COLOR_OFF} #{variant[1]}"
  end
  loop do
    answers = ask_stylish('Enter choices:').split ' '
    break if answers.any? && (answers - (0...variants.length)
            .to_a.map(&:to_s)).empty?
  end
  answers.delete '0'
  answers.uniq.each { |answer| result.push values[answer.to_i] }
  result
end

#pgsql_db_exist?(db_name) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/onotole/helpers.rb', line 121

def pgsql_db_exist?(db_name)
  system "psql -l | grep #{db_name}"
end

#quiet_suffixObject



130
131
132
# File 'lib/onotole/helpers.rb', line 130

def quiet_suffix
  AppBuilder.quiet ? ' > /dev/null' : ''
end

#rails_generator(command) ⇒ Object



117
118
119
# File 'lib/onotole/helpers.rb', line 117

def rails_generator(command)
  bundle_command "exec rails generate #{command} -f #{quiet_suffix}"
end

#raise_on_missing_translations_in(environment) ⇒ Object



47
48
49
50
# File 'lib/onotole/helpers.rb', line 47

def raise_on_missing_translations_in(environment)
  config = 'config.action_view.raise_on_missing_translations = true'
  uncomment_lines("config/environments/#{environment}.rb", config)
end

#serve_static_files_lineObject



56
57
58
# File 'lib/onotole/helpers.rb', line 56

def serve_static_files_line
  "config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?\n"
end

#touch(file_name) ⇒ Object



146
147
148
# File 'lib/onotole/helpers.rb', line 146

def touch(file_name)
  `touch #{file_name}`
end

#user_choose?(gem) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/onotole/helpers.rb', line 109

def user_choose?(gem)
  AppBuilder.user_choice.include? gem
end

#user_gems_from_args_or_default_setObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/onotole/helpers.rb', line 134

def user_gems_from_args_or_default_set
  gems_flags = []
  options.each { |gem, usage| gems_flags.push(gem.to_sym) if usage }
  gems = GEMPROCLIST & gems_flags
  if gems.empty?
    AppBuilder.user_choice = DEFAULT_GEMSET
  else
    gems.each { |gem| AppBuilder.user_choice << gem }
  end
  add_user_gems
end

#variant_in_options?(variants) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/onotole/helpers.rb', line 23

def variant_in_options?(variants)
  variants.keys[1..-1].map { |a| options[a] }.include? true
end

#yes_no_question(gem_name, gem_description) ⇒ Object



4
5
6
7
8
# File 'lib/onotole/helpers.rb', line 4

def yes_no_question(gem_name, gem_description)
  gem_name_color = "#{gem_name.capitalize}.\n"
  variants = { none: 'No', gem_name.to_sym => gem_name_color }
  choice "Use #{gem_name}? #{gem_description}", variants
end