Module: Kowl::Actions

Included in:
Generators::Base, Overrides::AppGenerator
Defined in:
lib/kowl/actions.rb

Instance Method Summary collapse

Instance Method Details

#add_extension_routes(options) ⇒ String

Unless all extensions are skipped [mailer && sidekiq], will will add their mounts depending on if the add requires auth or not

Parameters:

  • options (Hash)

    The entire generators options passed to it

Returns:

  • (String)

    the applications extensions mount paths for routes file



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/kowl/actions.rb', line 158

def add_extension_routes(options)
  ext_routes = "#{database_route(options[:database])}#{mailer_route(options[:skip_mailer])}#{sidekiq_route(options)}"
  routes_str = if options[:noauth]
                 <<~ROUTES
                   # If authentication has not been used; only allow this routes to be used in development to prevent authorized access
                   if Rails.env.development?
                     #{ext_routes.squeeze("\n").strip}
                   end
                 ROUTES
               else
                 <<~ROUTES
                   # Require the person hitting this page ot be an application admin
                   authenticate :user, -> (user) { user.admin? } do
                     #{ext_routes.squeeze("\n").strip}
                   end
                 ROUTES
               end
  optimize_indentation(routes_str, 2)
end

#add_package(pkg = '') ⇒ Boolean

Used to install yarn package is NPM/Yarn is available

Parameters:

  • pkg (String) (defaults to: '')

    The Yarn/npm package in which to install with the Rails Application

Returns:

  • (Boolean)

    if the yarn package was successfully installed or not



22
23
24
25
26
# File 'lib/kowl/actions.rb', line 22

def add_package(pkg = '')
  return false if pkg.blank?

  system("bin/yarn add #{pkg}")
end

#append_to_file(file, str) ⇒ Boolean

Append a string to the end of a specied file

Parameters:

  • file (String)

    Filename to append the string to

  • str (String)

    String to add to the specified file

Returns:

  • (Boolean)

    if the string was appended to the file or not



32
33
34
# File 'lib/kowl/actions.rb', line 32

def append_to_file(file, str)
  File.open(file, 'a+') { |f| f << str }
end

#database_route(database = 'sqlite3') ⇒ String

Add PgHero engine mount to the routes if the database iss postgresql

Parameters:

  • database (String) (defaults to: 'sqlite3')

    A string containing the applications defined database adapter

Returns:

  • (String)

    the pghero dashboard mount path, if postgresql will be used



140
141
142
143
144
# File 'lib/kowl/actions.rb', line 140

def database_route(database = 'sqlite3')
  return '' unless database.to_s == 'postgresql'

  "  mount PgHero::Engine, at: \"pghero\"\n"
end

#dev_config(str) ⇒ Boolean

Add config to the development environment

Parameters:

  • str (String)

    A heredoc containing configuration changes for the development environment

Returns:

  • (Boolean)

    if the string was successfully injected into the development config file



121
122
123
# File 'lib/kowl/actions.rb', line 121

def dev_config(str)
  inject_into_file('config/environments/development.rb', optimize_indentation(str, 2), after: "config.assets.quiet = true\n")
end

#dup_file(source, destination) ⇒ Boolean?

Dupliate a specified file (make a copy)

Parameters:

  • source (String)

    The originating file in which you wish to duplicate

  • destination (String)

    The destination of the file in which you are duplicating to

Returns:

  • (Boolean, nil)

    Returns nil if the source or destination where not specified. Otherwise if should return true/false if the file was actually duplicated



47
48
49
50
51
# File 'lib/kowl/actions.rb', line 47

def dup_file(source, destination)
  return nil if source.blank? || destination.blank?

  FileUtils.cp(source, destination) if file_exists?(source)
end

#file_exists?(file) ⇒ Boolean

Validate if a specified file exists

Parameters:

  • file (String)

    Filename to validate against

Returns:

  • (Boolean)

    Returns whether if the file exists or not



39
40
41
# File 'lib/kowl/actions.rb', line 39

def file_exists?(file)
  File.file? file
end

#mailer_gems(mailer = 'sparkpost') ⇒ String

Gemfile specific functions

Adds the specified mailer gem to the application

Parameters:

  • mailer (String) (defaults to: 'sparkpost')

    Specfic mailer in which you would like to use

Returns:

  • (String)

    the applications mailer gem that will be added to the Gemfile



184
185
186
187
188
189
190
# File 'lib/kowl/actions.rb', line 184

def mailer_gems(mailer = 'sparkpost')
  if mailer.to_s == 'postmark'
    "gem 'postmark-rails'"
  else
    "gem 'sparkpost_rails'"
  end
end

#mailer_route(skip_mailer) ⇒ String

Routes

Adds a development only route to access LetterOpener when developmenting the application

Parameters:

  • skip_mailer (Boolean)

    A flag to determine if you want to skip using a mailer

Returns:

  • (String)

    the LetterOpener mount path (unless you have specied to skip_mailer)



131
132
133
134
135
# File 'lib/kowl/actions.rb', line 131

def mailer_route(skip_mailer)
  return '' if skip_mailer

  "  mount LetterOpenerWeb::Engine, at: \"/letter_opener\" if Rails.env.development?\n"
end

#mk_dir(path) ⇒ Boolean

Create a specific directory

Parameters:

  • path (String)

    The path in which you wish to create

Returns:

  • (Boolean)

    if the filepath was created or not



56
57
58
# File 'lib/kowl/actions.rb', line 56

def mk_dir(path)
  FileUtils.mkdir_p(path) unless File.directory?(path)
end

#move_file(file, destination) ⇒ Boolean?

Move a file to a specified location

Parameters:

  • file (String)

    The original file location in which you want to move the file from

  • destination (String)

    The destintion location of where you would like to move this file to

Returns:

  • (Boolean, nil)

    Return nil if the file or destrination are blank. Otherwise it will return true/false if the file was successfully moved



81
82
83
84
85
# File 'lib/kowl/actions.rb', line 81

def move_file(file, destination)
  return nil if file.blank? || destination.blank?

  FileUtils.mv(file, destination, force: true) if file_exists?(file)
end

#pry_gems(skip_pry = false) ⇒ nil, String

Outputs a set of Gemfile entries for the application developer to use pry in the rails console

Parameters:

  • skip_pry (Boolean) (defaults to: false)

    if the developer wants to skip using pry with their application

Returns:

  • (nil, String)

    return the string to be added to the applications Gemfile



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kowl/actions.rb', line 195

def pry_gems(skip_pry = false)
  return nil if skip_pry

  gems = <<~PRY
    # Pry to cleanup the rails console
    gem 'pry', '0.12.2'
    gem 'pry-rails'               # pry console output
    gem 'spirit_hands'            # A combination of pry, awesome_print, hirb, and numerous other console extensions
  PRY
  optimize_indentation(gems, 2)
end

#rails_cmd(cmd = '') ⇒ Boolean

Used to call a rails system command

Parameters:

  • cmd (String) (defaults to: '')

    The rails command that will be executed, ie: db:migrate

Returns:

  • (Boolean)

    if the rails command was succcessfully executed or not



13
14
15
16
17
# File 'lib/kowl/actions.rb', line 13

def rails_cmd(cmd = '')
  return nil if cmd.blank?

  system("bin/rails #{cmd}")
end

#remove_dir(path) ⇒ Boolean

Parameters:

  • path (String)

    The specifc directory which you wish to remove

Returns:

  • (Boolean)

    if the FileDir was removed or not



64
65
66
# File 'lib/kowl/actions.rb', line 64

def remove_dir(path)
  FileUtils.remove_dir(path) if File.directory?(path)
end

#remove_file(file) ⇒ Boolean?

Delete a specific file from the projects directory structure

Parameters:

  • file (String)

    The specific file in which you wish to delete

Returns:

  • (Boolean, nil)

    Returns nil if the filename is blank, otherwise it should return true/false if the file was properly removed



71
72
73
74
75
# File 'lib/kowl/actions.rb', line 71

def remove_file(file)
  return nil if file.blank?

  FileUtils.rm(file, force: true) if file_exists?(file)
end

#remove_gem(gem) ⇒ nil

Used to remove a gem from the Gemfile and bundle installs afterwards

Parameters:

  • gem (String)

    The Gem name which you would like to remove from the gemfile

Returns:

  • (nil)

    Returns nil if the the method is called without a gem specified



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kowl/actions.rb', line 90

def remove_gem(gem)
  return nil if gem.blank?

  # Used to match if the user used double & single quotes ("|') around the gem and a variable amount of space
  # used to match the following string variations
  # gem 'puma'
  # gem "puma"
  # gem "puma", '~> 3.7'
  # gem 'puma', github: 'puma/puma'
  replace_string_in_file('Gemfile', "^[\s]?gem\s?[\'\"](#{gem})[\'\"](.+?)*[\s]?", '')
  # Because we're removing a gem from the gemfile we need to bundle again
  run 'bundle install --quiet'
end

#replace_string_in_file(filename, regex, replacement = '') ⇒ Boolean

Replace a matching string within a specific file

Parameters:

  • filename (String)

    The file in which you would like to change it’s contents

  • regex (String)

    The regular expression you would like to run against the line of text within the file

  • replacement (String) (defaults to: '')

    The string in which you would like to replace any matches against

Returns:

  • (Boolean)

    if the string was successfully replaced in the specified file



109
110
111
112
# File 'lib/kowl/actions.rb', line 109

def replace_string_in_file(filename, regex, replacement = '')
  content = File.read(filename).gsub(/#{regex}$/i, replacement)
  File.open(filename, 'wb') { |file| file.write(content) }
end

#robocop_test_engine(test_engine = '', skip_tests = false) ⇒ String

Determine which rubcop gem should be used (dependant on the requested test suite)

Parameters:

  • test_engine (String) (defaults to: '')

    The specified application test suite requested

  • skip_tests (Boolean) (defaults to: false)

    if you would like skip using tests with the application

Returns:

  • (String)

    The gemfile rubocop gem entry based on the applications test suite



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/kowl/actions.rb', line 227

def robocop_test_engine(test_engine = '', skip_tests = false)
  return '' if test_engine.blank? || skip_tests

  case test_engine.to_s
  when 'minitest'
    "gem 'rubocop-minitest', require: false"
  when 'rspec'
    "gem 'rubocop-rspec', require: false"
  else
    ''
  end
end

#sidekiq_route(options) ⇒ String

Unless specified the function will add a Sidekiq engine route to access in the routes

Parameters:

  • options (Hash)

    A flag to determine if you would like the generators to skip adding sidekiq to the applicatiion

Returns:

  • (String)

    the sidekiq mount path



149
150
151
152
153
# File 'lib/kowl/actions.rb', line 149

def sidekiq_route(options)
  return '' if options[:skip_sidekiq]

  "  mount Sidekiq::Web => '/sidekiq'\n"
end

#template_linter_gems(engine) ⇒ String

Determine which linter should be used for the aplication

Parameters:

  • engine (String)

    The specified applications templating engine (erb, hal, or slim)

Returns:

  • (String)

    An gem line that will be added to the gemfile



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/kowl/actions.rb', line 210

def template_linter_gems(engine)
  return '' if engine.blank?

  case engine.to_s
  when 'haml'
    "gem 'haml_lint'#{gemfile_requirement('haml_lint')}, require: false"
  when 'slim'
    "gem 'slim_lint'#{gemfile_requirement('slim_lint')}, require: false"
  else
    "gem 'erb_lint', github: 'Shopify/erb-lint', require: false"
  end
end