Skip to content

Commit

Permalink
Merge pull request #117 from TruemarkDev/devise-run-migrations-after-…
Browse files Browse the repository at this point in the history
…generating-table

Enhancements and fixes in some existing generators
  • Loading branch information
abhaynikam authored Jul 25, 2024
2 parents eb359c5 + 00799ed commit b23ae51
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 126 deletions.
22 changes: 9 additions & 13 deletions lib/boring_generators/generator_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,29 @@ def app_ruby_version
with_ruby_string = `grep "^ruby.*$" Gemfile` || `cat .ruby-version`

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

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

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

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

def check_and_install_gem(*args)
gem_name, = args

gem_file_content_array = File.readlines("Gemfile")


gem_exists = gem_file_content_array.any? { |line| line.include?(gem_name) }

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

bundle_install
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def verify_presence_of_devise_model

def add_doorkeeper_gem
say "Adding doorkeeper gem", :green
check_and_install_gem("doorkeeper")
bundle_install

check_and_install_gem "doorkeeper"
end

def run_doorkeeper_generators
Expand Down
11 changes: 11 additions & 0 deletions lib/generators/boring/devise/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class InstallGenerator < Rails::Generators::Base
desc: "Skip generating devise views"
class_option :skip_devise_model, type: :boolean, aliases: "-sm",
desc: "Skip generating devise model"
class_option :run_db_migrate, type: :boolean, aliases: "-rdm",
desc: "Run migrations after generating user table",
default: false

def add_devise_gem
say "Adding devise gem", :green
Expand Down Expand Up @@ -64,6 +67,14 @@ def add_devise_views
run "DISABLE_SPRING=1 bundle exec rails generate devise:views #{model_name.pluralize}"
end
end

def run_db_migrate
return unless options[:run_db_migrate]

Bundler.with_unbundled_env do
rails_command "db:migrate"
end
end
end
end
end
42 changes: 31 additions & 11 deletions lib/generators/boring/dotenv/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
# frozen_string_literal: true

require "boring_generators/generator_helper"

module Boring
module Dotenv
class InstallGenerator < Rails::Generators::Base
desc 'Adds dotenv gem to the application'
include BoringGenerators::GeneratorHelper

desc "Adds dotenv gem to the application"
source_root File.expand_path("templates", __dir__)

def add_dotenv_gem
say 'Adding dotenv gem', :green
return if gem_installed?("dotenv-rails")

Bundler.with_unbundled_env do
run 'bundle add dotenv-rails --group development'
end
say "Adding dotenv gem", :green

check_and_install_gem "dotenv-rails", group: :development
end

def configure_dotenv_gem
say 'Configuring dotenv gem', :green
say "Configuring dotenv gem", :green

template ".env", ".env"

template '.env', '.env'
create_file ".gitignore" unless File.exist?(".gitignore")

unless File.exist?('.gitignore')
create_file '.gitignore'
FileUtils.cp(".env", ".env.sample")

add_env_files_to_gitignore
end

private

def add_env_files_to_gitignore
if File.readlines(".gitignore").any? { |line| line.include?(".env") }
return
end

FileUtils.cp('.env', '.env.sample')
insert_into_file('.gitignore', "\n/.env\n")
ignore_content = <<~ENV_FILE_NAMES
\n
# Ignore all environment files (except templates).
/.env
!/.env.*
ENV_FILE_NAMES

insert_into_file(".gitignore", ignore_content)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/boring/favicon/build/build_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def build_favicon
end

def create_favicon_directory
unless File.exists?(FILE_FAVICO_DIR)
unless File.exist?(FILE_FAVICO_DIR)
Dir.mkdir FILE_FAVICO_DIR
end
end
Expand Down
65 changes: 48 additions & 17 deletions lib/generators/boring/pronto/base_generator.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,77 @@
# frozen_string_literal: true

require 'boring_generators/generator_helper'
require "boring_generators/generator_helper"

module Boring
module Pronto
class BaseGenerator < Rails::Generators::Base
desc "Adds Pronto gem with various extensions"

class_option :skip_extensions, type: :array, aliases: "-se",
desc: "List of extensions to skip. Available options: brakeman, flay, reek, rubocop",
class_option :skip_extensions,
type: :array,
aliases: "-se",
desc:
"List of extensions to skip. Available options: brakeman, flay, reek, rubocop",
enum: %w[brakeman flay reek rubocop],
default: []

include BoringGenerators::GeneratorHelper

def add_pronto_gems
say "Adding pronto gems", :green
pronto_gem_content = <<~RUBY
\n
\t# Pronto is a code linter runner that can be used with git and GitHub pull requests
\tgem "pronto"

gem_content = <<~RUBY.strip
#{pronto_gem_content}
#{pronto_brakemen_gem_content}
#{pronto_flay_gem_content}
#{pronto_reek_gem_content}
#{pronto_rubocop_gem_content}
RUBY

return if gem_content.blank?

insert_into_file "Gemfile", "\n#{gem_content}\n"

Bundler.with_unbundled_env { run "bundle install" }
end

private

def pronto_gem_content
return if gem_installed?("pronto")

<<~RUBY.strip
# Pronto is a code linter runner that can be used with git and GitHub pull requests
gem "pronto"
RUBY
insert_into_file "Gemfile", pronto_gem_content
Bundler.with_unbundled_env do
run "bundle install"
end
end

def pronto_brakemen_gem_content
return unless options[:skip_extensions].exclude?('brakeman')
return if gem_installed?('pronto-brakeman')
return if options[:skip_extensions].include?("brakeman")
return if gem_installed?("pronto-brakeman")

"\tgem \"pronto-brakeman\", require: false"
"gem \"pronto-brakeman\", require: false"
end

def pronto_flay_gem_content
return unless options[:skip_extensions].exclude?('flay')
return if gem_installed?('pronto-flay')
return if options[:skip_extensions].include?("flay")
return if gem_installed?("pronto-flay")

"gem \"pronto-flay\", require: false"
end

def pronto_reek_gem_content
return if options[:skip_extensions].include?("reek")
return if gem_installed?("pronto-reek")

"gem \"pronto-reek\", require: false"
end

def pronto_rubocop_gem_content
return if options[:skip_extensions].include?("rubocop")
return if gem_installed?("pronto-rubocop")

"\tgem \"pronto-flay\", require: false"
"gem \"pronto-rubocop\", require: false"
end
end
end
Expand Down
83 changes: 45 additions & 38 deletions lib/generators/boring/pronto/gitlab_ci/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InstallGenerator < Boring::Pronto::BaseGenerator
def add_configuration
@ruby_version = options.ruby_version || app_ruby_version

if File.exists?(".gitlab-ci.yml")
if File.exist?(".gitlab-ci.yml")
add_pronto_configuration
add_lint_stage
show_readme
Expand All @@ -27,50 +27,57 @@ def add_configuration
end

private
def create_gitlab_ci_with_pronto
say "Creating .gitlab-ci.yml with Pronto configurations", :yellow
template ".gitlab-ci.yml", ".gitlab-ci.yml"
end

def add_pronto_configuration
return if pronto_configuration_exists?
def create_gitlab_ci_with_pronto
say "Creating .gitlab-ci.yml with Pronto configurations", :yellow
template ".gitlab-ci.yml", ".gitlab-ci.yml"
end

say "Adding Pronto configurations to .gitlab-ci.yml", :green
inject_into_file ".gitlab-ci.yml", pronto_ci_content, before: /\Z/
end
def add_pronto_configuration
return if pronto_configuration_exists?

def add_lint_stage
return if lint_stage_exists?
say "Adding Pronto configurations to .gitlab-ci.yml", :green
inject_into_file ".gitlab-ci.yml", pronto_ci_content, before: /\Z/
end

if stages_exists?
inject_into_file ".gitlab-ci.yml", optimize_indentation("\n- lint", 2).chomp, after: /stages:/
else
inject_into_file ".gitlab-ci.yml", stages_configuration, before: /pronto:/
end
end
def add_lint_stage
return if lint_stage_exists?

def show_readme
readme "README"
if stages_exists?
inject_into_file ".gitlab-ci.yml",
optimize_indentation("\n- lint", 2).chomp,
after: /stages:/
else
inject_into_file ".gitlab-ci.yml",
stages_configuration,
before: /pronto:/
end
end

def pronto_configuration_exists?
gitlab_ci_file_content["pronto"]
end
def show_readme
readme "README"
end

def lint_stage_exists?
gitlab_ci_file_content["stages"] && gitlab_ci_file_content["stages"].include?("lint")
end
def pronto_configuration_exists?
gitlab_ci_file_content["pronto"]
end

def stages_exists?
gitlab_ci_file_content["stages"]
end
def lint_stage_exists?
gitlab_ci_file_content["stages"] &&
gitlab_ci_file_content["stages"].include?("lint")
end

def gitlab_ci_file_content
@gitlab_ci_file_content ||= YAML.safe_load(File.open(".gitlab-ci.yml"), aliases: true) || {}
end
def stages_exists?
gitlab_ci_file_content["stages"]
end

def gitlab_ci_file_content
@gitlab_ci_file_content ||=
YAML.safe_load(File.open(".gitlab-ci.yml"), aliases: true) || {}
end

def pronto_ci_content
<<~RUBY
def pronto_ci_content
<<~RUBY
pronto:
image: ruby:#{@ruby_version}
stage: lint
Expand All @@ -91,14 +98,14 @@ def pronto_ci_content
# Run pronto on branch of current merge request
- bundle exec pronto run -f gitlab_mr -c origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
RUBY
end
end

def stages_configuration
<<~RUBY
def stages_configuration
<<~RUBY
stages:
- lint
RUBY
end
end
end
end
end
Expand Down
Loading

0 comments on commit b23ae51

Please sign in to comment.