Module: Kowl::Actions
- Included in:
- Generators::Base, Overrides::AppGenerator
- Defined in:
- lib/kowl/actions.rb
Instance Method Summary collapse
-
#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.
-
#add_package(pkg = '') ⇒ Boolean
Used to install yarn package is NPM/Yarn is available.
-
#append_to_file(file, str) ⇒ Boolean
Append a string to the end of a specied file.
-
#database_route(database = 'sqlite3') ⇒ String
Add PgHero engine mount to the routes if the database iss postgresql.
-
#dev_config(str) ⇒ Boolean
Add config to the development environment.
-
#dup_file(source, destination) ⇒ Boolean?
Dupliate a specified file (make a copy).
-
#file_exists?(file) ⇒ Boolean
Validate if a specified file exists.
-
#mailer_gems(mailer = 'sparkpost') ⇒ String
Gemfile specific functions.
-
#mailer_route(skip_mailer) ⇒ String
Routes.
-
#mk_dir(path) ⇒ Boolean
Create a specific directory.
-
#move_file(file, destination) ⇒ Boolean?
Move a file to a specified location.
-
#pry_gems(skip_pry = false) ⇒ nil, String
Outputs a set of Gemfile entries for the application developer to use pry in the rails console.
-
#rails_cmd(cmd = '') ⇒ Boolean
Used to call a rails system command.
-
#remove_dir(path) ⇒ Boolean
Delete a specific directory path src: github.com/solidusio/solidus/blob/master/core/lib/generators/spree/dummy/dummy_generator.rb#L128.
-
#remove_file(file) ⇒ Boolean?
Delete a specific file from the projects directory structure.
-
#remove_gem(gem) ⇒ nil
Used to remove a gem from the Gemfile and bundle installs afterwards.
-
#replace_string_in_file(filename, regex, replacement = '') ⇒ Boolean
Replace a matching string within a specific file.
-
#robocop_test_engine(test_engine = '', skip_tests = false) ⇒ String
Determine which rubcop gem should be used (dependant on the requested test suite).
-
#sidekiq_route(options) ⇒ String
Unless specified the function will add a Sidekiq engine route to access in the routes.
-
#template_linter_gems(engine) ⇒ String
Determine which linter should be used for the aplication.
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
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() ext_routes = "#{database_route([:database])}#{mailer_route([:skip_mailer])}#{sidekiq_route()}" routes_str = if [: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
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
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
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
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)
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
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
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
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
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
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
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
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
Delete a specific directory path src: github.com/solidusio/solidus/blob/master/core/lib/generators/spree/dummy/dummy_generator.rb#L128
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
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
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
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)
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
149 150 151 152 153 |
# File 'lib/kowl/actions.rb', line 149 def sidekiq_route() return '' if [:skip_sidekiq] " mount Sidekiq::Web => '/sidekiq'\n" end |
#template_linter_gems(engine) ⇒ String
Determine which linter should be used for the aplication
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 |