Skip to content

Instantly share code, notes, and snippets.

@overdrivemachines
Last active January 28, 2024 08:40
Show Gist options
  • Save overdrivemachines/1c4cac1519ad0215e2aa5d41d3af1016 to your computer and use it in GitHub Desktop.
Save overdrivemachines/1c4cac1519ad0215e2aa5d41d3af1016 to your computer and use it in GitHub Desktop.
template.rb - Ruby on Rails Template
require 'net/http'
require 'uri'
# === Remove Comments in Gemfile ===
# Path to the Gemfile
gemfile_path = 'Gemfile'
# Read the content of the Gemfile
gemfile_content = File.read(gemfile_path)
# Remove comments from the Gemfile
gemfile_content.gsub!(/^#.*$/, '')
# Remove the entire development group
gemfile_content.gsub!(
/group :development do\n.*\nend\n/m,
''
)
# Remove the entire development/test group
gemfile_content.gsub!(
/group :development, :test do\n.*\nend\n/m,
''
)
# Remove the empty lines
gemfile_content.gsub!(/^\s*\n/, '')
# Write the modified content back to the Gemfile
File.write(gemfile_path, gemfile_content)
# === Add gems ===
gem 'sassc-rails'
gem 'image_processing', '~> 1.2'
gem_group :development do
gem 'web-console'
gem 'letter_opener'
gem 'rails-erd'
gem 'chusaku', require: false
gem 'annotate'
end
gem_group :development, :test do
gem 'debug', platforms: %i[mri windows]
gem 'faker'
end
run 'bundle install'
# === ERD (Entity Relationship Diagram) ===
erdconfig_content = <<~ERDCONFIG
attributes: content,foreign_key
filetype: png
ERDCONFIG
File.write('.erdconfig', erdconfig_content)
run 'bundle exec rails g erd:install'
# === Annotate Models and Routes file ===
run 'curl -LJ --output lib/tasks/routes.rake https://github.com/overdrivemachines/dipen_chauhan/raw/master/lib/tasks/routes.rake'
run 'rails g annotate:install'
# === Add edit_credentials.sh ===
edit_credentials_content = <<~EDITCREDENTIALS
#!/bin/bash
EDITOR="code --wait" rails credentials:edit
EDITCREDENTIALS
File.write('edit_credentials.sh', edit_credentials_content)
run 'chmod +x edit_credentials.sh' # Add execute permissions
# === Email Configuration ===
# Add custom configuration to development.rb inside the configure block
development_config_content = <<~DEVELOPMENTCONFIG
# Custom configuration for development environment
host = "localhost:3000"
config.action_mailer.default_url_options = { host: host, protocol: 'http' }
# letter_opener gem configuration: https://github.com/ryanb/letter_opener
# Now any email will pop up in your browser instead of being sent.
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true
DEVELOPMENTCONFIG
# Update development.rb to insert custom configuration inside the existing configure block
development_rb_path = 'config/environments/development.rb'
development_rb_content = File.read(development_rb_path)
development_rb_content.gsub!(
/(Rails\.application\.configure do\s*\n)/,
"\\1#{development_config_content}"
)
# Write the modified content back to development.rb
File.write(development_rb_path, development_rb_content)
# === README.md file ===
# Path to the README.md file
readme_file_path = 'README.md'
# Define the content to replace README.md
readme_content = <<~MARKDOWN
# App Name
![Preview](preview.png)
# Features
# Model
Generated by Rails ERD. Run rails erd to regenerate (must have graphviz).
![ERD Diagram](erd.png)
## Version
- #{`ruby -v`.strip}
- #{`rails -v`.strip}
## What I Learned
## References
MARKDOWN
# Write the new content to the README.md file
File.write(readme_file_path, readme_content)
# === Rubocop ====
# Download and save .rubocop.yml file
rubocop_yml_url = 'https://gist.githubusercontent.com/overdrivemachines/2ca00f777a901fa5255a107872a6db1f/raw/ae7a17c4a19215910033ba9595d2a1e99e16a7ed/.rubocop.yml'
rubocop_yml_content = Net::HTTP.get(URI.parse(rubocop_yml_url))
File.write('.rubocop.yml', rubocop_yml_content)
run 'rubocop -a Gemfile'
run 'rubocop -a config/environments/development.rb'
# === Git commit ====
# Add a block to run after bundling gems
after_bundle do
git :init
git add: '.'
git commit: "-a -m 'Initial commit'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment