Skip to content

Instantly share code, notes, and snippets.

@ryochin
Last active March 16, 2025 01:48
Show Gist options
  • Save ryochin/28f9928609688e8ab66990d482b6ac72 to your computer and use it in GitHub Desktop.
Save ryochin/28f9928609688e8ab66990d482b6ac72 to your computer and use it in GitHub Desktop.
Rails env builder
version: '3'
vars:
PROJECT: rails80
RUBY_VERSION: 3.3.4
NODE_VERSION: 20.10.0
BUN_VERSION: 1.2.0
RAILS_VERSION: 8.0.2
DATABASE: postgresql # mysql | postgresql
JAVASCRIPT: importmap
CSS: sass
tasks:
rails-help:
desc: Show rails new help
preconditions:
- test -f Gemfile
cmd: bundle exec rails new --help
new:
desc: Setup new env
cmds:
- task: clean
- task: set-versions
- task: setup-bundler
- task: install-rails
- task: create-rails-project
- task: setup-gitignore
- task: setup-database-config
- task: git-commit
- task: create-compose-config
- task: show-instrunction
clean:
desc: Clean up generated files
cmds:
- rm -rf .bundle .devcontainer .gitignore .github .kamal .rubocop.yml Gemfile* README.md Rakefile app bin config config.ru db lib log public script storage test tmp *.js* *.dev *.lock
- rm -rf .dockerignore .gitattributes .node-version .tool-versions compose.yml Dockerfile .ruby-lsp
- rm -rf .git
- rm -f Gemfile*
set-versions:
internal: true
cmds:
- "asdf set ruby {{.RUBY_VERSION}}"
- "asdf set nodejs {{.NODE_VERSION}}"
- "asdf set bun {{.BUN_VERSION}}"
setup-bundler:
internal: true
cmds:
- rm -rf .bundle
- bundle init
- bundle config --local build.mysql2 "--with-ldflags=-L/opt/homebrew/opt/openssl/lib --with-mysql-dir=/opt/homebrew/opt/[email protected]"
- bundle config --local build.pg "--with-pg-config=/opt/homebrew/opt/postgresql@16/bin/pg_config"
install-rails:
internal: true
status:
- test -f Gemfile.lock
cmds:
- "echo 'gem \"rails\", \"{{.RAILS_VERSION}}\"' >> Gemfile"
- bundle install
create-rails-project:
desc: Create Rails Project
cmds:
- |
bundle exec rails new \
--force \
--name={{.PROJECT}} \
--skip-keeps \
--skip-action-mailbox \
--skip-action-text \
--skip-active-storage \
--skip-hotwire \
--database={{.DATABASE}} \
--javascript={{.JAVASCRIPT}} \
--css={{.CSS}} \
--devcontainer \
.
- defer: rm -f .ruby-version .node-version
setup-gitignore:
internal: true
cmds:
- gibo dump macOS Ruby Rails > .gitignore
- echo "/app/assets/builds/" >> .gitignore
- echo "/compose*.yml" >> .gitignore
- echo "/new-rails*.sh" >> .gitignore
- echo "/Taskfile.yml" >> .gitignore
setup-database-config:
internal: true
cmds:
- "perl -pl -i -e 's{(\\s*)host: .+}{$1host: 127.0.0.1}' config/database.yml"
- "sed -i '' 's/#host: /host: /' config/database.yml"
- "sed -i '' 's/#username: /username: /' config/database.yml"
- "sed -i '' 's/#password:/password: password/' config/database.yml"
- "echo 'db: docker compose up {{.DATABASE}}' >> Procfile.dev"
git-commit:
internal: true
cmds:
- git add .
- git commit -m "initial import"
create-compose-config:
internal: true
cmds:
- |
cat << END > compose.yml
services:
redis:
image: "redis:7.0.7-bullseye"
ports:
- 6379:6379
volumes:
- "redis:/data"
END
- task: create-compose-config-mysql
- task: create-compose-config-postgresql
- |
cat << END >> compose.yml
volumes:
redis: {}
END
create-compose-config-mysql:
internal: true
status:
- test "{{.DATABASE}}" != "mysql"
cmds:
- |
cat << END >> compose.yml
mysql:
image: mysql@sha256:355617769102e9d2ebb7d5879263a12d230badb7271c91748b2c7b0ac6971083
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
END
create-compose-config-postgresql:
internal: true
status:
- test "{{.DATABASE}}" != "postgresql"
cmds:
- |
cat << END >> compose.yml
postgresql:
image: postgres:16-bullseye
ports:
- "5432:5432"
environment:
TZ: "Asia/Tokyo"
PGTZ: "Asia/Tokyo"
POSTGRES_USER: "{{.PROJECT}}"
POSTGRES_PASSWORD: "password"
PGUSER: "{{.PROJECT}}"
END
show-instrunction:
desc: Show instraction after installation
silent: true
cmds:
- |
echo "=> setup:"
echo " docker compose up {{.DATABASE}}"
echo " bin/rails db:create"
echo ""
echo "=> next steps:"
echo " bin/dev"
echo " bin/rails g scaffold blogs title:string content:text active:boolean"
echo " bin/rails db:migrate"
echo " open http://localhost:3000/blogs"
build:
desc: Build docker image
cmd: "docker build -t {{.PROJECT}} ."
default:
cmd: task -l --sort=none
silent: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment